Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added weather data #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/*
*.swp
*.swp
/.settings
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.agmip.translators</groupId>
<artifactId>translator-apsim</artifactId>
<version>1.0-SNAPSHOT</version>
<name>translator-apsim</name>
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/agmip/translators/apsim/ApsimOutput.java
Original file line number Diff line number Diff line change
@@ -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;


Expand Down
63 changes: 63 additions & 0 deletions src/main/java/org/agmip/translators/apsim/weather/APSIM.java
Original file line number Diff line number Diff line change
@@ -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) +"";
}


}
72 changes: 72 additions & 0 deletions src/main/java/org/agmip/translators/apsim/weather/Record.java
Original file line number Diff line number Diff line change
@@ -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;
}



}
55 changes: 55 additions & 0 deletions src/main/java/org/agmip/translators/apsim/weather/Station.java
Original file line number Diff line number Diff line change
@@ -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<Record> records = new HashSet<Record>();
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<Record> getRecords() {
return records;
}
public void setRecords(Set<Record> records) {
this.records = records;
}




}

27 changes: 27 additions & 0 deletions src/main/java/org/agmip/translators/apsim/weather/Weather.java
Original file line number Diff line number Diff line change
@@ -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<Station> data = new ArrayList<Station>();

public List<Station> getData() {
return data;
}

public void setData(List<Station> data) {
this.data = data;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testTranslation()
JSONAdapter j = new JSONAdapter();
LinkedHashMap<String, Object> 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);
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/org/agmip/translators/apsim/weather/APSIMTest.java
Original file line number Diff line number Diff line change
@@ -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<Record> 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);
}

}

}