#!/bin/env perl
print "Content-type: text/html\n\n";

use Time::JulianDay;

$rootDir    = "/Net/data/qscat/data/glob_qscat_hd/";
$urlBase    = "https://www.coaps.fsu.edu/cgi-bin/dods/nph-nc/dods_data";
$dataRoot   = "QuikSCAT_WINDS_halfd";
$name       = "QuikSCAT_WINDS_halfd";
$fileBase   = "gcv_QSCAT_";
$fileSuffix = ".nc";

chdir("$rootDir");

open (DAT, "> $rootDir/catalog.dat");

$urlLength = 0;

open (YEARS, "ls -1 . |");
while ( <YEARS> ) {
    chomp();
    $year = $_;

    if (-d "$year") {

	for ($jday = 1; $jday <= 12; $jday++) {
	    $jdaystring = sprintf("%.2d", $jday);
	    
	    $file = "$year/$fileBase" . $year.$jdaystring.$fileSuffix;
	    
	    print "$file\n";
	    
	    if (-e $file) {
		$jd1=julian_day($year-1,12,31);
		$jd1 += $jday;
		($yr, $m, $d) = inverse_julian_day($jd1);
		$date = sprintf("%.4d/%.2d ",
				$year, $m);
		print DAT "$date";
		$url = "$urlBase/$dataRoot/$file";
		$blah = length("$url");
		if ($blah > $urlLength) {
		    $urlLength = $blah;
		}
		print DAT "$url\n";
	    }
	}
    }
}

close YEARS;
close DAT;

$urlLength += 9 - 1;

open(FMT, ">$rootDir/catalog.fmt");
print FMT <<DONE;
ASCII_input_data "$name"
year         1  4   int32 0
month        6  7   int32 0
DODS_URL     9  $urlLength  text  0
DONE
close FMT;
