V1_0.java [src/java/d/csm_soils] 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 d.csm_soils;

import csip.Config;
import csip.ModelDataService;
import csip.api.server.PayloadParameter;
import csip.api.server.ServiceException;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.Resource;
import csip.utils.Validation;
import d.dataNodes.DSSATData;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.Path;
import soils.Component;
import soils.MapUnit;
import static soils.db.DBResources.SDM;
import soils.db.SOILS_DATA;
import soils.db.SOILS_DB_Factory;
import soils.exceptions.WEPPException;

/**
 *
 * @author sidereus
 */
@Name("DSSAT Soil Input")
@Description("Gets data from the NRCS Soil Data Mart and generates a soil component sol input file for the DSSAT model")
@Path("d/csm_soils/1.0")
@Resource(from = soils.db.DBResources.class)
public class V1_0 extends ModelDataService {

  static final String KEY_COKEY = "cokey";
  static final String FILE_NAME = "file_name";
  static final String LONGITUDE = "longitude";
  static final String LATITUDE = "latitude";
  static final String ID_SOIL = "id_soil";


    @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("fpp.version", "csm 1.0");
      }
    };
  }


    @Override
    protected void doProcess()  throws ServiceException {

    PayloadParameter payload = parameter();
    String cokey = payload.getString(KEY_COKEY);
    String fileName = payload.getString(FILE_NAME);
    double longitude = payload.getDouble(LONGITUDE);
    double latitude = payload.getDouble(LATITUDE);
    String id_soil = payload.getString(ID_SOIL);

    if (!fileName.endsWith(".SOL")) {
      fileName = fileName + ".SOL";
    }

    DecimalFormat df = new DecimalFormat("#.###");
    longitude = Double.parseDouble(df.format(longitude));
    latitude = Double.parseDouble(df.format(latitude));

    Validation.checkCokey(cokey);

    Component comp = null;
    MapUnit mapUnit = null;

    try (SOILS_DATA soilsDb = SOILS_DB_Factory.createEngine(getClass(), LOG, Config.getString("soils.gis.database.source"))) {
      comp = new Component();
      comp.cokey(cokey);
      if (soilsDb.validateComponent(Integer.parseInt(comp.cokey()))) {
        if (null == (mapUnit = soilsDb.findWEPPDataByCokey(comp))) {
          throw new ServiceException("Could not retrieve the proper SDM soils data for this cokey");
        }
      } else {
        throw new ServiceException("The cokey value provided does not exist in the SDM database.  Cannot continue.");
      }
    } catch (ServiceException | SQLException | WEPPException ex) {
      throw new RuntimeException(ex);
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }

    DSSATData data = new DSSATData();
    data.executeComputation(comp, workspace().getFile(fileName).getAbsolutePath(), id_soil, longitude, latitude);
    results().put(workspace().getFile(fileName));
  }

}