V3_0.java [src/java/d/soils/rse02_wepot] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package d.soils.rse02_wepot;

import adb.DBResources;
import static adb.DBResources.EROSION_SQLSVR;
import static adb.DBResources.R2GIS_SQLSVR;
import csip.Config;
import csip.ModelDataService;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import static csip.annotations.ResourceType.FILE;
import csip.api.server.PayloadParameter;
import d.util.WWErosionResultData;
import d.util.WindWaterErosion;
import static d.util.WindWaterErosion.WWE_TIFF_FILE;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import static soils.db.DBResources.SDM;
import soils.db.SOILS_DATA;
import soils.db.SOILS_DB_Factory;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
@Name("wepot")
@Description("This service computes water and wind erodibility potentials for"
    + " an area of analysis (AoA).  The service clips SSURGO soil mapunits "
    + "with AoA geometry, determines the dominant soil component in the AoA, "
    + "gets parameters from the SSURGO component table, including a climate "
    + "factor from the C Factor layer, and computes the following equations:  "
    + "Wind Erosion Potential = C*I/T ;  Water Erosion Potential = K*(LS)/T")
@Path("d/wepot/3.0")

//@Resource(file = "/data/us_cvalues.zip", type = ARCHIVE)
@Resource(file = "/data/us_cvalues_topo2ras_masked.tif", type = FILE, id = WWE_TIFF_FILE)
@Resource(file = "/data/us_cvalues_topo2ras_masked.tfw", type = FILE, id = "tfw_file")
@Resource(file = "/data/us_cvalues_topo2ras_masked.tif.aux.xml", type = FILE, id = "tifxml_file")

@Resource(from = soils.db.DBResources.class)
@Resource(from = DBResources.class)
public class V3_0 extends ModelDataService {

  private final static String USE_IRRIGATION_ADJUSTMENT = "has_irrigation";

  @Override
  protected Map<String, Object> getConfigInfo() {
    return new LinkedHashMap<String, Object>() {
      {
        put("soils.gis.database.source", resources().getResolved("soils.gis.database.source"));
        put(SDM, resources().getResolved(SDM));
        put(R2GIS_SQLSVR, resources().getResolved(R2GIS_SQLSVR));
        put("cfactor_gis_raster_file", resources().getResolved(WWE_TIFF_FILE));
        put("fpp.version", "wepot 3.0");
      }
    };
  }

  @Override
  protected void doProcess() throws Exception {
    PayloadParameter params = parameter();

    try (SOILS_DATA soilsDb = SOILS_DB_Factory.createEngine(getClass(), LOG, Config.getString("soils.gis.database.source")); Connection gisDb = resources().getJDBC(EROSION_SQLSVR);) {
      soils.AoA.setMapUnitsRequired(false);
      WindWaterErosion.loadGisRaster(() -> resources().getFile(WWE_TIFF_FILE));

      WindWaterErosion aoaErosion = new WindWaterErosion(soilsDb, gisDb, getParamMap(), LOG);

      aoaErosion.getErosion(() -> resources().getJDBC(R2GIS_SQLSVR), params.getBoolean(USE_IRRIGATION_ADJUSTMENT, false)); //Will throw exception on failure

      aoaErosion.outputSoilsData(true);
      setOutputOdering(aoaErosion);
      aoaErosion.toJSON((String name, Object val, String descr, String unit) -> results().put(name, val, descr, unit));

    }
  }

  protected void setOutputOdering(WindWaterErosion aoaErosion) {
    aoaErosion.setOutputColumns(new ArrayList<String>(Arrays.asList(
        WWErosionResultData.AOA_AREA, WWErosionResultData.AOA_IFACTOR, WWErosionResultData.AOA_KFACTOR, WWErosionResultData.AOA_TFACTOR,
        WWErosionResultData.CFACTOR, WWErosionResultData.CFACTOR_PCT, WWErosionResultData.CFACTOR_SOURCE,
        WWErosionResultData.WATER_EP, WWErosionResultData.WATER_KFACTOR, WWErosionResultData.WATER_LSFACTOR, WWErosionResultData.WATER_TFACTOR,
        WWErosionResultData.WIND_EP, WWErosionResultData.WIND_IFACTOR, WWErosionResultData.WIND_TFACTOR,
        WWErosionResultData.DOM_WATER_COKEY, WWErosionResultData.DOM_WATER_COMPNAME, WWErosionResultData.DOM_WATER_COMPONENT_AREA, WWErosionResultData.DOM_WATER_COMPONENT_AREA_PERCENT,
        WWErosionResultData.DOM_WIND_COKEY, WWErosionResultData.DOM_WIND_COMPNAME, WWErosionResultData.DOM_WIND_COMPONENT_AREA, WWErosionResultData.DOM_WIND_COMPONENT_AREA_PERCENT,
        WWErosionResultData.WEIGHTED_AVG_WATER_EP, WWErosionResultData.HEIGHEST_WATER_EP, WWErosionResultData.HEIGHEST_WATER_EP_AREA, WWErosionResultData.HEIGHEST_WATER_EP_AREA_PERCENT,
        WWErosionResultData.HEIGHEST_WATER_EP_COKEY, WWErosionResultData.HEIGHEST_WATER_EP_COMPNAME, WWErosionResultData.HEIGHEST_WATER_EP_KFACTOR,
        WWErosionResultData.HEIGHEST_WATER_EP_LSFACTOR, WWErosionResultData.HEIGHEST_WATER_EP_TFACTOR,
        WWErosionResultData.WEIGHTED_AVG_WIND_EP, WWErosionResultData.HEIGHEST_WIND_EP, WWErosionResultData.HEIGHEST_WIND_EP_AREA, WWErosionResultData.HEIGHEST_WIND_EP_AREA_PERCENT,
        WWErosionResultData.HEIGHEST_WIND_EP_COKEY, WWErosionResultData.HEIGHEST_WIND_EP_COMPNAME,
        WWErosionResultData.HEIGHEST_WIND_EP_IFACTOR, WWErosionResultData.HEIGHEST_WIND_EP_TFACTOR
    )));
  }
}