#!/usr/bin/perl
# Routine to reformat the PDS IMG files in the Stardust directory into FITS
# files with detached PDS labels in a new directory.
$SRCDIR = "../data/calibration";
$NEWDIR = "../data/new_cal";
$TMPLBL = "TMP.LBL";
$TMPHDR = "TMP.HDR";
$TMPDAT = "TMP.DAT";
opendir(SRC,$SRCDIR);
foreach $file (readdir(SRC))
{ next if ($file !~ /\.IMG$/);
run_label($file);
system "spltimg < $SRCDIR/$file | swpinv | fitspad -n > $TMPDAT";
$fits = "$NEWDIR/$file";
$fits =~ tr/A-Z/a-z/;
$fits =~ s/\.img$/.fit/;
system "cat $TMPHDR $TMPDAT > $fits";
unlink $TMPHDR;
unlink $TMPDAT;
}
closedir(SRC);
#===========================================================================
sub run_label
# Open image file, read it to create new PDS label and FITS header.
{ local ($file) = $_[0];
my ($img,$label,$not_done);
my ($fitsfile,$junk);
$img = "$SRCDIR/$file";
$label = "$NEWDIR/$file";
$label =~ tr/A-Z/a-z/;
$label =~ s/\.img/.lbl/;
$fitsfile = $file;
$fitsfile =~ s/IMG/FIT/;
# Open the various files:
open(IMG,$img) || die "Could not open source image pipe ($!)";
open(LBL,"| fixlen -c > $label") || die "Could not open label pipe ($!)";
open(HDR,"| fixlen -r 81 | mkstrm | fitspad >$TMPHDR") ||
die "Could not open header pipe ($!)";
# Digging through the label file:
$not_done = 1;
while (($line=) && $not_done)
{ next if ($line =~ /LABEL_RECORDS/);
next if ($line =~ /CHECKSUM/);
next if ($line =~ /MAXIMUM/);
next if ($line =~ /MINIMUM/);
next if ($line =~ /^ *MEAN *=/);
next if ($line =~ /STANDARD_DEVIATION/);
if ($line =~ /RECORD_BYTES/)
{ $line =~ s/4096/2880/;}
elsif ($line =~ /FILE_RECORDS/)
{ $line =~ s/1024/1455/; }
elsif ($line =~ /\^IMAGE/)
{ printf LBL "^FITS_HEADER = (\"$fitsfile\",1)\n";
$line =~ s/3/("$fitsfile",2)/;
}
elsif ($line =~ /SAMPLE_TYPE/)
{ printf LBL " AXIS_ORDER_TYPE = \"FIRST_INDEX_FASTEST\"\n";
printf LBL " UNIT = \"W/m^2/sR\"\n";
$line =~ s/PC_REAL/IEEE_REAL/;
}
elsif ($line =~ /^\s*END\s*$/)
{ $not_done = 0; }
elsif ($line =~ /^\s*OBJECT\s+=/)
{ printf LBL "OBJECT = FITS_HEADER\n";
printf LBL " HEADER_TYPE = \"FITS\"\n";
printf LBL " BYTES = 2880\n";
printf LBL " RECORDS = 1\n";
printf LBL "END_OBJECT = FITS_HEADER\n";
printf LBL "\n";
}
elsif ($line =~ /MISSING_CONSTANT/)
{ $line =~ s/=.*$/= 0.00/; }
elsif ($line =~ /_DISPLAY_DIRECTION/)
{ $line =~ s/'/"/g; } #'
elsif ($line =~ /START_TIME\s*=\s*(\S+)\s/)
{ $start_time = $1; }
elsif ($line =~ /TARGET_NAME\s+=\s*"(.*)"/)
{ $target_name = $1;
$target_name =~ s/N\/A/N\/A /;
}
elsif ($line =~ /EXPOSURE_DURATION\s+=\s+([0-9.]+))
{ $exposure = $1; }
elsif ($line =~ /MISSION_NAME/)
{ $junk = ; } # Discard extra INSTRUMENT_HOST_NAME line
elsif ($line =~ /SCAN_MIRROR_RATE/)
{ $line =~ s/SEC/SECOND/; }
elsif ($line =~ /RIGHT_ASCENSION\s*= (.*))
{ $ra = $1; }
elsif ($line =~ /DECLINATION\s*= (.*))
{ $dec = $1; }
$line =~ s/ ;
printf LBL $line;
}
close(IMG);
close(LBL);
# Writing the FITS header:
printf HDR "SIMPLE = T\n";
printf HDR "BITPIX = -32\n";
printf HDR "NAXIS = 2\n";
printf HDR "NAXIS1 = 1022\n";
printf HDR "NAXIS2 = 1024\n";
printf HDR "MISSION = \'STARDUST\'\n";
printf HDR "INSTRUM = \'NAVCAM\'\n";
printf HDR "DATE-OBS= %-20.20s / Start time of observation\n", $start_time;
printf HDR "TARGET = \'$target_name\'\n";
printf HDR "EXPOSURE= %20.1f / Exposure time in milliseconds\n", $exposure;
printf HDR "RA = %20.20s\n",$ra;
printf HDR "DEC = %20.20s\n",$dec;
printf HDE "FILTER = \'OPNAV \'\n";
printf HDR "WAVELEN = 698.8 / Wavelength in nanometers\n";
printf HDR "END\n";
close(HDR);
# That's it for the label.
return;
}
#=============================================================================