#!/usr/bin/perl # Routine to detach PDS header from CIDA tables and insert format files # in-line. # # 30 Aug 2005, acr. # #------------------------------------------------------------------------- $LBLTMP = "__LABEL.HOLD"; $TABTMP = "__TABLE.HOLD"; $NEWTMP = "__LABEL2.HOLD"; foreach $file (@ARGV) { # First, do a straight split of the header from the data, writing each # into its own temp file: open (INP,"$file" ) || die "Could not open '$file' for reading (!$)"; open (TMPLBL, "> $LBLTMP") || die "Could not open $LBLTMP for processing ($!)"; open (TMPTAB, "> $TABTMP") || die "Could not open $TABTMP for processing ($!)"; $line=; while ($line !~ /^END\s*$/) { printf TMPLBL $line; $line=; } printf TMPLBL $line; close(TMPLBL); $records = 0; while ($line=) { printf TMPTAB $line; $records++; } close(TMPTAB); # Get the size of the old header: $labelsize = -s $LBLTMP; $datasize = -s $TABTMP; # And now we're ready to re-write the label: open (INP,"rmcr $LBLTMP | ") || die "Could not re-open label file ($!)"; open (TMP,"| ppodl -p > $NEWTMP") || die "Could not open output file ($!)"; while ($line=) { if ($line =~ /^RECORD_TYPE /) { printf TMP $line; $line = "FILE_RECORDS = $records"; } elsif ($line =~ /_TABLE\s*=\s*([0-9]+)/) { $offset = $1 - $labelsize; $line =~ s/$1/("$file",$offset)/; } elsif ($line =~ /^\s*\^STRUCTURE\s*=\s*"(.*)"/) { $format = $1; $format =~ tr/A-Z/a-z/; # Skip forward to the end of the keywords: $line = ; while ($line !~ /END_OBJECT/) { printf TMP $line; $line = ; } # Copy in the format file: open (FMT,$format) || die "Could not open $format for input ($!)"; while ($fline=) { printf TMP $fline; } close(FMT); # And continue: } printf TMP $line; } close(TMP); # Now we replace the input files and delete the leftover temporary file: $labelname = $file; $labelname =~ s/\.tab/.lbl/; unlink $LBLTMP; rename $TABTMP, $file; rename $NEWTMP, $labelname; }