// -------------------------------------------------------------
// This determines the image file name. There are classes
// defined in here for each type of data.
// -------------------------------------------------------------

// -------------------------------------------------------------
// Utility routine that either returns an animation image, if 
// animation is on, or fn.image_file_name.
// -------------------------------------------------------------

function image_file_name(a, fn, tm, dt) {
    if(a.am_on) {
	if(a.image_loaded()) {
	    return a.current_image().src;
	} else {
	    return "img" + fn.image_size + "/loading_image_animation.jpg";
	}
    } else {
	return fn.image_file_name(tm, dt);
    }
}

// -------------------------------------------------------------
// This catchs the stuff that is done in general for all the 
// image file name classes. Work is forwarded to 
// this.image_file_name_base. 
// -------------------------------------------------------------

function general_image_file_name(tm, ft) {
    var version = this.available.version(tm, this.data_type);
    if(version) {
	return this.image_file_name_base(ft) + "_" + 
	    general_image_file_name_time(tm) +
	    "_" + version + ".jpg";
    } else {
	if(this.available.no_overflight(tm)) {
	    return "img" + this.image_size + "/" +
		this.image_file_name_base_nov(ft) + "_nooverflight.jpg";
	} else {
	    return "img" + this.image_size + "/image_not_available.jpg";
	}
    }
}

// -------------------------------------------------------------
// Give time part of name.
// -------------------------------------------------------------

function general_image_file_name_time(tm) {
    var res;
    res = tm.season + "_";
    if(tm.day != 0) {
	res = res + tm.day + "_";
    }
    return res + tm.year;
}

// -------------------------------------------------------------
// Return input version, or undefined if not available.
// -------------------------------------------------------------

function general_input_version(tm) {
    return this.available.input_version(tm, this.data_type);
}

// -------------------------------------------------------------
// Image file name for Aerosol.
// 
// Usage:
//   var fn = new AerosolFileName(available, image_size, band, 
//                                particle_type);
//                             // available is object of type Available
//                             // Image size is either "small" or 
//                             // "large"
//   var name = fn.image_file_name(time, field_type_index)
//   var is = fn.image_size;   // Either "small" or "large".
//   var dt = fn.data_type;    // Data type, e.g, CGAS.
//   var v = fn.input_version(time);    
//                             // Input file version.
// -------------------------------------------------------------

function AerosolFileName(a, is, b, pt) {
    this.available = a;
    this.image_size = is;
    this.data_type = CGAS;
    this.input_version = general_input_version;
    this.image_file_name = aerosol_image_file_name;
    this.image_file_name_base = aerosol_image_file_name_base;
    this.image_file_name_base_nov = aerosol_image_file_name_base_nov;
    this.band = b;
    this.particle_type = pt;
}

function aerosol_image_file_name(tm, ft) {
    var version = this.available.version(tm, this.data_type);
    var input_version = this.available.input_version(tm, this.data_type);

// Special handling. Optical depth (ft = 0) is available for all versions,
// but other particle types is only available from F03 on, with an input 
// version of F07 on.

    if(version &&
       (ft ===0 ||
	(parseInt(version.substring(1,3)) >= 3 &&
	 parseInt(input_version.substring(1,3)) >= 7))) {
	return this.image_file_name_base(ft) + "_" + 
	    general_image_file_name_time(tm) +
	    "_" + version + ".jpg";
    } else {
	if(this.available.no_overflight(tm)) {
	    return "img" + this.image_size + "/" +
		this.image_file_name_base_nov(ft) + "_nooverflight.jpg";
	} else {
	    return "img" + this.image_size + "/image_not_available.jpg";
	}
    }
}

function aerosol_image_file_name_base_nov(ft) {
    if(ft ===0) {
	return (this.image_size == "small" ? "odepth_small" : 
		"odepth");
    } else if(ft ==1) {
	return (this.image_size == "small" ? "angexp_small" : 
		"angexp");
    } else if(ft ==2) {
	return (this.image_size == "small" ? "ssa_small" : 
		"ssa");
    } else if(ft ==3) {
	return (this.image_size == "small" ? "odfrac_small" : 
		"odfrac");
    } else if(ft ==4) {
	return (this.image_size == "small" ? "numfrac_small" : 
		"numfrac");
    } else {
	alert("Unknown file type for aerosol_image_file_name_base. ft: " + 
	      ft);
    }
}

function aerosol_image_file_name_base(ft) {
    if(ft ===0) {
	return (this.image_size == "small" ? "data/odepth_small" : 
		"datahidden/odepth_small");
    } else if(ft ==1) {
	return (this.image_size == "small" ? "data/angexp_small" : 
		"datahidden/angexp_small");
    } else if(ft ==2) {
	return (this.image_size == "small" ? "data/ssa_small" : 
		"datahidden/ssa_small") +
	    "_" + this.band_type;
    } else if(ft ==3) {
	return (this.image_size == "small" ? "data/odfrac_small" : 
		"datahidden/odfrac_small") +
	    "_" + this.particle_type +
	    "_" + this.band_type;
    } else if(ft ==4) {
	return (this.image_size == "small" ? "data/numfrac_small" : 
		"datahidden/numfrac_small") +
	    "_" + this.particle_type;
    } else {
	alert("Unknown file type for aerosol_image_file_name_base. ft: " + 
	      ft);
    }
}

// -------------------------------------------------------------
// Image file name for Land.
// This has the additional field biome_type.
// -------------------------------------------------------------

function LandFileName(a, is, bt) {
    this.available = a;
    this.image_size = is;
    this.biome_type = bt;
    this.data_type = CGLS;
    this.input_version = general_input_version;
    this.image_file_name = land_image_file_name;
    this.image_file_name_base = land_image_file_name_base;
    this.image_file_name_base_nov = land_image_file_name_base_nov;
    if(is =="small") {
	this.base  = new Array("data/ndvi_small", 
			       "data/dhrpar_small", 
			       "data/dhrsw_small",
			       "data/fpar_small", 
			       "data/lai_small", 
			       "data/dhr_small_natural",
			       "data/dhr_small_infrared");
    } else {
	this.base  = new Array("datahidden/ndvi_small", 
			       "datahidden/dhrpar_small", 
			       "datahidden/dhrsw_small",
			       "datahidden/fpar_small", 
			       "datahidden/lai_small", 
			       "datahidden/dhr_small_natural",
			       "datahidden/dhr_small_infrared");
    }
}

function land_image_file_name(tm, ft) {
    var version = this.available.version(tm, this.data_type);
    var input_version = this.available.input_version(tm, this.data_type);

// Special handling. Some of the parameters aren't available for older data
// with an input version < F04.

    if(version &&
       (parseInt(input_version.substring(1,3)) < 3 ||
	parseInt(input_version.substring(1,3)) >= 4 ||
	(this.biome_type == 6 &&
	(ft ===0 ||
	 ft ==2 ||
	 ft ==5 ||
	 ft ==6)))) {
	return this.image_file_name_base(ft) + "_" + 
	    general_image_file_name_time(tm) +
	    "_" + version + ".jpg";
    } else {
	if(this.available.no_overflight(tm)) {
	    return "img" + this.image_size + "/" +
		this.image_file_name_base_nov(ft) + "_nooverflight.jpg";
	} else {
	    return "img" + this.image_size + "/image_not_available.jpg";
	}
    }
}

function land_image_file_name_base_nov(ft) {
    var a = new Array("ndvi_small", 
		      "dhrpar_small", 
		      "dhrsw_small",
		      "fpar_small", 
		      "lai_small", 
		      "dhr_small_natural",
		      "dhr_small_infrared");
    if(ft < 0 ||
       ft >= this.base.length) {
	alert("Unknown file type for land_image_file_name_base. ft: " + ft);
    }
    return a[ft];
}

function land_image_file_name_base(ft) {
    if(ft < 0 ||
       ft >= this.base.length) {
	alert("Unknown file type for land_image_file_name_base. ft: " + ft);
    }
    if(USE_BIOME_TYPE) {
	return this.base[ft] + "_" + this.biome_type;
    } else {
	return this.base[ft];
    }
}

// -------------------------------------------------------------
// Image file name for Radiance.
// This has the additional fields image_type and rccm_type
// -------------------------------------------------------------

function RadianceFileName(a, is, it, rt) {
    this.available = a;
    this.image_size = is;
    this.image_type = it;
    this.rccm_type = rt;
    this.data_type = CGGRP;
    this.input_version = general_input_version;
    this.image_file_name = general_image_file_name;
    this.image_file_name_base = radiance_image_file_name_base;
    this.image_file_name_base_nov = radiance_image_file_name_base_nov;
}

function radiance_image_file_name_base_nov(ft) {
    return (this.image_size =="small" || this.image_size =="hidden" ? "rad_small" : "rad");
}

function radiance_image_file_name_base(ft) {
    if(ft <0 ||
       ft >8) {
	alert("Unknown file type for radiance_image_file_name_base. ft: " + 
	      ft);
    }
    return (this.image_size =="small" ? "data/rad_small_" : "datahidden/rad_small_") +
	ft + "_" + this.image_type + "_" + this.rccm_type;
}

// -------------------------------------------------------------
// Image file name for SDCM.
// This has the additional field sdcm_type.
// -------------------------------------------------------------

function SdcmFileName(a, is, st) {
    this.available = a;
    this.image_size = is;
    this.sdcm_type = st;
    this.data_type = SDCM;
    this.input_version = general_input_version;
    this.image_file_name = general_image_file_name;
    this.image_file_name_base = sdcm_image_file_name_base;
}

function sdcm_image_file_name_base(ft) {
    if(ft <0 ||
       ft >1) {
	alert("Unknown file type for sdcm_image_file_name_base. ft: " + 
	      ft);
    }
    return (this.image_size =="small" ? "data/sdcm_small_" : "datahidden/sdcm_small") +
	(ft ===0 ? "bw_" : "nw_") + this.sdcm_type;
}

// -------------------------------------------------------------
// Image file name for RCCM.
// This has the additional field RCCM type 2.
// -------------------------------------------------------------

function RccmFileName(a, is, rt) {
    this.available = a;
    this.image_size = is;
    this.data_type = RCCM;
    this.input_version = general_input_version;
    this.rccm_type2 = rt;
    this.image_file_name = rccm_image_file_name;
    this.image_file_name_base = rccm_image_file_name_base;
    this.image_file_name_base_nov = rccm_image_file_name_base_nov;
}

function rccm_image_file_name(tm, ft) {
    var version = this.available.version(tm, this.data_type);

// Special handling. Only available from F03 on.

    if(version &&
       parseInt(version.substring(1,3)) >= 3) {
	return this.image_file_name_base(ft) + "_" + 
	    general_image_file_name_time(tm) +
	    "_" + version + ".jpg";
    } else {
	if(this.available.no_overflight(tm)) {
	    return "img" + this.image_size + "/" +
		this.image_file_name_base_nov(ft) + "_nooverflight.jpg";
	} else {
	    return "img" + this.image_size + "/image_not_available.jpg";
	}
    }
}

function rccm_image_file_name_base_nov(ft) {
    return (this.image_size =="small" || this.image_size =="hidden" ? "rccm_small" : "rccm");
}

function rccm_image_file_name_base(ft) {
    if(ft <0 ||
       ft >8) {
	alert("Unknown file type for rccm_image_file_name_base. ft: " + 
	      ft);
    }
    return (this.image_size =="small" ? "data/rccm_small_" : "datahidden/rccm_small_") 
	+ ft + "_" + this.rccm_type2;
}

// -------------------------------------------------------------
// Image file name for Albedo.
// This takes the additional arguments band type, range_type, 
// height_type, wind_type and resolution_type.
// This also defined image_file_name_height, which is like 
// image_file_name but takes the height as an argument.
// -------------------------------------------------------------

function AlbedoFileName(a, is, bt, rt, ht, wt, rst) {
    this.available = a;
    this.image_size = is;
    this.band_type2 = bt;
    this.range_type = rt;
    this.height_type = ht;
    this.wind_type = wt;
    this.resolution_type = rst;
    this.data_type = CGAL;
    this.input_version = general_input_version;
    this.image_file_name = general_image_file_name;
    this.height_index = albedo_height_index;
    this.image_file_name_base = albedo_image_file_name_base;
    this.image_file_name_height = albedo_image_file_name_height;
}

function albedo_height_index () {
    if(this.height_type >= 4 &&
       this.wind_type ==1) {
	return parseInt(this.height_type) + 10;
    } else {
	return this.height_type;
    }
}

function albedo_image_file_name_height(st, ft, ht) {
    var height_keep = this.height_type;
    this.height_type = ht;
    var res = this.image_file_name(st, ft);
    this.height_type = height_keep;
    return res;
}

function albedo_image_file_name_base(ft) {
    if(ft ===0) {
	return (this.image_size =="small" ? "data/expansive_small_" : 
		"datahidden/expansive_small_") + this.band_type2 +
	    "_" + (this.range_type ==1 ? "overflow_" : "") +
	    this.resolution_type;
    } else if(ft ==1) {
	return (this.image_size =="small" ? "data/local_small_" : 
		"datahidden/local_small_") + this.band_type2 + 
	    "_" + this.height_index() +
	    "_" + (this.range_type ==1 ? "overflow_" : "") +
	    this.resolution_type;
    } else if(ft ==2) {
	return (this.image_size =="small" ? "data/restrictive_small_" : 
		"datahidden/restrictive_small_") + this.band_type2 + 
	    "_" + this.height_index() +
	    "_" + (this.range_type ==1 ? "overflow_" : "") +
	    this.resolution_type;
    } else if(ft ==3) {
	return (this.image_size =="small" ? "data/forcing_small_" : 
		"datahidden/forcing_small_") + this.band_type2 +
	    "_" + this.resolution_type;
    } else {
	alert("Unknown file type for albedo_image_file_name_base. ft: " + 
	      ft);
    }
}
