#!/usr/bin/perl # Routine to fix the reduced data tabulation files that mention the old # file names. The data files and the associate labels are both updated. # # Format: fixfebfn "date" # # where "date" is a 4-character string with the month/day digits to be used # in the file names. Note that this must be quoted. # #============================================================================== use File::Find; # Save the argument: die "Usage: fixfeb \"date\". \n" if (@ARGV != 1); $date = $ARGV[0]; # Run: find({wanted => \&fixit},"."); # That's it for the main routine. #------------------------------------------------------------------------------ sub fixit # Routine to adjust byte counts in label files and file name values in # table files. We are cd'ed to the local directory when this runs. { my ($filename,$line); my ($datafile); my $TMPFILE = "zzzTMP"; $filename = $_; # We're looking for .lbl and .tab files that are NOT observational: return if ($filename =~ /^kp[0-9]/); # In the data files we need to change the file name, which is the first # field in the delimited table: if ($filename =~ /tab$/) { open (OLD, $filename) || die "Could not open $File::Find::name for reading ($!)."; open (NEW, ">$TMPFILE") || die "Could not open $File::Find::dir/$TEMPFILE for writing ($!)."; while ($line=) { $line =~ s/^"n[a-z]0([0-9]{3})"/"kp05${date}_$1"/; printf NEW $line; } close(NEW); close(OLD); rename $TMPFILE, $filename; } # In the label files, we want to add 6 bytes to the length of the # IMAGE_NAME column, which is always the first column, and then add # 6 to the START_BYTE of every column after the first one. if ($filename =~ /lbl$/) { open (OLD, "varlen -rmcr $filename |") || die "Could not open $File::Find::name pipe for reading ($!)."; open (NEW, "| fixlen -c >$TMPFILE") || die "Could not open $File::Find::dir/$TMPFILE pipe for writing ($!)."; # Transfer lines until ROW_BYTES, which will need updating: $line = ; while ($line !~ /^\s*RECORD_BYTES/) { printf NEW $line; $line = ; } $line =~ /([0-9]+)\s*$/; $bytes = $1; $bytes += 6; $line =~ s/[0-9]+/$bytes/; printf NEW $line; # Transfer bytes looking for ROW_BYTES, which will need updating: $line = ; while ($line !~ /^\s*ROW_BYTES\s*=/) { printf NEW $line; $line = ; } $line =~ /([0-9]+)\s*/; $bytes = $1; $bytes += 6; $line =~ s/[0-9]+/$bytes/; printf NEW $line; # Transfer lines until the first BYTES line is found: $line = ; while ($line !~ /^\s*BYTES\s*=\s*([0-9]+)\s*$/) { printf NEW $line; $line = ; } $line =~ /^\s*BYTES\s*=\s*([0-9]+)\s*$/; $bytes = $1; # The filename change add 6 bytes to this field $bytes += 6; $line =~ s/[0-9]+/$bytes/; # the only digits on the line printf NEW $line; # The next line is always the related FORMAT statement, where the # number of bytes is repeated as part of the format: $line = ; $line =~ s/[0-9]+/$bytes/; printf NEW $line; # Now we want to loop through all the START_BYTE fields, and ultimately # to the end of the file: while ($line=) { if ($line =~ /^\s*START_BYTE\s*=\s+([0-9]+)\s*$/) { $bytes = $1 + 6; $line =~ s/[0-9]+/$bytes/; } printf NEW $line; } # Should be done: close(OLD); close(NEW); rename $TMPFILE, $filename; } return; }