#! /usr/bin/perl -w use SAS; require "esas_parameters_init.pl"; require "esas_GVariables.pm"; #.. Create object to store parameters #.. Define Global Variables use vars qw($gv); $gv = esas_GVariables->new(); #.. Init Program Parameters &convregion_parameters_init(@ARGV); $task="conv-region"; $version="0.2.1"; $date="2011-02-18"; $author="K. D. Kuntz with adaptation by S. L. Snowden"; print "\n"; print "Task: $task \n"; print "Version: $version \n"; print "Date: $date \n"; print "Author: $author \n\n"; # conv-region does the following: For each instrument of each obsid # the targ_file is copied to the working directory where its coordinates # are converted to DETX,DETY and the sizes are converted to pixels. # The local instr-bkg_region-det.fits region file is then appended, # and the result written to instr-det-targ.fits. # The reg_instr.txt file is then written to use this region file, # i.e, &®ION(instr-det-targ.fits) # # usage: conv-region targfile=det-targ.fits # # obsidlist: ASCII file has one obsid per line, i.e., 0112290201 # targfile: FITS region file in sky coordinates # The FITS file format is the same as that used to construct # instr-bkg_region-det.fits with the following exceptions: # Instead of DETX,DETY the columns are RA,DEC and the values # are in decimal degrees. The R column is in arcseconds. $obsid_list=$gv->ConvRegion_obsidlist; $targ_file=$gv->ConvRegion_targfile; chomp($obsid_list); chomp($targ_file); print "ObsID List: $obsid_list\n"; print "Input Region File: $targ_file\n"; system "more ".$obsid_list; # Extract the ObsIDs from the ASCII list and loop over them open (DIR_HNDL,$obsid_list); while($line=){ chomp($line); ($proc,$odf)=split(/ /,$line); $good=substep($proc,$odf,$ccdc); } # substep does the work of this routine sub substep{ chdir($proc); system "pwd"; # Set the environment variables $sas_ccf=$proc."/ccf.cif"; $sas_odf=$odf; { local $ENV{"SAS_CCF"} = $sas_ccf; local $ENV{"SAS_ODF"} = $sas_odf; $temp="SAS_CCF"; print "$ENV{$temp}\n"; $temp="SAS_ODF"; $temp_odf=$ENV{$temp}; print "$temp_odf\n"; # Determine the exposure prefixes system "ls *-clean.fits > temp.list"; open (LIST_HNDL,"temp.list"); @file_list=; close(LIST_HNDL); system "rm -f temp.list"; # Create the name of the output target region file for this instrument $det_filen="det-targ-ori.fits"; $det_file="det-targ.fits"; # Copy the file in ra(decimal),dec(decimal),r(arcmin) to this directory system "cp ../../".$targ_file." ".$det_filen; # Loop over the instruments foreach $evt_file (@file_list){ system "cp ".$det_filen." ".$det_file; chomp($evt_file); print "$evt_file\n"; $plac=index($evt_file,"-"); $prefix=substr($evt_file,0,$plac); $subfix=substr($evt_file,$plac-4,4); $instr=substr($evt_file,0,$plac-4); # Convert to detector coordinates $good=edit_region(); # End loop over instruments } system "rm -f ".$det_file." ".$det_filen; # End environment } chdir("../.."); # End subroutine } sub edit_region{ # Create the standard region file $file_name="reg-".$instr.".txt"; open (REG_HNDL,">$file_name"); # Get the number of rows in the table system "fstruct \"".$det_file."\[1\]\""; $n_row=`pget fstruct naxis2`; print "Rows: $n_row\n"; # Loop over the rows $ist=0; for($row=1;$row<=$n_row;$row++){ print "$row \n"; # Read the RA value(s) $ra_vals=`fdump \"$det_file\[1\]\" rows=$row columns=RA showrow=no showcol=no showunit=no prhead=no outfile=STDOUT mode=l`; # Remove excess carriage returns $ra_vals =~ s/\f//g; # Remove excess white space from middle $ra_vals =~ s/\s+/ /g; # Remove leading white space $ra_vals =~ s/^\s+//; # Remove trailing white space $ra_vals =~ s/\s+$//; # Split string into an array @ra_vals=split(/\s+/,$ra_vals); # Read the Dec value(s) $dec_vals=`fdump \"$det_file\[1\]\" rows=$row columns=Dec showrow=no showcol=no prhead=no outfile=STDOUT mode=l`; $dec_vals =~ s/\f//g; $dec_vals =~ s/\s+/ /g; $dec_vals =~ s/^\s+//; $dec_vals =~ s/\s+$//; @dec_vals=split(/\s+/,$dec_vals); $n_col=@dec_vals; print "Columns: $n_col\n"; # Read in the radius value(s), conversion from arcsec to pixels below $r_vals=`fdump \"$det_file\[1\]\" rows=$row columns=R showrow=no showcol=no showunit=no prhead=no outfile=STDOUT mode=l`; $r_vals =~ s/\f//g; $r_vals =~ s/\s+/ /g; $r_vals =~ s/^\s+//; $r_vals =~ s/\s+$//; @r_vals=split(/\s+/,$r_vals); $n_rcol=@r_vals; # Read in the rotang value(s), conversion from arcsec to pixels below $rot_vals=`fdump \"$det_file\[1\]\" rows=$row columns=ROTANG showrow=no showcol=no showunit=no prhead=no outfile=STDOUT mode=l`; $rot_vals =~ s/\f//g; $rot_vals =~ s/\s+/ /g; $rot_vals =~ s/^\s+//; $rot_vals =~ s/\s+$//; @rot_vals=split(/\s+/,$rot_vals); # $n_col=@rot_vals; # Read in the shape $sh_val=`fdump \"$det_file\[1\]\" rows=$row columns=SHAPE showrow=no showcol=no showunit=no prhead=no outfile=STDOUT mode=l`; $sh_val =~ s/\f//g; $sh_val =~ s/\s+/ /g; $sh_val =~ s/^\s+//; $sh_val =~ s/\s+$//; @sh_val=split(/\s+/,$sh_val); # Convert from RA,Dec to DETX,DETY @detx=(); @dety=(); for($col=0;$col<$n_col;$col++){ # Copy the file in ra(decimal),dec(decimal),r(arcmin) to this directory chomp($ra_vals[$col]); chomp($dec_vals[$col]); chomp($rot_vals[$col]); if($ra_vals[$col] ne '' && $dec_vals[$col] ne ''){ if($ra_vals[$col] != 0 && $dec_vals[$col] != 0){ $comm="esky2det datastyle=user ra=".$ra_vals[$col]. " dec=".$dec_vals[$col]. " outunit=det withheader=no calinfoset=".$evt_file; # print "$comm \n"; $junk=`$comm`; # print "$junk \n"; $junk =~ s/\s+/ /g; $junk =~ s/^\s+//; $junk =~ s/\s+$//; @junk=split(/\s/,$junk); $junk=@junk; push @detx,$junk[0]; push @dety,$junk[1]; } } } # Insert values back into table # Assumes no blank values in entry 0 $n_col=@r_vals; for($col=1;$col<=$n_col;$col++){ $r_vals[$col-1]=$r_vals[$col-1]/0.05; system "ftedit \"".$det_file."\[1\]\" R 1 ". "element=".$col." ".$r_vals[$col-1]; } print "$sh_val \n"; $let=substr($sh_val,0,1); if ($let eq '!'){ $shap=substr($sh_val,1,8); if(($shap ne 'CIRCLE') and ($shap ne 'circle') and ($shap ne 'ANNULUS')and ($shap ne 'annulus')){ print "Unsupported Region Shape: $shap \n"; die; } else { if($shap eq 'CIRCLE'){ if($row == 1){ print REG_HNDL "&&!(((DETX,DETY) in circle(".$detx[0].",".$dety[0].",".$r_vals[0]."))"; } else { if($ist == 0){ print REG_HNDL ")&&!(((DETX,DETY) in circle(".$detx[0].",".$dety[0].",".$r_vals[0]."))"; $ist=1; } else { print REG_HNDL "||((DETX,DETY) in circle(".$detx[0].",".$dety[0].",".$r_vals[0]."))"; } } } else { if($shap eq 'ANNULUS'){ if($row == 1){ print REG_HNDL "&&!(((DETX,DETY) in annulus(".$detx[0].",".$dety[0].",".$r_vals[0].",".$r_vals[1]."))"; } else { if($ist == 0){ print REG_HNDL ")&&!(((DETX,DETY) in annulus(".$detx[0].",".$dety[0].",".$r_vals[0].",".$r_vals[1]."))"; $ist=1; } else { print REG_HNDL "||((DETX,DETY) in annulus(".$detx[0].",".$dety[0].",".$r_vals[0].",".$r_vals[1]."))"; } } } } } } else { $shap=substr($sh_val,0,8); if(($shap ne 'CIRCLE') and ($shap ne 'circle') and ($shap ne 'ANNULUS')and ($shap ne 'annulus')){ print "Unsupported Region Shape: $shap \n"; die; } else { if($shap eq 'CIRCLE'){ if($row == 1){ print REG_HNDL "&&(((DETX,DETY) in circle(".$detx[0].",".$dety[0].",".$r_vals[0]."))"; } else { print REG_HNDL "||((DETX,DETY) in circle(".$detx[0].",".$dety[0].",".$r_vals[0]."))"; } } else { if($shap eq 'ANNULUS'){ if($row == 1){ print REG_HNDL "(((DETX,DETY) in annulus(".$detx[0].",".$dety[0].",".$r_vals[0].",".$r_vals[1]."))"; } else { print REG_HNDL "||((DETX,DETY) in annulus(".$detx[0].",".$dety[0].",".$r_vals[0].",".$r_vals[1]."))"; } } } } } $shap=substr($sh_val,0,8); # End loop over rows } print REG_HNDL ")"; close(REG_HNDL); # End of subroutine edit_region }