#!/usr/bin/perl ### make_raw_envisat.pl use Env qw(INT_SCR INT_BIN SAR_IDL_DIR OUR_SCR VOR_DIR INS_DIR PS_BIN); use lib "$INT_SCR"; #### Location of Generic.pm use Generic; ###Usage info/check sub Usage{ `$INT_SCR/pod2man.pl $PS_BIN/make_raw_envisat.pl`; exit 1; } @ARGV >= 1 or Usage(); @args = @ARGV; $rawdata = shift; #$frame = shift; $orbit_type = "HDR"; #$Lstart = shift; #$Lend = shift; #$Pstart = shift; #$Pend = shift; ################# Message "Checking I/O"; ################# @Infiles = ($rawdata, $auxdata, $orbdata); #@Outfiles = ("$date.raw", "$date.raw.rsc"); #&IOcheck(\@Infiles); Log ("make_raw_envisat.pl", @args); ##ERS GENERAL DIFINITIONS### # #$C = 299792458; ## new value $ONE_WAY_DELAY = 3.4449923715353358e-06; ## old value ##$ONE_WAY_DELAY = -2.0168e-06; #$RANGE_SAMPLING_FREQUENCY = 18.950420e6; #$ANTENNA_SIDE = -1; #$ANTENNA_LENGTH = 12; #$SEC_PER_PRI_COUNT = 210.94e-09; # ################ ## FR 20020903 #$ANTENNA_LENGTH = 10; # ################ ##Soren's paper #$RANGE_SAMPLING_FREQUENCY = 18.962468e6; #$SEC_PER_PRI_COUNT = 210.943006e-9; #$ONE_WAY_DELAY = 6.622e-6 / 2; ##chirp_length 37.12e-6 ##chirp bandwidth 15.50829e6; # #$PLANET_GM = 3.98600448073E+14; #$PLANET_SPINRATE = 7.29211573052E-05; # ###END ERS GENERAL DEFINITIONS ###ENVISAT GENERAL DIFINITIONS #$ONE_WAY_DELAY = $ANTENNA_SIDE = -1; # value from handbook (Eric Fielding)) $ANTENNA_LENGTH_hndbk = 10; #from read_raw.pro #$ANTENNA_LENGTH = 11.1; $chirp_slope = 5.88741157e11; $PLANET_GM = 3.98600448073E+14; $PLANET_SPINRATE = 7.29211573052E-05; $C = 299792458; $pi = atan2(1,1)*4; $r2d = 180/$pi; $ae = 6378137; #WGS84 reference ellipsoid $flat = 1./298.257223563; #$sat = ENVISAT; $sat = ERS1; #trick ###END ENVISAT GENERAL DEFINITIONS open(RAW,$rawdata); for ($i=1; $i<65; $i++) { ($name,$value) = split /\=/,; chomp($value); #Message "Name: $name"; #Message "Value: $value"; if ($name eq "PRODUCT") {@tmp = split /\_/,$value; $orbnum=$tmp[7]; ($unkn,$foo)=split /\./,$tmp[8];} if ($name eq "SWATH") {($foo,$swath)=split /\"/,$value;} if ($name eq "SENSING_START") {($foo,$sense_st)=split /\"/,$value;} if ($name eq "SENSING_STOP") {($foo,$sense_sp)=split /\"/,$value;} if ($name eq "PROC_CENTER") {($foo,$proc_center)=split /\"/,$value;} } close(RAW); ($year,$month,$day,$first_line_utc)= split /\:/, Date_to_UTC($sense_st); $yr=$year; $yr =~ s/^(..)(..)$/\2/; $date = $year.$month.$day; $shortdate = $yr.$month.$day; ($foo,$foo,$foo,$last_line_utc)= split /\:/, Date_to_UTC($sense_sp); $header = $date."_".$orbnum."_".$unkn."_".$swath; ####### Message "Retrieving Orbit file from $VOR_DIR"; ####### @files = `ls -1 $VOR_DIR/DOR_VOR*`; chomp($orbdata = Get_Filename($sense_st, $sense_sp, @files)); Message "Using orbit file: $orbdata"; if (!$orbdata) {Message "Orbit file was not found in $VOR_DIR";exit 1;} ####### Message "Retrieving Instrument file from $INS_DIR"; ####### @files = `ls -1 $INS_DIR/ASA_INS*`; chomp($auxdata = Get_Filename($sense_st, $sense_sp, @files)); Message "Using Instrument file: $auxdata"; if (!$auxdata) {Message "Instrument file was not found in $INS_DIR";exit 1;} ############################# Message "Running IDL script"; ############################# $datfile=$header."_raw_byte.dat"; if (-s $shortdate.".raw") { Message "$shortdate.raw already exists, NOT RUNNING READ_RAW"; } else { $read_raw_args = "/dumpbyte, filename=\"".$rawdata."\", auxname=\"".$auxdata."\""; # $read_raw_args = "/dumpbyte, /debug, filename=\"".$rawdata."\", auxname=\"/data/pkgs6/SAR/ENVISAT_IDL/ASA_INS_AXVIEC20030424_154116_20030428_000000_20031231_235959\""; Message "read_raw,par,$read_raw_args"; open(ARGUS,">read_raw.in"); print ARGUS "read_raw,par,".$read_raw_args; print ARGUS "\nexit\n"; close(ARGUS); system "$SAR_IDL_DIR/idl read_raw.in"; if (-s $datfile) { system "/bin/mv $datfile $shortdate.raw"; } else { Message "read_raw failed, $datfile does not exist"; exit 1; } } $mphfile=$header."_mph.txt"; $logfile=$header."_log.txt"; $focusfile=$header."_focusing.in"; $hdrfile="hdr_data_points_".$shortdate.".rsc"; #if (-s $mphfile && -s $datfile && -s $focusfile){ if (-s $mphfile && -s $focusfile){ ################################ Message "Writing the imagery resource file"; ################################ $center_utc = ($first_line_utc+$last_line_utc)/2; ($foo, $cn_ms) = split /\./, $center_utc; while (length($cn_ms) < 3) {$cn_ms .="0";} $cn_s = ($center_utc - ($cn_ms/1000))%60; while (length($cn_s) < 2) {$cn_s ="0".$cn_s;} $cn_mn = (($center_utc - ($cn_ms/1000) - $cn_s)/60)%60; while (length($cn_mn) < 2) {$cn_mn ="0".$cn_mn;} $cn_hr = (($center_utc - ($cn_ms/1000) - $cn_s)/60 - $cn_mn)/60; while (length($cn_hr) < 2) {$cn_hr ="0".$cn_hr;} #Use_rsc "$shortdate.raw write FIRST_FRAME $frame"; Use_rsc "$shortdate.raw write FIRST_FRAME_SCENE_CENTER_TIME $year$month$day$cn_hr$cn_mn$cn_s$cn_ms"; Use_rsc "$shortdate.raw write FIRST_FRAME_SCENE_CENTER_LINE 000"; Use_rsc "$shortdate.raw write DATE $shortdate"; Use_rsc "$shortdate.raw write FIRST_LINE_YEAR $year"; Use_rsc "$shortdate.raw write FIRST_LINE_MONTH_OF_YEAR $month"; Use_rsc "$shortdate.raw write FIRST_LINE_DAY_OF_MONTH $day"; Use_rsc "$shortdate.raw write FIRST_CENTER_HOUR_OF_DAY $cn_hr"; Use_rsc "$shortdate.raw write FIRST_CENTER_MN_OF_HOUR $cn_mn"; Use_rsc "$shortdate.raw write FIRST_CENTER_S_OF_MN $cn_s"; Use_rsc "$shortdate.raw write FIRST_CENTER_MS_OF_S $cn_ms"; Use_rsc "$shortdate.raw write PROCESSING_FACILITY $proc_center"; open(FOCUS,$focusfile); $foo = ; $foo = ; $foo = ; $width = ; $foo = ; $foo = ; $wavelength = ; $fsamp_hz = ; $chirp_hz = ; $ANTENNA_LENGTH = ; $foo = ; $pulse_length = ; $prf = ; $two_way_delay = ; $length = ; $foo = ; $foo = ; $foo = ; $ibias = ; $qbias = $ibias; close(FOCUS); $Lstart or $Lstart = 1; $Lend or $Lend = $length; $Pstart or $Pstart = 1; $Pend or $Pend = $width; $Pend = $Pend * 2; $last_utc = $first_line_utc+$length/$prf; #Message "Original last line UTC: $last_line_utc New last line UTC: $last_utc"; $starting_range = ($two_way_delay/2) * $C; $range_pixel_size = $C / $fsamp_hz /2; Use_rsc "$shortdate.raw write PLATFORM ENVISAT"; Use_rsc "$shortdate.raw write ORBIT_NUMBER $orbnum"; Use_rsc "$shortdate.raw write STARTING_RANGE $starting_range"; Use_rsc "$shortdate.raw write RANGE_PIXEL_SIZE $range_pixel_size"; Use_rsc "$shortdate.raw write TWO_WAY_DELAY $two_way_delay"; Use_rsc "$shortdate.raw write PRF $prf"; Use_rsc "$shortdate.raw write ANTENNA_SIDE $ANTENNA_SIDE"; Use_rsc "$shortdate.raw write ANTENNA_LENGTH $ANTENNA_LENGTH_hndbk"; Use_rsc "$shortdate.raw write FILE_LENGTH $length"; Use_rsc "$shortdate.raw write XMIN $Pstart"; Use_rsc "$shortdate.raw write XMAX $Pend"; Use_rsc "$shortdate.raw write WIDTH $Pend"; Use_rsc "$shortdate.raw write YMIN $Lstart"; Use_rsc "$shortdate.raw write YMAX $Lend"; Use_rsc "$shortdate.raw write RANGE_SAMPLING_FREQUENCY $fsamp_hz"; Use_rsc "$shortdate.raw write PLANET_GM $PLANET_GM"; Use_rsc "$shortdate.raw write PLANET_SPINRATE $PLANET_SPINRATE"; ###### Message "Reformating Orbit information into HDR style"; ###### Get_Orbit_Data(); ($q1,$q2,$Lat,$Lon,$height_mid, $x0, $y0, $z0, $vx0, $vy0,$vz0) = split /\s+/, `$INT_SCR/state_vector.pl $year$month$day $center_utc $sat $orbit_type $shortdate`; Status "state_vector.pl"; if ($orbit_type eq "HDR"){ $r = sqrt($x0**2+$y0**2+$z0**2); $r1 = sqrt($x0**2+$y0**2); $Lat = atan2($z0,$r1); $Lon = atan2($y0,$x0); $H = $r-$ae; for ($i=1; $i<7; $i++){ $N = $ae/(sqrt(1-$flat*(2-$flat)*sin($Lat)**2)); $TanLat = $z0/$r1/(1-(2-$flat)*$flat*$N/($N+$H)); $Lat = atan2($TanLat,1); $H = $r1/cos($Lat)-$N; } $height_mid=$H; } $N = $ae/sqrt(1-$flat*(2-$flat)*sin($Lat)**2); $earth_radius_mid=$N; $ve=-sin($Lon)*$vx0+cos($Lon)*$vy0; $vn=-sin($Lat)*cos($Lon)*$vx0-sin($Lat)*sin($Lon)*$vy0+cos($Lat)*$vz0; $hdg = atan2($ve,$vn); $e2 = $flat*(2-$flat); $M = $ae*(1-$e2)/(sqrt(1-$e2*sin($Lat)**2))**3; $earth_radius_mid_old = $N*$M/($N*(cos($hdg))**2+$M*(sin($hdg))**2); Message "old earthradius: $earth_radius_mid_old new earthradius: $earth_radius_mid"; ($q1,$q2,$q3,$q4,$height_top, $x0, $y0, $z0, $vx, $vy,$vz) = split /\s+/, `$INT_SCR/state_vector.pl $year$month$day $first_line_utc $sat $orbit_type $shortdate`; Status "state_vector.pl"; if ($orbit_type eq "HDR"){ $r = sqrt($x0**2+$y0**2+$z0**2); $r1 = sqrt($x0**2+$y0**2); $Lat = atan2($z0,$r1); $Lon = atan2($y0,$x0); $H = $r-$ae; for ($i=1; $i<7; $i++){ $N = $ae/(sqrt(1-$flat*(2-$flat)*sin($Lat)**2)); $TanLat = $z0/$r1/(1-(2-$flat)*$flat*$N/($N+$H)); $Lat = atan2($TanLat,1); $H = $r1/cos($Lat)-$N; } $height_top=$H; } $height_dt=($height_mid-$height_top)/($center_utc-$first_line_utc); if ($vz0 > 0) {$orbit_direction = "ascending";} else {$orbit_direction = "descending";} $velocity_mid=sqrt($vx0**2 + $vy0**2 + $vz0**2); $Latd=$Lat*$r2d; $Lond=$Lon*$r2d; $hdgd=$hdg*$r2d; Use_rsc "$shortdate.raw write FIRST_LINE_UTC $first_line_utc"; Use_rsc "$shortdate.raw write CENTER_LINE_UTC $center_utc"; Use_rsc "$shortdate.raw write LAST_LINE_UTC $last_line_utc"; Use_rsc "$shortdate.raw write HEIGHT $height_top"; Use_rsc "$shortdate.raw write HEIGHT_DT $height_dt"; Use_rsc "$shortdate.raw write VELOCITY $velocity_mid"; Use_rsc "$shortdate.raw write LATITUDE $Latd"; Use_rsc "$shortdate.raw write LONGITUDE $Lond"; Use_rsc "$shortdate.raw write HEADING $hdgd"; Use_rsc "$shortdate.raw write EARTH_RADIUS $earth_radius_mid"; Use_rsc "$shortdate.raw write ORBIT_DIRECTION $orbit_direction"; Use_rsc "$shortdate.raw write FILE_START $file_start"; Use_rsc "$shortdate.raw write WAVELENGTH $wavelength"; Use_rsc "$shortdate.raw write PULSE_LENGTH $pulse_length"; Use_rsc "$shortdate.raw write CHIRP_SLOPE $chirp_slope"; Use_rsc "$shortdate.raw write I_BIAS $ibias"; Use_rsc "$shortdate.raw write Q_BIAS $qbias"; ############################### Message "Doppler Computation"; ############################### #$line_0=100; #$line_1=$length - 200; # #$dop_rng1=0; #$dop_rng2=0; #$dop_rng3=0; # #`$INT_BIN/dopiq < ) ){ if( $Line =~ /Peg Lat\/Lon , H =\s+(\S+)\s+(\S+)\s+(\S+)/ ){ $Latd = $1; $Lond = $2; } if( $Line =~ /Peg Heading =\s+(\S+)/ ){ $hdgd = $1; } if( $Line =~ /Vertical Fit:\s+(\S+)\s+(\S+)\s+(\S+)/ ){ @Height_poly = ( $1, $2, $3 ); } if( $Line =~ /Platform SCH Velocity \(m\/s\):\s+(\S+)\s+(\S+)\s+(\S+)/ ){ @VelocitySCH = ( $1, $2, $3 ); $velocity_mid = Norm( @VelocitySCH ); } if( $Line =~ /Platform SCH Acceleration \(m\/s\^2\):\s+(\S+)\s+(\S+)\s+(\S+)/ ){ @AccelerationSCH = ( $1, $2, $3 ); } } close PEGOUT; Use_rsc "$shortdate.raw write HEIGHT $Height_poly[0]"; Use_rsc "$shortdate.raw write HEIGHT_DS $Height_poly[1]"; Use_rsc "$shortdate.raw write HEIGHT_DDS $Height_poly[2]"; Use_rsc "$shortdate.raw write VELOCITY $velocity_mid"; Use_rsc "$shortdate.raw write VELOCITY_S $VelocitySCH[0]"; Use_rsc "$shortdate.raw write VELOCITY_C $VelocitySCH[1]"; Use_rsc "$shortdate.raw write VELOCITY_H $VelocitySCH[2]"; Use_rsc "$shortdate.raw write ACCELERATION_S $AccelerationSCH[0]"; Use_rsc "$shortdate.raw write ACCELERATION_C $AccelerationSCH[1]"; Use_rsc "$shortdate.raw write ACCELERATION_H $AccelerationSCH[2]"; Use_rsc "$shortdate.raw write LATITUDE $Latd"; Use_rsc "$shortdate.raw write LONGITUDE $Lond"; Use_rsc "$shortdate.raw write HEADING $hdgd"; Use_rsc "$shortdate.raw write RLOOKS 1"; Use_rsc "$shortdate.raw write ALOOKS 1"; Use_rsc "$shortdate.raw write BEAM $swath"; } else { Message "IDL script did not complete successfully"; exit 1; } exit 0; sub Get_Orbit_Data() { local($date); local($input); local($i); local($j); local($sense_st); local($sense_sp); local($st_utc); local($sp_utc); local($orbnum); open(ORBIT,$orbdata); for ($i=1; $i<44; $i++) { ($name,$value) = split /\=/,; chomp($value); if ($name eq "SENSING_START") {($foo,$sense_st)=split /\"/,$value;} if ($name eq "SENSING_STOP") {($foo,$sense_sp)=split /\"/,$value;} if ($name eq "NUM_DSD") {$j = $value*8;} } ($st_yr,$st_mon,$st_day,$st_utc) = split /\:/, Date_to_UTC($sense_st); ($sp_yr,$sp_mon,$sp_day,$sp_utc) = split /\:/, Date_to_UTC($sense_sp); CHECK_ORB_DATE: { if (($sp_yr < $year ) || ($st_yr > $year )) {Message "Orbit file is for wrong year"; exit 1;} if ($sp_yr > $year ) {$sp_mon += 12}; if ($st_yr < $year ) {$st_mon -= 12}; if (($sp_mon < $month) || ($st_mon > $month)) {Message "Orbit file is for wrong month"; exit 1;} if ($sp_mon > $month) {$sp_day += 31;} if ($st_mon < $month) {$sp_day -= 31;} if (($sp_day < $day) || ($st_day > $day)) {Message "Orbit file is for wrong day"; exit 1;} if ($sp_day > $day) {$sp_utc += 86400;} if ($st_day < $day) {$st_utc -= 86400;} if (($sp_utc < $first_line_utc) || ($st_utc > $last_line_utc)) {Message "Orbit file is for wrong time"; exit 1;} } for ($i; $i<44+$j; $i++) { ($name,$value) = split /\=/,; chomp($value); } $i=0; while () { ($caldate, $time, $foo, $orbnum, $x, $y, $z, $vx, $vy, $vz, $foo) = split /\s+/; $input = $caldate." ".$time; ($orb_yr, $orb_mon, $orb_day,$utc_time) = split /\:/, Date_to_UTC($input); if ($orb_day == $day) { if (abs($utc_time - $center_utc) <= 175) { $buff[$i]=$utc_time." ".$x." ".$y." ".$z." ".$vx." ".$vy." ".$vz; $i++; } } } open(HDR,">$hdrfile"); foreach $line (@buff) {print HDR "${line}\n";} close(HDR); close(ORBIT); } sub Get_Filename() { local($datastart, $datastop, @files) = @_; local($orbpath, $orbname, $startdate, $stopdate); local($st_year, $st_mon, $st_day); ($dst_year,$dst_mon,$dst_day,$dst_utc)= split /\:/, Date_to_UTC($datastart); $dst_caldate = "$dst_year"."$dst_mon"."$dst_day"; $dst_ddate = Date_to_DecDate($dst_caldate); ($dsp_year,$dsp_mon,$dsp_day,$dsp_utc)= split /\:/, Date_to_UTC($datastop); $dsp_caldate = "$dsp_year"."$dsp_mon"."$dsp_day"; $dsp_ddate = Date_to_DecDate($dsp_caldate); foreach $file (@files) { ($foo,$foo,$foo,$foo,$orbstart, $orb_sttime, $orbstop, $orb_sptime)=split /\_/, $file; $sp_ddate = Date_to_DecDate($orbstop); if ($sp_ddate >= $dst_ddate) { $st_ddate = Date_to_DecDate($orbstart); if ($st_ddate <= $dsp_ddate) { if ($orbstop != $dsp_caldate && $orbstart != $dst_caldate) {$orbname=$file; last;} if ($orbstop == $dsp_caldate && $orb_sptime > $dsp_utc) { if ($orbstart != $dst_caldate) {$orbname=$file; last;} if ($orbstart == $dst_caldate && $orb_sttime < $dst_utc) {$orbname=$file; last;} } } } } return $orbname; } sub Date_to_UTC() { local($full_date) = @_; local($caldate); local($time); local($date); local($mon); local($year); local($day); local($hr); local($mn); local($s); local($ms); local($time_utc); ($caldate,$time)=split /\ /,$full_date; ($day,$mon,$year)=split /\-/,$caldate; ($hr, $mn, $s)=split /\:/,$time; ($s, $ms)=split /\./,$s; $ms =~ s/^(\d{3})(\d*)$/\1/; MONTH: { if ($mon eq "JAN") {$mon="01";last MONTH;} if ($mon eq "FEB") {$mon="02";last MONTH;} if ($mon eq "MAR") {$mon="03";last MONTH;} if ($mon eq "APR") {$mon="04";last MONTH;} if ($mon eq "MAY") {$mon="05";last MONTH;} if ($mon eq "JUN") {$mon="06";last MONTH;} if ($mon eq "JUL") {$mon="07";last MONTH;} if ($mon eq "AUG") {$mon="08";last MONTH;} if ($mon eq "SEP") {$mon="09";last MONTH;} if ($mon eq "OCT") {$mon="10";last MONTH;} if ($mon eq "NOV") {$mon="11";last MONTH;} if ($mon eq "DEC") {$mon="12";last MONTH;} } $time_utc = ($hr * 3600) + ($mn * 60) + $s + $ms/1000; $output = $year.":".$mon.":".$day.":".$time_utc; return $output; } sub Date_to_DecDate() { local($caldate) = @_; local($mon); local($year); local($day, $days); $_ = $caldate; s/^(.{4})(..)(..)$/\1\2\3/; ($year, $mon, $day) = ($1, $2, $3); DAYS_OF_MONTH: { if ($mon eq "01") {$days=0;last DAYS_OF_MONTH;} if ($mon eq "02") {$days=31;last DAYS_OF_MONTH;} if ($mon eq "03") {$days=59;last DAYS_OF_MONTH;} if ($mon eq "04") {$days=90;last DAYS_OF_MONTH;} if ($mon eq "05") {$days=120;last DAYS_OF_MONTH;} if ($mon eq "06") {$days=151;last DAYS_OF_MONTH;} if ($mon eq "07") {$days=181;last DAYS_OF_MONTH;} if ($mon eq "08") {$days=212;last DAYS_OF_MONTH;} if ($mon eq "09") {$days=243;last DAYS_OF_MONTH;} if ($mon eq "10") {$days=273;last DAYS_OF_MONTH;} if ($mon eq "11") {$days=304;last DAYS_OF_MONTH;} if ($mon eq "12") {$days=334;last DAYS_OF_MONTH;} } $julday = $days + $day; $div = 365; if ($year % 4 == 0 && $mon > 2) {$julday++;$div++} $decdate = $year+($julday/$div); return $decdate; } =pod =head1 USAGE B I< rawdata_filename> =head1 FUNCTION Creates I.raw and I.raw.rsc from level 0 envisat data files =head1 ROUTINES CALLED idl read_raw state_vector.pl dopiq-new GetPeg.pl DopUnw.pl =head1 CALLED BY none =head1 FILES USED Level_0_Datafile =head1 FILES CREATED I.raw I.raw.rsc dop.out =head1 HISTORY Shell Script : Francois ROGEZ 96/98 Perl Script : Rowena LOHMAN 04/18/98 Modifications: Frederic CRAMPE, Oct 21, 1998 Hacked for ENVISAT: Ingrid JOHANSON, Feb 19 2004 =head1 LAST UPDATE Frederic CRAMPE, Oct 06, 1999 =cut