#!/usr/bin/perl # Routine to find and remove the spurious FITS headers from some of the # NNSN FITS files, add a block of nulls to make the byte counts work, and # insert an explanatory NOTE into the corresponding PDS labels. # # Format: fixshort [fits_file_list] # # 23 Aug 2006, acr. # #============================================================================ $TMPFILE = "__bob.tmp"; $FMT = "%-78.78s\r\n"; # Label line output format # Initialize a block of nulls to write on the end of affected FITS files: for ($i=0; $i<2880; $i++) { substr($nullbuff,$i,1) = "\x00"; } # Loop through the input files: while ($file = shift @ARGV) { open (FIT,"$file") || die "Could not open $file ($!)"; open (TMP,">$TMPFILE") || die "Could not open $TMPFITS ($!)"; # Transfer the first two blocks, which are always OK: read(FIT,$block,5760); syswrite(TMP,$block,5760); # Read the third block and look for a spurious label: read(FIT,$block,2880); if (substr($block,0,10) ne "SIMPLE = ") # Not a label - nothing to do { close(FIT); close(TMP); unlink $TMPFILE; printf "$file is OK\n"; next; } # All right, we know a couple things: We have a file that needs # correcting; and the spurious FITS header is only one block long. # So, we'll begin by transferring the rest of the existing FITS # blocks to the temp file: while (!eof FIT) { read(FIT,$block,2880); syswrite(TMP,$block,2880); } close(FIT); # Now add the block of null bytes, to balance the byte counts: syswrite(TMP,$nullbuff,2880); # And we're done with the FITS file: close(TMP); rename $TMPFILE, $file; # Now, we need to find the corresponding PDS label and add a note: $labelfile = $file; $labelfile =~ s/fit$/lbl/; open(LBL,$labelfile) || die "Could not open $labelfile ($!)"; open(TMP,">$TMPFILE") || die "Could not open $TMPFILE ($!)"; # We want to insert a NOTE just above the "END_OBJECT = IMAGE" line # in the label (because it's easy to find), so we read through the # label until we find that line: $line = ; while ($line !~ /END_OBJECT\s*=\s*IMAGE/) { printf TMP $line; $line = ; } # Now we insert the note: printf TMP $FMT," NOTE = \""; printf TMP $FMT," This file was damaged during reprocessing in 1990."; printf TMP $FMT," As a result, the last 3.5 records in the image "; printf TMP $FMT," contain null bytes."; printf TMP $FMT," \""; # And transfer the rest of the label: printf TMP $line; while (!eof LBL) { $line = ; printf TMP $line; } close(TMP); close(LBL); rename $TMPFILE, $labelfile; }