// -------------------------------------------------------------
// Define class Available.
//
// Usage:
//    var a = new Available();
//    var v = a.version(tm, dt);
//                           // Tm is time, dt is datatype 
//                           // (e.g., CGAS). Return version, or
//                           // false if data is not available.
//    var v = a.input_version(tm, dt);
//                           // Tm is time, dt is datatype 
//                           // (e.g., CGAS). Return input version 
//                           // (e.g., version of AS_AEROSOL file), or
//                           // false if data is not available.
//    var esdt = a.esdt(tm, dt);
//                           // Return ESDT string for given time
//                           // data type, or false if data isn't
//                           // available.
//    var tm = a.latest_month(dt);
//                           // Return Time for latest month that
//                           // has available data.
//    var t = a.no_overflight(tm)
//                           // Returns true if there is no 
//                           // overflight for given day.
// -------------------------------------------------------------

function Available() {
    this.available_image = new Array(3);
    this.available_image[0] = available_init_0();
    this.available_image[1] = available_init_1();
    this.available_image[2] = available_init_2();

    this.inputv = new Array(3);
    this.inputv[0] = input_version_init_0();
    this.inputv[1] = input_version_init_1();
    this.inputv[2] = input_version_init_2();

    this.no_overflight_array = no_overflight_init();

    this.version_to_esdt = new Array(NUMBER_TYPE);
    for(var i = 0; i < this.version_to_esdt.length; ++i) {
	this.version_to_esdt[i] = new Array(4);
	for(var j = 0; j < this.version_to_esdt[i].length; ++j) {
	    this.version_to_esdt[i][j] = new Array();
	}
    }

    this.version_to_esdt[CGAS][TIME_DAY]["F02_0012"] = "UAEMRDAE.004";
    this.version_to_esdt[CGAS][TIME_MONTH]["F02_0012"] = "UAEMRMAE.004";
    this.version_to_esdt[CGAS][TIME_SEASON]["F02_0012"] = "UAEMRQAE.004";
    this.version_to_esdt[CGLS][TIME_DAY]["F02_0012"] = "UAEMRDLS.004";
    this.version_to_esdt[CGLS][TIME_MONTH]["F02_0012"] = "UAEMRMLS.004";
    this.version_to_esdt[CGLS][TIME_SEASON]["F02_0012"] = "UAEMRQLS.004";
    this.version_to_esdt[CGGRP][TIME_DAY]["F02_0013"] = "UAEMRDRD.004";
    this.version_to_esdt[CGGRP][TIME_MONTH]["F02_0013"] = "UAEMRMRD.005";
    this.version_to_esdt[CGGRP][TIME_SEASON]["F02_0013"] = "UAEMRQRD.005";

    this.version = available_version;
    this.input_version = available_input_version;
    this.latest_month = available_latest_month;
    this.esdt = available_esdt;
    this.no_overflight = available_no_overflight;
}

// -------------------------------------------------------------
// Check to see if the given image is available or not. If it is, 
// return the version. Otherwise, return false.
// -------------------------------------------------------------

function available_version(tm, dt) {
    if(tm.day != 0) {
	return this.available_image[dt][tm.season + " " + tm.day +
					" " + tm.year];
    }
    return this.available_image[dt][tm.season + " " + tm.year];
}

// -------------------------------------------------------------
// Return input file version, or false if no data available.
// -------------------------------------------------------------

function available_input_version(tm, dt) {
    if(tm.day != 0) {
	return this.inputv[dt][tm.season + " " + tm.day + " " + tm.year];
    }
    return this.inputv[dt][tm.season + " " + tm.year];
}

// -------------------------------------------------------------
// Determine latest available month for given data type. Returns
// A time.
// -------------------------------------------------------------

function available_latest_month(dt) {
    var first = 1;
    var max_tm;
    for(var i in this.available_image[dt]) {
	var t = i.split(" ");
	if(t.length ==3) {
	    var tm = new Time(t[0], t[2], t[1]);
	    if(tm.season_type() ==TIME_DAY) {
		if(first ==1 ||
		   compare_time(tm, max_tm) > 0) {
		    max_tm = tm;
		    first = 0;
		}
	    }
	}
    }
    return max_tm;
}

// -------------------------------------------------------------
// Check to see if the given image is available or not. If it is, 
// return the ESDT. Otherwise, return false.
// -------------------------------------------------------------

function available_esdt(tm, dt) {
    var v = this.version(tm, dt);
    if(!v) {
	return false;
    }
    return this.version_to_esdt[dt][tm.season_type()][v];
}

// -------------------------------------------------------------
// Return true if day isn't expected to have an overflight.
// -------------------------------------------------------------

function available_no_overflight(tm) {
    if(tm.day != 0) {
	return this.no_overflight_array[tm.season + " " + tm.day + " " + tm.year];
    } else {
	return 0;
    }
    
}

// -------------------------------------------------------------
// List of days that aren't expected to have overflights.
// -------------------------------------------------------------

function no_overflight_init() {
    var a = new Array();
    a["JUL 2 2004"] = 1;
    a["JUL 5 2004"] = 1;
    a["JUL 7 2004"] = 1;
    a["JUL 9 2004"] = 1;
    a["JUL 14 2004"] = 1;
    a["JUL 16 2004"] = 1;
    a["JUL 18 2004"] = 1;
    a["JUL 21 2004"] = 1;
    a["JUL 23 2004"] = 1;
    a["JUL 25 2004"] = 1;
    a["JUL 30 2004"] = 1;
    a["AUG 1 2004"] = 1;
    a["AUG 3 2004"] = 1;
    a["AUG 6 2004"] = 1;
    a["AUG 8 2004"] = 1;
    a["AUG 10 2004"] = 1;
    a["AUG 15 2004"] = 1;
    a["AUG 17 2004"] = 1;
    a["AUG 19 2004"] = 1;
    a["AUG 22 2004"] = 1;
    a["AUG 24 2004"] = 1;
    a["AUG 26 2004"] = 1;
    a["AUG 31 2004"] = 1;
    a["SEP 2 2004"] = 1;
    a["SEP 4 2004"] = 1;
    a["SEP 9 2004"] = 1;
    a["SEP 11 2004"] = 1;
    a["SEP 18 2004"] = 1;
    a["SEP 20 2004"] = 1;
    a["SEP 25 2004"] = 1;
    a["SEP 27 2004"] = 1;
    a["OCT 4 2004"] = 1;
    a["OCT 6 2004"] = 1;
    a["OCT 11 2004"] = 1;
    a["OCT 13 2004"] = 1;
    a["OCT 20 2004"] = 1;
    a["OCT 22 2004"] = 1;
    a["OCT 27 2004"] = 1;
    a["OCT 29 2004"] = 1;
    return a;
}
