#!/usr/freeware/bin/perl

use Time::JulianDay;

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

@hours = ('00', '06', '12', '18');

chdir("$rootDir");

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

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

    #print "$dataRoot/$year\n";

    if (-d "$year") {
	for ($jday = 1; $jday <= 366; $jday++) {
	    $jdaystring = sprintf("%.3d", $jday);
	    for ($hour = 0; $hour < 4; $hour++) {
		$file = "$year/".$hours[$hour]."/$fileBase".
		        $year.$jdaystring.$hours[$hour].$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/%.2d:%s %.3d ",
				    $year, $m, $d, $hours[$hour], $jday);
		    print DAT "$date";
		    $url = "$urlBase/$dataRoot/$file";
		    $blah = length("$url");
		    if ($blah > $urlLength) {
			$urlLength = $blah;
		    }
		    print DAT "$url\n";
		}
	    }
	}
    }
}
close YEARS;
close DAT;

$urlLength += 19 - 1;

open(FMT, ">$rootDir/catalog.fmt");
print FMT <<DONE;
ASCII_input_data "$name"
year         1  4   int32 0
month        6  7   int32 0
day          9 10   int32 0
hours       12 13   int32 0
julian_day  15 17   int32 0
DODS_URL    19 $urlLength  text  0
DONE
close FMT;
