#! /bin/sh # This is the LHEA perl script: /cvmfs/extras-fp7.egi.eu/extras/heasoft/ftools/x86_64-unknown-linux-gnu-libc2.19-0/bin/pcaprepobsid # The purpose of this special block is to make this script work with # the user's local perl, regardless of where that perl is installed. # The variable LHEAPERL is set by the initialization script to # point to the local perl installation. #------------------------------------------------------------------------------- eval ' if [ "x$LHEAPERL" = x ]; then echo "Please run standard LHEA initialization before attempting to run /cvmfs/extras-fp7.egi.eu/extras/heasoft/ftools/x86_64-unknown-linux-gnu-libc2.19-0/bin/pcaprepobsid." exit 3 elif [ "$LHEAPERL" = noperl ]; then echo "During LHEA initialization, no acceptable version of Perl was found." echo "Cannot execute script /cvmfs/extras-fp7.egi.eu/extras/heasoft/ftools/x86_64-unknown-linux-gnu-libc2.19-0/bin/pcaprepobsid." exit 3 elif [ `$LHEAPERL -v < /dev/null 2> /dev/null | grep -ic "perl"` -eq 0 ]; then echo "LHEAPERL variable does not point to a usable perl." exit 3 else # Force Perl into 32-bit mode (to match the binaries) if necessary: if [ "x$HD_BUILD_ARCH_32_BIT" = xyes ]; then if [ `$LHEAPERL -V 2> /dev/null | grep -ic "USE_64_BIT"` -ne 0 ]; then VERSIONER_PERL_PREFER_32_BIT=yes export VERSIONER_PERL_PREFER_32_BIT fi fi exec $LHEAPERL -x $0 ${1+"$@"} fi ' if(0); # Do not delete anything above this comment from an installed LHEA script! #------------------------------------------------------------------------------- #!/usr/bin/perl # # File: pcaprepobs # # Description: Prepare one PCA observation directory for analysis # # Author: C. B. Markwardt # use Getopt::Std; getopts(':h'); if (defined $opt_h) { print <= 1); print "----------------------------------------\n" if ($chatter >= 2); undef $breakfile if ($breakfile =~ m/^none$/i); $clobstr = $clobber ? "YES" : "NO"; $cleanstr = $cleanup ? "YES" : "NO"; die "ERROR: $indir is not readable directory" if (! -d "$indir" ); die "ERROR: $indir does not have pca/ subdirectory" if (! -d "$indir/pca" ); if (! -d "$outdir" ) { mkdir("$outdir") or die "ERROR: could not create $outdir"; } die "ERROR: $outdir is not writable" if (! -w "$outdir" ); if ("$srcra" =~ m/^INDEF$/i || "$srcdec" =~ m/^INDEF$/i) { undef($srcra); undef($srcdec); } else { $srcra = $srcra + 0.0; $srcdec = $srcdec + 0.0; die "ERROR: Invalid 'ra' and 'dec' keywords" if ($srcra == 0 && $srcdec == 0); } my ($apidfile,$obs_info,$cmd,$logfile); # ======================= # List of standard output files $okfile = "$outdir/pcaprepobsid_done.txt"; unlink($okfile); $apidfile = "$outdir/appid.lis"; $hklis = "$outdir/FP_pcahk.lis"; $filtlis = "$outdir/FP_xtefilt.lis"; $std2lis = "$outdir/FP_std2.lis"; $bkg2lis = "$outdir/FP_bkg2.lis"; $dtstd2lis = "$outdir/FP_dtstd2.lis"; $dtbkg2lis = "$outdir/FP_dtbkg2.lis"; # ======================= # Remove most of them. This task does not take too kindly to # having previous versions lying around so warn against this. &headas_clobberfile($apidfile); &headas_clobberfile($hklis); &headas_clobberfile($filtlis); &headas_clobberfile($std2lis); &headas_clobberfile($bkg2lis); &headas_clobberfile($dtstd2lis); &headas_clobberfile($dtbkg2lis); die "ERROR: $filtlis already exists and clobber not set" if ( -f $filtlis); die "ERROR: $hklis already exists and clobber not set" if ( -f $hklis); die "ERROR: $std2lis already exists and clobber not set" if ( -f $std2lis); die "ERROR: $bkg2lis already exists and clobber not set" if ( -f $bkg2lis); die "ERROR: $dtstd2lis already exists and clobber not set" if ( -f $dtstd2lis); die "ERROR: $dtbkg2lis already exists and clobber not set" if ( -f $dtbkg2lis); # ======================= # Make APID file for xtefilt to use die "ERROR: $apidfile already exists" if (-f $apidfile); make_apid_file("$apidfile"); # Parse the FMI file for useful information (time and obsid) $obs_info = xte_obs_info("$indir"); print "$obs_info->{obsid} $obs_info->{datestart} $obs_info->{datestop}\n" if ($chatter >= 2); # Determine output filter file name $filtroot = "$outdir/FP_".$obs_info->{hexindex}; $filtfile = "$filtroot".".xfl"; $logfile = "$outdir/xtefilt.log"; &headas_clobberfile($filtfile); unlink($filtroot); # This could be a junk file lying around from previous run die "ERROR: filter file $filtfile already exists" if ( -f "$filtfile" ); # ======================= # Prepare to run XTEFILT $cmd = "xtefilt -a '$apidfile' ". "-f '$filtroot' ". "-o '".$obs_info->{obsid}."' ". "-p '$indir' ". "-t '$timestep' "; $cmd .= "-b '$breakfile' " if (defined($breakfile)); print "COMMAND: $cmd\n" if ($chatter >= 5); init_log($logfile,"XTEFILT",$cmd); system("$cmd >> $logfile 2>&1"); die "ERROR: xtefilt failed; see $logfile" if ($?); # Verify that filter file run worked unlink($filtroot); # Remove junk files left around by failed process if ( ! -f "$filtfile" ) { die "ERROR: xtefilt failed; see $logfile"; } $filt = SimpleFITS->open("$filtfile",ext=>"XTE_MKF",access=>"readonly"); die "ERROR: filter file $filtfile is invalid; see $logfile " if ( ! $filt ); $filt->close(); # Log which filter file was created print " FILT: $filtfile\n" if ($chatter >= 2); if (defined($srcra) && defined($srcdec)) { &recompute_offset_elv("$filtfile",$srcra,$srcdec,$chatter,$cleanup); print " (recomputed for RA=$srcra DEC=$srcdec)\n" if ($chatter >= 2); } add_list_item($filtlis,$filtfile,$abspath,1); # ======================= # Find the housekeeping files @hkfiles = <$indir/pca/FH5[abcde]_*>; if ($#hkfiles != 4) { die "ERROR: all five housekeeping files are not present in $indir/pca"; } # First item clobbers, the rest append add_list_item($hklis,shift(@hkfiles),$abspath,1); foreach my $hkfile (@hkfiles) { add_list_item($hklis,$hkfile,$abspath,0); } # ======================= # Now start to process standard2 files with pcaprepfile2 @std2files = <$indir/pca/FS4a*>; foreach my $std2file (@std2files) { next if (! -r "$std2file" ); next if ($std2file !~ m|^(.*)/(FS4a_[0-9a-f]+-[0-9a-f]+)(.gz)?$|); my ($std2root,$bkgfile,$dtfile,$dtbkgfile); $std2root = "$outdir/$2"; $bkgfile = $std2root."_bkg"; $dtfile = $std2root."_dt"; $dtbkgfile = $std2root."_dtbkg"; $logfile = $std2root.".log"; &headas_clobberfile($bkgfile); &headas_clobberfile($dtfile); &headas_clobberfile($bkgfile); &headas_clobberfile($dtbkgfile); &headas_clobberfile($logfile); $cmd = "pcaprepfile2 ". "infile='$std2file' ". "filterfile='$filtfile' ". "hkfiles='\@".$hklis."' ". "bkgfile='$bkgfile' ". "dtfile='$dtfile' ". "dtbkgfile='$dtbkgfile' ". "modelfile='$modelfile' ". "bkginterval='$bkginterval' ". "bkglayers='$layers' ". "bkgfullspec='$fullspec' ". "saahfile='$saahfile' ". "breakfile='$breakfile' ". "zero_bad='$zero_bad' ". "transition_bad='$transition_bad' ". "propane_bad='$propane_bad' ". "chatter=5 cleanup='$cleanstr' clobber='$clobstr'"; print "COMMAND: $cmd\n" if ($chatter >= 5); init_log($logfile,"PCAPREPFILE2",$cmd); system("$cmd >> $logfile 2>&1"); die "ERROR: pcaprepfile2 failed; see $logfile" if ($?); if (! -f "$bkgfile" || ! -f "$dtfile" || ! -f "$dtbkgfile" ) { die "ERROR: pcaprepfile2 failed; see $logfile"; } print " STD2: $std2file\n" if ($chatter >= 2); # Add this standard2 file to the list add_list_item($std2lis,$std2file,$abspath,0); add_list_item($bkg2lis,$bkgfile,$abspath,0); add_list_item($dtstd2lis,$dtfile,$abspath,0); add_list_item($dtbkg2lis,$dtbkgfile,$abspath,0); } # Final clean up if ($status == 0) { add_list_item($okfile,localtime(),0,1); } return $status; } # # xte_obs_info - retrieve information about this observation # indir - directory containing observation FMI file # # Returns hash: # => obsid - observation ID # => tstart - start time (MET) # => tstop - stop time (MET) # => datestart - start time (UTC YYYY-MM-DDThh:mm:ss) # => datestop - stop time (UTC YYYY-MM-DDThh:mm:ss) # => hexindex - start-stop time in hex # sub xte_obs_info { my ($indir) = (@_); my ($fmifile,$fmifilename); $fmifilename = "$indir/FMI"; die "ERROR: could not read $indir" if (! -d "$indir" || ! -r "$indir" ); die "ERROR: FMI file not readable in $indir" if (! -f "$fmifilename" || ! -r "$fmifilename" ); $fmifile = SimpleFITS->open("$fmifilename",ext=>"XTE_MI",access=>"readonly"); die "ERROR: could not open $fmifilename for reading" if (! $fmifile ); if ($fmifile->nrows() > 1) { die "ERROR: pcaprepobsid should only be run on a single OBSID, not full proposal"; } my ($row,$obsid,$tstart,$tstop,$datestart,$datestop, $timestart,$timestop,$timestr_hex); $row = 1; $obsid = ($fmifile->readcol("ObsId",{rows=>$row})) [0]; $tstart = ($fmifile->readcol("StartMET",{rows=>$row})) [0]; $tstop = ($fmifile->readcol("StopMET", {rows=>$row})) [0]; $datestart = ($fmifile->readcol("StartDate", {rows=>$row})) [0]; $datestop = ($fmifile->readcol("StopDate", {rows=>$row})) [0]; $timestart = ($fmifile->readcol("StartTime", {rows=>$row})) [0]; $timestop = ($fmifile->readcol("StopTime", {rows=>$row})) [0]; $fmifile->close(); $datestart = old_date_to_new_date($datestart) . "T" . "$timestart"; $datestop = old_date_to_new_date($datestop) . "T" . "$timestop"; $timestr_hex = sprintf("%08x-%08x", int($tstart), int($tstop)); my $obs_info = {'obsid' => $obsid, 'tstart' => $tstart, 'tstop' => $tstop, 'datestart' => $datestart, 'datestop' => $datestop, 'hexindex' => $timestr_hex}; } # # recompute_offset_elv - recompute OFFSET and ELV columns for new RA/DEC # # filtfile - name of old filter file (will be replaced in-place) # ra - RA of target # dec - Dec of target # chatter - chatter level # cleanup - boolean whether to clean up or not # sub recompute_offset_elv { my ($filtfile,$ra,$dec,$chatter,$cleanup) = (@_); my ($exprfile,$tmpfile,$oldfile); $exprfile = "$filtfile".".expr"; $tmpfile = "$filtfile".".tmp"; $oldfile = "$filtfile".".old"; print " (renaming $filtfile to $oldfile before recomputing)\n" if ($chatter >= 5); rename($filtfile,$oldfile) or die "ERROR: could not rename $filtfile"; push @scratchfiles, $oldfile; print " (creating $exprfile)\n" if ($chatter >= 5); open(OUT,">$exprfile") or die "ERROR: could not open $exprfile"; print OUT <<"END_EXPR1"; // First * indicates include all input columns *; #RA_OBJ( [deg] J2000 Right Ascension of target) = $ra; #DEC_OBJ( [deg] J2000 Declination of target) = $dec; #R_EARTH( [km] Earth radius)=6378.16; // Earth-center to satellite radius R = SQRT(ACSSCPOSX**2+ACSSCPOSY**2+ACSSCPOSZ**2); // Half-angle subtended by earth HALFANG = ASIN(R_EARTH/R)/#deg; // COS(MU) where MU is the EARTH-SPACECRAFT-TARGET angle (dot product) COSMU = (ACSSCPOSX*COS(#RA_OBJ*#deg)*COS(#DEC_OBJ*#deg) + ACSSCPOSY*SIN(#RA_OBJ*#deg)*COS(#DEC_OBJ*#deg)+ACSSCPOSZ*SIN(#DEC_OBJ*#deg))/R; // Backup copies of original values OLD_ELV = ELV; OLD_OFFSET = OFFSET; // Target elevation above earth limb [deg] ELV = 180-ACOS(COSMU)/#deg-HALFANG; // Target offset from pointing direction [deg] OFFSET = ANGSEP(#RA_OBJ,#DEC_OBJ,POINT_RA,POINT_DEC); END_EXPR1 if ($cleanup) { print OUT <<"END_EXPR2"; -R; -HALFANG; -COSMU; -OLD_ELV; -OLD_OFFSET; END_EXPR2 } close(OUT); push @scratchfiles, $exprfile; $cmd = "ftcopy infile='$oldfile"."[col @".$exprfile."]' ". "outfile='$filtfile' clobber=YES copyall=YES chatter=1"; print "COMMAND: $cmd\n" if ($chatter >= 5); @result = `$cmd 2>&1`; if ($? || ! -f "$filtfile") { print "Error log\n=========================\n"; print @result; print "=========================\n"; die "ERROR: could not create $tmpfile"; } } # # init_log - initialize a lot file representing command output # # logfile - name of log file (will be clobbered) # title - title of task to be run # cmd - full command line to be run # sub init_log { my ($logfile,$title,$cmd) = (@_); unlink($logfile); open(LOG,">$logfile") or die "ERROR: could not open $logfile"; print LOG "$title ".localtime()."\n"; print LOG "$cmd\n"; print LOG "======================="; close(LOG); } # ================================================================== # Append item to list # $listfile - output file name # $item - string to be appended to file (newline is added) # $abspath - if true, then convert relative to absolute path # $clobber - if true sub add_list_item { my ($listfile,$item,$abspath,$clobber) = (@_); unlink($listfile) if ($clobber); open(OUT,">>$listfile") or die "ERROR: could not append to $listfile"; print OUT ($abspath ? abs_path($item) : $item)."\n"; close(OUT); } # # old_date_to_new_date - convert old FITS date formats to new # # old_date_str - old date format string DD/MM/YY # # RETURNS: new date format YYYY-MM-DD sub old_date_to_new_date { my ($old_date_str) = (@_); if ($old_date_str =~ m|(\d\d)/(\d\d)/(\d\d)|) { my ($day,$mo,$yr); $day = $1; $mo = $2; $yr = $3; return sprintf("%04d-%02d-%02d", ($yr < 30) ? (2000 + $yr) : (1900 + $yr), $mo, $day); } return "$old_date_str"; } # Create APID list for xtefilt sub make_apid_file { my ($outfile) = (@_); open(OUT,">$outfile") or die "ERROR: could not open $outfile"; print OUT <<'END_APID_LIST'; 14 ACSESTQ1 14 ACSESTQ2 14 ACSESTQ3 14 ACSESTQ4 15 ACSMODESTAT 16 ACSSCPOSX 16 ACSSCPOSY 16 ACSSCPOSZ 16 ACSSCVELX 16 ACSSCVELY 16 ACSSCVELZ 17 ACSEARTHLAT 17 ACSEARTHLON 20 ACSLUNARX 20 ACSLUNARY 20 ACSLUNARZ 21 ACSALTITUDE 24 ACSEARTHX 24 ACSSUNX 24 ACSSUNY 24 ACSSUNZ 74 VpX1LCntPcu0 74 VpX1RCntPcu0 74 Q6VxVpXeCntPcu0 74 VpX1LCntPcu1 74 VpX1RCntPcu1 74 Q6VxVpXeCntPcu1 74 VpX1LCntPcu2 74 VpX1RCntPcu2 74 Q6VxVpXeCntPcu2 74 VpX1LCntPcu3 74 VpX1RCntPcu3 74 Q6VxVpXeCntPcu3 74 VpX1LCntPcu4 74 VpX1RCntPcu4 74 Q6VxVpXeCntPcu4 74 X2LX2RCntPcu0 74 X3LX3RCntPcu0 74 X1LX2LCntPcu0 74 X1RX2RCntPcu0 74 X2LX3LCntPcu0 74 X2RX3RCntPcu0 83 ctLldPm pm_0 89 ctLldPm pm_1 90 cmdhvXE cmdhvXE_PCU0 91 cmdhvXE cmdhvXE_PCU1 92 cmdhvXE cmdhvXE_PCU2 93 cmdhvXE cmdhvXE_PCU3 94 cmdhvXE cmdhvXE_PCU4 90 hvXEon hvXEon_PCU0 91 hvXEon hvXEon_PCU1 92 hvXEon hvXEon_PCU2 93 hvXEon hvXEon_PCU3 94 hvXEon hvXEon_PCU4 90 hvXErly hvXErly_PCU0 91 hvXErly hvXErly_PCU1 92 hvXErly hvXErly_PCU2 93 hvXErly hvXErly_PCU3 94 hvXErly hvXErly_PCU4 90 evXEgood evXEgood_PCU0 91 evXEgood evXEgood_PCU1 92 evXEgood evXEgood_PCU2 93 evXEgood evXEgood_PCU3 94 evXEgood evXEgood_PCU4 251 hvD0 hvD0_0 251 hvD1 hvD1_0 251 hvD2 hvD2_0 251 hvD3 hvD3_0 252 hvD0 hvD0_1 252 hvD1 hvD1_1 252 hvD2 hvD2_1 252 hvD3 hvD3_1 295 TIME_SINCE_BRK0 295 TIME_SINCE_BRK1 295 TIME_SINCE_BRK2 295 TIME_SINCE_BRK3 295 TIME_SINCE_BRK4 295 TIME_SINCE_BRK END_APID_LIST close(OUT); }