<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.ServiceException;
import csip.annotations.*;
import gisobjects.GISObjectException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import m.climate.ThreddsModelDataService;
import m.climate.ET;

import javax.ws.rs.Path;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import ucar.ma2.InvalidRangeException;

/**
 * MACA extraction
 *
 * @author od
 */
@Name("MACA")
@Description("MACA weather extraction using thredds server")
@Path("m/maca/3.0")
@Resources({
  @Resource(file = "/bin/python/maca_extraction.py", type = ResourceType.FILE, id = "maca_extraction"), //@Resource(file = "/bin/python/requirements.txt", type = ResourceType.FILE, id = "requirements"),
})
public class V3_0 extends ThreddsModelDataService {

  JSONArray results;

  @Override
  public void doProcess() throws ServiceException, InterruptedException {
    String forecastModel = parameter().getString("forecast_model");
    String forecastOption = parameter().getString("forecast_option");
    JSONObject inputZoneFeatures = parameter().getJSON("input_zone_features");
    String startDateStr = parameter().getString("start_date");
    String endDateStr = parameter().getString("end_date");
    String ts = parameter().getString("timestep", "daily");
    Boolean calcET = parameter().getBoolean("calc_pm", false);

    try {
      runExtraction(forecastModel, forecastOption, inputZoneFeatures, startDateStr, endDateStr, ts, calcET);
    } catch (GISObjectException | IOException | InvalidRangeException | JSONException | ParseException | ServiceException ex) {
      throw new ServiceException(ex);
    }
  }

  @Override
  protected void postProcess() throws Exception {
    results().put("output", results);
  }

  public void runExtraction(String forecastModel, String forecastOption, JSONObject inputZoneFeatures,
      String startDateStr, String endDateStr, String ts, boolean calcET) throws ServiceException, JSONException, IOException, GISObjectException, ParseException, InvalidRangeException, InterruptedException {

    setHeader(Arrays.asList("date", "tasmin", "tasmax", "pcpn", "rsds", "huss", "uas", "vas", "rhsmin", "rhsmax"));
    setParameters(Arrays.asList("date", "tasmin", "tasmax", "pr", "rsds", "huss", "uas", "vas", "rhsmin", "rhsmax"));

    HashMap&lt;String, String&gt; sourceUnits = new HashMap&lt;&gt;();
    sourceUnits.put("tasmin", "degK");
    sourceUnits.put("tasmax", "degK");
    sourceUnits.put("pr", "mm");
    sourceUnits.put("rsds", "W/m^2");
    sourceUnits.put("huss", "kg/kg");
    sourceUnits.put("uas", "m/s");
    sourceUnits.put("vas", "m/s");
    sourceUnits.put("rhsmin", "%");
    sourceUnits.put("rhsmax", "%");
    setSourceUnits(sourceUnits);

    setLogger(LOG);

    List&lt;String&gt; ncFiles = new ArrayList&lt;&gt;();
    String thredds_template = "http://eds0.engr.colostate.edu:8080/thredds/dodsC/macav2/macav2_"
        + "%1$s_daily.ncml{param}_%2$s_r1i1p1_%3$s.nc";

    // Historical period
    ncFiles.add(String.format(thredds_template, "historical", forecastModel, "historical"));

    // Forecast period
    ncFiles.add(String.format(thredds_template, "future", forecastModel, forecastOption));

    results = extractFromThredds(ncFiles, inputZoneFeatures, startDateStr, endDateStr, ts);
    //results = extractFromThreddsPython(forecastModel, forecastOption, inputZoneFeatures,
    //        startDateStr, endDateStr, ts);

    if (calcET) {
      // Add Potential ET
      ET et = new ET(LOG);
      et.addETtoFeatures(results);
    }
  }

  public static void main(String[] args) throws Exception {
    JSONObject inputZoneFeatures = new JSONObject("{ "
        + " \"type\":\"FeatureCollection\", "
        + " \"features\":[ "
        + "   { "
        + "     \"type\":\"Feature\", "
        + "     \"properties\":{ "
        + "       \"name\": \"pt one\", "
        + "       \"gid\": 1 "
        + "     }, "
        + "     \"geometry\":{ "
        + "       \"type\":\"Point\", "
        + "       \"coordinates\":[ "
        + "         -104.9220085144043, "
        + "         40.53148031584938 "
        + "       ] "
        + "     } "
        + "   } "
        + " ] "
        + " }");

    JSONObject fortCollins = new JSONObject(
        "{ "
        + " \"type\":\"FeatureCollection\", "
        + " \"features\":[ "
        + "   { "
        + "     \"type\":\"Feature\", "
        + "     \"properties\":{ "
        + "       \"name\": \"pt one\", "
        + "       \"gid\": 1 "
        + "     }, "
        + "     \"geometry\":{ "
        + "     \"type\": \"Polygon\",  "
        + "       \"coordinates\": [[ "
        + "          [-105.155, 40.6],  "
        + "          [-105.155, 40.4],  "
        + "          [-104.985, 40.4],  "
        + "          [-104.985, 40.6], "
        + "          [-105.155, 40.6],  "
        + "       ]]"
        + "     } "
        + "   } "
        + " ] "
        + " }");

    V3_0 v = new V3_0();
    v.runExtraction("bcc-csm1-1", "historical", fortCollins, "1950-01-01", "1959-01-30", "monthly", true);
    v.logger.info(v.results.toString());
  }
}
</pre></body></html>