示例#1
0
  public void writeFile(String filePath, Map input) {
    Velocity.init();
    VelocityContext context = new VelocityContext();

    // Weather variable.
    BucketEntry weather = MapUtil.getBucket(input, "weather").get(0);
    context.put("wst_insi", MapUtil.getValueOr(weather.getValues(), "wst_insi", "?"));

    // Soil metadata
    context.put("classification", MapUtil.getValueOr(weather.getValues(), "classification", "?"));
    context.put("soil_site", MapUtil.getValueOr(weather.getValues(), "soil_site", "?"));
    context.put("soil_name", MapUtil.getValueOr(weather.getValues(), "soil_name", "?"));
    context.put("sl_source", MapUtil.getValueOr(weather.getValues(), "sl_source", "?"));
    context.put("soil_lat", MapUtil.getValueOr(weather.getValues(), "soil_lat", "?"));
    context.put("soil_long", MapUtil.getValueOr(weather.getValues(), "soil_long", "?"));

    // Setup the soil layers.
    SoilLayer[] layers;
    BucketEntry soil = MapUtil.getBucket(input, "soil").get(0);
    ArrayList data = soil.getDataList();
    layers = new SoilLayer[data.size()];
    double cumThickness = 0.0;
    for (int i = 0; i < data.size(); i++) {
      layers[i] = new SoilLayer();
      layers[i].ReadFrom((Map) data.get(i));
      cumThickness = layers[i].calcThickness(cumThickness);
    }
    context.put("layers", layers);

    // Write template.
    Template template = Velocity.getTemplate("src\\main\\resources\\AgMIPTemplate.apsim");
    FileWriter F;
    try {

      F = new FileWriter("Test.apsim");
      template.merge(context, F);
      F.close();

    } catch (IOException ex) {
      Logger.getLogger(ApsimOutput.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
  @Test
  public void testGetPaddyIrrigation() throws IOException, Exception {
    log.debug("== testGetPaddyIrrigation() Test Start ==");
    String num = "3";
    String percRate = "2";
    String dept = "150";
    String[] offsets = {"-3", "4", "11"};
    String[] maxVals = {"20", "30", "50"};
    String[] minVals = {"5", "10", "15"};
    // planting data is 19990415
    HashMap expected = new HashMap();
    AcePathfinderUtil.insertValue(expected, "idate", "19990412");
    AcePathfinderUtil.insertValue(expected, "irop", "IR010");
    AcePathfinderUtil.insertValue(expected, "irval", "150");
    AcePathfinderUtil.insertValue(expected, "idate", "19990412");
    AcePathfinderUtil.insertValue(expected, "irop", "IR008");
    AcePathfinderUtil.insertValue(expected, "irval", "2");
    AcePathfinderUtil.insertValue(expected, "idate", "19990412");
    AcePathfinderUtil.insertValue(expected, "irop", "IR009");
    AcePathfinderUtil.insertValue(expected, "irval", "20");
    AcePathfinderUtil.insertValue(expected, "idate", "19990412");
    AcePathfinderUtil.insertValue(expected, "irop", "IR011");
    AcePathfinderUtil.insertValue(expected, "irval", "5");
    AcePathfinderUtil.insertValue(expected, "idate", "19990419");
    AcePathfinderUtil.insertValue(expected, "irop", "IR009");
    AcePathfinderUtil.insertValue(expected, "irval", "30");
    AcePathfinderUtil.insertValue(expected, "idate", "19990419");
    AcePathfinderUtil.insertValue(expected, "irop", "IR011");
    AcePathfinderUtil.insertValue(expected, "irval", "10");
    AcePathfinderUtil.insertValue(expected, "idate", "19990426");
    AcePathfinderUtil.insertValue(expected, "irop", "IR009");
    AcePathfinderUtil.insertValue(expected, "irval", "50");
    AcePathfinderUtil.insertValue(expected, "idate", "19990426");
    AcePathfinderUtil.insertValue(expected, "irop", "IR011");
    AcePathfinderUtil.insertValue(expected, "irval", "15");
    ArrayList<HashMap<String, String>> expArr =
        MapUtil.getBucket(expected, "management").getDataList();

    HashMap<String, Object> data = new HashMap<String, Object>();
    AcePathfinderUtil.insertValue(data, "pdate", "19990415");
    ArrayList<HashMap<String, String>> actArr =
        ExperimentHelper.getPaddyIrrigation(data, num, percRate, dept, offsets, maxVals, minVals);

    assertEquals("getPaddyIrrigation: unexpected output", expArr, actArr);
    log.info("getPaddyIrrigation Output: {}", actArr.toString());
    log.debug("== testGetPaddyIrrigation() Test End ==");
  }
  @Test
  public void testgetAutoEventDate_machakos_givenPdates() throws IOException, Exception {

    log.debug("==testgetAutoEventDate_machakos_givenPdates() Test Start ==");
    ArrayList<ArrayList<HashMap<String, String>>> expected = new ArrayList();
    int expDur = 2;
    HashMap pEvent = new HashMap();
    int pdate = 19820203;
    int edate = 19820206;
    pEvent.put("event", "planting");
    pEvent.put("crid", "MAZ");
    HashMap iEvent = new HashMap();
    int idate = 19820215;
    iEvent.put("event", "irrigation");
    iEvent.put("irop", "ir001");
    HashMap iaEvent = new HashMap();
    int iadate = 19820315;
    iaEvent.put("event", "auto_irrig");
    iaEvent.put("irmdp", "1234");
    HashMap feEvent = new HashMap();
    int fedate = 19820415;
    feEvent.put("event", "fertilizer");
    feEvent.put("fecd", "123");
    HashMap tEvent = new HashMap();
    int tdate = 19820515;
    tEvent.put("event", "tillage");
    tEvent.put("tiimp", "123456");
    HashMap omEvent = new HashMap();
    int omdate = 19820615;
    omEvent.put("event", "organic_matter");
    omEvent.put("omcd", "12345");
    HashMap hEvent = new HashMap();
    int hdate = 19820715;
    hEvent.put("event", "harvest");
    hEvent.put("harm", "111");
    HashMap cEvent = new HashMap();
    int cdate = 19820815;
    cEvent.put("event", "chemicals");
    cEvent.put("chcd", "222");
    HashMap mEvent = new HashMap();
    int mdate = 19820915;
    mEvent.put("event", "mulch");
    mEvent.put("mltp", "333");

    for (int i = 0; i < expDur; i++) {
      expected.add(new ArrayList());
      expected.get(i).add(createEvent(pEvent, pdate, i));
      expected.get(i).get(expected.get(i).size() - 1).put("edate", edate + 10000 * i + "");
      expected.get(i).add(createEvent(iEvent, idate, i));
      expected.get(i).add(createEvent(iaEvent, iadate, i));
      expected.get(i).add(createEvent(feEvent, fedate, i));
      expected.get(i).add(createEvent(tEvent, tdate, i));
      expected.get(i).add(createEvent(omEvent, omdate, i));
      expected.get(i).add(createEvent(hEvent, hdate, i));
      expected.get(i).add(createEvent(cEvent, cdate, i));
      expected.get(i).add(createEvent(mEvent, mdate, i));
    }

    HashMap<String, Object> data = new HashMap();
    AcePathfinderUtil.insertValue(data, "pdate", "19820203");
    MapUtil.getBucket(data, "management").getDataList().get(0).put("edate", "19820204");
    AcePathfinderUtil.insertValue(data, "crid", "MAZ");
    AcePathfinderUtil.insertValue(data, "idate", "19820213");
    AcePathfinderUtil.insertValue(data, "irop", "ir001");
    //        AcePathfinderUtil.insertValue(data, "iadate", "19820313");
    AcePathfinderUtil.insertValue(data, "irmdp", "1234");
    MapUtil.getBucket(data, "management").getDataList().get(2).put("date", "19820313");
    AcePathfinderUtil.insertValue(data, "fedate", "19820413");
    AcePathfinderUtil.insertValue(data, "fecd", "123");
    AcePathfinderUtil.insertValue(data, "tdate", "19820513");
    AcePathfinderUtil.insertValue(data, "tiimp", "123456");
    AcePathfinderUtil.insertValue(data, "omdat", "19820613");
    AcePathfinderUtil.insertValue(data, "omcd", "12345");
    AcePathfinderUtil.insertValue(data, "hadat", "19820713");
    AcePathfinderUtil.insertValue(data, "harm", "111");
    AcePathfinderUtil.insertValue(data, "cdate", "19820813");
    AcePathfinderUtil.insertValue(data, "chcd", "222");
    AcePathfinderUtil.insertValue(data, "mladat", "19820913");
    AcePathfinderUtil.insertValue(data, "mltp", "333");
    data.put("exp_dur", expDur + "");
    data.put("origin_pdate", "19820201");
    String[] pdates = {"19820203", "19830203"};

    log.info("Inputs: {}", data);
    ArrayList<ArrayList<HashMap<String, String>>> results =
        ExperimentHelper.getAutoEventDate(data, pdates);
    log.info("Results: {}", results);

    assertEquals("getAutoEventDate: unexpected result", expected, results);
  }
  @Test
  public void testGetAutoEvent() throws IOException, Exception {

    ArrayList<ArrayList<HashMap<String, String>>> expected = new ArrayList();
    int expDur = 3;
    int sc_year = 2010;
    HashMap pEvent = new HashMap();
    int pdate = 20100203;
    int edate = 20100204;
    pEvent.put("event", "planting");
    pEvent.put("crid", "MAZ");
    HashMap iEvent = new HashMap();
    int idate = 20100213;
    iEvent.put("event", "irrigation");
    iEvent.put("irop", "ir001");
    HashMap iaEvent = new HashMap();
    int iadate = 20100313;
    iaEvent.put("event", "auto_irrig");
    iaEvent.put("irmdp", "1234");
    HashMap feEvent = new HashMap();
    int fedate = 20100413;
    feEvent.put("event", "fertilizer");
    feEvent.put("fecd", "123");
    HashMap tEvent = new HashMap();
    int tdate = 20100513;
    tEvent.put("event", "tillage");
    tEvent.put("tiimp", "123456");
    HashMap omEvent = new HashMap();
    int omdate = 20100613;
    omEvent.put("event", "organic_matter");
    omEvent.put("omcd", "12345");
    HashMap hEvent = new HashMap();
    int hdate = 20100713;
    hEvent.put("event", "harvest");
    hEvent.put("harm", "111");
    HashMap cEvent = new HashMap();
    int cdate = 20100813;
    cEvent.put("event", "chemicals");
    cEvent.put("chcd", "222");
    HashMap mEvent = new HashMap();
    int mdate = 20100913;
    mEvent.put("event", "mulch");
    mEvent.put("mltp", "333");

    for (int i = 0; i < expDur; i++) {
      expected.add(new ArrayList());
      expected.get(i).add(createEvent2(pEvent, pdate, i));
      expected.get(i).get(expected.get(i).size() - 1).put("edate", edate + 10000 * i + "");
      expected.get(i).add(createEvent2(iEvent, idate, i));
      expected.get(i).add(createEvent2(iaEvent, iadate, i));
      expected.get(i).add(createEvent2(feEvent, fedate, i));
      expected.get(i).add(createEvent2(tEvent, tdate, i));
      expected.get(i).add(createEvent2(omEvent, omdate, i));
      expected.get(i).add(createEvent2(hEvent, hdate, i));
      expected.get(i).add(createEvent2(cEvent, cdate, i));
      expected.get(i).add(createEvent2(mEvent, mdate, i));
    }

    HashMap<String, Object> data = new HashMap();
    AcePathfinderUtil.insertValue(data, "pdate", "19820203");
    MapUtil.getBucket(data, "management").getDataList().get(0).put("edate", "19820204");
    AcePathfinderUtil.insertValue(data, "crid", "MAZ");
    AcePathfinderUtil.insertValue(data, "idate", "19820213");
    AcePathfinderUtil.insertValue(data, "irop", "ir001");
    //        AcePathfinderUtil.insertValue(data, "iadate", "19820313");
    AcePathfinderUtil.insertValue(data, "irmdp", "1234");
    MapUtil.getBucket(data, "management").getDataList().get(2).put("date", "19820313");
    AcePathfinderUtil.insertValue(data, "fedate", "19820413");
    AcePathfinderUtil.insertValue(data, "fecd", "123");
    AcePathfinderUtil.insertValue(data, "tdate", "19820513");
    AcePathfinderUtil.insertValue(data, "tiimp", "123456");
    AcePathfinderUtil.insertValue(data, "omdat", "19820613");
    AcePathfinderUtil.insertValue(data, "omcd", "12345");
    AcePathfinderUtil.insertValue(data, "hadat", "19820713");
    AcePathfinderUtil.insertValue(data, "harm", "111");
    AcePathfinderUtil.insertValue(data, "cdate", "19820813");
    AcePathfinderUtil.insertValue(data, "chcd", "222");
    AcePathfinderUtil.insertValue(data, "mladat", "19820913");
    AcePathfinderUtil.insertValue(data, "mltp", "333");
    data.put("exp_dur", expDur + "");
    data.put("sc_year", sc_year + "");

    //        HashMap dummyEvent = new HashMap();
    //        dummyEvent.put("date", "abc");
    //        MapUtil.getBucket(data, "management").getDataList().add(dummyEvent);

    log.info("Inputs: {}", data);
    ArrayList<ArrayList<HashMap<String, String>>> results = ExperimentHelper.getAutoEvent(data);
    log.info("Results: {}", results);

    assertEquals("getAutoEvent: unexpected result", expected, results);
  }