package RGSMakeFluxed;
use strict;
use English;
use Carp;
use vars
qw(@ISA $VERSION $name $author $date $version @instList $numberOfStreams);
@ISA = qw(Module);
use ModuleResources;
use ModuleUtil;
$name = __PACKAGE__;
$VERSION = '1.18';
$version = $VERSION;
$author = 'Ian Stewart,Duncan John Fyfe,Ed Chapin';
$date = '2013-12-05';
#
# ChangeLog
# =========
#
# version 1.18 - 2013-12-05 EC
# ------------
#
# Set tryspectrumgrid=no in rgsfluxer call
#
# version 1.12 - 2007-02-28 DJF
# ------------
#
# Change rgsfluxer back to 1000 bins.
#
# version 1.11 - 2007-01-17 DJF
# ------------
#
# Change rgsfluxer bins back to default 3400.
# Remove use of instrument number in the order field. The degenerate namespace this resolved has been removed in rgssources.
# Explicitly use order 1.
#
# version 1.10 - 2007-01-05 DJF
# ------------
#
# Change instList from ('obs') to ('all'). ('obs') is a special case.
#
# version 1.09 - 2006-08-15 DJF
# ------------
#
# + Make fluxed plot X axis start at 0
#
# version 1.08 - 2006-07-28 DJF
# ------------
#
# + Changed title and axis lables for fluxed spectrum plot ala andy Pollocks suggestions.
# + Fluxed spectrum title now in mixed case.
#
# version 1.07 - 2006-07-13 DJF
# ------------
#
# + Change rgsfluxer bins from default 3400 to 1000.
#
# version 1.06 - 2006-02-02 DJF
# ------------
#
# + Rgd order added to PDF filenames.
#
# version 1.05 - 2005-12-01 IMS
# ------------
# + Added a test for the existence of the source list.
#
# version 1.04 - 2005-11-25 IMS
# ------------
# + The instrument for the fluxed spectrum output files is now 'rgs'. (Product file names had been without any instrument field and thus too short.)
#
# version 1.03 - 2005-11-24 IMS
# ------------
# + Added label to pdf fluxed spectrum.
#
# version 1.02 - 2005-11-24 IMS
# ------------
# + Shortened a few intermediate content strings - the 'device' file name was too long for fplot.
#
# version 1.01 - 2005-11-24 IMS
# ------------
# + Removed 'order' and 'rgsorder' from newFile calls in which the class is intermediate. These don't seem to be recognized arguments in this case.
#
# version 1.00 - 2005-11-23 IMS
# ------------
# + Now a PDF of the fluxed spectrum is also made.
# + Some source numbering of both the fits and the pdf product files is now done. This is tricky because the same source may have a different row number in the rgs1 source list to that in the rgs2. The scheme I have adopted is to use the row number from the rgs1 list if the source appears there, from the rgs2 list if not. Whether rgs 1 or 2 is indicated by the 'order' argument of newFile.
# + Added PStoPDF and writeASCIIFile to the list of subroutines of ModuleResources called.
#
# version 0.04 - 2005-11-22 IMS
# ------------
#
# + Added some prints to try to work out why it isn't running.
#
# version 0.03 - 2005-11-21 IMS
# ------------
#
# + Changed instrument from 'rgs' to 'obs' to try to get it to run!
#
# version 0.02 - 2005-11-14 IMS
# ------------
#
# + Task rgsfluxer now not called for a given source unless spectra/rmf lists (i) each contain at least 1 member; (ii) have the same number of members.
#
# version 0.01 - 2005-10-18 IMS
# ------------
#
# + Initial version.
# Declare instruments of interest
#@instList = qw(rgs);
@instList = qw(rgs);
# Number of Streams
sub numberOfStreams
{
return 1;
}
# Rules evaluated to determine when this module can run.
sub evaluateRules
{
# If upstream module has been ignored, so is this one
if (allIgnored(module => 'RGSProducts'))
{
return ignore();
# } else {
}
if (allComplete(module => 'RGSProducts'))
{
return start();
# } else {
}
}
# The guts of the module
sub performAction
{
info("Module version number: $version");
my $order = 1;
my %collect = ();
foreach my $inst ( "rgs2", "rgs1" )
{
foreach my $exp_id ( listExposures( instrument => $inst ) )
{
my $srcList = findFile( class => 'product'
, instrument => $inst
, exp_id => $exp_id
, content => 'RGS source list'
);
if (!defined($srcList) || $srcList eq '' || !fileExists(file => $srcList))
{
info("Can't find $inst source list for expid $exp_id");
next;
}
my %src_param = &ModuleUtil::get_rgssources_param( $srcList , 'SRCLIST' , $inst );
# Cycle through the processed sources (proposal and pointing sources, which always occupy the first 2 rows of the rgs source list, are included unilaterally) and find all existing spectra for each source:
foreach my $src ( sort ( keys %src_param ) )
{
my $src_num = $src_param{$src}{INDEX};
my $src_label = $src_param{$src}{LABEL};
my $srcSpecFile = findFile(
class => 'product'
, instrument => $inst
, exp_id => $exp_id
, src_num => $src_num
, order => $order
, rgsorder => $order
, content => 'RGS SOURCE SPECTRUM'
);
### spectra must be paired with an appropriate rmf. However at present we are not making rmfs for order 2.
my $ResponseMatrix = findFile(
class => 'product'
, instrument => $inst
, exp_id => $exp_id
, src_num => $src_num
, order => $order
, rgsorder => $order
, content => "RGS RESPONSE MATRIX"
);
if ($srcSpecFile && $ResponseMatrix)
{
push @{ $collect{$src_label}{spectrum} } , $srcSpecFile;
push @{ $collect{$src_label}{matrix} } , $ResponseMatrix ;
$collect{$src_label}{src_num} ||= $src_num;
}
} # end loop over source list rows
} # end loop over exposures
} # end loop over instruments
foreach my $group ( sort( keys( %collect ) ) )
{
&make_fluxed_spectrum( $collect{$group}{src_num} , $order , $collect{$group}{spectrum} , $collect{$group}{matrix} );
}
return success();
}
sub make_fluxed_spectrum
{
my ( $src_num , $order , $spectra , $matrices ) = (@_);
my $rtn = 1;
# Now generate the fluxed spectrum for each source:
my @allspectra = @$spectra;
my @allmatrices = @$matrices;
if (!defined($allspectra[0]) || !defined($allmatrices[0]))
{
info("Empty list of spectra or RMFs");
$rtn = 0;
}
elsif (@allspectra!=@allmatrices)
{
info("Different numbers in spectra and RMF lists");
$rtn = 0;
}
if ($rtn)
{
my $fluxed = newFile(
class => 'product'
, instrument => 'rgs'
, src_num => $src_num
, order => $order
, rgsorder => $order
, content => 'RGS FLUXED SPECTRUM'
, format => 'FITS'
);
doCommand(
'rgsfluxer', file => $fluxed
, pha => [@allspectra]
, rmf => [@allmatrices]
, bins => 1000
, tryspectrumgrid => 'no'
)
or return exception();
my $tempFluxed = newFile(
class => 'intermediate'
, instrument => 'rgs'
, src_num => $src_num
, order => $order
, rgsorder => $order
, content => 'filt fluxed'
);
doCommand(
'evselect'
, table => "${fluxed}:FLUXED"
, keepfilteroutput => 'yes'
, withfilteredset => 'yes'
, filteredset => $tempFluxed
, writedss => 'no'
, expression => '(CHANNEL in [5.98:38.02])&&(!isNull(FLUX))' # reccommended by Andy Pollock 22 Nov 05.
, destruct => 'yes'
)
or return exception();
my $fplotCommandFile = newFile(
class => 'intermediate'
, content => 'fluxed pco file'
, format => 'ASCII'
, extension => 'pco'
);
my $fname = $fluxed;
$fname =~ s/\.FIT$|\.FTZ$//g;
my $title = "Source $src_num RGS Fluxed Spectrum ($fname)";
writeASCIIFile(
name => $fplotCommandFile
, text => [join("\n", "LA X Wavelength (Angstroms)"
, 'LA Y Fluxed Counts (Photons/cm^2/s/Angstrom)'
, "LA T $title"
, 'RA X 0'
, 'PLOT'
, 'QUIT'
, ''
)]
);
my $fluxedPs = newFile(
class => 'intermediate'
, instrument => 'rgs'
, src_num => $src_num
, order => $order
, rgsorder => $order
, content => 'fluxed'
, format => 'PS'
);
doCommand(
'fplot'
, infile => $tempFluxed
, xparm => 'CHANNEL'
, yparm => 'FLUX'
, rows => '-'
, device => "$fluxedPs/cps"
, pltcmd => '@'."$fplotCommandFile"
)
or return exception();
my $fluxedPdf = newFile(
class => 'product'
, instrument => 'rgs'
, src_num => $src_num
, order => $order
, rgsorder => $order
, content => 'RGS FLUXED SPECTRUM'
, format => 'PDF'
);
PStoPDF(
source => $fluxedPs
, destination => $fluxedPdf
)
or return exception();
}
return $rtn;
}
1;