#! /usr/bin/env isis-script
% -*- mode: SLang; mode: fold -*-
%; Time-stamp: <2007-03-27 16:59:52 dph>
%; Directory:  ~dph/h3/CXC/TG/Scripts_devel/Add_pha/Sl/
%; File:       add_pha
%; Author:     David P. Huenemoerder <dph@space.mit.edu>
%; Orig. version: 2006.07.03
%;========================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This file is part of add_pha (sum Chandra PHA spectra)
% Copyright (C) 2007 Massachusetts Institute of Technology 
%
% This software was developed by the MIT Kavli Institute for
% Astrophysics and Space Research under contract SV3-73016 from the
% Smithsonian Institution.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either  version 2 of the
% License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
% 02110-1301, USA
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% version:  0.1.0
%          
% purpose: paramio/unix command version of add_pha_counts.sl
%          
% PARAMIO interface (used if ASCDS_INSTALL is defined in the env):
%
% USAGE:  add_pha[ infile=filespec[ outfile=file[ comments=string|@file]]]
%         add_pha  in_filespec[ out_file[ comments=string | @file] ]
%
%
% POSITIONAL interface  (ONLY IF  ASCDS_INSTALL env not defined):
%
% USAGE:  add_pha infilespec oufile[ comments1,comments2,...commentsM]
%         add_pha  infilespec1,infile2,..infileN out_file [comments1,comments2,...commentsM]
%         add_pha  @infile_list outfile[ @comments_list]
%

require( "add_pha_counts");  % add_pha_counts.sl must be in the isis path

private variable infile, outfile, comments, clobber ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
%
% check for existence of CIAO environment; if present, load paramio

#if$ASCDS_INSTALL

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%+  paramio dependencies:
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% +
% path adjustment, to find paramio source and module:
%
add_to_isis_load_path( getenv("ASCDS_INSTALL") + "/share/slsh/local-packages" );

if ( ( _slang_version / 10000 ) >= 2 )
   add_to_isis_module_path( getenv("ASCDS_INSTALL") + "/lib/slang/v2/modules" ) ; 
else
   add_to_isis_module_path( getenv("ASCDS_INSTALL") + "/lib/slang/modules" ) ; 
%-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

require( "paramio" );

private variable fp = paramopen( NULL, "rw", __argv ) ; 

if (fp == NULL )
verror("\n %% %S: No par file, or param specification error.\n", path_basename( __argv[0]) ) ;

infile   = pget(  fp, "infile" ) ;
outfile  = pget(  fp, "outfile" ) ;
comments = pget(  fp, "comments" ) ; 
clobber  = pgetb( fp, "clobber" ) ; 
%
%-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


#else

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%+ If not ciao env, then do simple positional command-line processing:
%  (still allowing comma-delimited list and @-files)
%

% -------------- debug: --------------------------------
#if(0)
() = printf("%% --- __argc=%d\n", __argc);
#endif

comments = "" ; 
clobber  = 1;

if ( __argc == 4) comments = __argv[3] ;
if ( __argc >= 3)
{
    infile   = __argv[1] ;
    outfile  = __argv[2] ;
}
if ( ( __argc < 3 ) or ( __argc > 4 ) )
{
    message("\n%% add_pha: add counts columns in input file[s], write Type I pha output file.");
    message("%%  Incorrect number of arguments.");
    message("%% USAGES: add_pha infile outfile[ comments]");
    message("%%   infile = filesspec[,filespec[,filespec[,...]]]");
    message("%%          | @listfile");
    message("%%   outfile = filename");
    message("%%   comments = string[,string,[...]]");
    message("%%            | @listfile");
    message("");
    message("%% EXAMPLES:");
    message("add_pha 1.fits,2.fits 12.fits \"1.fits + 2.fits\"");
    message("add_pha @12.list 12.fits testing,1.fits,2.fits");
    message("add_pha a.fits\"[tg_part==2 && (tg_m==1 || tg_m==-1)]\" out.fits \"MEG +/-1\"");
    message("");
    exit;
}

#endif

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  The common code:
%+
% -------------- debug: --------------------------------
#if(0)
message("");
print(__argv);
() = printf( "\n%% infile  = %s\n", infile ) ; 
() = printf( "%% outfile = %s\n", outfile ) ;
() = printf( "%% comments = %s\n\n", comments ) ; 
#endif


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Parse infile arguments:
%
if ( infile[0] == '@' )
{
% if the 1st character of the infile is "@", then read a list of file
% specifications, 1 per line.  Remove newlines:
%
    infile = array_map( String_Type, 
                        &strtrim, 
                        fgetslines( fopen(infile[[1:]], "r" ) ) ); 
}
else  if ( is_substr( infile, ",") )
{
% If we don't have an "@"-file, check for a comma-delimited list;
% if found, convert to a string array, removing any spaces:
%
    infile = strchop( strtrans( infile, " ", ""), ',', 0) ; 
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Parse infile arguments; (slightly different from infile)
%
if ( comments[0] == '@' )
{
% If the 1st character of the comment param is an "@", then read a
% list of comments from a file into a string array, removing newlines:
%
    comments = array_map( String_Type, 
                          &strtrim, 
                          fgetslines( fopen(comments[[1:]], "r" ) ) ) ; 
}
else  if ( is_substr( comments, ",") )
{
% Otherwise, check for a comma-delimited list and convert to string
% array (keeping spaces):
%
    comments = strchop( comments, ',', 0) ; 
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% If we allow clobbering, or the file doesn't exist, go ahead and write:
%
if ( clobber or ( lstat_file( outfile ) == NULL ) )
{
  add_pha_counts( infile, outfile, comments ) ;
}
else 
{
  vmessage("\n%% %S: clobber=no, output failed.\n", path_basename(__argv[0])); 
}


