#!/usr/local/bin/perl -w # # gp.UpdateXYZ # # Script to add new entries to XYZ files # overwriting existing entries if necessary. # (Adapted from gp.UpdateLnev) # # .xyz file format: tab-delimited # # Formerly it was fixed format with this format # Width Entry (1 space between each field after first three) # ----- ----- # 4 0 yyyy Year # 2 4 mm Month # 2 6 dd Day # # 2 9 nn P(reliminary) or F(inal) flag # 8 12 yyy.ffff Year and fractional year # 3 20 nnn Day of year (sometimes called julian day) # 15 24 sxxxxxxx.yyyy X component (m) # 7 40 xx.yyyy X sigma (m) # 15 48 sxxxxxxx.yyyy Y component (m) # 7 66 xx.yyyy Y sigma (m) # 15 74 sxxxxxxx.yyyy Z component (m) # 7 90 xx.yyyy Z sigma (m) # 8 98 sx.yyyyyyEszz Y-X covariance (m) # 8 107 sx.yyyyyyEszz Z-X covariance (m) # 8 116 sx.yyyyyyEszz Z-Y covariance (m) # 50 125 free format Comment including date processed # # W. Prescott 2 October 1996 # # Modifications: # 1996/11/07 whp Installed call to this subroutine in gp.results # 1996/11/29 whp Converted to a tab-delimited xyz file. # 1997/03/21 whp Read path to xyz directory from argument list. # 1998/10/16 whp XYZs for Track are kept separate from # XYZs for other campaigns. # Fixes an undesirable feature that mixed these. # Changed number of arguments # 2000/02/16 whp Path now points to the directory containing xyz files # (Formerly it pointed to a higher level directory # i.e. Path now should be /GPSdata/postprocess/xyz/Campaign # formerly it was /GPSdata/postprocess/xyz) # 2001/01/30 whp Set $ENV{PATH}, $ENV{SHELL}, $ENV{IFS} # to avoid security violation in suid script # $ENV{PATH} = "/bin:/usr/bin:/goa/local/gp_dir"; $ENV{SHELL} = "/bin/sh"; $ENV{IFS} = "" if !defined $ENV{IFS} || $ENV{IFS} ne ""; # use untaint qw(untaint); sub untaint { # from Programming Perl, 2nd Ed., p. 358 my ($string) = @_; if ( $string =~ m@^([-\@\w./]+)$@ ) { $string = $1; } else { die "Bad data in $string\n"; } return $string; } # Check number of arguments. # ------------------------- if ($#ARGV != 3) { print "Usage: gp.UpdateXYZ takes four arguments\n"; print " StaCovFileName "; print " Label "; print " Path-to-xyz-directory "; print " Campaign-name\n"; print " e.g. gp.UpdateXYZ Bard.Track.19961002.stacov.fixed "; print " Label XYZDir campaign\n"; exit 1; } $Filename = $ARGV[0]; $Label = $ARGV[1]; $Path = $ARGV[2]; $Campaign = $ARGV[3]; # Get solution date from SaveObsDate # (Note: This assumes that a SaveObsDate # file exists. # An alternative is to create one # after first line of stacov is read.) # -------------------------------------------- open (DateHdl, "SaveObsDate") || die "SaveObsDate not found"; @Line = grep (/datelong/,); ($skip,$datelong) = split(' ',$Line[0],3); seek (DateHdl,0,0); @Line = grep (/decyr/,); ($skip,$decyr) = split(' ',$Line[0],3); seek (DateHdl,0,0); @Line = grep (/doy/,); ($skip,$doy) = split(' ',$Line[0],3); close (DateHdl); # Find type of orbit used for this solution # ----------------------------------------- open (OrbitHdl, "ProcessFlags"); while () { ($abbrev,$value) = split(" ",$_); $Options{$abbrev} = $value; } close (OrbitHdl); $Flag = $Options{"OrbitType"}; if ($Flag eq "") {$Flag = "F";} # Read stacov file # ------------- open (StaCovHdl, "$Filename"); # Read and store coordinates # -------------------------- $NumLines = 0; while () { $Line[$NumLines] = $_; $NumLines++; } close (StaCovHdl); $STAflag = "1"; # true $VELflag = "0"; # false $CurrentLineNo = 0; $CurrentStationNo = 0; while ($STAflag) { $CurrentLineNo++; $ID = substr($Line[$CurrentLineNo],12,3); if ($ID ne "STA") { $CurrentLineNo--; $STAflag = "0"; # false $NoStations = $CurrentLineNo/3; if ($ID eq "VEL") {$VELflag = "1";} # true next; } $TempStationName = substr($Line[$CurrentLineNo],7,4); ($StationName = $TempStationName) =~ tr/A-Z/a-z/; $Names[$CurrentStationNo] = $StationName; $XX{$StationName} = substr($Line[$CurrentLineNo],17,32); $SX{$StationName} = substr($Line[$CurrentLineNo],51,23); $CurrentLineNo++; $YY{$StationName} = substr($Line[$CurrentLineNo],17,32); $SY{$StationName} = substr($Line[$CurrentLineNo],51,23); $CurrentLineNo++; $ZZ{$StationName} = substr($Line[$CurrentLineNo],17,32); $SZ{$StationName} = substr($Line[$CurrentLineNo],51,23); $CurrentStationNo++; } # Skip velocities # --------------- if ($VELflag) { $CurrentLineNo += 3*$NoStations; } $Campaign = &untaint($Campaign); # Read and store covariances # -------------------------- for ($i=0; $i<$NoStations; $i++) { # Skip unneeded values $CurrentLineNo += 2*3*$i; $CurrentLineNo++; $SYX{$Names[$i]} = substr($Line[$CurrentLineNo],12,24); # Skip unneeded values $CurrentLineNo += 3*$i; $CurrentLineNo++; $SZX{$Names[$i]} = substr($Line[$CurrentLineNo],12,24); $CurrentLineNo++; $SZY{$Names[$i]} = substr($Line[$CurrentLineNo],12,24); } # End loop through stacov file # Write solutions to XYZ files # ---------------------------- for ($i=0; $i<$NoStations; $i++) { # Note 0 is false, 1 (and everything else) is true. # ------------------------------------------------- $Names[$i] = &untaint($Names[$i]); chomp($StationInCamp = `StaInCampTF.sh $Campaign $Names[$i]`); if (!$StationInCamp) { next;} # Open old XYZ file # ----------------- $Fullname = "$Path/$Names[$i].xyz"; %XYZ = (); if (open (XYZHdl, $Fullname)) { # Read contents # ------------- while () { $Date = substr($_,0,8); $XYZ{$Date} = $_; } close (XYZHdl); } else { print "gp.UpdateXYZ: Cannot open old $Fullname. ", "New station?\n"; } # Add current solution (or replace old one) # ----------------------------------------- # Get current time and date # ------------------------- $DateTime = `CurrTimeDate.pl`; $Comment = "Updated:$DateTime-by-gp.UpdateXYZ from $Label"; # Create tab-delimited string # --------------------------- $String = sprintf("%8.8s \t %2.2s \t %8.4f \t %3d \t ", $datelong,$Flag,$decyr,$doy); $String .= sprintf("%15.4f \t %7.4f \t ", $XX{$Names[$i]},$SX{$Names[$i]}); $String .= sprintf("%15.4f \t %7.4f \t ", $YY{$Names[$i]},$SY{$Names[$i]}); $String .= sprintf("%15.4f \t %7.4f \t ", $ZZ{$Names[$i]},$SZ{$Names[$i]}); $String .= sprintf("%8.4f \t ", $SYX{$Names[$i]}); $String .= sprintf("%8.4f \t ", $SZX{$Names[$i]}); $String .= sprintf("%8.4f \t ", $SZY{$Names[$i]}); $String .= sprintf("%s\n", $Comment); # Create fixed format string # -------------------------- # $String = sprintf("%8.8s %2.2s %8.4f %3d ", # $datelong,$Flag,$decyr,$doy); # $String .= sprintf("%15.4f %7.4f ", # $XX{$Names[$i]},$SX{$Names[$i]}); # $String .= sprintf("%15.4f %7.4f ", # $YY{$Names[$i]},$SY{$Names[$i]}); # $String .= sprintf("%15.4f %7.4f ", # $ZZ{$Names[$i]},$SZ{$Names[$i]}); # $String .= sprintf("%8.4f ", # $SYX{$Names[$i]}); # $String .= sprintf("%8.4f ", # $SZX{$Names[$i]}); # $String .= sprintf("%8.4f ", # $SZY{$Names[$i]}); # $String .= sprintf("%-50.50s\n", # $Comment); $XYZ{$datelong} = $String; # Save updated XYZ file # --------------------- $Fullname = &untaint($Fullname); open (XYZHdl, ">$Fullname.new"); foreach $Key (sort(keys %XYZ)) { print (XYZHdl $XYZ{$Key}); } close (XYZHdl); if (-e "$Fullname.new") { system ("mv -f $Fullname.new $Fullname"); } } # End for loop on number of stations in stacov file.