#!/usr/bin/perl # hans muller, sax/sdc/sron 12 05 2000 # Script to get filename of appropriate "nearest" SAX/WFC response # matrix for a given date and [x,y] detector pixel # Written not that elegantly, but somehow works alright # Script explains itself, when called without parameters: if ( $ARGV[0] == "" && $ARGV[1] == 0 && $ARGV[2] == 0 && $ARGV[3] == 0 ) { print " ********* script which_rmf.pl ******** \n"; print " * Find the nearest WFC responsematrix* \n"; print " * for a given wfc, date and pixel * \n"; print " * * \n"; print " * Parameters: * \n"; print " * 1. mod. julian daynumber * \n"; print " * 2. wfc (w1 or w2) * \n"; print " * 3. xpix * \n"; print " * 4. ypix * \n"; print " * * \n"; print " * Examples: * \n"; print " * >> which_rmf 51550 w1 100 375 * \n"; print " * >> which_rmf 51550.3 w1 36.5 405.5 * \n"; print " ************************************** \n"; } else { # initialise input variables #--------------------------- $mjd = $ARGV[0]; $wfc = $ARGV[1]; @inpos = ( $ARGV[2], $ARGV[3] ); $separ = "_"; # error condition: pixel out of bounds #------------------------------------- if ( $ARGV[2] > 510 || $ARGV[2] < 0 || $ARGV[3] > 510 || $ARGV[3] < 0 || $ARGV[2] == 0 && $ARGV[3] == 0 ) { print " WFC pixel values have to lie between 0 and 511, exit \n"; exit(); } # error condition: date too early #-------------------------------- if ( $ARGV[0] < 50200 ) { print " Mjd date = ", $ARGV[0] , ": SAX was not yet launched, exit \n"; exit(); } # error condition: date too late #-------------------------------- if ( $ARGV[0] > 52000 ) { print " Mjd date = ", $ARGV[0] , ": SAX will have gone by then, exit \n"; exit(); } # error condition: wrong instrument #-------------------------------- if ( $ARGV[1] ne "w1" && $ARGV[1] ne "w2" && $ARGV[1] ne "wfc1" && $ARGV[1] ne "wfc2" ) { print " WFC instrument ", $ARGV[1], " is unknown , exit \n"; exit(); } # change wfc names to file convention #------------------------------------- if ( $wfc == "w1" ) { $wfc = "wfc1";} if ( $wfc == "w2" ) { $wfc = "wfc2";} # define response matrix pixel positions (grid of 5x5) # and epochs (8, mod. julian date) #-------------------------------------- @where = ( 90, 173, 255, 337, 420 ); @when = ( 50265, 50449, 50630, 50814, 50995, 51179, 51360, 51544 ); @date_string = ( "01jul96", "01jan97", "01jul97", "01jan98", "01jul98", "01jan99", "01jul99", "01jan00"); $minimum = 100000.0; $minimum_t = 100000.0; # loop over response matrix positions to find the # most suitable response matrix: i.e., the one defined # for the position closest by # number of grid positions: 5 x 5 #---------------------------------------------------- foreach $i(0..4) { foreach $j(0..4) { @rmfpos = ($where[$i], $where[$j]); &distance ( @inpos, @rmfpos ); if ( $distance < $minimum ) { $minimum = $distance ; $separ1 = "_"; $separ2 = "_"; if ( $where[$i] < 100 ) { $separ1 = "_0";} if ( $where[$j] < 100 ) { $separ2 = "_0";} $rmf_file = "${wfc}${separ1}$where[$i]${separ2}$where[$j]"; # $uncor_rmf_file = "${wfc}_u_$where[$i]_$where[$j].rmf"; } } } #loop over dates to get nearest epoch #------------------------------------ foreach $i(0..7) { &time_distance ( $when[$i], $mjd ); if ( $time_distance < $minimum_t ) { $minimum_t = $time_distance ; $when_min = $when[$i]; $imin = $i; } } #print " Minimum time distance = ", $minimum_t, " days for mjd ", $when_min, " rmf string = ", $date_string[$imin],"\n"; # conclude to response matrix file name #-------------------------------------- $rmf_file = $rmf_file."_".$date_string[$imin].".rmf"; #print " Rmf file = ", $rmf_file , "\n"; # output minimum distance and suitable response matrix file name #-------------------------------------------------------------- $~ = 'MYFORMAT'; write; # calculate the Euclidean distance between 2 points #-------------------------------------------------- sub distance { $diff_x = $_[0] - $_[2]; $diff_y = $_[1] - $_[3]; $distance = sqrt ( $diff_x*$diff_x + $diff_y*$diff_y ) ; # print " Distance between ($_[0],$_[1]) and ($_[2],$_[3]) = " , $distance , "\n" ; } # calculate the time distance between 2 dates #-------------------------------------------- sub time_distance { $diff_t = $_[0] - $_[1]; $time_distance = abs ( $diff_t ); # print " Distance between ($_[0],$_[1]) = ", $time_distance, "\n" ; } format MYFORMAT = Distance =@###.# pixels, @###.# days to closest responsematrix @<<<<<<<<<<<<<<<<<<<<<<< $minimum $minimum_t $rmf_file . }