WeatherStatistics.java [src/java/util] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package util;
import csip.Config;
import csip.utils.Parallel;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
*
* @author sidereus
*/
public class WeatherStatistics {
double[] avgPrecip = new double[12];
double[] stdPrecip = new double[12];
double[] skwPrecip = new double[12];
double[] tmin = new double[12];
double[] tmax = new double[12];
public void compute(Stream<String> climateDataStream) throws IOException, Exception {
List<String> tstream = climateDataStream.collect(Collectors.toList());
Parallel.run(Config.getBoolean("ghg.serial.climateStats", false),
() -> {
precipitationStats(tstream);
},
() -> {
tminStats(tstream);
},
() -> {
tmaxStats(tstream);
}
);
}
private void precipitationStats(List<String> tstream) throws IOException {
for (int i = 1; i < 13; i++) { // 12 months
String m = Integer.toString(i);
DoubleStatistics value = tstream.stream().map(str -> str.split("\\s+"))
.filter(str -> str[1].equals(m))
.map(str -> new WeatherDailyValue(str[2], str[6]))
.collect(Collectors.groupingBy(WeatherDailyValue::getYear,
Collectors.summingDouble(WeatherDailyValue::getPrecip)))
.values()
.stream()
.collect(
DoubleStatistics::new,
DoubleStatistics::accept,
DoubleStatistics::combine);
avgPrecip[i - 1] = value.getAverage();
stdPrecip[i - 1] = value.getStandardDeviation();
skwPrecip[i - 1] = value.getSkewness();
}
}
private void tmaxStats(List<String> tstream) throws IOException {
for (int i = 1; i < 13; i++) { // 12 months
String m = Integer.toString(i);
double value = tstream.stream().map(str -> str.split("\\s+"))
.filter(str -> str[1].equals(m))
.mapToDouble(val -> Double.parseDouble(val[4]))
.average().orElse(Double.NaN);
tmax[i - 1] = value;
}
}
private void tminStats(List<String> tstream) throws IOException {
for (int i = 1; i < 13; i++) { // 12 months
String m = Integer.toString(i);
double value = tstream.stream().map(str -> str.split("\\s+"))
.filter(str -> str[1].equals(m))
.mapToDouble(val -> Double.parseDouble(val[5]))
.average().orElse(Double.NaN);
tmin[i - 1] = value;
}
}
public double getAvgPrecip(int month) {
return avgPrecip[month];
}
public double getStdPrecip(int month) {
return stdPrecip[month];
}
public double getSkwPrecip(int month) {
return skwPrecip[month];
}
public double getMonthlyTmin(int month) {
return tmin[month];
}
public double getMonthlyTmax(int month) {
return tmax[month];
}
}