// -------------------------------------------------------------
// 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 {
	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, odepth_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, od) {
    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.band = b;
    this.particle_type = pt;
    this.odepth_type = od;
}

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.
// Likewise, optical depth range is used for field other than optical depth,
// but only starting with F07. For versions before that, only the "ALL" or
// this.odepth_type = 5 are available.

    if(version &&
       (ft != 0 ||
	parseInt(version.substring(1,3), 10) >= 8 ||
	this.band_type ==1) &&
       (ft ===0 ||
	(parseInt(version.substring(1,3), 10) >= 3 &&
	 parseInt(input_version.substring(1,3), 10) >= 7)) &&
       (parseInt(version.substring(1,3), 10) < 8 ||
	(ft != 4 && ft != 5)) &&
       (ft ===0 ||
	this.odepth_type ==5 ||
	(parseInt(version.substring(1,3), 10) >= 7))) {
	return this.image_file_name_base(ft, version) + "_" + 
	    general_image_file_name_time(tm) +
	    "_" + version + ".jpg";
    } else {
	return "img" + this.image_size + "/image_not_available.jpg";
    }
}

function aerosol_image_file_name_base(ft, version) {
    var od = "";
    if(parseInt(version.substring(1,3), 10) >= 7) {
	od = "_" + this.odepth_type;
    }
    if(ft ===0) {
	if(parseInt(version.substring(1,3), 10) >= 8) {
	    return (this.image_size == "small" ? "data/odepth_small" : 
		    "data/odepth") + "_" + this.band_type;
	} else {
	    return (this.image_size == "small" ? "data/odepth_small" : 
		    "data/odepth");
	}
    } else if(ft ==1) {
	return (this.image_size == "small" ? "data/angexp_small" : 
		"data/angexp") + od;
    } else if(ft ==2) {
	return (this.image_size == "small" ? "data/ssa_small" : 
		"data/ssa") +
	    "_" + this.band_type + od;
    } else if(ft ==3) {
	return (this.image_size == "small" ? "data/odfrac_small" : 
		"data/odfrac") +
	    "_" + this.particle_type +
	    "_" + this.band_type + od;
    } else if(ft ==4) {
	if(parseInt(version.substring(1,3), 10) >= 8) {
	    return "img" + this.image_size + "/image_not_available.jpg";
	} else {
	    return (this.image_size == "small" ? "data/numfrac_small" : 
		    "data/numfrac") +
		"_" + this.particle_type + od;
	}
    } else if(ft ==5) {
	if(parseInt(version.substring(1,3), 10) >= 8) {
	    return "img" + this.image_size + "/image_not_available.jpg";
	} else {
	    return (this.image_size == "small" ? "data/volfrac_small" : 
		    "data/volfrac") +
		"_" + this.particle_type + od;
	}
    } 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;
    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("data/ndvi", 
			       "data/dhrpar", 
			       "data/dhrsw",
			       "data/fpar", 
			       "data/lai", 
			       "data/dhr_natural",
			       "data/dhr_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), 10) < 3 ||
	parseInt(input_version.substring(1,3), 10) >= 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 {
	return "img" + this.image_size + "/image_not_available.jpg";
    }
}

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;
}

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_" : "data/rad_") +
	ft + "_" + this.image_type + "_" + this.rccm_type;
}

// -------------------------------------------------------------
// Image file name for CGCL.
// This has the additional field height_type2, wind_type, 
// wind_index, cloud_type. 
// This also defined image_file_name_height, which is like 
// image_file_name but takes the height as an argument.
// -------------------------------------------------------------

function CloudFileName(a, is, ht, wi, wt, ct) {
    this.available = a;
    this.image_size = is;
    this.height_type2 = ht;
    this.wind_index = wi;
    this.wind_type = wt;
    this.cloud_type = ct;
    this.data_type = CGCL;
    this.input_version = general_input_version;
    this.image_file_name = general_image_file_name;
    this.image_file_name_base = cloud_image_file_name_base;
    this.image_file_name_height = cloud_image_file_name_height;
}

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

function cloud_image_file_name_base(ft) {
    if(ft <0 ||
       ft >2) {
	alert("Unknown file type for cloud_image_file_name_base. ft: " + 
	      ft);
    }
    if(ft ==0) {
	return (this.image_size =="small" ? "data/stereo_height_small_" :
		"data/stereo_height_") + this.height_type2 + "_" +
	    this.wind_type;
    } else if(ft ==1) {
	return (this.image_size =="small" ? "data/wind_speed_small_" :
		"data/wind_speed_") + this.height_type2 + "_" +
	    this.wind_index;
    } else {
	return (this.image_size =="small" ? "data/cloud_fraction_small_" : 
		"data/cloud_fraction_") + this.cloud_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;
}

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), 10) >= 3) {
	return this.image_file_name_base(ft) + "_" + 
	    general_image_file_name_time(tm) +
	    "_" + version + ".jpg";
    } else {
	return "img" + this.image_size + "/image_not_available.jpg";
    }
}

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_" : "data/rccm_") 
	+ 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) + 15;
    } 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_" : 
		"data/expansive_") + this.band_type2 +
	    "_" + (this.range_type ==1 ? "overflow_" : "") +
	    this.resolution_type;
    } else if(ft ==1) {
	if(USE_HEIGHT) {
	    return (this.image_size =="small" ? "data/local_small_" : 
		    "data/local_") + this.band_type2 + 
		"_" + this.height_index() +
		"_" + (this.range_type ==1 ? "overflow_" : "") +
		this.resolution_type;
	} else {
	    return (this.image_size =="small" ? "data/local_small_" : 
		    "data/local_") + this.band_type2 + "_" +
		    this.resolution_type;
	}
    } else if(ft ==2) {
	if(USE_HEIGHT) {
	    return (this.image_size =="small" ? "data/restrictive_small_" : 
		    "data/restrictive_") + this.band_type2 + 
		"_" + this.height_index() +
		"_" + (this.range_type ==1 ? "overflow_" : "") +
		this.resolution_type;
	} else {
	    return (this.image_size =="small" ? "data/restrictive_small_" : 
		    "data/restrictive_") + this.band_type2 + "_" +
		    this.resolution_type;
	}
    } else if(ft ==3) {
	return (this.image_size =="small" ? "data/forcing_small_" : 
		"data/forcing_") + this.band_type2 +
	    "_" + this.resolution_type;
    } else {
	alert("Unknown file type for albedo_image_file_name_base. ft: " + 
	      ft);
    }
}
