From 95ca580fa9d4b27b9218b593b9370e695c86d4c8 Mon Sep 17 00:00:00 2001 From: ioannis Date: Tue, 10 Jul 2012 17:07:40 -0400 Subject: [PATCH] Adding weather translator for APSIM --- .gitignore | 3 +- pom.xml | 1 - .../agmip/translators/apsim/ApsimOutput.java | 9 +- .../translators/apsim/weather/APSIM.java | 63 ++++++++++++ .../translators/apsim/weather/Record.java | 72 ++++++++++++++ .../translators/apsim/weather/Station.java | 55 +++++++++++ .../translators/apsim/weather/Weather.java | 27 ++++++ .../translators/apsim/TranslationTest.java | 2 +- .../translators/apsim/weather/APSIMTest.java | 96 +++++++++++++++++++ 9 files changed, 319 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/agmip/translators/apsim/weather/APSIM.java create mode 100644 src/main/java/org/agmip/translators/apsim/weather/Record.java create mode 100644 src/main/java/org/agmip/translators/apsim/weather/Station.java create mode 100644 src/main/java/org/agmip/translators/apsim/weather/Weather.java create mode 100644 src/test/java/org/agmip/translators/apsim/weather/APSIMTest.java diff --git a/.gitignore b/.gitignore index 6ce4500..e41b700 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target/* -*.swp \ No newline at end of file +*.swp +/.settings diff --git a/pom.xml b/pom.xml index 5599ec0..ebe0457 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - org.agmip.translators translator-apsim 1.0-SNAPSHOT translator-apsim diff --git a/src/main/java/org/agmip/translators/apsim/ApsimOutput.java b/src/main/java/org/agmip/translators/apsim/ApsimOutput.java index d98abb9..ab9f8ca 100644 --- a/src/main/java/org/agmip/translators/apsim/ApsimOutput.java +++ b/src/main/java/org/agmip/translators/apsim/ApsimOutput.java @@ -1,20 +1,17 @@ package org.agmip.translators.apsim; -import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.StringWriter; import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import org.agmip.util.MapUtil.BucketEntry; + import org.agmip.core.types.TranslatorOutput; import org.agmip.util.MapUtil; -import org.apache.velocity.VelocityContext; +import org.agmip.util.MapUtil.BucketEntry; import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; diff --git a/src/main/java/org/agmip/translators/apsim/weather/APSIM.java b/src/main/java/org/agmip/translators/apsim/weather/APSIM.java new file mode 100644 index 0000000..3c071f3 --- /dev/null +++ b/src/main/java/org/agmip/translators/apsim/weather/APSIM.java @@ -0,0 +1,63 @@ +package org.agmip.translators.apsim.weather; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; + +import java.util.Calendar; + + +public class APSIM { + + + public static void generateWeatherFiles(File path, Weather w) throws Exception{ + for(Station s: w.data){ + if(s.wsta_insi==null) throw new Exception("Cant create file. WSTA_INSI missing"); + + BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path,s.wsta_insi+".met")))); + br.write("!title: "); + br.write(s.wsta_site); + br.newLine(); + br.write("tav = "); + br.write(s.tav); + br.newLine(); + br.write("amp = "); + br.newLine(); + br.write("year day radn maxt mint rain wind dewp vers rh \n"); + br.write("() () (MJ/m2) (oC) (oC) (mm) (km) (oC) () (%)\n"); + + for(Record r : s.records){ + br.write(GetYear(r.w_date)+" "); + br.write(GetDay(r.w_date)+" "); + br.write(r.srad +" "); + br.write(r.tmax +" "); + br.write(r.tmin +" "); + br.write(r.rain +" "); + br.write(r.wind +" "); + br.write(r.dewp +" "); + br.write(r.vprs +" "); + br.write(r.rhum +" "); + br.newLine(); + } + + br.close(); + } + } + + public static String GetYear(String w_date) { + String[] part = w_date.split("-"); + return part[0]; + } + + public static String GetDay(String w_date) { + String[] part = w_date.split("-"); + Calendar c = Calendar.getInstance(); + c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(part[2])); + c.set(Calendar.MONTH, Integer.parseInt(part[1])-1); + c.set(Calendar.YEAR, Integer.parseInt(part[0])); + return c.get(Calendar.DAY_OF_YEAR) +""; + } + + +} diff --git a/src/main/java/org/agmip/translators/apsim/weather/Record.java b/src/main/java/org/agmip/translators/apsim/weather/Record.java new file mode 100644 index 0000000..4eee4aa --- /dev/null +++ b/src/main/java/org/agmip/translators/apsim/weather/Record.java @@ -0,0 +1,72 @@ +package org.agmip.translators.apsim.weather; + +public class Record { + + String w_date ="?"; + String srad="?"; //radn + String tmax="?"; //maxt + String tmin="?"; //mint + String rain="?"; + + String wind="?"; + String dewp="?"; + String vprs="?"; //vers + String rhum="?"; //rh + public String getW_date() { + return w_date; + } + public void setW_date(String w_date) { + this.w_date = w_date; + } + public String getSrad() { + return srad; + } + public void setSrad(String srad) { + this.srad = srad; + } + public String getTmax() { + return tmax; + } + public void setTmax(String tmax) { + this.tmax = tmax; + } + public String getTmin() { + return tmin; + } + public void setTmin(String tmin) { + this.tmin = tmin; + } + public String getRain() { + return rain; + } + public void setRain(String rain) { + this.rain = rain; + } + public String getWind() { + return wind; + } + public void setWind(String wind) { + this.wind = wind; + } + public String getDewp() { + return dewp; + } + public void setDewp(String dewp) { + this.dewp = dewp; + } + public String getVprs() { + return vprs; + } + public void setVprs(String vprs) { + this.vprs = vprs; + } + public String getRhum() { + return rhum; + } + public void setRhum(String rhum) { + this.rhum = rhum; + } + + + +} diff --git a/src/main/java/org/agmip/translators/apsim/weather/Station.java b/src/main/java/org/agmip/translators/apsim/weather/Station.java new file mode 100644 index 0000000..d3e714a --- /dev/null +++ b/src/main/java/org/agmip/translators/apsim/weather/Station.java @@ -0,0 +1,55 @@ +package org.agmip.translators.apsim.weather; + +import java.util.HashSet; +import java.util.Set; + + +public class Station { + String wsta_site; + String wsta_lat; + String wsta_long; + String wsta_insi; + String tav; + Set records = new HashSet(); + public String getWsta_site() { + return wsta_site; + } + public void setWsta_site(String wsta_site) { + this.wsta_site = wsta_site; + } + public String getWsta_lat() { + return wsta_lat; + } + public void setWsta_lat(String wsta_lat) { + this.wsta_lat = wsta_lat; + } + public String getWsta_long() { + return wsta_long; + } + public void setWsta_long(String wsta_long) { + this.wsta_long = wsta_long; + } + public String getWsta_insi() { + return wsta_insi; + } + public void setWsta_insi(String wsta_insi) { + this.wsta_insi = wsta_insi; + } + public String getTav() { + return tav; + } + public void setTav(String tav) { + this.tav = tav; + } + public Set getRecords() { + return records; + } + public void setRecords(Set records) { + this.records = records; + } + + + + +} + diff --git a/src/main/java/org/agmip/translators/apsim/weather/Weather.java b/src/main/java/org/agmip/translators/apsim/weather/Weather.java new file mode 100644 index 0000000..4a1b181 --- /dev/null +++ b/src/main/java/org/agmip/translators/apsim/weather/Weather.java @@ -0,0 +1,27 @@ +package org.agmip.translators.apsim.weather; + +import java.util.ArrayList; +import java.util.List; + +/** + * An APSIM Weather info compartment consists of several station information. + * + * + * @author Ioannis N. Athanasiadis, http://eco.logismi.co + * @since July 10, 2012 + * + */ +public class Weather { + List data = new ArrayList(); + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } + + + +} diff --git a/src/test/java/org/agmip/translators/apsim/TranslationTest.java b/src/test/java/org/agmip/translators/apsim/TranslationTest.java index 2a4613c..f27df79 100644 --- a/src/test/java/org/agmip/translators/apsim/TranslationTest.java +++ b/src/test/java/org/agmip/translators/apsim/TranslationTest.java @@ -45,7 +45,7 @@ public void testTranslation() JSONAdapter j = new JSONAdapter(); LinkedHashMap result; - String filePath = "src\\test\\resources\\UFGA8201_mzx.json"; + File filePath = new File( "src/test/resources/UFGA8201_mzx.json"); FileInputStream in = new FileInputStream(filePath); InputStreamReader inreader = new InputStreamReader(in); BufferedReader reader = new BufferedReader(inreader); diff --git a/src/test/java/org/agmip/translators/apsim/weather/APSIMTest.java b/src/test/java/org/agmip/translators/apsim/weather/APSIMTest.java new file mode 100644 index 0000000..2bd9b31 --- /dev/null +++ b/src/test/java/org/agmip/translators/apsim/weather/APSIMTest.java @@ -0,0 +1,96 @@ +package org.agmip.translators.apsim.weather; + + +import java.io.File; +import java.io.IOException; + +import java.util.Set; + +import junit.framework.TestCase; + +import org.agmip.translators.apsim.weather.APSIM; +import org.agmip.translators.apsim.weather.Record; +import org.agmip.translators.apsim.weather.Station; +import org.agmip.translators.apsim.weather.Weather; +import org.codehaus.jackson.JsonGenerationException; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.annotate.JsonSerialize; +import org.junit.Test; + +public class APSIMTest extends TestCase{ + Weather w= new Weather(); + + public void setUp() throws Exception{ + Station ws1 = new Station(); + w.getData().add(ws1); + ws1.setTav("11"); + ws1.setWsta_site("Station Name, Somewhere in the Galaxy"); + ws1.setWsta_insi("STA0001"); + ws1.setWsta_lat("-11.2345"); + ws1.setWsta_long("-11.2345"); + + Set recs = ws1.getRecords(); + + + { + Record r = new Record(); + r.w_date = "2012-01-02"; + r.srad = "0"; //radn + r.tmax = "13"; //maxt + r.tmin = "-12"; //mint + r.rain ="0.2"; + recs.add(r); + } + + { + Record r = new Record(); + r.w_date = "2012-01-01"; + r.srad = "0.3"; //radn + r.tmax = "18"; //maxt + r.tmin = "0.2"; //mint + r.rain ="0.2"; + recs.add(r); + } + + + + + } + + @Test + public void testGenerateWeatherFiles() { + try { + APSIM.generateWeatherFiles(new File("src/test/resources/"), w); + } catch (Exception e) { + assertTrue(false); + } + } + + @Test + public void testGetYear() { + assertEquals("2012", APSIM.GetYear("2012-12-12")); + assertEquals("2000", APSIM.GetYear("2000-2-1")); + } + + @Test + public void testGetDay() { + assertEquals("1",APSIM.GetDay("2012-1-1")); + assertEquals("32",APSIM.GetDay("2012-2-1")); + } + + + @Test + public void testJSON(){ + + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); + try { + System.out.println( mapper.writeValueAsString(w) ); + } catch (Exception e) { + assertTrue(false); + } + + } + +}