#!/usr/bin/perl

# Routine to make preliminary PDS labels from the information in the 
# FITS headers of the IR filter table FITS files.
#
# Format: % makelbl irfc.hdr
#
# Output is to irfc.lbl
#
# 04 May 2006, A.C.Raugh
#=======================================================================

if (@ARGV==0)
  { die "Usage: makelbl irfc.hdr\n"; }

foreach $file (@ARGV)
  { if ($file !~ /\.hdr/)
      { printf STDERR "$file is not a FITS header, is it?\n";
        next;
      }

    # Find and open files:

    $filename = $file;
    $filename =~ s/\.hdr//;

    $outfile = "$filename.lbl";

    $FILE = "$filename.tab";
    $FILE =~ tr/a-z/A-Z/;

    open(HDR,$file) || die "Could not open $file for reading ($!)";
    open(LBL,"| ppodl -p >$outfile") || 
        die "Could not open $outfile for writing ($!)";

    # Constants:

    $target = "CALIBRATION";
    $scale  = 0.00001;

    # We'll completely parse the FITS header first, then write the label.

    $cc = 0;
    read HDR, $line, 80;
    $lc = 1;
    while ($line !~ /^END /)
      { if ($line =~ /^NAXIS2/)
          { $rows   = substr($line,27,3); }
        elsif ($line =~ /^FILE-NUM/)
          { $filenum = substr($line,24,6); }
        elsif ($line =~ /^DATE-OBS/)
          { if (substr($line,11,6) eq "      ")
              { $date = " "; }
            else
              { $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/)
          { if (substr($line,20,6) eq "      ")
              { $time = " "; }
            else
              { $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);
                $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/)
          { $line =~ /'(.*)'\s+$/;
            $observer = $1;
          }
        elsif ($line =~ /^INSTRUME/)
          { $instrument = substr($line,11,60);
            $instrument =~ s/'\s+$//;
          }
        elsif ($line =~ /^OBSVTORY/)
          { $observatory = substr($line,11,60);
            $observatory =~ s/'\s+//;
          }
        elsif ($line =~ /^LOCATION/)
          { $location = substr($line,11,60);
            $location =~ s/'\s+//;
          }
        elsif ($line =~ /^TELESCOP/)
          { $telescope = substr($line,11,60);
            $telescope =~ s/'\s+//;
          }
        elsif ($line =~ /^FILTER/)
          { $filter_name = substr($line,11,20);
            $filter_name =~ s/\s*'\s+//;
            $target_desc = "Laboratory filter curve for filter $filter_name";
            $name = "$filter_name FILTER CURVE TABLE";
          }
        elsif ($line =~ /^COMMENT/)
          { $val = substr($line,10,70);
            $val =~ s/\s+$//;
            $val =~ s/"/''/g;
            $comment[$cc] = "  $val";
            $cc++;
          }
        elsif ($line =~ /^HISTORY/)
          { $val = substr($line,10,70);
            $val =~ s/\s+$//;
            $comment[$cc] = "  $val";
            $cc++;
          }

        # Next line:

        read HDR,$line,80;
        $lc++;
      }

    # Done with the FITS file.

    close(HDR);
    $lc = $lc / 36.0;
    $header_records = ($lc == int($lc))? $lc : (int($lc)+1);
    $lc = ($lines * $samples * 4) / 2800;
    $data_records   = ($lc == int($lc))? $lc : (int($lc)+1);
    $file_records = $header_records + $data_records;

    $start_time = ($date eq " ")? "\"N/A\"" : $date . $time;
    $stop_time  = ($date eq " ")? "\"N/A\"" : "\"UNK\"";

    # 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 = 12\n";
    printf LBL "FILE_RECORDS = $rows\n\n";
    printf LBL "DATA_SET_ID = \"IHW-C-IRFCURV-3-EDR-HALLEY-V2.0\"\n";
    printf LBL "PRODUCT_ID = \"$filename\"\n";
    printf LBL "PRODUCT_NAME = \"IHW IR FILTERS, $name\"\n";
    printf LBL "PRODUCT_CREATION_TIME = 2006-05-04\n";
    printf LBL "\n";
    printf LBL "INSTRUMENT_HOST_NAME = \"IHW INFRARED STUDIES NETWORK\"\n";
    printf LBL "INSTRUMENT_HOST_ID = \"IRSN\"\n";
    printf LBL "INSTRUMENT_NAME = \"INFRARED FILTER REFERENCE CURVES\"\n";
    printf LBL "INSTRUMENT_ID = \"IRFCURV\"\n";
    printf LBL "TARGET_NAME = \"$target\"\n";
    printf LBL "TARGET_DESC = \"$target_desc\"\n" if ($target_desc);
    printf LBL "START_TIME = $start_time\n";
    printf LBL "STOP_TIME = $stop_time\n";
    printf LBL "FILTER_NAME = $filter_name\n";
    printf LBL "OBSERVATION_ID = \"$filenum\"\n";
    printf LBL "OBSERVER_FULL_NAME = \"$observer\"\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 "        Location: $location\n";
    printf LBL "  East Longitude: $obs_long\n";
    printf LBL "  North Latitude: $obs_lat\n";
    printf LBL "       Telescope: $telescope\n";
    printf LBL "      Instrument: $instrument\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 "^TABLE       = \"$FILE\"\n";
    printf LBL "\n";
    printf LBL "OBJECT = TABLE\n";
    printf LBL "NAME = \"FILTER_CURVE\"\n";
    printf LBL "ROWS = $rows\n";
    printf LBL "ROW_BYTES = 12\n";
    printf LBL "COLUMNS = 2\n";
    printf LBL "INTERCHANGE_FORMAT = \"ASCII\"\n";
    printf LBL "\n";
    printf LBL "OBJECT = COLUMN\n";
    printf LBL "COLUMN_NUMBER = 1\n";
    printf LBL "NAME = \"WAVELENGTH\"\n";
    printf LBL "START_BYTE = 1\n";
    printf LBL "BYTES = 4\n";
    printf LBL "DATA_TYPE = \"ASCII_REAL\"\n";
    printf LBL "FORMAT = \"F4.2\"\n";
    printf LBL "UNIT = \"MICRON\"\n";
    printf LBL "END_OBJECT = COLUMN\n";
    printf LBL "\n";
    printf LBL "OBJECT = COLUMN\n";
    printf LBL "COLUMN_NUMBER = 2\n";
    printf LBL "NAME = \"TRANSMISSION\"\n";
    printf LBL "START_BYTE = 6\n";
    printf LBL "BYTES = 5\n";
    printf LBL "DATA_TYPE = \"ASCII_REAL\"\n";
    printf LBL "FORMAT = \"F5.2\"\n";
    printf LBL "DESCRIPTION = \"Filter transmission in arbitrary units\"\n";
    printf LBL "END_OBJECT = COLUMN\n";
    printf LBL "\n";
    printf LBL "END_OBJECT = TABLE\n";
    printf LBL "\n";
    printf LBL "END\n";

    # Done with the label:

    close(LBL);

    # Create the data file:

    system "gettab $filename.dat $rows $filename.tab";

    # Next file:

  }