Download

package GlobalHK;
use strict;
use English;
use Carp;
use vars
  qw(@ISA $VERSION $name $author $date $version @instList $numberOfStreams);
@ISA = qw(Module);
use ModuleResources;

# Declare identity, version, author, date, etc.
$name    = __PACKAGE__;
$VERSION = '1.76';
$version = $VERSION;
$author="Richard West,Dean Hinshaw,Duncan John Fyfe,Eduardo Ojero,Ed Chapin, Jose Vicente Perea, Pedro Rodriguez";
$date="2018-05-30";

#
# ChangeLog
# =========
#
# Version 1.76 2018-05-30 (JVP)
#
# + Copy EPICModes.fits 'Observing modes Pileup and OoT data' from Pipeline configuration
#   to intermediate/ directory
#
# Version 1.75 2018-04-09 (PR, JVP)
#
# + Create Spacecraft orbit file
#
# Version 1.74 2015-12-22 (JVP)
#
# + Original Attitude History File from ODF (ATS) modified
#   if processing a Moving Object (SSO) in the obeject reference system.
#
# Version 1.73 2014-03-13 (EC)
#
# + Set withpreqgti => 'no' in atthkgen (PPS SCR 0007190)
#
# Version 1.72 - 2014-02-21 (EOP)
#
# + Changed the order of execution of preqgti and atthkgen: First preqgti, then 
#   atthkgen so as the output of preqgti is used in atthkgen.
#   (PPS PR 0007177)
#
# Version 1.71 - 2013-10-18 (EC)
#
# + Use preqgti instead of tabgtigen
#
# Version 1.70 - 2013-01-09 (EO)
#
# + Fixed typo in expresion changed in version 1.69. Must be TYPEID=='S'
#
# Version 1.69 - 2012-12-21 (EO)
#
# + Changed the expresion to be applied in tabgtigen, from 
#
#   expression="(TYPEID=='P')" 
#
# to 
#
#   expression="(TYPEID=='P')||(TYPEID='S')"
#
#
# Version 1.68 - 2012-11-20 (EO)
#
# + To be able to produce the whole image of a set of mosaic observations, the command  
# 
#     tabgtigen table=$hkFile:ATTHK expression => '!isNull(DAHFPNT) && DAHFPNT < 3.0/60.0'
#   
#   is replaced by 
#
#     tabgtigen table=$AttHistFile  expression => "(TYPEID=='P')" timecolumn=VALTIME
#
#   where $AttHistFile is the Reconstruted Attitude file in the ODF or AHF, 
#   that is filename RRRR_IIIIIIIIII_SCX00000ATS.FIT, where RRRR is the Rev. Number and
#   IIIIIIIIII is the ODF ID number.
#
# Version 1.67 - 2006-09-28 (DJF)
# ------------
#
# + Commented out redundant work.  Was calling findFile and newFile then doing nothing with returned values.
#
# Version 1.66 - 2006-07-05 (DJF)
# ------------
#
# + Changed numberOfStreams to use standerdized function.
#
# Version 1.65 - 2002-02-04 (DH)
# ------------
#
# + Remove creation of 'Good Attitude Data GTI', as it will no longer
#   be used.
# + Add '!isNull(DAHFPNT) &&' to tabgti expression for creating the
#   attitude GTI.  Note that the cutoff of 3 acrmin is tighter than
#   the rgsproc default of 5 acrmin.
#
# Version 1.64 - 2001-03-21 (DH)
# ------------
#
# + Create GTI of good attitude data, for use by other modules
#
# Version 1.63 - 2001-03-16 (DH)
# ------------
#
# + Print out version number in performAction() for
#   tracking purposes.
#
# Version 1.62 - 2001-02-14 (DH)
# ------------
#
# + Change column name for test to make attitude GTIs
#   from DAHFNOM to DAHFPNT.
#
# Version 1.61 - 2000-11-22 (DH)
# ------------
#
# + First production version.
#

@instList=qw(all);

sub numberOfStreams
{
        return 1;
}

sub evaluateRules {
    # ignore for slew processing
    return ignore() if ($ENV{'PCMS_ISSLEW'});
    #
    start() if complete(module => 'MakeCIF', instrument => 'all');
}

sub performAction {

    info("Module version number: $version");

    	#------------------------------------------------------------------------------------------------------
    	# Modify ATS file from the ODF if processing a Moving Object (SSO) in the obeject reference system:
	#
	# 	option: --sso_object
    	#
	my $sso_object = $ENV{PCMS_SSO_OBJECT};
	
	if ( $sso_object ){
		info("-------------------------------------------------------------------------");
		info("Moving Object (SSO) option active --sso_object");
		info("Modifying the original Attitude History File (ATS) from the ODF ...");
		
		# Find spacecraft attitude file
		#	odf/__SCX00000ATS.FIT
		my $ATSFileSky = findFile(
					class => 'odf'
					, content => 'Spacecraft attitude'
					, required => 'true'
					);
					
		my $ATSFileSky_save = "$ATSFileSky.origSky";
		info("Saving the original ATS file: $ATSFileSky to $ATSFileSky_save");
		copyFile(source => $ATSFileSky, destination => $ATSFileSky_save); 
		
		# Find tracking Ephemeris file for the moving object (SSO)
		#	odf/__SCX00000SSO.FIT
		my $trackEphemFile = findFile(
					class => 'odf'
					, content => 'SSO Ephemeris'
					, required => 'true'
					);
		
		  if (!fileExists( file => $trackEphemFile )) {
		  info("SSO Ephemeris file not found. Aborting...");
		  return exception();
		  }
		
		# Search for the Median of RA, DEC from the tracking Ephemeris file as the reference attitude
		#   RA,Dec median values are in keywords of the tracking Ephemeris file: MRA, MDEC keywords
		my $refra = readFITSKeyword(
					    file => $trackEphemFile
					    , extension => 2
					    , keyword => 'MRA'
					    );
		my $refdec = readFITSKeyword(
					    file => $trackEphemFile
					    , extension => 2
					    , keyword => 'MDEC'
					    );
		
		info("Original ATS file SKY  : $ATSFileSky");
		info("SSO ephemeris file     : $trackEphemFile");
		info("Reference attitude from Ephemeris, refra, refdec = $refra, $refdec");
		
		
		# Define output ATS file in SSO reference system
		my $ATSFileSSO = newFile(class => 'intermediate',
				 	 content => 'ATS SSO');
		
		
		# Converting Attitide History from Sky to Object ref. system
		doCommand(
			  'attmove'
        		  , input => $ATSFileSky
        		  , output => $ATSFileSSO
			  , track => $trackEphemFile
        		  , withrefatt => 'yes'
			  , refra => $refra
			  , refdec => $refdec
        		 ) or return exception();
			 
		# Overwrite the original ATS file from the ODF:
		info("Overwriting the original ATS file:");
		copyFile(source => $ATSFileSSO, destination => $ATSFileSky);
		info("New ATS file referenced to SSO : $ATSFileSky");
		info("-------------------------------------------------------------------------");
	
	}
	#------------------------------------------------------------------------------------------------------
    
    
    #
    # Create GTI from attitude data
    #

    my $gtiFile=newFile(class      => 'intermediate',
                        instrument => 'all',
                        content    => 'Attitude GTI');

    # Command replaced to allow computation of full image for mosaic observations

    doCommand('preqgti',
              preqgtifile          => $gtiFile
             ) or return exception();


    #
    # Create attitude HK file
    #

    my $hkFile=newFile(class      => 'product',
		       instrument => 'all',
		       content    => 'Attitude time series');

    doCommand('atthkgen',
	      atthkset            => $hkFile,
	      timestep            => 1.0,
              withpreqgti         => 'no',
              preqgtifile         => $gtiFile
             ) or return exception();


     # Create orbit file

#        my $inOrbit=findFile(class => 'odf',
#    			 content => 'Reconstructed orbit',
#    			 required => 'true');
        my $outOrbit=newFile(class => 'product',
			     instrument => 'all',
                             content => 'Spacecraft orbit');

        doCommand('orbit',
	          useobsid => 'yes',
		  outfile => $outOrbit,
		  timestep => 1)
          or return exception();


    #
    # Copy EPICModes 'Observing modes Pileup and OoT data' from Pipeline configuration
    #    to intermediate/ directory
    #
    my $modesPileupOoTFile = newFile(
			     class => 'intermediate'
			     , content => 'Observing modes Pileup and OoT data'
			     ) or return exception();

    # Pile-up and OoT info in table in Pipeline configuration dir:
    my $modesPileupOoTInput = $ENV{PCMS_ROOT} . '/pipeline/etc/EPICModes.fits';

    copyFile(
             source => $modesPileupOoTInput
             , destination => $modesPileupOoTFile
             ) or return exception();

    info("Copied EPIC Observing modes table, Pileup data and OoT data table in place");


    success();
}

1;