diff --git a/enunciate.xml b/enunciate.xml index db71c4be2b8f8b49001cf2ad2fd77ed60b8cf8c0..53627989c31b982f5807f3a57b23e5903c740567 100644 --- a/enunciate.xml +++ b/enunciate.xml @@ -5,7 +5,7 @@ The VIPSLogic service API gives access to stored pest predictions and organisms </description> <copyright>NIBIO</copyright> - <contact>Tor-Einar Skog</contact> + <contact><a href="https://www.nibio.no/ansatte/tor-einar-skog" target="new">Tor-Einar Skog</a></contact> <application root="/"/> diff --git a/nb-configuration.xml b/nb-configuration.xml index 2f83dadc32daaac6803a54762414e0936fc975ed..9da91a87437747225232f5c8e15d5128360b5222 100755 --- a/nb-configuration.xml +++ b/nb-configuration.xml @@ -23,9 +23,7 @@ Any value defined here will override the pom.xml file value but is only applicab <org-netbeans-modules-css-prep.sass_2e_mappings>/scss:/css</org-netbeans-modules-css-prep.sass_2e_mappings> <org-netbeans-modules-css-prep.sass_2e_enabled>false</org-netbeans-modules-css-prep.sass_2e_enabled> <org-netbeans-modules-css-prep.less_2e_compiler_2e_options/> - <org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>WildFly</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server> <org-netbeans-modules-projectapi.jsf_2e_language>Facelets</org-netbeans-modules-projectapi.jsf_2e_language> - <org-netbeans-modules-maven-jaxws._5f_C_5f_DMIWeatherService_2e_svc>https://dmiweatherservice-plant.dlbr.dk/DMIWeatherService.svc?wsdl</org-netbeans-modules-maven-jaxws._5f_C_5f_DMIWeatherService_2e_svc> <netbeans.compile.on.save>none</netbeans.compile.on.save> <netbeans.hint.jdkPlatform>JDK_1.8_SUN</netbeans.hint.jdkPlatform> </properties> diff --git a/pom.xml b/pom.xml index bc623282a18e2fb8fb19c881278bb40481004a43..5a57294a1f966f3d235699b551895130dea8709c 100755 --- a/pom.xml +++ b/pom.xml @@ -1,376 +1,336 @@ <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/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <groupId>no.nibio.vips.</groupId> - <artifactId>VIPSLogic</artifactId> - <packaging>war</packaging> - <version>1.0-SNAPSHOT</version> -<properties> + <groupId>no.nibio.vips.</groupId> + <artifactId>VIPSLogic</artifactId> + <packaging>war</packaging> + <version>1.0-SNAPSHOT</version> + <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <geotools.version>13.4</geotools.version> </properties> - <name>VIPSLogic</name> - <url>http://maven.apache.org</url> + <name>VIPSLogic</name> + <url>http://maven.apache.org</url> -<repositories> - <repository> - <id>jitpack.io</id> - <url>https://jitpack.io</url> - </repository> - <repository> - <id>bedatadriven</id> - <name>bedatadriven public repo</name> - <url>https://nexus.bedatadriven.com/content/groups/public/</url> - </repository> - <repository> - <id>unidata-releases</id> - <name>Unidata Releases</name> - <url>https://artifacts.unidata.ucar.edu/content/repositories/unidata-releases/</url> - </repository> -</repositories> - <dependencies> - <dependency> - <groupId>edu.ucar</groupId> - <artifactId>cdm</artifactId> - <version>4.6.10</version> - </dependency> - <dependency> - <groupId>org.flywaydb</groupId> - <artifactId>flyway-core</artifactId> - <version>4.2.0</version> -</dependency> - <dependency> - <groupId>com.github.bjornharrtell</groupId> - <!--groupId>org.wololo</groupId--> - <artifactId>jts2geojson</artifactId> - <version>0.10.0</version> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-spatial</artifactId> - <version>5.0.10.Final</version> - <exclusions> - <exclusion> - <artifactId>postgresql</artifactId> - <groupId>postgresql</groupId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>dom4j</groupId> - <artifactId>dom4j</artifactId> - <version>1.6.1</version> - <scope>provided</scope> - </dependency> + <repositories> + <repository> + <id>jitpack.io</id> + <url>https://jitpack.io</url> + </repository> + <repository> + <id>bedatadriven</id> + <name>bedatadriven public repo</name> + <url>https://nexus.bedatadriven.com/content/groups/public/</url> + </repository> + <repository> + <id>unidata-releases</id> + <name>Unidata Releases</name> + <url>https://artifacts.unidata.ucar.edu/content/repositories/unidata-releases/</url> + </repository> + </repositories> + <dependencies> + <dependency> + <groupId>edu.ucar</groupId> + <artifactId>cdm</artifactId> + <version>4.6.10</version> + </dependency> + <dependency> + <groupId>org.flywaydb</groupId> + <artifactId>flyway-core</artifactId> + <version>4.2.0</version> + </dependency> + <dependency> + <groupId>com.github.bjornharrtell</groupId> + <!--groupId>org.wololo</groupId--> + <artifactId>jts2geojson</artifactId> + <version>0.10.0</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-spatial</artifactId> + <version>5.0.10.Final</version> + <exclusions> + <exclusion> + <artifactId>postgresql</artifactId> + <groupId>postgresql</groupId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>dom4j</groupId> + <artifactId>dom4j</artifactId> + <version>1.6.1</version> + <scope>provided</scope> + </dependency> - <dependency> - <groupId>org.jboss.resteasy</groupId> - <artifactId>resteasy-jaxrs</artifactId> - <version>3.0.9.Final</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.jboss.resteasy</groupId> - <artifactId>resteasy-client</artifactId> - <version>3.0.9.Final</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>no.nibio.vips.common</groupId> - <artifactId>VIPSCommon</artifactId> - <version>1.0-SNAPSHOT</version> - <type>jar</type> - <exclusions> - <exclusion> - <groupId>org.python</groupId> - <artifactId>jython-standalone</artifactId> - </exclusion> - <exclusion> - <groupId>org.renjin</groupId> - <artifactId>renjin-script-engine</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>org.reflections</groupId> - <artifactId>reflections</artifactId> - <version>0.9.9</version> - <type>jar</type> - </dependency> - <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient</artifactId> - <version>4.5</version> - <type>jar</type> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.10</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - <version>2.5</version> - <type>jar</type> - </dependency> - <dependency> - <groupId>de.micromata.jak</groupId> - <artifactId>JavaAPIforKml</artifactId> - <version>2.2.0</version> - </dependency> - <!-- https://mvnrepository.com/artifact/net.postgis/postgis-jdbc --> -<dependency> - <groupId>net.postgis</groupId> - <artifactId>postgis-jdbc</artifactId> - <version>2.2.1</version> -</dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-jaxrs</artifactId> + <version>3.0.9.Final</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.jboss.resteasy</groupId> + <artifactId>resteasy-client</artifactId> + <version>3.0.9.Final</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>no.nibio.vips.common</groupId> + <artifactId>VIPSCommon</artifactId> + <version>1.0-SNAPSHOT</version> + <type>jar</type> + <exclusions> + <exclusion> + <groupId>org.python</groupId> + <artifactId>jython-standalone</artifactId> + </exclusion> + <exclusion> + <groupId>org.renjin</groupId> + <artifactId>renjin-script-engine</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.reflections</groupId> + <artifactId>reflections</artifactId> + <version>0.9.9</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.5</version> + <type>jar</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.10</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + <version>2.5</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>de.micromata.jak</groupId> + <artifactId>JavaAPIforKml</artifactId> + <version>2.2.0</version> + </dependency> + <!-- https://mvnrepository.com/artifact/net.postgis/postgis-jdbc --> + <dependency> + <groupId>net.postgis</groupId> + <artifactId>postgis-jdbc</artifactId> + <version>2.2.1</version> + </dependency> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-annotations</artifactId> - <version>2.7.4</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-core</artifactId> - <version>2.7.4</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.core</groupId> - <artifactId>jackson-databind</artifactId> - <version>2.7.4</version> - </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <version>1.2.14</version> - <type>jar</type> - </dependency> - <dependency> - <groupId>org.passay</groupId> - <artifactId>passay</artifactId> - <version>1.1.0</version> - </dependency> - <dependency> - <groupId>org.hibernate</groupId> - <artifactId>hibernate-core</artifactId> - <version>5.0.10.Final</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - <version>9.4.1211</version> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.fasterxml.jackson.dataformat</groupId> - <artifactId>jackson-dataformat-csv</artifactId> - <version>2.6.2</version> - <type>jar</type> - </dependency> - <dependency> - <groupId>javax</groupId> - <artifactId>javaee-web-api</artifactId> - <version>7.0</version> - <type>jar</type> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>javax</groupId> - <artifactId>javaee-api</artifactId> - <version>7.0</version> - <type>jar</type> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.freemarker</groupId> - <artifactId>freemarker</artifactId> - <version>2.3.24-incubating</version> - </dependency> - <dependency> - <groupId>it.sauronsoftware.cron4j</groupId> - <artifactId>cron4j</artifactId> - <version>2.2.5</version> -</dependency> -<!--dependency> - <groupId>org.openid4java</groupId> - <artifactId>openid4java</artifactId> - <version>0.9.8</version> -</dependency--> -<dependency> - <groupId>com.google.api-client</groupId> - <artifactId>google-api-client</artifactId> - <version>1.19.1</version> -</dependency> - <!--dependency> - <groupId>edu.vt.middleware</groupId> - <artifactId>vt-password</artifactId> - <version>3.1.2</version> - </dependency--> -<dependency> - <groupId>commons-fileupload</groupId> - <artifactId>commons-fileupload</artifactId> - <version>1.3.1</version> -</dependency> -<dependency> - <groupId>com.ibm.icu</groupId> - <artifactId>icu4j</artifactId> - <version>52.1</version> -</dependency> -<dependency> -<groupId>jaxen</groupId> -<artifactId>jaxen</artifactId> -<version>1.1.1</version> -</dependency> -<!--dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - <version>9.4-1211</version> - <scope>provided</scope> -</dependency--> -<dependency> - <groupId>org.openjdk.jol</groupId> - <artifactId>jol-core</artifactId> - <version>0.9</version> -</dependency> -<dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-annotations</artifactId> + <version>2.7.4</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-core</artifactId> + <version>2.7.4</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.7.4</version> + </dependency> + <dependency> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + <version>1.2.14</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.passay</groupId> + <artifactId>passay</artifactId> + <version>1.1.0</version> + </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-core</artifactId> + <version>5.0.10.Final</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + <version>9.4.1211</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-csv</artifactId> + <version>2.6.2</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>javax</groupId> + <artifactId>javaee-web-api</artifactId> + <version>7.0</version> + <type>jar</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>javax</groupId> + <artifactId>javaee-api</artifactId> + <version>7.0</version> + <type>jar</type> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.freemarker</groupId> + <artifactId>freemarker</artifactId> + <version>2.3.24-incubating</version> + </dependency> + <dependency> + <groupId>it.sauronsoftware.cron4j</groupId> + <artifactId>cron4j</artifactId> + <version>2.2.5</version> + </dependency> + <!--dependency> + <groupId>org.openid4java</groupId> + <artifactId>openid4java</artifactId> + <version>0.9.8</version> + </dependency--> + <dependency> + <groupId>com.google.api-client</groupId> + <artifactId>google-api-client</artifactId> + <version>1.19.1</version> + </dependency> + <!--dependency> + <groupId>edu.vt.middleware</groupId> + <artifactId>vt-password</artifactId> + <version>3.1.2</version> + </dependency--> + <dependency> + <groupId>commons-fileupload</groupId> + <artifactId>commons-fileupload</artifactId> + <version>1.3.1</version> + </dependency> + <dependency> + <groupId>com.ibm.icu</groupId> + <artifactId>icu4j</artifactId> + <version>52.1</version> + </dependency> + <dependency> + <groupId>jaxen</groupId> + <artifactId>jaxen</artifactId> + <version>1.1.1</version> + </dependency> + <!--dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + <version>9.4-1211</version> + <scope>provided</scope> + </dependency--> + <dependency> + <groupId>org.openjdk.jol</groupId> + <artifactId>jol-core</artifactId> + <version>0.9</version> + </dependency> + <dependency> <groupId>org.geotools</groupId> <artifactId>gt-api</artifactId> <version>17.2</version> </dependency> <dependency> - <groupId>org.geotools</groupId> - <artifactId>gt-epsg-hsql</artifactId> - <version>17.2</version> - </dependency> - <dependency> - <groupId>com.webcohesion.enunciate</groupId> - <artifactId>enunciate-core-annotations</artifactId> - <version>2.10.1</version> - </dependency> + <groupId>org.geotools</groupId> + <artifactId>gt-epsg-hsql</artifactId> + <version>17.2</version> + </dependency> + <dependency> + <groupId>com.webcohesion.enunciate</groupId> + <artifactId>enunciate-core-annotations</artifactId> + <version>2.10.1</version> + </dependency> - <dependency> - <groupId>com.webcohesion.enunciate</groupId> - <artifactId>enunciate-rt-util</artifactId> - <version>2.10.1</version> - </dependency> - <dependency> - <groupId>com.bedatadriven</groupId> - <artifactId>jackson-datatype-jts</artifactId> - <version>2.2</version> - </dependency> - </dependencies> + <dependency> + <groupId>com.webcohesion.enunciate</groupId> + <artifactId>enunciate-rt-util</artifactId> + <version>2.10.1</version> + </dependency> + <dependency> + <groupId>com.bedatadriven</groupId> + <artifactId>jackson-datatype-jts</artifactId> + <version>2.2</version> + </dependency> + </dependencies> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>2.0.2</version> - <configuration> - <source>1.8</source> - <target>1.8</target> - </configuration> - </plugin> - <plugin> - <groupId>org.jvnet.jax-ws-commons</groupId> - <artifactId>jaxws-maven-plugin</artifactId> - <version>2.3</version> - <executions> - <execution> - <goals> - <goal>wsimport</goal> - </goals> - <configuration> - <wsdlFiles> - <wsdlFile>dmiweatherservice-plant.dlbr.dk/DMIWeatherService.svc.wsdl</wsdlFile> - </wsdlFiles> - <packageName>no.nibio.vips.util.weather.dnmipointweb</packageName> - <vmArgs> - <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg> - </vmArgs> - <wsdlLocation>https://dmiweatherservice-plant.dlbr.dk/DMIWeatherService.svc?wsdl</wsdlLocation> - <staleFile>${project.build.directory}/jaxws/stale/DMIWeatherService.svc.stale</staleFile> - <keep>true</keep> - </configuration> - <id>wsimport-generate-DMIWeatherService.svc</id> - <phase>generate-sources</phase> - </execution> - </executions> - <dependencies> - <dependency> - <groupId>javax.xml</groupId> - <artifactId>webservices-api</artifactId> - <version>2.0</version> - </dependency> - </dependencies> - <configuration> - <sourceDestDir>${project.build.directory}/generated-sources/jaxws-wsimport</sourceDestDir> - <xnocompile>true</xnocompile> - <verbose>true</verbose> - <extension>true</extension> - <catalog>${basedir}/src/jax-ws-catalog.xml</catalog> - </configuration> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-war-plugin</artifactId> - <version>2.0.2</version> - <configuration> - <webResources> - <resource> - <directory>src</directory> - <targetPath>WEB-INF</targetPath> - <includes> - <include>jax-ws-catalog.xml</include> - <include>wsdl/**</include> - </includes> - </resource> - <resource> - <directory>src</directory> - <targetPath>WEB-INF/classes/META-INF</targetPath> - <includes> - <include>jax-ws-catalog.xml</include> - <include>wsdl/**</include> - </includes> - </resource> - </webResources> - </configuration> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - <version>2.10</version> - <configuration> - <argLine>-Xmx6048m</argLine> - </configuration> - </plugin> - <plugin> - <groupId>com.webcohesion.enunciate</groupId> - <artifactId>enunciate-maven-plugin</artifactId> - <version>2.10.1</version> - <executions> - <execution> - <goals> - <!--goal>assemble</goal--> - <goal>docs</goal> - </goals> - <configuration> - <docsDir>${project.build.directory}/${project.build.finalName}/public/RESTdocs</docsDir> - </configuration> - </execution> - </executions> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.0.2</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-war-plugin</artifactId> + <version>2.0.2</version> + <configuration> + <webResources> + <resource> + <directory>src</directory> + <targetPath>WEB-INF</targetPath> + <includes> + <include>jax-ws-catalog.xml</include> + <include>wsdl/**</include> + </includes> + </resource> + <resource> + <directory>src</directory> + <targetPath>WEB-INF/classes/META-INF</targetPath> + <includes> + <include>jax-ws-catalog.xml</include> + <include>wsdl/**</include> + </includes> + </resource> + </webResources> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.10</version> + <configuration> + <argLine>-Xmx6048m</argLine> + </configuration> + </plugin> + <plugin> + <groupId>com.webcohesion.enunciate</groupId> + <artifactId>enunciate-maven-plugin</artifactId> + <version>2.10.1</version> + <executions> + <execution> + <goals> + <!--goal>assemble</goal--> + <goal>docs</goal> + </goals> + <configuration> + <docsDir>${project.build.directory}/${project.build.finalName}/public/RESTdocs</docsDir> + </configuration> + </execution> + </executions> - </plugin> - </plugins> - </build> + </plugin> + </plugins> + </build> </project> diff --git a/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java b/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java index b4151523e06fd40be80ff3d0fcf1e0b1cb0aa4c3..329f1f0b04924e43899dbc5bdc2437055cd05e24 100755 --- a/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java +++ b/src/main/java/no/nibio/vips/logic/VIPSLogicApplication.java @@ -54,7 +54,6 @@ public class VIPSLogicApplication extends Application resources.add(no.nibio.vips.logic.messaging.sms.SMSHandlingService.class); resources.add(no.nibio.vips.logic.modules.applefruitmoth.AppleFruitMothService.class); resources.add(no.nibio.vips.logic.service.ObservationService.class); - resources.add(no.nibio.vips.logic.service.WeatherProxyService.class); resources.add(no.nibio.vips.logic.service.ModelFormService.class); resources.add(no.nibio.vips.logic.service.JacksonConfig.class); //resources.add(no.nibio.vips.coremanager.service.ManagerResourceImpl.class); @@ -76,7 +75,6 @@ public class VIPSLogicApplication extends Application resources.add(no.nibio.vips.logic.service.ModelFormService.class); resources.add(no.nibio.vips.logic.service.ObservationService.class); resources.add(no.nibio.vips.logic.service.VIPSMobileService.class); - resources.add(no.nibio.vips.logic.service.WeatherProxyService.class); resources.add(no.nibio.vips.observationdata.ObservationDataService.class); } } \ No newline at end of file diff --git a/src/main/java/no/nibio/vips/logic/service/LogicService.java b/src/main/java/no/nibio/vips/logic/service/LogicService.java index 5e171c638249e5ec2e992a91839677efe1647224..8de4c36b950ec9b9ad16238cb21c3da4f9824595 100755 --- a/src/main/java/no/nibio/vips/logic/service/LogicService.java +++ b/src/main/java/no/nibio/vips/logic/service/LogicService.java @@ -86,9 +86,30 @@ public class LogicService { /** * Get all results for one pest prediction - * @param forecastConfigurationId + * @param forecastConfigurationId Database id of the configured forecast * @param userUUID if the forecast is private, the correct userUUID must be supplied. - * @return + * @return JSON with result data. A list of ForecastResult objects. Example: + * <pre> + * { + "forecastResultId": 5710137, + "validTimeStart": "2019-01-22T23:00:00.000+0000", + "validTimeEnd": null, + "warningStatus": 0, + "forecastConfigurationId": -1000, + "validGeometry": { // NORMALLY SET ONLY IF the result set contains results for multiple locations + "type": "Point", + "coordinates": [ + 10.333252, + 57.179002 + ] + }, + "keys": [ // A list of the existing parameter names in this result object + "GRIDZYMOSE.WHS" + ], + "allValues": { // Parameters with values. Everything is a String. Client must convert to numbers if necessary + "GRIDZYMOSE.WHS": "0" + } + }</pre> */ @GET @Path("forecastresults/{forecastConfigurationId}") diff --git a/src/main/java/no/nibio/vips/logic/service/WeatherProxyService.java b/src/main/java/no/nibio/vips/logic/service/WeatherProxyService.java deleted file mode 100755 index 6bd8b9605e63ca59d979ebcc0044e9d226eda0d3..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/logic/service/WeatherProxyService.java +++ /dev/null @@ -1,483 +0,0 @@ -/* - * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.logic.service; - -import com.vividsolutions.jts.geom.Envelope; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.webcohesion.enunciate.metadata.Facet; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URLDecoder; -import java.text.MessageFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import javax.ws.rs.FormParam; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import no.nibio.vips.entity.PointWeatherObservationList; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.logic.util.SystemTime; -import no.nibio.vips.util.XDate; -import no.nibio.vips.util.weather.ALabDataParser; -import no.nibio.vips.util.weather.CebaowangDataParser; -import no.nibio.vips.util.weather.FruitWebDavisDataParser; -import no.nibio.vips.util.weather.MetosAPIDataParser; -import no.nibio.vips.util.weather.MetosRIMProDataParser; -import no.nibio.vips.util.weather.ParseWeatherDataException; -import no.nibio.vips.util.weather.USPestDataParser; -import no.nibio.vips.util.weather.YrWeatherForecastProvider; -import no.nibio.vips.util.weather.dnmipointweb.DMIPointWebDataParser; -import no.nibio.vips.util.weather.metnothredds.MetNoThreddsDataParser; -import no.nibio.vips.util.weather.metnothredds.TooMuchDataToAskForException; -import org.jboss.resteasy.annotations.GZIP; - -/** - * @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -@Path("rest/weather/proxy") -public class WeatherProxyService { - @GET - @POST - @Path("metosrimpro/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getMetosRIMProWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - observations = new MetosRIMProDataParser().getWeatherObservations(stationId, timeZone, startDate1); - } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - @GET - @POST - @Path("fmi/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getFMIVIPSWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET, - @FormParam("endDate") String endDatePOST, - @QueryParam("endDate") String endDateGET - ) - { - String FMI_URL_TEMPLATE="http://192.194.211.203/weather/resources/fmi/temporal/vips/{0}/{1}/{2}"; - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - XDate startDate1 = new XDate(format.parse(startDateParam)); - String endDateParam = endDatePOST != null ? endDatePOST : endDateGET; - XDate endDate1 = new XDate(format.parse(endDateParam)); - URI uri = UriBuilder.fromUri(MessageFormat.format(FMI_URL_TEMPLATE, stationId, startDate1.getISOUTCFormat(), endDate1.getISOUTCFormat())).build(); - //observations = new MetosRIMProDataParser().getWeatherObservations(stationId, timeZone, startDate1); - return Response.temporaryRedirect(uri).build(); - } catch (ParseException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - } - - @GET - @POST - @Path("metosapi/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getMetosAPIWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - observations = new MetosAPIDataParser().getWeatherObservations(stationId, timeZone, startDate1); - } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - @GET - @POST - @Path("fruitwebdavis/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getFruitWebDavisWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET, - @FormParam("endDate") String endDatePOST, - @QueryParam("endDate") String endDateGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - String endDateParam = endDatePOST != null ? endDatePOST : endDateGET != null ? endDateGET : null; - Date endDate1 = endDateParam != null ? format.parse(endDateParam) : null; - observations = new FruitWebDavisDataParser().getWeatherObservations(URLDecoder.decode(stationId, "UTF-8"), timeZone, startDate1, endDate1); - } catch (ParseException | ParseWeatherDataException | NullPointerException | UnsupportedEncodingException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - /** - * Fetches data from the Chinese Cebaowang weather data service - * Used in the SINOGRAIN project(s) - * - * @param stationId - * @param timeZonePOST - * @param timeZoneGET - * @param startDatePOST - * @param startDateGET - * @param endDatePOST - * @param endDateGET - * @return - */ - @GET - @POST - @Path("cebaowang/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getCebaowangWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET, - @FormParam("endDate") String endDatePOST, - @QueryParam("endDate") String endDateGET - ) - { - List<WeatherObservation> weatherObservations; - TimeZone timeZone = TimeZone.getTimeZone(timeZonePOST != null ? timeZonePOST : timeZoneGET); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - try - { - Date startDate = format.parse(startDatePOST != null ? startDatePOST : startDateGET); - Date endDate = format.parse(endDatePOST != null ? endDatePOST : endDateGET); - weatherObservations = new CebaowangDataParser().getWeatherObservations(stationId, timeZone, startDate, endDate); - } - catch(ParseException | ParseWeatherDataException ex) - { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(weatherObservations).build(); - } - - @GET - @POST - @Path("alab/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getALabWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET, - @FormParam("userName") String userNamePOST, - @QueryParam("userName") String userNameGET, - @FormParam("password") String passwordPOST, - @QueryParam("password") String passwordGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - String userName = userNamePOST != null ? userNamePOST:userNameGET; - String password = passwordPOST != null ? passwordPOST:passwordGET; - observations = new ALabDataParser().getWeatherObservations(stationId, timeZone, startDate1, userName, password); - } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - @GET - @POST - @Path("uspest/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getUSPestWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET - ) - { - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - observations = new USPestDataParser().getWeatherObservations(stationId, timeZone, startDate1); - } catch (ParseException | ParseWeatherDataException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - /** - * Getting data from the Danish Meteorological Institute's PointWeb weather service - * @param stationId - * @param timeZonePOST - * @param timeZoneGET - * @param startDatePOST - * @param startDateGET - * @param endDatePOST - * @param endDateGET - * @return - */ - @Facet("restricted") - @GET - @POST - @Path("dmipointweb/{stationId}") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getDMIPointWebWeatherData( - @PathParam("stationId") String stationId, - @FormParam("timeZone") String timeZonePOST, - @QueryParam("timeZone") String timeZoneGET, - @FormParam("startDate") String startDatePOST, - @QueryParam("startDate") String startDateGET, - @FormParam("endDate") String endDatePOST, - @QueryParam("endDate") String endDateGET - ) - { - // Station is id'ed by decimal degrees position - // Format is &stationId=long,lat. E.g. &stationId=9.583,56.488 (sample is from Follum, Denmark) - List<WeatherObservation> observations; - try - { - String timeZoneParam = timeZonePOST != null ? timeZonePOST : timeZoneGET != null ? timeZoneGET : "UTC"; - TimeZone timeZone = TimeZone.getTimeZone(timeZoneParam); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - String startDateParam = startDatePOST != null ? startDatePOST : startDateGET; - Date startDate1 = format.parse(startDateParam); - String endDateParam = endDatePOST != null ? endDatePOST : endDateGET; - Date endDate1; - if(endDateParam != null) - { - endDate1 = format.parse(endDateParam); - } - else - { - // Expecting endDate to be 6 days after today if not specified - Calendar cal = Calendar.getInstance(); - cal.setTime(SystemTime.getSystemTime()); - cal.add(Calendar.DATE, 6); - endDate1 = cal.getTime(); - } - String[] coordinates = stationId.split(","); - Double longitude = Double.parseDouble(coordinates[0]); - Double latitude = Double.parseDouble(coordinates[1]); - observations = new DMIPointWebDataParser().getData(longitude, latitude, startDate1, endDate1); - } catch (ParseException | NullPointerException ex) { - return Response.serverError().entity(ex).build(); - } - return Response.ok().entity(observations).build(); - } - - @GET - @POST - @Path("forecasts/yr/") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getYRForecasts( - @QueryParam("longitude") Double longitude, - @QueryParam("latitude") Double latitude, - @QueryParam("altitude") Double altitude - ) - { - try - { - return Response.ok().entity(new YrWeatherForecastProvider().getWeatherForecasts(longitude, latitude, altitude)).build(); - } - catch(ParseWeatherDataException ex) - { - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(ex.getMessage()).build(); - } - } - - @Facet("restricted") - @GET - @Path("metno/thredds/point/") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getMetNoThreddsPointData( - @QueryParam("longitude") Double longitude, - @QueryParam("latitude") Double latitude, - @QueryParam("timeZone") String timeZoneStr, - @QueryParam("startDate") String startDateStr, - @QueryParam("startTime") String startTimeStr, - @QueryParam("endDate") String endDateStr, - @QueryParam("endTime") String endTimeStr, - @QueryParam("elementMeasurementTypes") String[] elementMeasurementTypes - - ) - { - try - { - TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH"); - format.setTimeZone(timeZone); - startTimeStr = startTimeStr != null ? startTimeStr : "00"; - endTimeStr = endTimeStr != null ? endTimeStr : "23"; - - Date startDate = format.parse(startDateStr + " " + startTimeStr); - Date endDate = format.parse(endDateStr + " " + endTimeStr); - MetNoThreddsDataParser dp = new MetNoThreddsDataParser(); - List<WeatherObservation> retVal = dp.getPointData(longitude, latitude, startDate, endDate, Arrays.asList(elementMeasurementTypes)); - Collections.sort(retVal); - return Response.ok().entity(retVal).build(); - } - catch(ParseException pe) - { - return Response.status(Response.Status.BAD_REQUEST).entity(pe.getMessage()).build(); - } - } - - /** - * This is a real memory hog, use only if you know how!! - * Sample test with wget: - * wget --header='Accept-Encoding: gzip' "http://logic.vips.nibio.no/rest/weather/proxy/metno/thredds/grid?elementMeasurementTypes=Q0&north=57.91&south=54.36&west=8.04&east=12.83&startDate=2017-03-01&endDate=2017-10-06&timeZone=Europe/Oslo" -O ./denmark_q0.gz - * @param north - * @param south - * @param east - * @param west - * @param timeZoneStr - * @param startDateStr - * @param startTimeStr - * @param endDateStr - * @param endTimeStr - * @param elementMeasurementTypes - * @return - */ - @Facet("restricted") - @GET - @Path("metno/thredds/grid/") - @GZIP - @Produces("application/json;charset=UTF-8") - public Response getMetNoThreddsGridData( - @QueryParam("north") Double north, - @QueryParam("south") Double south, - @QueryParam("east") Double east, - @QueryParam("west") Double west, - @QueryParam("timeZone") String timeZoneStr, - @QueryParam("startDate") String startDateStr, - @QueryParam("startTime") String startTimeStr, - @QueryParam("endDate") String endDateStr, - @QueryParam("endTime") String endTimeStr, - @QueryParam("elementMeasurementTypes") String[] elementMeasurementTypes - - ) - { - try - { - TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH"); - format.setTimeZone(timeZone); - startTimeStr = startTimeStr != null ? startTimeStr : "00"; - endTimeStr = endTimeStr != null ? endTimeStr : "23"; - - Date startDate = format.parse(startDateStr + " " + startTimeStr); - Date endDate = format.parse(endDateStr + " " + endTimeStr); - MetNoThreddsDataParser dp = new MetNoThreddsDataParser(); - - // Creating an envelope with the given bounds - Envelope envelope = new Envelope(west, east, south, north); - GeometryFactory gf = new GeometryFactory(); - List<PointWeatherObservationList> retVal = dp.getGridData(gf.toGeometry(envelope), 10.0, startDate, endDate, Arrays.asList(elementMeasurementTypes)); - return Response.ok().entity(retVal).build(); - } - catch(ParseException | TooMuchDataToAskForException e) - { - return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); - } - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/ALabDataParser.java b/src/main/java/no/nibio/vips/util/weather/ALabDataParser.java deleted file mode 100755 index 7daff5b79814a8ca278d1dd7fb908e4d455427ed..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/ALabDataParser.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather; - -import java.io.IOException; -import java.net.URL; -import java.text.DateFormat; -import java.text.MessageFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.util.InvalidAggregationTypeException; -import no.nibio.vips.util.WeatherObservationListException; -import no.nibio.vips.util.WeatherUtil; -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.Node; -import org.dom4j.io.SAXReader; - -/** - * Reads/parses data from A-Lab weather stations - * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class ALabDataParser { - - private static final Map<String, String> parameters; - static { - Map<String, String> staticMap = new HashMap<>(); - staticMap.put("119", "TM"); - staticMap.put("120", "UM"); - staticMap.put("121", "RRACC"); // Accumulated rain over an unkown amount of time! - staticMap.put("122", "DD2"); - staticMap.put("123", "FF2"); - staticMap.put("124", "PPP"); - staticMap.put("125", "TJM10"); - staticMap.put("126", "VAN10p"); - parameters = Collections.unmodifiableMap(staticMap); - } - // - public final static String ALAB_URL_TEMPLATE = "http://xml.a-log.net/xml-export.cgi?devid={0}&username={1}&password={2}&measurements=all&from={3}&to={4}"; - - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone, Date startDate, String userName, String password) throws ParseWeatherDataException - { - List<WeatherObservation> retVal = new ArrayList<>(); - DateFormat timeParseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - timeParseFormat.setTimeZone(timeZone); - - WeatherUtil wUtil = new WeatherUtil(); - startDate = wUtil.normalizeToExactDate(startDate, timeZone); - Date endDate = wUtil.normalizeToExactDate(new Date(), timeZone); - DateFormat urlDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - urlDateFormat.setTimeZone(timeZone); - String from = urlDateFormat.format(startDate); - String to = urlDateFormat.format(endDate); - - // Get the raw data from service, serialize into WeatherObservations - Map<String, List<WeatherObservation>> serialized = new HashMap<>(); - try { - URL aLabURL = new URL(MessageFormat.format(ALabDataParser.ALAB_URL_TEMPLATE, stationID,userName,password,from,to)); - Document doc = this.getDocument(aLabURL); - - // Get all measurements at 15 min intervals - // Must aggregate afterwards - List<Node> list = doc.selectNodes("//Measurement"); - - for(Node node:list) - { - Date timeMeasured = timeParseFormat.parse(node.valueOf("@Time")); - List<Node> valueNodes = node.selectNodes("Value"); - for(Node valueNode : valueNodes) - { - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timeMeasured); - obs.setElementMeasurementTypeId(ALabDataParser.parameters.get(valueNode.valueOf("@Type"))); - // Set logInterval to 15min - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_15M); - obs.setValue(Double.valueOf(valueNode.getText())); - - if(!serialized.containsKey(obs.getElementMeasurementTypeId())) - { - serialized.put(obs.getElementMeasurementTypeId(), new ArrayList<WeatherObservation>()); - } - serialized.get(obs.getElementMeasurementTypeId()).add(obs); - - } - //System.out.println(timeMeasured); - } - } - catch(IOException | DocumentException | ParseException ex) - { - throw new ParseWeatherDataException(ex.getMessage()); - } - - try - { - // Convert from 15 min intervals to hourly intervals - // Bear in mind that RRACC must be converted to RR - for(String param:serialized.keySet()) - { - List<WeatherObservation> paramObs = serialized.get(param); - paramObs = wUtil.removeDuplicateWeatherObservations(paramObs, 0.05); - if(!param.equals("RRACC")) - { - retVal.addAll(wUtil.getAggregateHourlyValues(paramObs, timeZone, WeatherObservation.LOG_INTERVAL_ID_15M, WeatherUtil.AGGREGATION_TYPE_AVERAGE)); - } - else - { - //System.out.println("RRACC being calculated"); - retVal.addAll(wUtil.getIncrementalValuesFromAccumulated(paramObs, timeZone, WeatherObservation.LOG_INTERVAL_ID_1H)); - } - } - }catch (WeatherObservationListException | InvalidAggregationTypeException ex) - { - throw new ParseWeatherDataException(ex.getMessage()); - } - Collections.sort(retVal); - - - return retVal; - } - - private Document getDocument(URL url) throws IOException, DocumentException - { - //System.out.println("URL=" + url.toString()); - /*BufferedReader in = new BufferedReader( - new InputStreamReader(url.openStream())); - - String inputLine; - while ((inputLine = in.readLine()) != null) - { - System.out.println(inputLine); - }*/ - - Document doc = new SAXReader().read(url); - return doc; - - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/CebaowangDataParser.java b/src/main/java/no/nibio/vips/util/weather/CebaowangDataParser.java deleted file mode 100644 index 9b5fcdab73be658358d8b37a3885e70175784d62..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/CebaowangDataParser.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.ibm.icu.text.MessageFormat; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLEncoder; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.AbstractMap.SimpleEntry; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.util.WeatherElements; - -/** - * Parses data from the Chinese weather service http://www.cebaowang.com:8089, used in the - * SINOGRAIN2 project - * - * @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class CebaowangDataParser { - public final static String CEBAOWANG_URL_PATH = "http://www.cebaowang.com:8089/external/data/upload"; - public final static String CEBAOWANG_URL_QUERY_TEMPLATE = "?startDate={0}&endDate={1}&{2}"; - private Map<String, String> parameters = Collections.unmodifiableMap((Map<String,String>) Stream.of - ( - new SimpleEntry<>("kongQiWenDu", WeatherElements.TEMPERATURE_MEAN), - new SimpleEntry<>("xiangDuiShiDu", WeatherElements.RELATIVE_HUMIDITY_MEAN), - new SimpleEntry<>("luDianWenDu",WeatherElements.DEW_POINT_TEMPERATURE), - //new SimpleEntry<>("tianJiangYuLiang", null), // Daily rainfall - new SimpleEntry<>("xiaoShiJiangYuLiang", WeatherElements.PRECIPITATION), - //new SimpleEntry<>("dianChiDianYa", null), // Voltage - new SimpleEntry<>("shiRunShiJian", WeatherElements.LEAF_WETNESS_DURATION), - //new SimpleEntry<>("riZhaoShiShu", null), // Sunshine hours the current day - new SimpleEntry<>("fiveTuRangHanShuiLiang", WeatherElements.SOIL_WATER_CONTENT_5CM), - new SimpleEntry<>("tenTuRangHanShuiLiang", WeatherElements.SOIL_WATER_CONTENT_10CM), - new SimpleEntry<>("twentyFiveTuRangHanShuiLiang", WeatherElements.SOIL_WATER_CONTENT_25CM), - new SimpleEntry<>("fiftyTuRangHanShuiLiang", WeatherElements.SOIL_WATER_CONTENT_50CM), - new SimpleEntry<>("fiveTuRangWenDu", WeatherElements.SOIL_TEMPERATURE_5CM_MEAN), - new SimpleEntry<>("tenTuRangWenDu", WeatherElements.SOIL_TEMPERATURE_10CM_MEAN), - new SimpleEntry<>("twentyFiveTuRangWenDu", WeatherElements.SOIL_TEMPERATURE_25CM_MEAN), - new SimpleEntry<>("fiftyTuRangWenDu", WeatherElements.SOIL_TEMPERATURE_50CM_MEAN), - new SimpleEntry<>("tuRangDaoDianLv", WeatherElements.SOIL_CONDUCTIVITY_5CM) - ) - .collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())) - ); - - private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone, Date startDate, Date endDate) throws ParseWeatherDataException - { - format.setTimeZone(timeZone); - List<WeatherObservation> retVal = null; - try - { - URL cebaowangURL = new URL(CEBAOWANG_URL_PATH + MessageFormat.format( - CEBAOWANG_URL_QUERY_TEMPLATE, - URLEncoder.encode(format.format(startDate),"UTF-8"), - URLEncoder.encode(format.format(endDate),"UTF-8"), - stationID - ) - ); - //System.out.println(cebaowangURL); - BufferedReader in = new BufferedReader( - new InputStreamReader(cebaowangURL.openStream()) - ); - String inputLine; - Date testTimestamp; - String all = new String(); - // Reading data into String of JSON data - while ((inputLine = in.readLine()) != null) - { - all += inputLine; - } - // Get objects from JSON parser method - retVal = getParsedObservations(all, timeZone); - } - catch(ParseException | IOException ex) - {ex.printStackTrace(); - throw new ParseWeatherDataException(ex.getMessage()); - } - - return retVal; - } - - public List<WeatherObservation> getParsedObservations(String jsonTxt, TimeZone timeZone) throws IOException, ParseException - { - ObjectMapper oMapper = new ObjectMapper(); - JsonNode jNode = oMapper.readTree(jsonTxt); - List<WeatherObservation> retVal = new ArrayList<>(); - JsonNode data = jNode.get("data"); - - for(JsonNode dataForAGivenTime:data) - { - Date timeMeasured = this.format.parse(dataForAGivenTime.get("collectTime").asText()); - //System.out.println(timeMeasured + "=" + dataForAGivenTime.get("collectTime").asText()); - this.parameters.keySet().forEach(paramName->{ - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timeMeasured); - obs.setElementMeasurementTypeId(this.parameters.get(paramName)); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); // TODO: Check this!! Is every 2nd hour as of now - obs.setValue(dataForAGivenTime.get(paramName).asDouble()); - retVal.add(obs); - }); - } - - return retVal; - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/FruitWebDavisDataParser.java b/src/main/java/no/nibio/vips/util/weather/FruitWebDavisDataParser.java deleted file mode 100755 index 73ee35629be9fd25133adc905e9faa998b6fd026..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/FruitWebDavisDataParser.java +++ /dev/null @@ -1,247 +0,0 @@ -/* - * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URISyntaxException; -import java.net.URL; -import java.text.MessageFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import java.util.stream.Collectors; -import no.nibio.vips.entity.WeatherObservation; - -/** - * Reads/parses data from a Davis weather station using the fruitweb service: http://www.fruitweb.info/ - * @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class FruitWebDavisDataParser { - // Sample: https://www.fruitweb.info/sc/getFile.php?id=283&date=2016-01-01&pw=Kghr528Ub5 - // id and password combined must be the "remote ID" in VIPSLogic form - //public final static String FRUITWEB_URL_TEMPLATE = "http://wds.fruitweb.info/sc/getFile.php?{0}&date={1}"; - public final static String FRUITWEB_URL_TEMPLATE = "https://www.fruitweb.info/sc/getFile.php?{0}&date={1}"; - // Metos parameters, including name and aggregation type - private final static String[][] ELEMENT_MEASUREMENT_TYPES = { - {"RAIN","RR","SUM"}, - {"LW1","BT","SUM"}, - {"AIRTEMP","TM","AVG"}, - {"AIRHUM","UM","AVG"} - }; - - - - /** - * Using output designed for RIMPro, parsing into WeatherObservations - * @param stationID the METOS station ID - * @param timeZone - * @param startDate - * @return - */ - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone, Date startDate, Date endDate) throws ParseWeatherDataException - { - List<WeatherObservation> retVal = new ArrayList<>(); - SimpleDateFormat urlDFormat = new SimpleDateFormat("yyyy-MM-dd"); - SimpleDateFormat dFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); - urlDFormat.setTimeZone(timeZone); - dFormat.setTimeZone(timeZone); - // Assuming 1 hour resolution until we find a timestamp that says :30 - Integer logIntervalId = WeatherObservation.LOG_INTERVAL_ID_1H; - List<String[]> data = new ArrayList<>(); - String[] headers; - Map<Integer, Integer> elementOrdering = new HashMap<>(); - try { - URL fruitwebDavisURL = new URL(MessageFormat.format(FruitWebDavisDataParser.FRUITWEB_URL_TEMPLATE, stationID,urlDFormat.format(startDate))); - //System.out.println(fruitwebDavisURL.toString()); - BufferedReader in = new BufferedReader( - new InputStreamReader(fruitwebDavisURL.openStream())); - - String inputLine; - Date testTimestamp; - // We need to collect all the lines first, because we need to analyze - // If the resolution is 30 minutes or 1 hour - while ((inputLine = in.readLine()) != null) - { - //System.out.println(inputLine); - String[] lineData = inputLine.split(";"); - // Skip empty lines - if(lineData.length <= 1) - { - continue; - } - // Check for valid start of line - try { - testTimestamp = dFormat.parse(lineData[0] + " " + lineData[1]); - data.add(lineData); - if(lineData[1].split(":")[1].equals("30")) - { - logIntervalId = WeatherObservation.LOG_INTERVAL_ID_30M; - } - } catch (ParseException ex) { - - // Is this the heading line? - // Then we parse it to set the ordering of elements - if(lineData[0].equals("DATE")) - { - headers = lineData; - // DATE and TIME should always be the two first ones - for(int i=2;i<lineData.length;i++) - { - for(int j=0;j<ELEMENT_MEASUREMENT_TYPES.length;j++) - { - if(ELEMENT_MEASUREMENT_TYPES[j][0].equals(lineData[i])) - { - elementOrdering.put(i,j); - } - } - } - } - } - } - in.close(); - } catch (IOException ex) { - throw new ParseWeatherDataException(ex.getMessage()); - } - - - // Data comes in half-hour chunks (resolution = 30 minutes) - // Unlike Metos, the hour starts at :30 - if(logIntervalId.equals(WeatherObservation.LOG_INTERVAL_ID_30M)) - { - Boolean shouldBe30Now = true; - String[] data30 = null; - String[] data00 = null; - Date timestamp = null; - for(String[] lineData: data) - { - // Skip lines that are not exactly :00 or :30 - if( - !lineData[1].split(":")[1].equals("00") - && ! lineData[1].split(":")[1].equals("30") - ) - { - continue; - } - else if(lineData[1].split(":")[1].equals("30") && shouldBe30Now) - { - data30 = lineData; - - shouldBe30Now = false; - continue; // So that we summarize only after :00 data has been set too - } - else if(lineData[1].split(":")[1].equals("00") && !shouldBe30Now) - { - data00 = lineData; - try - { - timestamp = dFormat.parse(lineData[0] + " " + lineData[1]); - } - catch(ParseException ex) - { - throw new ParseWeatherDataException("Error with time stamp in weather data from Davis/FruitWeb station: " + ex.getMessage()); - } - shouldBe30Now = true; - } - else - { - throw new ParseWeatherDataException("Doesn't make sense at " + lineData[0] + " " + lineData[1] + "!"); - } - - for(Integer i=2;i<data30.length;i++) - { - Double aggregateValue = null; - Double value00 = Double.valueOf(data00[i].replaceAll(",",".")); - Double value30 = Double.valueOf(data30[i].replaceAll(",",".")); - Integer elementMeasurementTypeIndex = elementOrdering.get(i); - // This means there is an element type we don't collect - if(elementMeasurementTypeIndex == null) - { - continue; - } - //System.out.println("element " + i + "=" + MetosDataParser.elementMeasurementTypes[elementMeasurementTypeIndex][1]); - if(FruitWebDavisDataParser.ELEMENT_MEASUREMENT_TYPES[elementMeasurementTypeIndex][2].equals("AVG")) - { - aggregateValue = (value00 + value30) / 2; - } - else - { - aggregateValue = (value00 + value30); - } - - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timestamp); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - obs.setElementMeasurementTypeId(FruitWebDavisDataParser.ELEMENT_MEASUREMENT_TYPES[elementMeasurementTypeIndex][1]); - obs.setValue(aggregateValue); - retVal.add(obs); - } - } - } - // Data is hourly, easy to add - else - { - Date timestamp = null; - for(String[] lineData: data) - { - // If minute != 00 we skip the line - if(!lineData[1].split(":")[1].equals("00")) - { - continue; - } - try - { - timestamp = dFormat.parse(lineData[0] + " " + lineData[1]); - } - catch(ParseException ex) - { - throw new ParseWeatherDataException("Error with time stamp in weather data from Metos station: " + ex.getMessage()); - } - for(Integer i=2;i<lineData.length;i++) - { - Double value = Double.valueOf(lineData[i].replaceAll(",",".")); - Integer elementMeasurementTypeIndex = elementOrdering.get(i); - // This means there is an element type we don't collect - if(elementMeasurementTypeIndex == null) - { - continue; - } - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timestamp); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - obs.setElementMeasurementTypeId(FruitWebDavisDataParser.ELEMENT_MEASUREMENT_TYPES[elementMeasurementTypeIndex][1]); - obs.setValue(value); - retVal.add(obs); - } - } - } - - return retVal.stream().filter(obs -> - (obs.getTimeMeasured().compareTo(startDate) >= 0 && (endDate == null || obs.getTimeMeasured().compareTo(endDate) <= 0)) - ).collect(Collectors.toList()); - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/MaynoothUniDataParser.java b/src/main/java/no/nibio/vips/util/weather/MaynoothUniDataParser.java deleted file mode 100755 index 51b4291e8139c1144cd084148acb760512d98c16..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/MaynoothUniDataParser.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.text.MessageFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.util.WeatherUtil; - -/** - * @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class MaynoothUniDataParser { - public final static String MAYNOOTH_URL_TEMPLATE = "file:///home/treinar/prosjekter/vips/projects/2016_irland/hly375_test.csv"; - // Pparameters, including name and aggregation type - private final static String[][] elementMeasurementTypes = { - {"rain","RR","SUM"}, - {"temp","TM","AVG"}, - {"wetb","WETBULB","AVG"}, - {"dewp","DEWPOINT","AVG"}, - {"vappr","EM2MC","AVG"}, - {"rhum","UM","AVG"}, - {"msl","PPP","AVG"}, - {"wdsp","FM2","AVG"}, - {"wddir","DM2","AVG"}, - {"soglrad","Q0","AVG"} - }; - - /** - * Makes sure only data after the requested date is returned - * @param stationID - * @param timeZone - * @param startDate - * @return - * @throws ParseWeatherDataException - */ - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone, Date startDate) throws ParseWeatherDataException - { - // TODO: The endDate is introduced for testing purposes - Calendar cal = Calendar.getInstance(timeZone); - cal.setTime(startDate); - cal.set(Calendar.MONTH, Calendar.NOVEMBER); - Date endDate = cal.getTime(); - List<WeatherObservation> retVal = new ArrayList<>(); - List<WeatherObservation> allObservations = this.getWeatherObservations(stationID, timeZone); - for(WeatherObservation obs: allObservations) - { - if(obs.getTimeMeasured().compareTo(startDate) >= 0 && obs.getTimeMeasured().compareTo(endDate) <= 0) - { - retVal.add(obs); - } - } - return retVal; - } - - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone) throws ParseWeatherDataException - { - List<WeatherObservation> retVal = new ArrayList<>(); - SimpleDateFormat dFormat = new SimpleDateFormat("M/d/yyyy H:mm"); - dFormat.setTimeZone(timeZone); - // Assuming 1 hour resolution until we find a timestamp that says :30 - Integer logIntervalId = WeatherObservation.LOG_INTERVAL_ID_1H; - List<String[]> data = new ArrayList<>(); - String[] headers; - Map<Integer, Integer> elementOrdering = new HashMap<>(); - try - { - URL maynoothURL = new URL(MessageFormat.format(MaynoothUniDataParser.MAYNOOTH_URL_TEMPLATE, stationID)); - BufferedReader in = new BufferedReader(new InputStreamReader( - maynoothURL.openStream() - )); - - String inputLine; - Date testTimestamp = null; - - // We need to collect all the lines first, because we need to analyze - // If the resolution is 30 minutes or 1 hour - while ((inputLine = in.readLine()) != null) - { - //System.out.println(inputLine); - String[] lineData = inputLine.split(","); - // Skip empty lines - if(lineData.length <= 1) - { - continue; - } - // Check for valid start of line - try { - testTimestamp = dFormat.parse(lineData[0]); - data.add(lineData); - } catch (ParseException ex) { - - // Is this the heading line? - // Then we parse it to set the ordering of elements - if(lineData[0].equals("date")) - { - headers = lineData; - // Datum and Zeit should always be the two first ones - for(int i=1;i<lineData.length;i++) - { - for(int j=0;j<elementMeasurementTypes.length;j++) - { - if(elementMeasurementTypes[j][0].equals(lineData[i])) - { - elementOrdering.put(i,j); - } - } - } - } - } - } - in.close(); - } catch (IOException ex) { - throw new ParseWeatherDataException(ex.getMessage()); - } - - Date timestamp = null; - WeatherUtil wUtil = new WeatherUtil(); - for(String[] lineData: data) - { - // If minute != 00 we skip the line - if(!lineData[0].split(":")[1].equals("00")) - { - continue; - } - try - { - timestamp = dFormat.parse(lineData[0]); - } - catch(ParseException ex) - { - throw new ParseWeatherDataException("Error with time stamp in weather data from Maynooth Uni station: " + ex.getMessage()); - } - for(Integer i=1;i<lineData.length;i++) - { - // Skip empty columns - Double value; - if(lineData[i].trim().isEmpty()) - { - value = 0.0; - } - else - { - value = Double.valueOf(lineData[i].replaceAll(",",".")); - } - Integer elementMeasurementTypeIndex = elementOrdering.get(i); - // This means there is an element type we don't collect - if(elementMeasurementTypeIndex == null) - { - continue; - } - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timestamp); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - obs.setElementMeasurementTypeId(MaynoothUniDataParser.elementMeasurementTypes[elementMeasurementTypeIndex][1]); - if(obs.getElementMeasurementTypeId().equals("Q0")) - { - // Solar radiation in data set is in Joule / square centimeter / hour - obs.setValue(wUtil.calculateAverageHourWattsPerSquareMeterFromJoulePerSquareCentimeter(value)); - } - else - { - obs.setValue(value); - } - retVal.add(obs); - } - } - - return retVal; - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/MetosAPIDataParser.java b/src/main/java/no/nibio/vips/util/weather/MetosAPIDataParser.java deleted file mode 100644 index 14a64364e4bb624d96edde177bd046cfcd008698..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/MetosAPIDataParser.java +++ /dev/null @@ -1,346 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Optional; -import java.util.TimeZone; -import java.util.logging.Level; -import java.util.logging.Logger; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.util.WeatherElements; -import org.apache.http.Header; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.auth.AuthenticationException; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.AuthCache; -import org.apache.http.client.CookieStore; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.client.utils.URLEncodedUtils; -import org.apache.http.impl.auth.BasicScheme; -import org.apache.http.impl.client.BasicAuthCache; -import org.apache.http.impl.client.BasicCookieStore; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.cookie.BasicClientCookie; -import org.apache.http.message.BasicNameValuePair; - -/** - * - * Gets data from the Pessl METOS fieldclimate API. - * Read about the API here: - * - * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class MetosAPIDataParser { - - - // Mappping VIPS parameters and Metos sensors - // Fieldclimate codes should be in decreasing priority - // TODO; Separate parameter: Soil temperature. Search for name "Soil temperature", higher channel number = deeper sensor - private final ParamInfo[] PARAM_MAP = { - new ParamInfo(WeatherElements.PRECIPITATION, new Integer[] {6}, "sum"), - new ParamInfo(WeatherElements.LEAF_WETNESS_DURATION, new Integer[] {4}, "time"), - new ParamInfo(WeatherElements.GLOBAL_RADIATION, new Integer[] {600}, "avg"), - new ParamInfo(WeatherElements.WIND_SPEED_2M, new Integer[] {5}, "avg"), - new ParamInfo(WeatherElements.TEMPERATURE_MEAN, new Integer[] {0,16385}, "avg"), - new ParamInfo(WeatherElements.TEMPERATURE_MAXIMUM, new Integer[] {16385}, "max"), - new ParamInfo(WeatherElements.TEMPERATURE_MINIMUM, new Integer[] {16385}, "min"), - new ParamInfo(WeatherElements.RELATIVE_HUMIDITY_MEAN, new Integer[] {1,507,21778}, "avg") - }; - - - /** - * - * @return - * @throws UnsupportedEncodingException - * @throws IOException - */ - public String getAccessCode() throws UnsupportedEncodingException, IOException - { - String accessCode = null; - - HttpPost httppost = new HttpPost("https://oauth.fieldclimate.com/authorize?response_type=code&client_id=MetosDemo&state=xyz"); - CookieStore cookieStore = new BasicCookieStore(); - BasicClientCookie cookie = new BasicClientCookie("METOS","metos_srv_5"); - cookie.setPath("/"); - cookie.setDomain("oauth.fieldclimate.com"); - cookieStore.addCookie(cookie); - CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); - // Request parameters and other properties. - List<NameValuePair> params = new ArrayList<>(); - params.add(new BasicNameValuePair("username", System.getProperty("no.nibio.vips.logic.weather.FIELDCLIMATE_API_USERNAME"))); - params.add(new BasicNameValuePair("password", System.getProperty("no.nibio.vips.logic.weather.FIELDCLIMATE_API_PASSWORD"))); - params.add(new BasicNameValuePair("authorization", "true")); - httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); - - //Execute and get the response. - HttpResponse response = httpclient.execute(httppost); - /*for(Header h:response.getAllHeaders()) - { - System.out.println(h.getName() + ": " + h.getValue()); - }*/ - Header location = response.getFirstHeader("Location"); - if(location != null && location.getValue().split("code=").length > 1) - { - try - { - Optional<NameValuePair> code = URLEncodedUtils.parse(new URI(location.getValue()), "UTF-8").stream() - .filter(key -> key.getName().equals("code")) - .findFirst(); - accessCode = code.isPresent() ? code.get().getValue(): null; - } - catch(URISyntaxException ex) - { - ex.printStackTrace(); - } - } - //System.out.println("accessCode=" + accessCode); - //String responseString = EntityUtils.toString(response.getEntity(), "UTF-8"); - //System.out.println(responseString); - return accessCode; - } - - public String getToken() throws IOException, AuthenticationException - { - - String accessCode = this.getAccessCode(); - - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpPost httppost = new HttpPost("https://oauth.fieldclimate.com/token"); - - // Setting up HTTP BASIC authentication - CredentialsProvider provider = new BasicCredentialsProvider(); - UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( - System.getProperty("no.nibio.vips.logic.weather.FIELDCLIMATE_API_CLIENT_ID"), - System.getProperty("no.nibio.vips.logic.weather.FIELDCLIMATE_API_CLIENT_SECRET") - ); - - HttpHost targetHost = new HttpHost("oauth-stage.fieldclimate.com", 80, "https"); - AuthCache authCache = new BasicAuthCache(); - authCache.put(targetHost, new BasicScheme()); - BasicScheme basicScheme = new BasicScheme(); - final HttpClientContext context = HttpClientContext.create(); - context.setCredentialsProvider(provider); - context.setAuthCache(authCache); - httppost.addHeader(basicScheme.authenticate(credentials, httppost, context)); - - List<NameValuePair> params = new ArrayList<>(); - params.add(new BasicNameValuePair("grant_type","authorization_code")); - params.add(new BasicNameValuePair("code",accessCode)); - httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); - - - HttpResponse response = httpclient.execute(httppost); - BufferedReader s = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - String line = s.readLine(); - ObjectMapper m = new ObjectMapper(); - JsonNode jn = m.readTree(line); - return jn.findValue("access_token").asText(); - } - - public List<WeatherObservation> getParsedObservations(String jsonTxt, TimeZone timeZone) throws IOException, ParseException - { - - ObjectMapper oMapper = new ObjectMapper(); - JsonNode jNode = oMapper.readTree(jsonTxt); - List<WeatherObservation> retVal = new ArrayList<>(); - - JsonNode dates = jNode.get("dates"); - JsonNode data = jNode.get("data"); - SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - dFormat.setTimeZone(timeZone); - for(ParamInfo paramInfo:this.PARAM_MAP) - { - boolean foundParam = false; - for(Integer code:paramInfo.preferredCodes) - { - for(JsonNode aData:data) - { - if(aData.get("code").asInt() == code) - { - for(int i=0;i< dates.size();i++) - { - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(dFormat.parse(dates.get(i).asText())); - obs.setElementMeasurementTypeId(paramInfo.VIPSCode); - obs.setValue(aData.get("aggr").get(paramInfo.aggregationType).get(i).asDouble()); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - retVal.add(obs); - //System.out.println(obs.toString()); - } - foundParam = true; - } - if(foundParam) - { - break; - } - } - if(foundParam) - { - break; - } - } - } - - return retVal; - } - - - public List<WeatherObservation> getWeatherObservations(String stationId, TimeZone timeZone, Date startDate) throws ParseWeatherDataException - { - try - { - String accessToken = this.getToken(); - HttpGet httpget = new HttpGet("https://api.fieldclimate.com/v1/data/optimized/" + stationId + "/hourly/from/" + (startDate.getTime() / 1000)); - - httpget.addHeader("Accept", "application/json"); - httpget.addHeader("Authorization", "Bearer " + accessToken); - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpResponse response = httpclient.execute(httpget); - BufferedReader s = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - String all = ""; - String line; - while((line = s.readLine()) != null) - { - all += line; - //System.out.println("Linje"); - } - - return this.getParsedObservations(all, this.getWeatherStationTimeZone(stationId, accessToken)); // TODO TimeZone - } - catch(IOException | ParseException | AuthenticationException ex) - { - throw new ParseWeatherDataException(ex.getMessage()); - } - } - - private TimeZone getWeatherStationTimeZone(String stationId,String accessToken) throws IOException - { - HttpGet httpget = new HttpGet("https://api.fieldclimate.com/v1/station/" + stationId); - httpget.addHeader("Accept", "application/json"); - httpget.addHeader("Authorization", "Bearer " + accessToken); - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpResponse response = httpclient.execute(httpget); - BufferedReader s = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - String all = ""; - String line; - while((line = s.readLine()) != null) - { - all += line; - //System.out.println("Linje"); - } - ObjectMapper oMapper = new ObjectMapper(); - JsonNode jNode = oMapper.readTree(all); - Integer minutesOffset = jNode.get("config").get("timezone_offset").asInt(); - Integer GMTOffset = minutesOffset/60; - return GMTOffset >= 0 ? TimeZone.getTimeZone("GMT+" + GMTOffset) : TimeZone.getTimeZone("GMT" + GMTOffset); - } - - /** - * Data structure for easy parameter mapping - */ - private class ParamInfo { - - public final String VIPSCode; - public final Integer[] preferredCodes; - public final String aggregationType; - public ParamInfo(String VIPSCode, Integer[] preferredCodes, String aggregationType) - { - this.VIPSCode = VIPSCode; - this.preferredCodes = preferredCodes; - this.aggregationType = aggregationType; - } - - } - - /** - * For hacking/experimentation - * @throws IOException - * @throws AuthenticationException - */ - public void testAPI() throws IOException, AuthenticationException - { - - String accessToken = this.getToken(); - //HttpGet httpget = new HttpGet("https://api.fieldclimate.com/v1/station/000024A0"); - HttpGet httpget = new HttpGet("https://api.fieldclimate.com/v1/data/optimized/000024A0/hourly/last/5d"); - Calendar cal = Calendar.getInstance(); - cal.set(2017, Calendar.AUGUST, 1, 0, 0, 0); - //HttpGet httpget = new HttpGet("https://api.fieldclimate.com/v1/data/optimized/000024A0/hourly/from/" + (cal.getTime().getTime()/1000)); - //HttpGet httpget = new HttpGet("https://api.fieldclimate.com/v1/system/group/sensors"); - httpget.addHeader("Accept", "application/json"); - httpget.addHeader("Authorization", "Bearer " + accessToken); - CloseableHttpClient httpclient = HttpClients.createDefault(); - HttpResponse response = httpclient.execute(httpget); - BufferedReader s = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); - String all = ""; - String line = null; - while((line = s.readLine()) != null) - { - all += line; - //System.out.println("Linje"); - } - //System.out.println(all); - - - ObjectMapper oMapper = new ObjectMapper(); - JsonNode jNode = oMapper.readTree(all); - //Iterator keys = jNode.fieldNames(); - /*while(keys.hasNext()) - { - System.out.println(keys.next()); - } - - JsonNode dates = jNode.get("dates"); - JsonNode data = jNode.get("data"); - for(int i=0;i< dates.size();i++) - { - //System.out.println(dates.get(i).asText() + ": " + data.get("1_X_X_600").get("name").asText() + " = " + data.get("1_X_X_600").get("aggr").get("avg").get(i).asDouble()); - }*/ - try { - this.getParsedObservations(all, TimeZone.getTimeZone("Europe/Vilnius")); - } catch (ParseException ex) { - Logger.getLogger(MetosAPIDataParser.class.getName()).log(Level.SEVERE, null, ex); - } - //System.out.println(all); - } -} \ No newline at end of file diff --git a/src/main/java/no/nibio/vips/util/weather/MetosRIMProDataParser.java b/src/main/java/no/nibio/vips/util/weather/MetosRIMProDataParser.java deleted file mode 100755 index 5b1b35b86cb4db619e3f4737e6f1521f0df3974a..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/MetosRIMProDataParser.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; -import java.text.MessageFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; - -/** - * Reads/parses data from a Metos weather station - * For future versions (need authentication per station): http://www.fieldclimate.com/json_manual/ - * @copyright 2015 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class MetosRIMProDataParser { - public final static String METOS_URL_TEMPLATE = "http://www.fieldclimate.com/rimpro/all_sensors_csv.php?s={0}"; - // Metos parameters, including name and aggregation type - private final static String[][] elementMeasurementTypes = { - {"Solar radiation Dgt","Q0","AVG"}, - {"Precipitation","RR","SUM"}, - {"Wind speed","FM2","AVG"}, - {"Battery voltage","BATTERY","AVG"}, - {"Leaf Wetness","BT","SUM"}, - {"HC Air temperature","TM","AVG"}, - {"Air temperature","TM","AVG"}, - {"HC Relative humidity","UM","AVG"}, - {"Relative humidity","UM","AVG"} - }; - - /** - * Makes sure only data after the requested date is returned - * @param stationID - * @param timeZone - * @param startDate - * @return - * @throws ParseWeatherDataException - */ - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone, Date startDate) throws ParseWeatherDataException - { - List<WeatherObservation> retVal = new ArrayList<>(); - List<WeatherObservation> allObservations = this.getWeatherObservations(stationID, timeZone); - for(WeatherObservation obs: allObservations) - { - if(obs.getTimeMeasured().compareTo(startDate) >= 0) - { - retVal.add(obs); - } - } - return retVal; - } - - /** - * Using output designed for RIMPro, parsing into WeatherObservations - * @param stationID the METOS station ID - * @param timeZone - * @return - */ - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone) throws ParseWeatherDataException - { - List<WeatherObservation> retVal = new ArrayList<>(); - SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - dFormat.setTimeZone(timeZone); - // Assuming 1 hour resolution until we find a timestamp that says :30 - Integer logIntervalId = WeatherObservation.LOG_INTERVAL_ID_1H; - List<String[]> data = new ArrayList<>(); - String[] headers; - Map<Integer, Integer> elementOrdering = new HashMap<>(); - try { - URL metosURL = new URL(MessageFormat.format(MetosRIMProDataParser.METOS_URL_TEMPLATE, stationID)); - BufferedReader in = new BufferedReader( - new InputStreamReader(metosURL.openStream())); - - String inputLine; - Date testTimestamp = null; - - // We need to collect all the lines first, because we need to analyze - // If the resolution is 30 minutes or 1 hour - while ((inputLine = in.readLine()) != null) - { - String[] lineData = inputLine.split(";"); - // Skip empty lines - if(lineData.length <= 1) - { - continue; - } - // Check for valid start of line - try { - testTimestamp = dFormat.parse(lineData[0] + " " + lineData[1]); - data.add(lineData); - if(lineData[1].split(":")[1].equals("30")) - { - logIntervalId = WeatherObservation.LOG_INTERVAL_ID_30M; - } - } catch (ParseException ex) { - - // Is this the heading line? - // Then we parse it to set the ordering of elements - if(lineData[0].equals("Datum")) - { - headers = lineData; - // Datum and Zeit should always be the two first ones - for(int i=2;i<lineData.length;i++) - { - for(int j=0;j<elementMeasurementTypes.length;j++) - { - if(elementMeasurementTypes[j][0].equals(lineData[i])) - { - elementOrdering.put(i,j); - } - } - } - } - } - } - in.close(); - } catch (IOException ex) { - throw new ParseWeatherDataException(ex.getMessage()); - } - - - // Data comes in half-hour chunks (resolution = 30 minutes) - if(logIntervalId.equals(WeatherObservation.LOG_INTERVAL_ID_30M)) - { - Boolean shouldBe00Now = true; - String[] data00 = null; - String[] data30 = null; - Date timestamp = null; - for(String[] lineData: data) - { - // Skip lines that are not exactly :00 or :30 - if( - !lineData[1].split(":")[1].equals("00") - && ! lineData[1].split(":")[1].equals("30") - ) - { - continue; - } - else if(lineData[1].split(":")[1].equals("00") && shouldBe00Now) - { - data00 = lineData; - try - { - timestamp = dFormat.parse(lineData[0] + " " + lineData[1]); - } - catch(ParseException ex) - { - throw new ParseWeatherDataException("Error with time stamp in weather data from Metos station: " + ex.getMessage()); - } - shouldBe00Now = false; - continue; // So that we summarize only after :30 data has been set too - } - else if(lineData[1].split(":")[1].equals("30") && !shouldBe00Now) - { - data30 = lineData; - shouldBe00Now = true; - } - else - { - throw new ParseWeatherDataException("Doesn't make sense!"); - } - - for(Integer i=2;i<data00.length;i++) - { - Double aggregateValue = null; - Double value00 = Double.valueOf(data00[i].replaceAll(",",".")); - Double value30 = Double.valueOf(data30[i].replaceAll(",",".")); - Integer elementMeasurementTypeIndex = elementOrdering.get(i); - // This means there is an element type we don't collect - if(elementMeasurementTypeIndex == null) - { - continue; - } - //System.out.println("element " + i + "=" + MetosRIMProDataParser.elementMeasurementTypes[elementMeasurementTypeIndex][1]); - if(MetosRIMProDataParser.elementMeasurementTypes[elementMeasurementTypeIndex][2].equals("AVG")) - { - aggregateValue = (value00 + value30) / 2; - } - else - { - aggregateValue = (value00 + value30); - } - - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timestamp); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - obs.setElementMeasurementTypeId(MetosRIMProDataParser.elementMeasurementTypes[elementMeasurementTypeIndex][1]); - obs.setValue(aggregateValue); - retVal.add(obs); - } - } - } - // Data is hourly, easy to add - else - { - Date timestamp = null; - for(String[] lineData: data) - { - // If minute != 00 we skip the line - if(!lineData[1].split(":")[1].equals("00")) - { - continue; - } - try - { - timestamp = dFormat.parse(lineData[0] + " " + lineData[1]); - } - catch(ParseException ex) - { - throw new ParseWeatherDataException("Error with time stamp in weather data from Metos station: " + ex.getMessage()); - } - for(Integer i=2;i<lineData.length;i++) - { - Double value = Double.valueOf(lineData[i].replaceAll(",",".")); - Integer elementMeasurementTypeIndex = elementOrdering.get(i); - // This means there is an element type we don't collect - if(elementMeasurementTypeIndex == null) - { - continue; - } - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timestamp); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - obs.setElementMeasurementTypeId(MetosRIMProDataParser.elementMeasurementTypes[elementMeasurementTypeIndex][1]); - obs.setValue(value); - retVal.add(obs); - } - } - } - - - - return retVal; - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/USPestDataParser.java b/src/main/java/no/nibio/vips/util/weather/USPestDataParser.java deleted file mode 100755 index f47d8c3ac971b96394de77d07bebee0df90b4f55..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/USPestDataParser.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.StringReader; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.logic.util.SystemTime; -import no.nibio.vips.util.WeatherUtil; -import no.nibio.vips.util.WebUtil; - -/** - * @copyright 2016 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class USPestDataParser { - public final static String USPEST_URL_TEMPLATE = "http://uspest.org/risk/models"; - // Parameters, including name and aggregation type - private final static String[][] elementMeasurementTypes = { - {"rain","RR","SUM"}, - {"temp","TM","AVG"}, - {"dewpt","DEWPOINT","AVG"}, - {"rel_hum","UM","AVG"}, - {"windspeed","FM2","AVG"}, - {"leafwet","BT","AVG"} - }; - - /** - * Makes sure only data after the requested date is returned - * @param stationID - * @param timeZone - * @param startDate - * @return - * @throws ParseWeatherDataException - */ - public List<WeatherObservation> getWeatherObservations(String stationID, TimeZone timeZone, Date startDate) throws ParseWeatherDataException - { - List<WeatherObservation> retVal = new ArrayList<>(); - Map<Integer, Integer> elementOrdering = new HashMap<>(); - WeatherUtil wUtil = new WeatherUtil(); - SimpleDateFormat usPestDFormat = new SimpleDateFormat("MMM dd yyyy HH"); - usPestDFormat.setTimeZone(timeZone); - SimpleDateFormat postFormDFormat = new SimpleDateFormat("yyyyMMdd"); - //Date today = SystemTime.getSystemTime(); - Date today = new Date(); - try - { - today = SystemTime.getSystemTime(); - } - catch(NumberFormatException ex) - { - - } - - - Map<String, String> queryParams = new HashMap<>(); - queryParams.put("station", stationID); - Calendar cal = Calendar.getInstance(timeZone); - cal.setTime(startDate); - int startDOY = cal.get(Calendar.DAY_OF_YEAR); - queryParams.put("stm",String.valueOf(cal.get(Calendar.MONTH)+1)); // Start month - queryParams.put("std",String.valueOf(cal.get(Calendar.DATE))); // Start dayOfMonth - queryParams.put("sty",String.valueOf(cal.get(Calendar.YEAR))); // Start year - cal.setTime(today); - int todayDOY = cal.get(Calendar.DAY_OF_YEAR); - queryParams.put("span",String.valueOf(todayDOY + 10 - startDOY)); // Number of days from start // Seems as it needs to be not to far in the future - queryParams.put("fsm",String.valueOf(cal.get(Calendar.MONTH)+1)); // Forecast start month - queryParams.put("fsd",String.valueOf(cal.get(Calendar.DATE))); // Forecast start dayOfMonth - // fcast, Type of forecast - // 1 = after 7day use 10 year averages (default) - // 2 = after 7day use 30 year averages - // 3 = after 7day use data from 1 year ago (if available) - // 4 = after 7day use data from 2 years ago (if available) - // 5 = after 7day use NMME extended seasonal forecast - // 6 = after 7day use the CFSv2 extended seasonal forecast - queryParams.put("fcast","1"); - // Bad weather data detection and replacement config - queryParams.put("en_v2_tdp_missing", "on"); // Replace missing Temperature and Dew Point - queryParams.put("en_v2_tdp_flatline", "on"); // Replace flat line Temperature and Dew Point - queryParams.put("en_v2_tdp_range", "on"); // Replace range exceed Temperature and Dew Point - //queryParams.put("en_v2_tdp_extrap", "on"); // NOT RECOMMENDED - queryParams.put("en_v2_tdp_interp", "on"); // Replace data out of bounds of an interpolation - //queryParams.put("en_v2_tdp_elevreg", "on"); // NOT RECOMMENDED - queryParams.put("en_v2_windspeed_missing", "on"); // Replace missing wind speed - queryParams.put("en_v2_rain_missing", "on"); // Replance missing precipitation - queryParams.put("download_data","Download Data"); - queryParams.put("stdt",postFormDFormat.format(startDate)); // Probably start date, but why the double info (see stm/std) - queryParams.put("date",postFormDFormat.format(today)); // Should be "today" - - String[] weatherParams = {"temp", "dewpt","rel_hum","windspeed","rain","leafwet"}; - Map<String, String[]> arrP = new HashMap<>(); - arrP.put("wp", weatherParams); - // Keep track of which timestamps we already have, - // to make sure overlapping forecast values don't overwrite measured values - Set<Date> measuredDataTimestamps = new HashSet<>(); - //queryParams.keySet().stream().forEach(key -> System.out.println(key + "=" + queryParams.get(key)) ); - try - { - String output = WebUtil.getOutputFromPostRequest(USPestDataParser.USPEST_URL_TEMPLATE, queryParams, arrP); - try (BufferedReader reader = new BufferedReader(new StringReader(output))) { - reader.lines().forEach( - inputLine -> { - String[] lineData = inputLine.split(","); - //System.out.println(inputLine); - - // Skip empty lines - if(lineData.length <= 1) - { - return; - } - // Check for valid start of line - try { - - String dateAndTimeStr = lineData[0] + " " + lineData[1]; - Date timeMeasured = usPestDFormat.parse(dateAndTimeStr); - // If this time stamp already has been processed, this must be - // an overlapping forecast value. We skip it. - if(measuredDataTimestamps.contains(timeMeasured)) - { - return; - } - else - { - measuredDataTimestamps.add(timeMeasured); - } - // Do stuff - for(Integer i=2;i<lineData.length;i++) - { - // Skip empty columns - Double value = 0.0; - try - { - value = Double.valueOf(lineData[i].replaceAll(",",".")); - } - catch(NumberFormatException ex) {/*System.out.println("Error formatting \"" + lineData[i] + "\" lineData[" + i + "]");*/} - - Integer elementMeasurementTypeIndex = elementOrdering.get(i); - // This means there is an element type we don't collect - if(elementMeasurementTypeIndex == null) - { - continue; - } - WeatherObservation obs = new WeatherObservation(); - obs.setTimeMeasured(timeMeasured); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - obs.setElementMeasurementTypeId(USPestDataParser.elementMeasurementTypes[elementMeasurementTypeIndex][1]); - // Converting from US system to VIPS - // Temps measured in F -> C - if(obs.getElementMeasurementTypeId().equals("TM")) - { - obs.setValue(wUtil.getCelciusFromFahrenheit(value)); - } - // Rain measured in inches -> mm - else if(obs.getElementMeasurementTypeId().equals("RR")) - { - obs.setValue(wUtil.getMillimetersFromInches(value)); - } - // Leaf wetness [0...10] -> [0...60] - else if(obs.getElementMeasurementTypeId().equals("BT")) - { - obs.setValue(wUtil.getLeafWetnessValueFromUSPest(value)); - } - else - { - obs.setValue(value); - } - retVal.add(obs); - } - } catch (ParseException ex) { - - // Is this the heading line? - // Then we parse it to set the ordering of elements - if(lineData[0].equals("Date")) - { - // Date and hour should always be the two first ones - for(int i=2;i<lineData.length;i++) - { - for(int j=0;j<elementMeasurementTypes.length;j++) - { - if(elementMeasurementTypes[j][0].equals(lineData[i])) - { - elementOrdering.put(i,j); - } - } - } - } - } - } - ); - } - } - catch(IOException ex) - { - ex.printStackTrace(); - } - return retVal; - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/dnmipointweb/DMIPointWebDataParser.java b/src/main/java/no/nibio/vips/util/weather/dnmipointweb/DMIPointWebDataParser.java deleted file mode 100644 index d2da70360fc4ce96847b0a96f75d38e46f5de43f..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/dnmipointweb/DMIPointWebDataParser.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather.dnmipointweb; - -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TimeZone; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.util.WeatherElements; -import no.nibio.vips.util.WeatherUtil; - -/** - * Gets data from the Danish Meteorological Institute's Point Web service - * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class DMIPointWebDataParser { - - private static final Map<String, WeatherDataParameter> PARAM_MAP; - static - { - PARAM_MAP = new HashMap<>(); - PARAM_MAP.put(WeatherElements.TEMPERATURE_MEAN, WeatherDataParameter.AIRTEMP); - PARAM_MAP.put(WeatherElements.PRECIPITATION, WeatherDataParameter.PREC); - PARAM_MAP.put(WeatherElements.RELATIVE_HUMIDITY_MEAN, WeatherDataParameter.AIRRH); - PARAM_MAP.put(WeatherElements.WIND_SPEED_2M, WeatherDataParameter.WINDSPEED); - PARAM_MAP.put(WeatherElements.SOIL_TEMPERATURE_10CM_MEAN, WeatherDataParameter.SOILTEMP); - PARAM_MAP.put(WeatherElements.LEAF_WETNESS_DURATION, WeatherDataParameter.LEAFWET); - } - private final ZoneId danishZ = ZoneId.of("Europe/Copenhagen"); - private final TimeZone danishTZ = TimeZone.getTimeZone("Europe/Copenhagen"); - - public List<WeatherObservation> getData(Double longitude, Double latitude, Date dateFrom, Date dateTo) - { - List<WeatherObservation> rawObservations = new ArrayList<>(); - try { - IWeatherService proxy = new WeatherService().getSslOffloadedBasicHttpBindingIWeatherService(); - UseableArrayOfWeatherDataSource wdsource = new UseableArrayOfWeatherDataSource(); - wdsource.add(WeatherDataSource.OBS); - wdsource.add(WeatherDataSource.FORECAST); - //wdsource.add(WeatherDataSource.NORMAL); - - GregorianCalendar gc = new GregorianCalendar(danishTZ); - gc.setTime(dateFrom); - XMLGregorianCalendar calFrom = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); - XMLGregorianCalendar calTo = null; - if(dateTo != null) - { - gc.setTime(dateTo); - calTo = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); - } - - - UseableArrayOfWeatherDataParameter wdparam = new UseableArrayOfWeatherDataParameter(); - DMIPointWebDataParser.PARAM_MAP.values().stream().forEach( - listedParam -> wdparam.add(listedParam) - ); - - //WeatherResponse result = proxy.getWeatherData( - WeatherResponse result = proxy.getWeatherDataExtended( - String.valueOf(latitude).replace(".", ","), // Latitude ("X") Decimal separator is comma! - String.valueOf(longitude).replace(".", ","), // Longitude ("Y") Decimal separator is comma! - false, // true if UTM, false if decimal degrees - null, // UTM Zone if using UTM - WeatherInterval.HOUR, // Hourly or Daily data - wdsource, // Set of data sources - calFrom, // Start of period with data - calTo, // End of period with data - wdparam, // Set of requested parameters - 0.0, // Base temperature value - false, // Use base temperature? (t/f) - true // Get 6 days of forecasts (only for the proxy.getWeatherDataExtended method) - ); - - ArrayOfWeatherDataModel value = result.getWeahterDataList().getValue(); - value.getWeatherDataModel().stream().forEach(weatherDataModel -> { - DMIPointWebDataParser.PARAM_MAP.keySet().stream().forEach( - VIPSParam->{ - WeatherObservation obs = this.getWeatherObservation(VIPSParam, weatherDataModel); - if(obs != null) - { - rawObservations.add(obs); - } - } - ); - } - ); - - } catch (Exception ex) { - Logger.getLogger(DMIPointWebDataParser.class.getName()).log(Level.SEVERE, null, ex); - } - //System.out.println("Number of extracted weather data = " + retVal.size()); - // After "now", the DMI service provides forecast values seamlessly. After approx 48 hours these - // values turn into 6 hour intervals - // 2019-01-18: We keep all observations, leave it up to the client to decide what to do with the data. - // But we mark them with the correct measuring interval - - ZonedDateTime now = ZonedDateTime.now(); - //List<WeatherObservation> filteredObservations = new ArrayList<>(); - WeatherUtil wUtil = new WeatherUtil(); - List<WeatherObservation> retVal = new ArrayList<>(); - Set<String> parametersInResult = rawObservations.stream().map(obs->obs.getElementMeasurementTypeId()).collect(Collectors.toSet()); - parametersInResult.forEach((param) -> { - ZonedDateTime lastObsTime = null; - List<WeatherObservation> paramObs = wUtil.filterWeatherObservationsByParameter(rawObservations, param); - Collections.sort(paramObs); - for(WeatherObservation obs:paramObs) - { - ZonedDateTime obsTime = ZonedDateTime.ofInstant(obs.getTimeMeasured().toInstant(),danishZ); - - if(lastObsTime != null && obsTime.isAfter(now) && !obsTime.minusHours(6).isBefore(lastObsTime)) - { - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_6H); - } - retVal.add(obs); - lastObsTime = obsTime; - } - }); - Collections.sort(retVal); - return retVal; - } - - private WeatherObservation getWeatherObservation(String VIPSParam, WeatherDataModel wDataModel) { - WeatherObservation obs = new WeatherObservation(); - /*if(VIPSParam.equals(WeatherElements.RELATIVE_HUMIDITY_MEAN)) - { - System.out.println(wDataModel.getDateDay().toString()); - }*/ - // When the DMI weather service shifts from historic to forecast data, timezone information - // is added to the timestamp string. The consequence of this is that in a system - // where default timezone is something else than CET (UTC+1/2), there will be an - // overlap of values, meaning that to values, e.g - // 2019-01-27T22:00:00 - // 2019-01-27T23:00:00+01:00 - // Will both be translated to 2019-01-27T22:00:00 UTC if the default - // timezone on that system is UTC. - // So we add danishTZ here to avoid this. It only tok 8-10 hours to debug.... - obs.setTimeMeasured(wDataModel.getDateDay().toGregorianCalendar(danishTZ, null, null).getTime()); - obs.setLogIntervalId(WeatherObservation.LOG_INTERVAL_ID_1H); - obs.setElementMeasurementTypeId(VIPSParam); - - try - { - switch(VIPSParam) - { - case WeatherElements.TEMPERATURE_MEAN: - obs.setValue(wDataModel.getAirtemp().getValue()); break; - case WeatherElements.PRECIPITATION: - obs.setValue(wDataModel.getPrec().getValue()); break; - case WeatherElements.RELATIVE_HUMIDITY_MEAN: - obs.setValue(wDataModel.getAirrh().getValue()); break; - case WeatherElements.WIND_SPEED_2M: - obs.setValue(wDataModel.getWindspeed().getValue()); break; - case WeatherElements.SOIL_TEMPERATURE_10CM_MEAN: - obs.setValue(wDataModel.getSoiltemp().getValue()); break;// Not sure if it's 10 or 5 cm depth yet - case WeatherElements.SOIL_TEMPERATURE_5CM_MEAN: - obs.setValue(wDataModel.getSoiltemp().getValue()); break; - case WeatherElements.LEAF_WETNESS_DURATION: - obs.setValue(wDataModel.getLeafwet().getValue()); break; - default: return null; - } - } - catch(NullPointerException ex) // The requested parameter didn't exist - { - return null; - } - - return obs; - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/dnmipointweb/UseableArrayOfWeatherDataParameter.java b/src/main/java/no/nibio/vips/util/weather/dnmipointweb/UseableArrayOfWeatherDataParameter.java deleted file mode 100644 index 0cdc7e3ca656988406c83b982b2bdf3dca8c37ee..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/dnmipointweb/UseableArrayOfWeatherDataParameter.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather.dnmipointweb; - -/** - * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class UseableArrayOfWeatherDataParameter extends ArrayOfWeatherDataParameter{ - - public void add(WeatherDataParameter wdp) - { - this.getWeatherDataParameter().add(wdp); - } - -} diff --git a/src/main/java/no/nibio/vips/util/weather/dnmipointweb/UseableArrayOfWeatherDataSource.java b/src/main/java/no/nibio/vips/util/weather/dnmipointweb/UseableArrayOfWeatherDataSource.java deleted file mode 100644 index 124e800e57bff14792f336a2789b00918b674776..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/dnmipointweb/UseableArrayOfWeatherDataSource.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather.dnmipointweb; - -/** - * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class UseableArrayOfWeatherDataSource extends ArrayOfWeatherDataSource{ - - public void add(WeatherDataSource wds) - { - this.getWeatherDataSource().add(wds); - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/metnothredds/MetNoThreddsDataParser.java b/src/main/java/no/nibio/vips/util/weather/metnothredds/MetNoThreddsDataParser.java deleted file mode 100644 index 852901e3c9bc4a60c9df9178f4b4534e6f492253..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/metnothredds/MetNoThreddsDataParser.java +++ /dev/null @@ -1,561 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather.metnothredds; - -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.Polygon; -import java.util.TimeZone; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.Properties; -import java.util.Set; -import java.util.stream.Collectors; -import javax.measure.Measure; -import javax.measure.quantity.Area; -import javax.measure.unit.SI; -import javax.measure.unit.Unit; -import no.nibio.vips.entity.PointWeatherObservationList; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.gis.GISUtil; -import no.nibio.vips.gis.SimpleWGS84Coordinate; -import no.nibio.vips.util.SystemUtil; -import no.nibio.vips.util.WeatherElements; -import no.nibio.vips.util.WeatherUtil; -import org.apache.commons.io.FileUtils; -import ucar.ma2.Array; -import ucar.ma2.ArrayDouble; -import ucar.ma2.ArrayFloat; -import ucar.nc2.NetcdfFile; -import ucar.nc2.Variable; - -/** - * Digging into the Thredds archive of the Norwegian Meteorological Institute, - * collecting - * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class MetNoThreddsDataParser { - - private final String BASE_URL = "http://thredds.met.no/thredds/"; - private final String NCSS_URL = BASE_URL + "ncss/"; - private final String MEPS25_PATH = "meps25epsarchive/"; - private final String MEPS25_FILEBASENAME = "meps_mbr0_extracted_2_5km_"; - private final String NETCDF_EXT = ".nc"; - private final String STANDARD_PARAMS = "?accept=netcdf&temporal=all&&horizStride=1&disableLLSubset=on&timeStride=1"; - private Properties paramInfo; - private final String TMP_FILE_PATH; - private final List<String> accumulatedParams = Arrays.asList("Q0", "RR"); - private final SimpleDateFormat pathFormat; - private final SimpleDateFormat fileTimeStampFormat; - - // Approximate number of memory bytes per observation - // See the test class for more info - private final int AVERAGE_OBS_OBJ_SIZE = 40; - - private final WeatherUtil weatherUtil; - - public MetNoThreddsDataParser() - { - paramInfo = new Properties(); - try (InputStream in = this.getClass().getResourceAsStream("parametersources.properties")) { - paramInfo.load(in); - } - catch(IOException ex) - { - } - this.TMP_FILE_PATH = System.getProperty("no.nibio.vips.logic.weather.METNOTHREDDS_TMP_FILE_PATH"); - this.weatherUtil = new WeatherUtil(); - this.pathFormat = new SimpleDateFormat("yyyy/MM/dd/"); - this.pathFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - this.fileTimeStampFormat = new SimpleDateFormat("yyyyMMdd'T'00'Z'"); - this.fileTimeStampFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - } - - /** - * If you want a lot of data, split it into smaller, manageable parts - * @param geometry - * @param resolution - * @return - */ - public Geometry[] getManageableGeometries(Geometry geometry, Double resolution, Date dateFrom, Date dateTo, List<String> VIPSWeatherParameters) - { - Long presumableFreeMemory = SystemUtil.getPresumableFreeMemory(); - Integer divideBy = 1; - while(this.getMemoryNeededForRequest(this.getGeometryFraction(geometry, divideBy, 0, 0), resolution, dateFrom, dateTo, VIPSWeatherParameters) > presumableFreeMemory * 0.3) - { - divideBy++; - } - - Geometry[] retVal = new Geometry[divideBy * divideBy]; - for(int x=0;x<divideBy;x++) - { - for(int y=0;y<divideBy;y++) - { - retVal[x*divideBy + y] = this.getGeometryFraction(geometry, divideBy, x,y); - } - } - //System.out.println("divideBy=" + divideBy); - return retVal; - } - - public Geometry getGeometryFraction(Geometry geometry, Integer divideBy, Integer x, Integer y) - { - Polygon p = (Polygon) geometry.getEnvelope(); - Double lonLength = (this.getEast(p) - this.getWest(p)) / divideBy; - Double latLength = (this.getNorth(p) - this.getSouth(p)) / divideBy; - - //Double newNorth = ((this.getNorth(p) - this.getSouth(p))/divideBy) + this.getSouth(p); - //Double newEast = (this.getEast(p) - this.getWest(p)/divideBy) + this.getWest(p); - return new GISUtil().createRectangleFromBounds( - this.getSouth(p) + (latLength * (y +1)), - this.getSouth(p) + (latLength * y), - this.getWest(p) + (lonLength * (x +1)), - this.getWest(p) + (lonLength * x) - ); - } - - /** - * - * @param geometry - * @param resolution - * @param dateFrom - * @param dateTo - * @param VIPSWeatherParameters - * @return - * @throws TooMuchDataToAskForException - */ - public List<PointWeatherObservationList> getGridData(Geometry geometry, Double resolution, Date dateFrom, Date dateTo, List<String> VIPSWeatherParameters) throws TooMuchDataToAskForException - { - Long memoryNeededForRequest = this.getMemoryNeededForRequest(geometry, resolution, dateFrom, dateTo, VIPSWeatherParameters); - Long presumableFreeMemory = SystemUtil.getPresumableFreeMemory(); - //System.out.println("memoryNeeded=" + memoryNeededForRequest + ", free=" + presumableFreeMemory); - if(memoryNeededForRequest > presumableFreeMemory * 0.6) - { - throw new TooMuchDataToAskForException("The request is estimated to require " + memoryNeededForRequest + " bytes of memory, this is too much. Please ask for less!"); - } - - Set<String> metParamNames = VIPSWeatherParameters.stream().map( - param -> paramInfo.getProperty(param) - ).collect(Collectors.toSet()); - - - List<Date> archiveDates = getArchiveDates(dateFrom, dateTo); - Collections.sort(archiveDates); - //new GeometryFactory(). - Polygon gridBounds = (Polygon) geometry.getEnvelope(); - //System.out.println("geometryType=" + gridBounds.getGeometryType()); - //System.out.println("gridBounds=" + gridBounds); - - Map<Coordinate, Map<Long, WeatherObservation>> tempRetVal = new HashMap<>(); - archiveDates.stream().forEach(archiveDate -> { - File tmp = null; - NetcdfFile ncFile = null; - try - { - URL threddsURL = new URL(NCSS_URL + MEPS25_PATH + - this.pathFormat.format(archiveDate) + - MEPS25_FILEBASENAME + - this.fileTimeStampFormat.format(archiveDate) + - NETCDF_EXT + STANDARD_PARAMS + - "&north=" + this.getNorth(gridBounds) + "&west=" + this.getWest(gridBounds) + "&east=" + this.getEast(gridBounds) + "&south=" + this.getSouth(gridBounds) + - "&var=" + String.join(",", metParamNames) + - "&addLatLon=true"); - //System.out.println("URL: " + threddsURL.toString()); - Long timestamp = new Date().getTime(); - tmp = new File(this.TMP_FILE_PATH + timestamp + ".nc"); - FileUtils.copyURLToFile(threddsURL, tmp); - ncFile = NetcdfFile.open(tmp.getAbsolutePath()); - - Variable time = ncFile.findVariable("time"); - Array times = time.read(); - Date[] timeMeasured = new Date[times.getShape()[0]]; - for(int i=0; i<timeMeasured.length;i++) - { - timeMeasured[i] = new Date(times.getLong(i) * 1000); - } - // The points are in a matrix. Get the dims first - ArrayDouble.D2 longitude = (ArrayDouble.D2) ncFile.findVariable("lon").read(); - ArrayDouble.D2 latitude = (ArrayDouble.D2) ncFile.findVariable("lat").read(); - int[] latLonShape = longitude.getShape(); // Assuming they're identical! - Coordinate[][] coords = new Coordinate[latLonShape[0]][latLonShape[1]]; - // Depending on the resolution, we determine the steps - int step = (int) Math.floor(resolution / 2.5 - resolution % 2.5); - //System.out.println("Step="+step); - for(int y=0;y<latLonShape[0];y=y+step) - { - for(int x=0;x<latLonShape[1];x=x+step) - { - double lat = latitude.get(y, x); - double lon = longitude.get(y,x); - Coordinate c = new Coordinate(lon, lat); - coords[y][x] = c; - //System.out.println("lat/lon=" + lat + "/" + lon); - } - } - for(String metParamName:metParamNames) - { - String VIPSParamName = this.findVIPSParamNameFromMetParamName(metParamName); - //System.out.println("metParamName=" + metParamName); - Variable var = ncFile.findVariable(metParamName); - // All variables have the dims [time, height, y, x]. All - // variables represent only one height, and can therefore be reduced - // from 4 to three dimensions - // Assuming all weather parameters are of type "float" - - ArrayFloat.D3 varArray = (ArrayFloat.D3) var.read().reduce(1); // 1 is the height parameter - // We always skip the two first timestamps, since the model needs - // a couple of hours to spin up - for(int i=2;i<timeMeasured.length;i++) - { - if(timeMeasured[i].compareTo(dateFrom) < 0 || timeMeasured[i].after(dateTo)) - { - continue; - } - for(int y=0;y<latLonShape[0];y=y+step) - { - for(int x=0;x<latLonShape[1];x=x+step) - { - Map<Long, WeatherObservation> obsMapForPoint = tempRetVal.get(coords[y][x]); - if(obsMapForPoint == null) - { - obsMapForPoint = new HashMap<>(); - tempRetVal.put(coords[y][x], obsMapForPoint); - } - Double value = new Double(varArray.get(i, y, x)); - // For accumulated parameters, substract previous value in array - if(i>0 && this.accumulatedParams.contains(VIPSParamName)) - { - value = value - new Double(varArray.get(i-1, y, x)); - } - WeatherObservation newObs = new WeatherObservation( - timeMeasured[i], - VIPSParamName, - WeatherObservation.LOG_INTERVAL_ID_1H, - this.getConvertedValue(value, VIPSParamName) - ); - - obsMapForPoint.put(newObs.getValiditySignature(), newObs); - } - } - - } - } - - } - catch(IOException | ClassCastException ex) - { - ex.printStackTrace(); - } - finally { - if(ncFile != null) - { - try - { - ncFile.close(); - }catch(IOException ex) {} - } - if(tmp != null) - { - tmp.delete(); - } - } - }); - List<PointWeatherObservationList> retVal = new ArrayList<>(); - for(Coordinate c : tempRetVal.keySet()) - { - Map<Long, WeatherObservation> hashedObsForPoint = tempRetVal.get(c); - List<WeatherObservation> obsList = new ArrayList(hashedObsForPoint.values()); - Collections.sort(obsList); - PointWeatherObservationList obsForPoint = new PointWeatherObservationList(c,obsList,"UTC"); - retVal.add(obsForPoint); - } - return retVal; - } - - public List<WeatherObservation> getPointData(Double longitude, Double latitude, Date dateFrom, Date dateTo, List<String> VIPSWeatherParameters) - { - Set<String> metParamNames = VIPSWeatherParameters.stream().map( - param -> paramInfo.getProperty(param) - ).collect(Collectors.toSet()); - - List<Date> archiveDates = getArchiveDates(dateFrom, dateTo); - Collections.sort(archiveDates); - - Map<Long, WeatherObservation> retVal = new HashMap<>(); - archiveDates.stream().forEach(archiveDate -> { - File tmp = null; - NetcdfFile ncFile = null; - try - { - URL threddsURL = new URL(NCSS_URL + MEPS25_PATH + - this.pathFormat.format(archiveDate) + - MEPS25_FILEBASENAME + - this.fileTimeStampFormat.format(archiveDate) + - NETCDF_EXT + STANDARD_PARAMS + "&longitude=" + longitude + "&latitude=" + latitude - + "&var=" + String.join(",", metParamNames)); - //System.out.println("URL: " + threddsURL.toString()); - Long timestamp = new Date().getTime(); - tmp = new File(this.TMP_FILE_PATH + timestamp + ".nc"); - FileUtils.copyURLToFile(threddsURL, tmp); - ncFile = NetcdfFile.open(tmp.getAbsolutePath()); - - // Set up the array of timestamps - // Unfortunately, the time value is a Double - // Dimensions of the time array are [station, timestamp]. Since theres - // Only one station, we can reduce the array dims. The reduce() method - // Simply removes all dims with a length of 1. Handy! - ArrayDouble.D1 time = (ArrayDouble.D1) ncFile.findVariable("time").read().reduce(); - int[] timeDims = time.getShape(); - // Get the length of the timestamp dimension - Date[] timeMeasured = new Date[timeDims[0]]; - // Iterate, create parallel array with Dates - for(int i=0;i<timeDims[0];i++) - { - timeMeasured[i] = new Date(Math.round(time.get(i) * 1000)); - } - - // The position variables have 1 dimension and one element. Simple! - SimpleWGS84Coordinate coordinate = new SimpleWGS84Coordinate( - ((ArrayDouble.D1) ncFile.findVariable("latitude").read()).get(0), - ((ArrayDouble.D1) ncFile.findVariable("longitude").read()).get(0) - ); - // Can't use stream API here because the ncfile would need to be - // final and therefore not closeable in a try-finally statement - for(String metParamName:metParamNames) - { - String VIPSParamName = this.findVIPSParamNameFromMetParamName(metParamName); - - Variable var = ncFile.findVariable(metParamName); - - // The variables have two different dims. The variables that - // are height dependent (e.g. temperature) have [station, value, height] - // and the height independent (e.g. precipitation_amount) have - // [station, value]. Either way, all dims except value have a length - // of 1 and can be reduced. - // Assuming all weather parameters are of type "float" - try - { - ArrayFloat.D1 varArray = (ArrayFloat.D1) var.read().reduce(); - // We always skip the two first timestamps, since the model needs - // a couple of hours to spin up - for(int i=2;i<timeMeasured.length;i++) - { - if(timeMeasured[i].compareTo(dateFrom) < 0 || timeMeasured[i].after(dateTo)) - { - continue; - } - Double value = new Double(varArray.get(i)); - // For accumulated parameters, substract previous value in array - if(i>0 && this.accumulatedParams.contains(VIPSParamName)) - { - value = value - new Double(varArray.get(i-1)); - } - WeatherObservation newObs = new WeatherObservation( - timeMeasured[i], - VIPSParamName, - WeatherObservation.LOG_INTERVAL_ID_1H, - this.getConvertedValue(value, VIPSParamName) - ); - retVal.put(newObs.getValiditySignature(), newObs); - } - }catch(IOException ioe) - { - ioe.printStackTrace(); - } - } - - - } - catch(IOException ioe) - { - - } - finally { - if(ncFile != null) - { - try - { - ncFile.close(); - }catch(IOException ex) {} - } - if(tmp != null) - { - tmp.delete(); - } - } - }); - return new ArrayList(retVal.values()); - } - - public String findVIPSParamNameFromMetParamName(String metParamName) - { - // Need to treat the Properties object as a map and search for a key that - // maps to the given met param name - Optional<Entry<Object,Object>> opt = this.paramInfo.entrySet().stream().filter(propEntry -> propEntry.getValue().equals(metParamName)).findFirst(); - return opt.isPresent() ? (String) opt.get().getKey() : null; - } - - /** - * Performing the correct transformations between Met params and VIPS params - * @param value - * @param VIPSParamName - * @return - */ - public double getConvertedValue(Double value, String VIPSParamName) - { - switch(VIPSParamName){ - case WeatherElements.TEMPERATURE_MEAN: - case WeatherElements.TEMPERATURE_MAXIMUM: - case WeatherElements.TEMPERATURE_MINIMUM: - return this.weatherUtil.getCelciusFromKelvin(value); - case WeatherElements.RELATIVE_HUMIDITY_MEAN: - case WeatherElements.RELATIVE_HUMIDITY_INSTANTANEOUS: - return this.getPercentFromFraction(value); - case WeatherElements.PRECIPITATION: - return roundToDecimals(value, 1); - case WeatherElements.GLOBAL_RADIATION: - return Math.round(value/3600); - default: - return value; - } - } - - - - public double getPercentFromFraction(Double value) - { - return value * 100; - } - - public double roundToDecimals(Double value, Integer decimals) - { - return Math.round(value * Math.pow(10.0, decimals)) / Math.pow(10.0, decimals); - } - - /** - * Provide the list of date paths in which we can find files that contain - * data for the given period - * @param dateFrom - * @param dateTo - * @return - */ - public List<Date> getArchiveDates(Date dateFrom, Date dateTo) { - Date today = new Date(); - dateTo = dateTo.after(today) ? today : dateTo; - if(dateFrom.after(dateTo)) - { - return null; - } - TimeZone UTC = TimeZone.getTimeZone("UTC"); - - // If the date is set at earlier than xx:02:00 UTC - // Going one day back in time from dateFrom, since we skip the two - // first runs of each model - Calendar cal = Calendar.getInstance(UTC); - cal.setTime(dateFrom); - if(cal.get(Calendar.HOUR_OF_DAY) < 2) - { - cal.add(Calendar.DATE, -1); - dateFrom = cal.getTime(); - } - - // Adjusting to UTC midnight so that the last date (dateTo) is not - // overstepped - dateFrom = this.weatherUtil.normalizeToExactDate(dateFrom, UTC); - List<Date> retVal = new ArrayList<>(); - while(dateFrom.before(dateTo)) - { - retVal.add(dateFrom); - cal.setTime(dateFrom); - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.add(Calendar.DATE, 1); - dateFrom = cal.getTime(); - } - - return retVal; - } - - public double getNorth(Polygon envelope) - { - return envelope.getCoordinates()[2].y; - } - - public double getSouth(Polygon envelope) - { - return envelope.getCoordinates()[0].y; - } - - public double getWest(Polygon envelope) - { - return envelope.getCoordinates()[0].x; - } - - public double getEast(Polygon envelope) - { - return envelope.getCoordinates()[2].x; - } - - /** - * Calculating roughly the number of grid points within an envelope - * @param envelope - * @param resolution km-resolution of the grid points - * @return - */ - public Long getNumberOfGridPointsInEnvelope(Polygon envelope, Double resolution) - { - GISUtil gisUtil = new GISUtil(); - - Measure<Double,Area> m = gisUtil.calcArea(envelope); - //System.out.println("measure m = " + m); - Unit<Area> sq_km = (Unit<Area>) SI.KILOMETER.pow(2); - //System.out.println("Area=" + m.to(sq_km).getValue()); - Double side = Math.sqrt(m.to(sq_km).getValue()); - - - return Math.round((1 + side / resolution) * (1 + side / resolution)); - } - - public Long getNumberOfWeatherObservationsRequested(Geometry geometry, Double resolution, Date dateFrom, Date dateTo, List<String> VIPSWeatherParameters) - { - Polygon gridBounds = (Polygon) geometry.getEnvelope(); - Long gridPoints = this.getNumberOfGridPointsInEnvelope(gridBounds, resolution); - Long hoursInPeriod = Math.round((dateTo.getTime() - dateFrom.getTime()) / (3600.0 * 1000)); - return gridPoints * hoursInPeriod * VIPSWeatherParameters.size(); - } - - public Long getMemoryNeededForRequest(Geometry geometry, Double resolution, Date dateFrom, Date dateTo, List<String> VIPSWeatherParameters) - { - return this.getNumberOfWeatherObservationsRequested(geometry, resolution, dateFrom, dateTo, VIPSWeatherParameters) * this.AVERAGE_OBS_OBJ_SIZE; - } -} diff --git a/src/main/java/no/nibio/vips/util/weather/metnothredds/TooMuchDataToAskForException.java b/src/main/java/no/nibio/vips/util/weather/metnothredds/TooMuchDataToAskForException.java deleted file mode 100644 index 3031f9f7a0d79511a11a9d2f287c9cbe3ba01959..0000000000000000000000000000000000000000 --- a/src/main/java/no/nibio/vips/util/weather/metnothredds/TooMuchDataToAskForException.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ - -package no.nibio.vips.util.weather.metnothredds; - -/** - * This is simply a message telling that you're asking for too much data, - * putting the server at risk of running out of resources, most notably memory. - * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> - * @author Tor-Einar Skog <tor-einar.skog@nibio.no> - */ -public class TooMuchDataToAskForException extends Exception { - - public TooMuchDataToAskForException() - { - super(); - } - - public TooMuchDataToAskForException(String msg) - { - super(msg); - } - -} diff --git a/src/main/webapp/formdefinitions/poiForm.json b/src/main/webapp/formdefinitions/poiForm.json index d795e4ef6dd3615d52550bbb93ad008d308b867b..fbb1808a68733ba8baf1a5cd91475f436da8881b 100755 --- a/src/main/webapp/formdefinitions/poiForm.json +++ b/src/main/webapp/formdefinitions/poiForm.json @@ -71,7 +71,9 @@ { "name" : "userId", "dataType" : "INTEGER", - "required" : false + "fieldType" : "SELECT_SINGLE", + "required" : true, + "nullValue" : "-1" }, { "name" : "externalResourceIdentifier", diff --git a/src/main/webapp/formdefinitions/weatherStationForm.json b/src/main/webapp/formdefinitions/weatherStationForm.json index ba42241810dcfc9bed499b9ba68a0b9a3284055a..1bc2f92be78d4c23f1a599bada42d9b7102684ba 100755 --- a/src/main/webapp/formdefinitions/weatherStationForm.json +++ b/src/main/webapp/formdefinitions/weatherStationForm.json @@ -77,7 +77,9 @@ { "name" : "userId", "dataType" : "INTEGER", - "required" : false + "fieldType" : "SELECT_SINGLE", + "required" : true, + "nullValue" : "-1" }, { "name" : "externalResourceIdentifier", diff --git a/src/test/java/no/nibio/vips/util/weather/ALabDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/ALabDataParserTest.java deleted file mode 100755 index e30321ce9087fcc7ab0831645d611ad900998f73..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/ALabDataParserTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather; - -import no.nibio.vips.util.weather.ALabDataParser; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author treinar - */ -public class ALabDataParserTest { - - public ALabDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getWeatherObservations method, of class ALabDataParser. - */ - //@Test - public void testGetWeatherObservations_5args() throws Exception { - System.out.println("getWeatherObservations"); - String stationID = "000000000010FDC62301"; - TimeZone timeZone = TimeZone.getTimeZone("Europe/Helsinki"); - DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd"); - dFormat.setTimeZone(timeZone); - Date startDate = dFormat.parse("2015-01-01"); - String userName = "fmi"; - String password = "ectoncha"; - ALabDataParser instance = new ALabDataParser(); - List<WeatherObservation> expResult = null; - List<WeatherObservation> result = instance.getWeatherObservations(stationID, timeZone, startDate, userName, password); - assertNotNull(result); - /**for(WeatherObservation obs: result) - { - if(obs.getElementMeasurementTypeId().equals("RR")) - { - System.out.println(obs); - } - }*/ - } - - - -} diff --git a/src/test/java/no/nibio/vips/util/weather/CebaowangDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/CebaowangDataParserTest.java deleted file mode 100644 index 443e3e84dcb4e262e0ea09bed8ff82f58a54208d..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/CebaowangDataParserTest.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2019 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather; - -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author treinar - */ -public class CebaowangDataParserTest { - - public CebaowangDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getWeatherObservations method, of class CebaowangDataParser. - */ - @Test - public void testGetWeatherObservations() throws Exception { - System.out.println("getWeatherObservations"); - String stationID = "token=0819208daf3684b6e1b2f7d9b0e1bc64&deviceId=YBQ0000053"; - TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai"); - Calendar cal = Calendar.getInstance(timeZone); - cal.set(2019, Calendar.JANUARY, 10, 0,0); - Date startDate = cal.getTime(); - cal.add(Calendar.DATE, 20); - Date endDate = cal.getTime(); - CebaowangDataParser instance = new CebaowangDataParser(); - List<WeatherObservation> expResult = null; - List<WeatherObservation> result = instance.getWeatherObservations(stationID, timeZone, startDate, endDate); - //assertEquals(expResult, result); - assertNotNull(result); - result.forEach(obs->System.out.println(obs)); - - } - -} diff --git a/src/test/java/no/nibio/vips/util/weather/FruitWebDavisDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/FruitWebDavisDataParserTest.java deleted file mode 100755 index 1a0d884d442d8e2335beddfcb23d571c161d6b08..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/FruitWebDavisDataParserTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather; - -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author treinar - */ -public class FruitWebDavisDataParserTest { - - public FruitWebDavisDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getWeatherObservations method, of class FruitWebDavisDataParser. - */ - @Test - public void testGetWeatherObservations() throws Exception { - System.out.println("getWeatherObservations"); - //String stationID = "id=283&pw=Kghr528Ub5"; - String stationID = "id=537&pw=Kgsf72k388"; - TimeZone timeZone = TimeZone.getTimeZone("UTC"); - Calendar cal = Calendar.getInstance(timeZone); - cal.set(2017, Calendar.JANUARY, 1, 0, 0, 0); - Date startDate = cal.getTime(); - FruitWebDavisDataParser instance = new FruitWebDavisDataParser(); - cal.set(Calendar.MONTH, Calendar.AUGUST); - Date endDate = cal.getTime(); - - List<WeatherObservation> result = instance.getWeatherObservations(stationID, timeZone, startDate, endDate); - result.forEach((obs) -> { - System.out.println(obs); - }); - assertNotNull( result); - - } - -} diff --git a/src/test/java/no/nibio/vips/util/weather/MaynoothUniDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/MaynoothUniDataParserTest.java deleted file mode 100755 index 78632d848a5a1b8c15f6cb025735aadd79c61d51..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/MaynoothUniDataParserTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather; - -import com.fasterxml.jackson.databind.ObjectMapper; -import java.io.File; -import java.io.FileOutputStream; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author treinar - */ -public class MaynoothUniDataParserTest { - - public MaynoothUniDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getWeatherObservations method, of class MaynoothUniDataParser. - */ - @Test - public void testGetWeatherObservations_3args() throws Exception { - System.out.println("getWeatherObservations"); - - TimeZone timeZone = TimeZone.getTimeZone("Europe/Dublin"); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); - format.setTimeZone(timeZone); - - MaynoothUniDataParser instance = new MaynoothUniDataParser(); - ObjectMapper mapper = new ObjectMapper(); - // 2016-10-26: This is for printing out data from Mladen - for(int i=2006;i<=2016;i++) - { - String stationID = String.valueOf(i); - Date startDate = format.parse(stationID + "-05-01"); - List<WeatherObservation> result = instance.getWeatherObservations(stationID, timeZone, startDate); - - FileOutputStream out = new FileOutputStream(new File("/home/treinar/prosjekter/vips/projects/2016_irland/maynooth_" + stationID + ".json")); - mapper.writeValue(out, result); - assertNotNull(result); - } - //System.out.println("Number of observations=" + result.size()); - //result.stream().forEach((observation) -> System.out.println(observation)); - //assertNotNull(result); - - } - -} diff --git a/src/test/java/no/nibio/vips/util/weather/MetosAPIDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/MetosAPIDataParserTest.java deleted file mode 100644 index afeb9a16553379df4421a4b74806b1801096792b..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/MetosAPIDataParserTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather; - - -import java.io.IOException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import java.util.logging.Level; -import java.util.logging.Logger; -import no.nibio.vips.entity.WeatherObservation; -import org.apache.http.auth.AuthenticationException; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author treinar - */ -public class MetosAPIDataParserTest { - - public MetosAPIDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() throws NoSuchAlgorithmException, KeyStoreException { - - } - - @After - public void tearDown() { - } - - /** - * Test of getWeatherObservations method, of class MetosAPIDataParser. - */ - @Test - public void testGetWeatherObservations() throws Exception { - System.out.println("getWeatherObservations"); - String stationId = "000012DE"; - TimeZone timeZone = TimeZone.getTimeZone("Europe/Vilnius"); - Calendar cal = Calendar.getInstance(); - cal.setTimeZone(timeZone); - cal.set(2017,Calendar.MAY,20,0,0,0); - Date startDate = cal.getTime(); - MetosAPIDataParser instance = new MetosAPIDataParser(); - List<WeatherObservation> result = instance.getWeatherObservations(stationId, timeZone, startDate); - assertNotNull( result); - result.stream().forEach(obs -> System.out.println(obs.toString())); - - } - - /** - * Test of getAccessCode method, of class MetosAPIDataParser. - */ - //@Test - public void testGetAccessCode() { - try { - System.out.println("getAccessCode"); - MetosAPIDataParser instance = new MetosAPIDataParser(); - String expResult = ""; - String result = instance.getAccessCode(); - assertNotNull(result); - - } catch (IOException ex) { - Logger.getLogger(MetosAPIDataParserTest.class.getName()).log(Level.SEVERE, null, ex); - } - } - - //@Test - public void testGetToken() - { - System.out.println("getToken"); - try { - MetosAPIDataParser instance = new MetosAPIDataParser(); - - String result = instance.getToken(); - //System.out.println("access_token = " + result); - assertNotNull(result); - } catch (IOException | AuthenticationException ex) { - Logger.getLogger(MetosAPIDataParserTest.class.getName()).log(Level.SEVERE, null, ex); - } - } - - //@Test - public void testAPI(){ - System.out.println("testAPI"); - MetosAPIDataParser instance = new MetosAPIDataParser(); - try { - instance.testAPI(); - } catch (IOException ex) { - fail(ex.getMessage()); - } catch (AuthenticationException ex) { - Logger.getLogger(MetosAPIDataParserTest.class.getName()).log(Level.SEVERE, null, ex); - } - } - -} diff --git a/src/test/java/no/nibio/vips/util/weather/MetosDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/MetosDataParserTest.java deleted file mode 100755 index c00b6891052820adff7c07556f5177fca6b7f833..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/MetosDataParserTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2015 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather; - -import no.nibio.vips.util.weather.MetosRIMProDataParser; -import java.util.List; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; -import org.junit.Ignore; - -/** - * - * @author treinar - */ -public class MetosDataParserTest { - - public MetosDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getWeatherObservations method, of class MetosRIMProDataParser. - * Could be time consuming, so we test only when needed - */ - @Ignore // It appears that the current metos stations do not deliver data (as of 2016-01-14) - @Test - public void testGetWeatherObservations() throws Exception { - System.out.println("getWeatherObservations"); - String stationID = "00002085"; - MetosRIMProDataParser instance = new MetosRIMProDataParser(); - //List<WeatherObservation> expResult = null; - Integer expResult = 0; - List<WeatherObservation> result = instance.getWeatherObservations(stationID, TimeZone.getTimeZone("Europe/Sofia")); - assertNotNull(result); - assertNotSame(expResult, result.size()); - //System.out.println("Result sample: " + result.get(10).toString()); - - // Testing different station with different parameters - stationID = "00000428"; - result = instance.getWeatherObservations(stationID, TimeZone.getTimeZone("Europe/Sarajevo")); - assertNotNull(result); - assertNotSame(expResult, result.size()); - //System.out.println("Result sample: " + result.get(10).toString()); - /*for(WeatherObservation obs:result) - { - System.out.println(obs.toString()); - }*/ - } - -} diff --git a/src/test/java/no/nibio/vips/util/weather/USPestDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/USPestDataParserTest.java deleted file mode 100755 index a47ff992b2dd8585e14ad560fd888e76cd655a0c..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/USPestDataParserTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2016 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author treinar - */ -public class USPestDataParserTest { - - public USPestDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getWeatherObservations method, of class USPestDataParser. - */ - @Test - public void testGetWeatherObservations() throws Exception { - System.out.println("getWeatherObservations"); - String stationID = "FNWO3"; - TimeZone timeZone = TimeZone.getTimeZone("America/Los_Angeles"); - SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); - f.setTimeZone(timeZone); - Date startDate = f.parse("2016-10-01"); - USPestDataParser instance = new USPestDataParser(); - - List<WeatherObservation> result = instance.getWeatherObservations(stationID, timeZone, startDate); - //System.out.println("Result.size()=" + result.size()); - //result.stream().filter(obs->obs.getElementMeasurementTypeId().equals("RR")).forEach(obs->System.out.println(obs.toString())); - assertNotNull(result); - - } - -} diff --git a/src/test/java/no/nibio/vips/util/weather/dnmipointweb/DMIPointWebDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/dnmipointweb/DMIPointWebDataParserTest.java deleted file mode 100644 index 83155f8f01d9e5789f885385a6d5a6d251cfd132..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/dnmipointweb/DMIPointWebDataParserTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather.dnmipointweb; - -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.TimeZone; -import no.nibio.vips.entity.WeatherObservation; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author treinar - */ -public class DMIPointWebDataParserTest { - - public DMIPointWebDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getData method, of class DMIPointWebDataParser. - */ - @Test - public void testGetData() { - System.out.println("getData"); - DMIPointWebDataParser instance = new DMIPointWebDataParser(); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Copenhagen")); - cal.set(2000, Calendar.JULY, 5, 0, 0, 0); - Date dateFrom = cal.getTime(); - cal.set(Calendar.DATE, 15); - Date dateTo = cal.getTime(); - Double longitude = 10.0213; - Double latitude = 56.0199; - List<WeatherObservation> result = instance.getData(longitude, latitude, dateFrom, dateTo); - result.stream().forEach(r->System.out.println(r.toString())); - } - -} diff --git a/src/test/java/no/nibio/vips/util/weather/metnothredds/MetNoThreddsDataParserTest.java b/src/test/java/no/nibio/vips/util/weather/metnothredds/MetNoThreddsDataParserTest.java deleted file mode 100644 index 135ded529e59faf6a3703f0d631b099d8dab349d..0000000000000000000000000000000000000000 --- a/src/test/java/no/nibio/vips/util/weather/metnothredds/MetNoThreddsDataParserTest.java +++ /dev/null @@ -1,524 +0,0 @@ -/* - * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. - * - * This file is part of VIPSLogic. - * VIPSLogic is free software: you can redistribute it and/or modify - * it under the terms of the NIBIO Open Source License as published by - * NIBIO, either version 1 of the License, or (at your option) any - * later version. - * - * VIPSLogic is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * NIBIO Open Source License for more details. - * - * You should have received a copy of the NIBIO Open Source License - * along with VIPSLogic. If not, see <http://www.nibio.no/licenses/>. - * - */ -package no.nibio.vips.util.weather.metnothredds; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.vividsolutions.jts.geom.Coordinate; -import com.vividsolutions.jts.geom.Geometry; -import com.vividsolutions.jts.geom.GeometryFactory; -import com.vividsolutions.jts.geom.Polygon; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintWriter; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import no.nibio.vips.entity.ModelConfiguration; -import no.nibio.vips.entity.PointWeatherObservationList; -import no.nibio.vips.entity.Result; -import no.nibio.vips.entity.WeatherObservation; -import no.nibio.vips.logic.test.mock.MockModel; -import no.nibio.vips.model.ConfigValidationException; -import no.nibio.vips.model.ModelExcecutionException; -import no.nibio.vips.util.SystemUtil; -import no.nibio.vips.util.WeatherUtil; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; -import org.openjdk.jol.info.GraphLayout; -import org.wololo.geojson.Feature; -import org.wololo.geojson.FeatureCollection; -import org.wololo.jts2geojson.GeoJSONWriter; - -/** - * - * @author treinar - */ -public class MetNoThreddsDataParserTest { - - public MetNoThreddsDataParserTest() { - } - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - //@Test - public void testGetGridDataInPractice() - { - System.out.println("testGetGridDataInPractice"); - Coordinate[] coords = new Coordinate[5]; - /*coords[0] = new Coordinate(8.31,63.91); - coords[1] = new Coordinate(11.07,63.91); - coords[2] = new Coordinate(11.07,59.52); - coords[3] = new Coordinate(8.31,59.52); - coords[4] = new Coordinate(8.31,63.91);*/ - coords[0] = new Coordinate(-14.4103,71.8152); - coords[1] = new Coordinate(48.7212,71.8152); - coords[2] = new Coordinate(48.7212,51.7262); - coords[3] = new Coordinate(-14.4103,51.7262); - coords[4] = new Coordinate(-14.4103,71.8152); - GeometryFactory gFac = new GeometryFactory(); - Polygon pol = gFac.createPolygon(coords); - - List<String> weatherParameters = Arrays.asList("TM","RR","Q0","TX","TN","UM"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - Date start = new Date(); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.setTime(new Date()); - cal.add(Calendar.DATE, -25); - cal.set(Calendar.HOUR_OF_DAY, 5); - Date dateFrom = new WeatherUtil().normalizeToExactHour(cal.getTime(), TimeZone.getDefault()); - cal.add(Calendar.DATE, 27); - Date dateTo = cal.getTime(); - - List<PointWeatherObservationList> result = null; - - List<Result> modelResult = new ArrayList<>(); - try - { - result = instance.getGridData(pol, 10.0, dateFrom, dateTo, weatherParameters); - } - catch(TooMuchDataToAskForException ex) - { - Geometry[] manageableGeometries = instance.getManageableGeometries(pol,10.0, dateFrom, dateTo, weatherParameters); - System.out.println("The original polygon has been split into " + manageableGeometries.length + " parts"); - int counter = 0; - for(Geometry manageableGeometry:manageableGeometries) - { - counter++; - try - { - result = instance.getGridData(manageableGeometry, 10.0, dateFrom, dateTo, weatherParameters); - System.out.println("Batch #" + counter + " size=" + result.size()); - ModelConfiguration config = new ModelConfiguration(); - config.setConfigParameter("multiPointWeatherObservations", result); - config.setConfigParameter("timeZone","Europe/Oslo"); - MockModel mockModel = new MockModel(); - mockModel.setConfiguration(config); - modelResult = mockModel.getResult(); - // Simulate that we continue with the next iteration - // In a real setting, we'd need to store the modelResults in some - // permanent storage like files or DB (most likely) - result = null; config = null; mockModel = null; - System.gc(); - } - catch(TooMuchDataToAskForException | ConfigValidationException | ModelExcecutionException ex2) - { - fail(ex2.getMessage()); - } - } - } - assertNull(result); - System.out.println("Antall punkter=" + result.size()); - - Long timeSpent = new Date().getTime() - start.getTime(); - System.out.println("Time spent=" + (new Double(timeSpent)/1000) + " seconds"); - /*result.stream().forEach(mp->{ - System.out.println(mp); - //System.out.println(GraphLayout.parseInstance(mp).toPrintable()); - }); - */ - /*PointWeatherObservationList pwol = result.get(0); - WeatherObservation wo = pwol.getObservations().get(0); - System.out.println(ClassLayout.parseInstance(wo).toPrintable()); - System.out.println(GraphLayout.parseInstance(wo).toFootprint());*/ - System.out.println("Total memory used:\n" + GraphLayout.parseInstance(result).toFootprint()); - } - - @Test - public void testGetGridData() - { - try { - System.out.println("testGetGridData"); - Coordinate[] coords = new Coordinate[5]; - coords[0] = new Coordinate(8.31,63.91); - coords[1] = new Coordinate(11.07,63.91); - coords[2] = new Coordinate(11.07,59.52); - coords[3] = new Coordinate(8.31,59.52); - coords[4] = new Coordinate(8.31,63.91); - /* - coords[0] = new Coordinate(-14.4103,71.8152); - coords[1] = new Coordinate(48.7212,71.8152); - coords[2] = new Coordinate(48.7212,51.7262); - coords[3] = new Coordinate(-14.4103,51.7262); - coords[4] = new Coordinate(-14.4103,71.8152); - */ - GeometryFactory gFac = new GeometryFactory(); - Polygon pol = gFac.createPolygon(coords); - - List<String> weatherParameters = Arrays.asList("TM","RR","Q0","TX","TN","UM"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - Date start = new Date(); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.setTime(new Date()); - cal.add(Calendar.DATE, -25); - cal.set(Calendar.HOUR_OF_DAY, 5); - Date dateFrom = new WeatherUtil().normalizeToExactHour(cal.getTime(), TimeZone.getDefault()); - cal.add(Calendar.DATE, 27); - Date dateTo = cal.getTime(); - - - List<PointWeatherObservationList> result = instance.getGridData(pol, 10.0, dateFrom, dateTo, weatherParameters); - System.out.println("Antall punkter=" + result.size()); - assertNotNull(result); - Long timeSpent = new Date().getTime() - start.getTime(); - System.out.println("Time spent=" + (new Double(timeSpent)/1000) + " seconds"); - /*result.stream().forEach(mp->{ - System.out.println(mp); - //System.out.println(GraphLayout.parseInstance(mp).toPrintable()); - }); - */ - /*PointWeatherObservationList pwol = result.get(0); - WeatherObservation wo = pwol.getObservations().get(0); - System.out.println(ClassLayout.parseInstance(wo).toPrintable()); - System.out.println(GraphLayout.parseInstance(wo).toFootprint());*/ - System.out.println("Total memory used:\n" + GraphLayout.parseInstance(result).toFootprint()); - } catch (TooMuchDataToAskForException ex) { - ex.printStackTrace(); - fail(); - } - } - - //@Test - public void testGetManageableGeometries() - { - Double resolution = 10.0; - System.out.println("testGetManageableGeometries"); - Coordinate[] coords = new Coordinate[5]; - /*coords[0] = new Coordinate(8.31,63.91); - coords[1] = new Coordinate(11.07,63.91); - coords[2] = new Coordinate(11.07,59.52); - coords[3] = new Coordinate(8.31,59.52); - coords[4] = new Coordinate(8.31,63.91);*/ - coords[0] = new Coordinate(-14.4103,71.8152); - coords[1] = new Coordinate(48.7212,71.8152); - coords[2] = new Coordinate(48.7212,51.7262); - coords[3] = new Coordinate(-14.4103,51.7262); - coords[4] = new Coordinate(-14.4103,71.8152); - GeometryFactory gFac = new GeometryFactory(); - Polygon pol = gFac.createPolygon(coords); - - - List<String> weatherParameters = Arrays.asList("TM","RR", "Q0", "TX","TN","UM"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - Date start = new Date(); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.setTime(new Date()); - cal.add(Calendar.DATE, -50); - cal.set(Calendar.HOUR_OF_DAY, 5); - Date dateFrom = new WeatherUtil().normalizeToExactHour(cal.getTime(), TimeZone.getDefault()); - cal.add(Calendar.DATE, 52); - Date dateTo = cal.getTime(); - - - Geometry[] result = instance.getManageableGeometries(pol, resolution, dateFrom, dateTo, weatherParameters); - List<Feature> features = new ArrayList<>(); - GeoJSONWriter writer = new GeoJSONWriter(); - Map<String, Object> props = new HashMap<>(); - for(Geometry g:result) - { - System.out.println(g.toText()); - org.wololo.geojson.Geometry gj = writer.write(g); - features.add(new Feature(gj, props)); - } - FeatureCollection coll = new FeatureCollection(features.toArray(new Feature[0])); - try(PrintWriter out = new PrintWriter("/home/treinar/prosjekter/vips/projects/2017_SpotIT/grid_model/subgeomtest.geojson")){ - out.println(coll.toString()); - } - catch(FileNotFoundException ex) - { - ex.printStackTrace(); - } - try - { - ObjectMapper mp = new ObjectMapper(); - for(Geometry g:result) - { - List<PointWeatherObservationList> wData = instance.getGridData(g, 10.0, dateFrom, dateTo, weatherParameters); - System.out.println(mp.writeValueAsString(wData)); - } - } - catch(TooMuchDataToAskForException| JsonProcessingException ex) - { - ex.printStackTrace(); - } - - - } - - //@Test - public void testGetNumberOfWeatherObservationsRequested(){ - System.out.println("getNumberOfWeatherObservationsRequested"); - Coordinate[] coords = new Coordinate[5]; - coords[0] = new Coordinate(8.31,63.91); - coords[1] = new Coordinate(11.07,63.91); - coords[2] = new Coordinate(11.07,59.52); - coords[3] = new Coordinate(8.31,59.52); - coords[4] = new Coordinate(8.31,63.91); - GeometryFactory gFac = new GeometryFactory(); - Polygon pol = gFac.createPolygon(coords); - pol = (Polygon) pol.getEnvelope(); - - List<String> weatherParameters = Arrays.asList("TM","RR", "Q0", "TX","TN","UM"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.setTime(new Date()); - cal.add(Calendar.DATE, -25); - cal.set(Calendar.HOUR_OF_DAY, 5); - Date dateFrom = new WeatherUtil().normalizeToExactHour(cal.getTime(), TimeZone.getDefault()); - cal.add(Calendar.DATE, 27); - Date dateTo = cal.getTime(); - - - Long result = instance.getNumberOfWeatherObservationsRequested(pol, 10.0, dateFrom, dateTo, weatherParameters); - System.out.println("Weather observations requested=" + result); - } - - //@Test - public void testGetMemoryNeededForRequest(){ - System.out.println("getMemoryNeededForRequest"); - Coordinate[] coords = new Coordinate[5]; - /*coords[0] = new Coordinate(8.31,63.91); - coords[1] = new Coordinate(11.07,63.91); - coords[2] = new Coordinate(11.07,59.52); - coords[3] = new Coordinate(8.31,59.52); - coords[4] = new Coordinate(8.31,63.91);*/ - coords[0] = new Coordinate(-14.4103,71.8152); - coords[1] = new Coordinate(48.7212,71.8152); - coords[2] = new Coordinate(48.7212,51.7262); - coords[3] = new Coordinate(-14.4103,51.7262); - coords[4] = new Coordinate(-14.4103,71.8152); - GeometryFactory gFac = new GeometryFactory(); - Polygon pol = gFac.createPolygon(coords); - pol = (Polygon) pol.getEnvelope(); - - List<String> weatherParameters = Arrays.asList("TM","RR", "Q0", "TX","TN","UM"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.setTime(new Date()); - cal.add(Calendar.DATE, -25); - cal.set(Calendar.HOUR_OF_DAY, 5); - Date dateFrom = new WeatherUtil().normalizeToExactHour(cal.getTime(), TimeZone.getDefault()); - cal.add(Calendar.DATE, 27); - Date dateTo = cal.getTime(); - - - Long result = instance.getMemoryNeededForRequest(pol, 10.0, dateFrom, dateTo, weatherParameters); - System.out.println("Memory needed for request=" + result + " bytes"); - - System.out.println("presumableFreeMemory=" + SystemUtil.getPresumableFreeMemory()); - } - - //@Test - public void testGetNumberOfGridPointsInEnvelope()//(Polygon envelope, Double resolution) - { - System.out.println("getNumberOfGridPointsInEnvelope"); - Coordinate[] coords = new Coordinate[5]; - coords[0] = new Coordinate(8.31,63.91); - coords[1] = new Coordinate(11.07,63.91); - coords[2] = new Coordinate(11.07,59.52); - coords[3] = new Coordinate(8.31,59.52); - coords[4] = new Coordinate(8.31,63.91); - GeometryFactory gFac = new GeometryFactory(); - Polygon pol = gFac.createPolygon(coords); - pol = (Polygon) pol.getEnvelope(); - - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - Long result = instance.getNumberOfGridPointsInEnvelope(pol, 10.0); - - System.out.println("Number of grid points in envelope=" + result); - } - - /** - * Test of getPointData method, of class MetNoThreddsDataParser. - */ - //@Test - public void testGetPointData() { - System.out.println("getPointData"); - - Double longitude = 10.7946; - Double latitude = 59.6652; - - List<String> weatherParameters = Arrays.asList("TM","RR", "Q0", "TX","TN","UM"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - Date start = new Date(); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.setTime(new Date()); - cal.add(Calendar.DATE, -20); - cal.set(Calendar.HOUR_OF_DAY, 1); - Date dateFrom = new WeatherUtil().normalizeToExactHour(cal.getTime(), TimeZone.getDefault()); - cal.add(Calendar.DATE, 20); - Date dateTo = cal.getTime(); - - - List<WeatherObservation> result = instance.getPointData(longitude, latitude, dateFrom, dateTo, weatherParameters); - Long timeSpent = new Date().getTime() - start.getTime(); - System.out.println("Time spent=" + (new Double(timeSpent)/1000) + " seconds"); - - Collections.sort(result); - Long hoursIncluded = 1 + ((result.get(result.size()-1).getTimeMeasured().getTime() - result.get(0).getTimeMeasured().getTime()) / 3600000); - long expResult = weatherParameters.size() * hoursIncluded; - result.stream().forEach(obs->System.out.println(obs)); - assertEquals(expResult, result.size()); - assertTrue(result.size() > 10); - - } - - /** - * Shorthand for getting weather data for a specific application - */ - //@Test - public void testGetPointDataForAnneGrete() { - System.out.println("getPointData"); - - String[] placeNames = { - "2016_59.3916307_10.424473", - "2017_59.3859_10.433975", - "As", - "Saerheim", - "Ullensvang", - "Apelsvoll", - "Ramnes", - "Tjoelling", - "Kvithamar", - "Holt", - "Favang", - "Landvik" - }; - - Coordinate[] coords = { - new Coordinate(10.424473, 59.3916307), - new Coordinate(10.433975, 59.3859), - new Coordinate(10.781989, 59.660468), // Ås - new Coordinate(5.65078, 58.76053), // Særheim - new Coordinate(6.65381, 60.31853), // Ullensvang - new Coordinate(10.86952, 60.70024), // Apelsvoll - new Coordinate(10.23923, 59.38081), // Ramnes - new Coordinate(10.12513, 59.04641), // Tjølling - new Coordinate(10.87994, 63.48795), // Kvithamar - new Coordinate(18.90946, 69.65381), // Holt - new Coordinate(10.1872, 61.45822), // Fåvang - new Coordinate(8.522554, 58.340071) // Landvik - }; - - - - List<String> weatherParameters = Arrays.asList("TM","TX","TN"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - - Date start = new Date(); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.set(2016,Calendar.JANUARY,1, 0, 0, 0); - cal.set(Calendar.MILLISECOND, 0); - Date dateFrom = cal.getTime(); - cal.set(2017,Calendar.DECEMBER,31,23,59,0); - Date dateTo = cal.getTime(); - - List<WeatherObservation> result; - ObjectMapper mapper = new ObjectMapper(); - - for(int i=0;i<placeNames.length;i++) - { - System.out.println(placeNames[i]); - result = instance.getPointData(coords[i].x, coords[i].y, dateFrom, dateTo, weatherParameters); - Collections.sort(result); - File testOut = new File("/home/treinar/prosjekter/vips/projects/2017_gaardsvarsling/" + placeNames[i] + ".json"); - try(FileOutputStream oStream = new FileOutputStream(testOut);) - { - - - mapper.writeValue(oStream, result); - } - catch(IOException ex) - { - ex.printStackTrace(); - } - - } - Long timeSpent = new Date().getTime() - start.getTime(); - System.out.println("Time spent=" + (new Double(timeSpent)/1000) + " seconds"); - - } - - @Test - public void testFindVIPSParamNameFromMetParamName() - { - System.out.println("testFindVIPSParamNameFromMetParamName"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - String expResult = "TM"; - String result = instance.findVIPSParamNameFromMetParamName("air_temperature_2m"); - assertEquals(expResult, result); - result = instance.findVIPSParamNameFromMetParamName("foo_bar_1000m"); - assertNull(result); - } - - @Test - public void testGetArchiveDates() - { - System.out.println("testGetArchiveDates"); - MetNoThreddsDataParser instance = new MetNoThreddsDataParser(); - Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Oslo")); - cal.setTime(new Date()); - cal.add(Calendar.DATE, -2); - cal.set(Calendar.HOUR_OF_DAY, 0); - Date dateFrom = new WeatherUtil().normalizeToExactHour(cal.getTime(), TimeZone.getDefault()); - cal.add(Calendar.DATE, 5); - List<Date> result = instance.getArchiveDates(dateFrom, cal.getTime()); - SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HHZ"); - f.setTimeZone(TimeZone.getTimeZone("UTC")); - result.stream().forEach(r->System.out.println(f.format(r))); - assertEquals(4,result.size()); - - } - -}