<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.climate.maca;

import csip.annotations.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import m.climate.NetcdfModelDataService;

import javax.ws.rs.Path;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

/**
 * MACA extraction
 *
 * @author od
 */
@Name("MACA")
@Description("MACA weather extraction using local netcdf files")
@Path("m/maca/1.0")
public class V1_0 extends NetcdfModelDataService {

  JSONObject results = new JSONObject();

  @Override
  public void doProcess() throws Exception {
    String forecast_option = parameter().getString("forecast_option", "rcp45");
    String model = parameter().getString("model", "0");
    File maca_dir = new File("/mnt/csip-climate/MACA/");

    ArrayList&lt;List&lt;String&gt;&gt; ncFiles = new ArrayList&lt;List&lt;String&gt;&gt;();
    // Historical period
    ncFiles.add(Arrays.asList(String.format("%s/nwcsc_historical_%s.nc", maca_dir.toString(), model), "1950-01-01", "2005-12-31"));

    // Forecast period
    if ("rcp45".equals(forecast_option)) {
      ncFiles.add(Arrays.asList(String.format("%s/nwcsc_rcp45_%s.nc", maca_dir.toString(), model), "2006-01-01", "2099-12-31"));
    } else if ("rcp85".equals(forecast_option)) {
      ncFiles.add(Arrays.asList(String.format("%s/nwcsc_rcp85_%s.nc", maca_dir.toString(), model), "2006-01-01", "2099-12-31"));
    }

    setHeader(Arrays.asList("date", "tmpmin", "tmpmax", "pcpn", "dswrf", "sh", "windspeed"));
    setParameters(Arrays.asList("date", "tasmin", "tasmax", "precipitation", "surface_downwelling_shortwave_flux_in_air", "specific_humidity", "wind_speed"));
    HashMap&lt;String, String&gt; sourceUnits = new HashMap&lt;&gt;();
    sourceUnits.put("tasmin", "degK");
    sourceUnits.put("tasmax", "degK");
    sourceUnits.put("precipitation", "mm");
    sourceUnits.put("surface_downwelling_shortwave_flux_in_air", "W/m^2");
    sourceUnits.put("specific_humidity", "kg/kg");
    sourceUnits.put("wind_speed", "m/s");
    setSourceUnits(sourceUnits);

    JSONObject input_zone_features = parameter().getJSON("input_zone_features");
    String start_date_str = parameter().getString("start_date");
    String end_date_str = parameter().getString("end_date");

    JSONArray feature_results = extractFromNetcdf(input_zone_features, start_date_str, end_date_str, ncFiles);

    results.put("feature_results", feature_results);
  }

  @Override
  protected void postProcess() throws Exception {
    results().put("features", results.getJSONArray("feature_results"));
  }
}
</pre></body></html>