#!/usr/bin/perl use Time::Local; # Routine to make PDS labels and proper FITS files for the NNSN data. # # Format: % makelbl nnsn.hdr # # Output is to nnsn.lbl, nnsn.fit; nnsn.hdr and nnsn.img are removed # # 16 May 2006, A.C.Raugh #======================================================================= if (@ARGV==0) { die "Usage: makelbl nnsn.hdr\n"; } foreach $file (@ARGV) { if ($file !~ /\.hdr/) { printf STDERR "$file is not a FITS header, is it?\n"; next; } # Find and open files: $name = $file; $name =~ s/\.hdr//; $lblfile = "$name.lbl"; $imgfile = "$name.img"; $hdrfile = "$name.hdr"; $fitfile = "$name.fit"; $FILE = $fitfile; $FILE =~ tr/a-z/A-Z/; open(FIT,$file) || die "Could not open $file for reading ($!)"; open(LBL,"| ppodl -p >new/$lblfile") || die "Could not open $lblfile for writing ($!)"; # We'll completely parse the FITS header first, then write the label. $target = "1P/HALLEY 1 (1682 Q1)"; $cc = 0; read FIT, $line, 80; $lc = 1; while ($line !~ /^END /) { if ($line =~ /^NAXIS1/) { $samples = substr($line,27,3); } elsif ($line =~ /^NAXIS2/) { $lines = substr($line,27,3); } elsif ($line =~ /^BITPIX/) { $sample_bits = substr($line,28,2); } elsif ($line =~ /^FILE-NUM/) { $filenum = substr($line,24,6); } elsif ($line =~ /^DATE-OBS/) { $dd = substr($line,11,2); $mm = substr($line,14,2); $yy = substr($line,17,2); $date = "19$yy-$mm-${dd}T"; } elsif ($line =~ /^TIME-OBS/) { $d = substr($line,20,10); $d = $d * 24.0; $hh = int($d); $d = ($d - $hh) * 60.0; $mm = int($d); $d = ($d - $mm) * 60.0; $ss = int($d + 0.5); if ($ss == 60) { $ss = 0; $mm++; } if ($mm == 60) { $mm = 0; $hh++; } $time = sprintf "%02d:%02d:%02d",$hh, $mm, $ss; } elsif ($line =~ /^LONG-OBS/) { $dd = substr($line,11,3); $mm = substr($line,15,2); $ss = substr($line,18,2); $val = ($dd * 1.0) + ($mm / 60.0) + ($ss / 3600.0); $obs_long = sprintf "%8.4f",$val; } elsif ($line =~ /^LAT--OBS/) { $s = substr($line,11,1); $dd = substr($line,12,2); $mm = substr($line,15,2); $ss = substr($line,18,2); $val = ($dd * 1.0) + ($mm / 60.0) + ($ss / 3600.0); $obs_lat = sprintf "%s%07.4f",$s,$val; } elsif ($line =~ /^SYSTEM/) { $system = substr($line,11,8); } elsif ($line =~ /^OBSERVER= '([^']+)' \//) { $observer = $1; $observer =~ s/\s+$//; } elsif ($line =~ /^INSTRUME/) { $instrument = substr($line,11,60); $instrument =~ s/'\s+$//; } elsif ($line =~ /^OBSVTORY/) { $line =~ /'([^']+)'/; $observatory = $1; $observatory =~ s/\s+$//; } elsif ($line =~ /^ELEV-OBS/) { $elevation = substr($line,25,5); $elevation =~ s/\s+//; } elsif ($line =~ /^TELESCOP/) { $line =~ /'([^']+)'/; $telescope = $1; $telescope =~ s/'\s+//; } elsif ($line =~ /^APERTURE/) { $aperture = substr($line,25,5); $aperture =~ s/\s+//g; } elsif ($line =~ /^TELEFL/) { $focal_len = substr($line,21,10); $focal_len =~ s/\s+//g; } elsif ($line =~ /^PLTSCALE/) { $plate_scale = substr($line,21,10); $plate_scale =~ s/\s+//g; } elsif ($line =~ /^CROTA1/) { $pa = substr($line,21,10); $pa =~ s/\s+//g; $pa = $pa * 1.0; } elsif ($line =~ /^SENSE/) { $pa = (substr($line,29,1) eq "T")? $pa : 360.0 - $pa; } elsif ($line =~ /^DETECTOR/) { $detector = substr($line,11,40); $detector =~ s/\s*'.*//; } elsif ($line =~ /^DIGITIZE/) { $digitize = substr($line,11,40); if ($digitize !~ /^\s+'/) { $digitize =~ s/\s*'.*$//; } else { undef $digitize; } } elsif ($line =~ /^APSIZE/) { $apsize = substr($line,24,6); $apsize =~ s/\s+//g; } elsif ($line =~ /^FILTER\s+= '([^']+)' /) { $filter_name = $1; $filter_name =~ s/\s+$//; $filter_name =~ s/\s+/_/g; } elsif ($line =~ /^EXPOSURE/) { $exposure = substr($line,23,7); } elsif ($line =~ /^AIRM-MID/) { $airmass = substr($line,25,5); } elsif ($line =~ /^QUALITY/) { $line =~ /'([^']+)'/; $quality = $1; $quality =~ s/\s+$//; } elsif ($line =~ /^BUNIT/) { $unit = substr($line,11,20); $unit =~ s/\s*'\s+//; } elsif ($line =~ /^COMMENT/) { $val = substr($line,8,72); $val =~ s/^\s+//; $val =~ s/\s+$//; $val =~ s/"/''/g; $comment[$cc] = " $val"; $cc++; } elsif ($line =~ /^HISTORY/) { $val = substr($line,8,72); $val =~ s/^\s+//; $val =~ s/\s+$//; $comment[$cc] = " $val"; $cc++; } # Next line: read FIT,$line,80; $lc++; } # Done with the FITS file. close(FIT); $sample_bytes = $sample_bits / 8; $lc = $lc / 36.0; $header_records = ($lc == int($lc))? $lc : (int($lc)+1); $lc = ($lines * $samples * $sample_bytes) / 2880; $data_records = ($lc == int($lc))? $lc : (int($lc)+1); $file_records = $header_records + $data_records; $pas = $plate_scale * $apsize; # We calculate the start time by taking the observation time and # substracting half the exposure time: $obstime = $date . $time; $year = substr($date,0,4); $mon = substr($date,5,2)-1; $day = substr($date,8,2); $hour = substr($time,0,2); $min = substr($time,3,2); $sec = substr($time,6,2); $timeobs = timelocal($sec,$min,$hour,$day,$mon,$year); $time = $timeobs - ($exposure / 2.0); ($sec,$min,$hour,$day,$mon,$year) = localtime($time); $year += 1900; $mon++; $start_time = sprintf "%4d-%02d-%02dT%02d:%02d:%02d", $year, $mon, $day, $hour, $min, $sec; $time = $timeobs + ($exposure / 2.0); ($sec,$min,$hour,$day,$mon,$year) = localtime($time); $year += 1900; $mon++; $stop_time = sprintf "%4d-%02d-%02dT%02d:%02d:%02d", $year, $mon, $day, $hour, $min, $sec; # Now we write the PDS label: printf LBL "PDS_VERSION_ID = PDS3\n\n"; printf LBL "RECORD_TYPE = \"FIXED_LENGTH\"\n"; printf LBL "RECORD_BYTES = 2880\n"; printf LBL "FILE_RECORDS = $file_records\n\n"; printf LBL "DATA_SET_ID = \"IHW-C-NNSN-3-EDR-HALLEY-V2.0\"\n"; printf LBL "PRODUCT_ID = \"$name\"\n"; printf LBL "PRODUCT_NAME = \"IHW NNSN IMAGE $filenum\"\n"; printf LBL "PRODUCT_CREATION_TIME = 2006-05-16\n"; printf LBL "\n"; printf LBL "INSTRUMENT_HOST_NAME = \"IHW NEAR-NUCLEUS STUDIES NETWORK\"\n"; printf LBL "INSTRUMENT_HOST_ID = \"NNSN\"\n"; printf LBL "INSTRUMENT_NAME = \"IHW NEAR-NUCLEUS STUDIES NETWORK\"\n"; printf LBL "INSTRUMENT_ID = \"NNSN\"\n"; printf LBL "TARGET_NAME = \"$target\"\n"; printf LBL "START_TIME = $start_time\n"; printf LBL "STOP_TIME = $stop_time\n"; printf LBL "\n"; printf LBL "OBSERVATION_TIME = $obstime\n"; printf LBL "OBSERVATION_ID = \"$filenum\"\n"; printf LBL "OBSERVER_FULL_NAME = \"$observer\"\n"; printf LBL "FILTER_NAME = $filter_name\n"; printf LBL "EXPOSURE_DURATION = $exposure \n"; printf LBL "AIRMASS = $airmass\n"; printf LBL "DATA_QUALITY_DESC = \"$quality\"\n"; printf LBL "\n"; printf LBL "DESCRIPTION = \"\n"; printf LBL "System code: $system\n"; printf LBL "Observatory Info\n"; printf LBL " Name: $observatory\n"; printf LBL " Elevation: $elevation\n"; printf LBL " East Longitude: $obs_long\n"; printf LBL " North Latitude: $obs_lat\n"; printf LBL " Telescope: $telescope\n"; printf LBL " aperture $aperture meters\n"; printf LBL " effective focal length $focal_len meters\n"; printf LBL " Detector: $detector\n"; printf LBL " Digitizer: $digitize\n" if ($digitize); printf LBL " Plate Scale: $plate_scale arsec/mm\n"; printf LBL "\n"; printf LBL "Comments from the FITS header:\n"; printf LBL "\n"; for ($i=0; $i<$cc; $i++) { printf LBL "$comment[$i]\n"; } printf LBL "\"\n"; printf LBL "\n"; printf LBL "^FITS_HEADER = (\"$file\",1)\n"; printf LBL "^IMAGE = (\"$file\",%d)\n",$header_records+1; printf LBL "\n"; printf LBL "OBJECT = FITS_HEADER\n"; printf LBL "HEADER_TYPE = \"FITS\"\n"; printf LBL "RECORDS = %d\n",$header_records; printf LBL "BYTES = %d\n", $header_records * 2880; printf LBL "DESCRIPTION = \"FITS Standard NOST 100-2.0\"\n"; printf LBL "END_OBJECT = FITS_HEADER\n"; printf LBL "\n"; printf LBL "OBJECT = IMAGE\n"; printf LBL "LINES = $lines\n"; printf LBL "LINE_SAMPLES = $samples\n"; printf LBL "SAMPLE_BITS = $sample_bits\n"; printf LBL "SAMPLE_TYPE = \"MSB_INTEGER\"\n"; printf LBL "UNIT = \"$unit\"\n" if ($unit); printf LBL "AXIS_ORDER_TYPE = \"FIRST_INDEX_FASTEST\"\n"; printf LBL "LINE_DISPLAY_DIRECTION = \"DOWN\"\n"; printf LBL "SAMPLE_DISPLAY_DIRECTION = \"RIGHT\"\n"; printf LBL "PIXEL_ANGULAR_SCALE = (%4.2f,%4.2f)\n",$pas,$pas; printf LBL "DESCRIPTION = \"\n"; printf LBL " Plate scale: $plate_scale arcsec/mm\n"; printf LBL " Pixel size: $apsize mm/pixel\n\n"; printf LBL "Position angle of sample axis = $pa degrees,\n"; printf LBL "measured counterclockwise from North to East.\n"; printf LBL "\"\n"; printf LBL "END_OBJECT = IMAGE\n"; printf LBL "\n"; printf LBL "END\n"; # Done with the label: close(LBL); # Create the full FITS file: system "cat $hdrfile $imgfile > new/$fitfile"; # Next file: }