WeatherModelDataService.java [src/java/m/weather] Revision: default Date:
/*
* 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.weather;
import csip.ModelDataService;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
/**
*
* @author pattersd
*/
public class WeatherModelDataService extends ModelDataService {
File result_file = null;
boolean download_link_only = false;
@Override
protected void preProcess() throws Exception {
}
@Override
protected String process() throws Exception {
// Check for selection by region or point
JSONObject req = getRequest();
String req_fname = getWorkspaceDir() + "/" + "req.json";
Files.write(Paths.get(req_fname), req.toString().getBytes());
// Check for file return instead of direct return of results
download_link_only = getBooleanParam("download_link_only", false);
File f = getResourceFile("driver");
getResourceFile("weatherExtraction");
try {
getResourceFile("ghcnd downloader");
} catch(Exception e) {}
// Extract climate type from service name
String s = getServicePath();
int idx = s.indexOf("/")+1;
String climate_type = s.substring(idx, s.indexOf("/", idx));
// String input_zone_geojson = getStringParam("input_zone_features", "");
// // Save features to file because a large request can overflow the parameter buffer
// String geojson_fname = getWorkspaceDir() + "/" + "input_zone_features.geojson";
// Files.write(Paths.get(geojson_fname), input_zone_geojson.getBytes());
//
// // Check for selection by station ID list
// String station_names_json = getStringParam("station_list", null);
//
// // MACA stuff
// String forecast_model = getStringParam("forecast_model", "");
// String forecast_option = getStringParam("forecast_option", "");
//
// String units = getStringParam("units", "english");
// String start_date = getStringParam("start_date", "");
//
// final SimpleDateFormat format;
// if (start_date.indexOf("-") > 0) {
// format = new SimpleDateFormat("yyyy-MM-dd");
// }
// else {
// format = new SimpleDateFormat("MM/dd/yyyy");
// }
// String end_date = getStringParam("end_date", format.format(new Date()));
// args.add(input_zone_geojson != "" ? geojson_fname : station_names_json);
// if (!forecast_model.isEmpty()) {
// args.add(forecast_model);
// args.add(forecast_option);
// }
// args.add(units);
// args.add(start_date);
// args.add(end_date);
// if (hasParam("generate_average")) {
// args.add(getBooleanParam("generate_average") ? "True" : "False");
// }
// if (hasParam("IDW_center")) {
// args.add(getStringParam("IDW_center"));
// }
// if (hasParam("time")) {
// args.add(getStringParam("time"));
// }
// if (hasParam("climate_data")) {
// args.add(getStringParam("climate_data"));
// }
// if (hasParam("return_full_period")) {
// args.add(getBooleanParam("return_full_period") ? "True" : "False");
// }
List<String> args = new ArrayList<String>();
args.add("python");
args.add(f.getAbsolutePath());
args.add(climate_type);
args.add(req_fname);
for (String arg: args) {
LOG.info("arguments = " + arg);
}
ProcessBuilder pb = new ProcessBuilder(args);
// ubuntu 12.04 installs an older version of libhdf5-serial-dev. It doesn't seem to cause a problem.
Map<String, String> env = pb.environment();
env.put("HDF5_DISABLE_VERSION_CHECK", "1");
// Write console to file rather than read it directly because the output
// is too large and will cause the process to block.
result_file = new File(getWorkspaceDir(), "results.csv");
pb.redirectOutput(result_file);
Process p = pb.start();
BufferedReader bfr_error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
int exitCode = p.waitFor();
LOG.info("Exit Code : "+exitCode);
String line;
String error_log = "";
while ((line=bfr_error.readLine()) != null) {
LOG.info("Error line:"+line);
error_log += line + "\n";
}
return (error_log.isEmpty() || climate_type.equals("maca")) ? null : error_log;
}
@Override
protected void postProcess() throws Exception {
if (download_link_only) {
putResult(result_file, "results file");
}
else {
byte[] result = Files.readAllBytes(Paths.get(result_file.getPath()));
JSONObject results = new JSONObject(new String(result));
putResult("results", results, "JSON string");
}
}
}