diff --git a/pom.xml b/pom.xml index 0210e2630a8fc6af4f12e0b40d5e817c83485ab3..100b16589429bd146d1770ca425b4cb735aea8eb 100755 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,6 @@ <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.4.1</version> - <scope>provided</scope> </dependency> <dependency> <groupId>commons-validator</groupId> @@ -39,7 +38,6 @@ <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.4.1</version> - <scope>provided</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> @@ -114,6 +112,23 @@ <target>1.8</target> </configuration> </plugin> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <descriptorRefs> + <descriptorRef>jar-with-dependencies</descriptorRef> + </descriptorRefs> + </configuration> + <executions> + <execution> + <id>make-assembly</id> <!-- this is used for inheritance merges --> + <phase>package</phase> <!-- bind to the packaging phase --> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin> </plugins> </build> </project> diff --git a/src/main/java/no/nibio/vips/util/test/WeatherDataFileReader.java b/src/main/java/no/nibio/vips/util/test/WeatherDataFileReader.java new file mode 100644 index 0000000000000000000000000000000000000000..8f6917ab09dcc31cfebd7cd3d0ac84c3419386fb --- /dev/null +++ b/src/main/java/no/nibio/vips/util/test/WeatherDataFileReader.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2017 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon 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. + * + * VIPSCommon 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 VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.util.test; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.MappingJsonFactory; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import no.nibio.vips.entity.ModelConfiguration; +import no.nibio.vips.entity.WeatherObservation; +import no.nibio.vips.model.ConfigValidationException; + +/** + * @copyright 2017 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class WeatherDataFileReader { + + /** + * + * @param weatherDataFileName filename and path on classpath + * @param modelId 10 chars ID of model + * @return A Model Configuration with weather data. Can be filled with other configuration data + * @throws ConfigValidationException + */ + public ModelConfiguration getModelConfigurationWithWeatherData(String weatherDataFileName, String modelId) throws ConfigValidationException + { + try { + ModelConfiguration config = new ModelConfiguration(); + config.setModelId(modelId); + BufferedInputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream(weatherDataFileName)); + //InputStream i = this.getClass().getResourceAsStream(weatherDataFileName); + JsonFactory f = new MappingJsonFactory(); + JsonParser jp = f.createParser(inputStream); + JsonNode all = jp.readValueAsTree(); + List<WeatherObservation> observations = new ArrayList<>(); + ObjectMapper mapper = new ObjectMapper(); + + Date firstDate = null; + Date lastDate = null; + if(all.isArray()) + { + for(JsonNode node : all){ + Date timeMeasured = (Date)mapper.convertValue(node.get("timeMeasured").asText(), new TypeReference<Date>(){}); + if(firstDate == null || firstDate.compareTo(timeMeasured) > 0) + { + firstDate = timeMeasured; + } + if(lastDate == null || lastDate.compareTo(timeMeasured) < 0) + { + lastDate = timeMeasured; + } + //System.out.println(node.toString()); + WeatherObservation observation = new WeatherObservation(); + observation.setTimeMeasured(timeMeasured); + observation.setLogIntervalId(node.get("logIntervalId").asInt()); + observation.setElementMeasurementTypeId(node.get("elementMeasurementTypeId").asText()); + observation.setValue(node.get("value").asDouble()); + observations.add(observation); + } + + } + else + { + throw new ConfigValidationException("Data input from file is not a JSON array"); + } + config.setConfigParameter("observations", observations); + + + return config; + } catch (IOException ex) { + ex.printStackTrace(); + throw new ConfigValidationException(ex.getMessage()); + } + } +}