V1_0.java [src/java/m/crp/climate_wind_soil] Revision: fad70b35517781f034beb514ca90f123dc4edaf6 Date: Fri Dec 06 14:19:16 MST 2019
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API, and application suite.
*
* 2012-2019, 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 m.crp.climate_wind_soil;
import csip.Config;
import csip.ModelDataService;
import csip.PayloadParameter;
import csip.PayloadResults;
import csip.ServiceException;
import csip.annotations.Resource;
import static crp.utils.DBResources.LOCAL_SQLSERVER;
import gisobjects.GISObject;
import gisobjects.GISObjectFactory;
import gisobjects.db.GISEngine;
import gisobjects.db.GISEngineFactory;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONObject;
import crp.utils.Climate;
import crp.utils.FPPSoilParams;
import crp.utils.WindGen;
import soils.db.DBResources;
/**
*
* @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
*/
@Name("CliWindSoil")
@Description("Collects Climate Wind and Soil data for any given lat/lon for use in metamodeling")
@Path("m/cliwindsoil/1.0")
@Resource(from = DBResources.class)
public class V1_0 extends ModelDataService {
JSONObject aoa_geometry;
private boolean useCsipJSON = true;
private boolean returnIntersectPolys = true;
protected String soilURI = "http://csip.engr.colostate.edu:8083/csip-soils/d/fppsoilparams/2.0";
protected String climateURI = "http://csip.engr.colostate.edu:8083/csip-climate/m/cligen_prism/2.0";
protected String windURI = "http://csip.engr.colostate.edu:8083/csip-climate/m/windgen/2.0";
protected String outputFile;
protected boolean streamOutputFile = false;
@Override
public void preProcess() throws ServiceException {
PayloadParameter params = parameter();
soilURI = Config.getString("crp.soils", soilURI);
windURI = Config.getString("crp.windgen", windURI);
climateURI = Config.getString("crp.cligen", climateURI);
if (params.has("aoa_geometry")) {
aoa_geometry = getParam("aoa_geometry");
} else {
throw new ServiceException("Service requires an aoa_geometry input.");
}
if (params.has("stream_file")) {
streamOutputFile = params.getBoolean("stream_file");
}
}
@Override
public void doProcess() throws Exception {
if (null != aoa_geometry) {
try (Connection gisDb = resources().getJDBC(LOCAL_SQLSERVER);
GISEngine tEngine = GISEngineFactory.createGISEngine(gisDb);) {
String inputSUID = getMetainfo().getString("suid");
GISObjectFactory.setFixBadGeometries(true);
GISObject inputShape = GISObjectFactory.createGISObject(aoa_geometry, tEngine);
if ((null == inputShape) || !inputShape.isValid()) {
throw new ServiceException("Cannot generate GISObject shape from the input geoJSON");
}
inputShape.makeValid(GISObject.UsePurpose.all_purposes, GISObject.GISType.all_types);
writeOutputFile(inputShape.toJSON().toString(), "request.json");
FPPSoilParams soilCall = new FPPSoilParams(getMetainfo(), inputShape, soilURI);
soilCall.call();
writeOutputFile(soilCall.getResultSection().toString(), "soils.json");
Climate climateData = new Climate(getMetainfo(), inputShape, climateURI);
climateData.call();
writeOutputFile(climateData.data(), "climate.cli");
WindGen windData = new WindGen(getMetainfo(), inputShape, windURI);
windData.call();
writeOutputFile(windData.data(), "wind.win");
}
}
}
@Override
public void postProcess() throws Exception {
PayloadResults results = this.results();
results.put(getWorkspaceFile("wind.win"));
results.put(getWorkspaceDir());
}
protected void writeOutputFile(String data, String filename) throws ServiceException {
byte[] outBytes = data.getBytes();
FileOutputStream fileOut = null;
BufferedOutputStream bufferOut;
File outFile = getWorkspaceFile(filename);
try {
fileOut = new FileOutputStream(outFile);
} catch (FileNotFoundException ex) {
throw new ServiceException("Cannot open that file: " + filename, ex);
}
bufferOut = new BufferedOutputStream(fileOut);
try {
bufferOut.write(outBytes, 0, outBytes.length);
bufferOut.flush();
bufferOut.close();
} catch (IOException ex) {
throw new ServiceException("Cannot write to that file: " + filename, ex);
}
}
////////////////////////////////
//
// Private member functions
//
////////////////////////////////
/**
* Get the full JSON parameter record.
*
* @param name
* @return the JSON record.
*/
private JSONObject getParam(String name) throws ServiceException {
JSONObject p = getParamMap().get(name);
if (p == null) {
throw new ServiceException("Parameter not found: '" + name + "'");
}
return p;
}
}