Skip to content
Snippets Groups Projects
avhrr_avhrr_sno.js 3.99 KiB
"use strict";

// http://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-javascript
function UrlExists(url)
{
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}

var aa = {}; // avhrr_avhrr namespace

aa.sats =
 {
  // 'noaa-05': [1978, 1980],
  // 'noaa-06': [1979, 1982],
  // 'noaa-07': [1981, 1985],
  'noaa-08': [1985, 1985],
  'noaa-09': [1985, 1988],
  'noaa-10': [1986, 1990],
  'noaa-11': [1988, 1994],
  'noaa-12': [1991, 1998],
  'noaa-14': [1995, 2002],
  'noaa-15': [1998, 2015],
  'noaa-16': [2000, 2006],
  'noaa-17': [2003, 2009],
  'noaa-18': [2005, 2015],
  'noaa-19': [2009, 2015],
  'metop-02': [2006, 2015],
  'metop-01': [2012, 2015],
 };

aa.refresh_years = function() {
    var xsat = $('#avhrr_avhrr .xsat option:selected').text();
    var old_year = $('#avhrr_avhrr .year option:selected').text();
    var ysat = $('#avhrr_avhrr .ysat option:selected').text();
    $('#avhrr_avhrr .year').empty();
    var year_match = false;
    var year_start = aa.sats[xsat][0];
    for (var y = year_start; y <= aa.sats[xsat][1]; y++) {
        if (y == old_year) {
            year_match = true;
        }
        $('#avhrr_avhrr .year').append($("<option></option>")
                .attr("value", y)
                .text(y));
    }
    if (year_match) {
        // preserve year selection when possible
        $('#avhrr_avhrr .year').val(old_year);    
    }
};

aa.init_sats = function() {
    $.each(aa.sats, function(k, v) {
        $('#avhrr_avhrr .xsat').append($("<option></option>")
                .attr("value", k)
                .text(k));
    });
};

aa.build_filename = function(ysat) {
    var xsat = $('#avhrr_avhrr .xsat option:selected').text();
    var year = $('#avhrr_avhrr .year option:selected').text();
    var channel = $('#avhrr_avhrr .channel option:selected').text();
    // ex: ch2_noaa-19_vs_noaa-15_2012_np_sno_rel_slope.png
    var plot = channel + '_' + ysat + '_vs_' +
        xsat + '_' + year + '_np_sno_rel_slope.png';
    return plot;
};

aa.refresh_plot = function() {
    var ysat = $('#avhrr_avhrr .ysat option:selected').text();
    var plot = aa.build_filename(ysat);
    $('#avhrr_avhrr .plot').attr("src", "avhrr_cal_img/"+plot);
    console.log('avhrr_avhrr new plot: ' + plot);
};

aa.refresh_ysats = function() {
    var xsat = $('#avhrr_avhrr .xsat option:selected').text();
    var year = $('#avhrr_avhrr .year option:selected').text();
    var x_start = aa.sats[xsat][0];
    var x_end = aa.sats[xsat][1];

    $('#avhrr_avhrr .ysat').empty();
    $.each(aa.sats, function(ysat, bounds) {
        var y_start = bounds[0];
        var y_end = bounds[1];
        if (y_end >= x_start && y_start <= x_end && 
                y_end >= year && y_start <= year &&
                xsat != ysat
                ) {
            if (UrlExists("avhrr_cal_img/"+aa.build_filename(ysat))) {            
                // TODO: this is messed up if "ch3a" is selected; we would
                //       want to list the ysat even though we don't have an
                //       image for this particular channel+sat+year combo.
                $('#avhrr_avhrr .ysat').append($("<option></option>")
                        .attr("value", ysat)
                        .text(ysat));
            }
        }
    });
    if ($('#avhrr_avhrr .ysat option').size() == 0) {
        $('#avhrr_avhrr .ysat').append($("<option></option>")
                .text("(no matches)"));
    }
};

// on page load:
$(function() {
    aa.init_sats();
    aa.refresh_years();
    aa.refresh_ysats();
    aa.refresh_plot();
    $('#avhrr_avhrr .xsat').change(function() {
        aa.refresh_ysats();
        aa.refresh_years();
        aa.refresh_plot();
    });
    $('#avhrr_avhrr .year').change(function() {
        aa.refresh_ysats();
        aa.refresh_plot();
    });
    $('#avhrr_avhrr .ysat').change(function() {
        aa.refresh_plot();
    });
    $('#avhrr_avhrr .channel').change(function() {
        aa.refresh_plot();
    });
});