I have a .CSV file containing weather Data for the whole year, I'd like to calculate the values needed for every day and print it to another .CSV file .
The .csv file looks like this :
stationid datestamp wgust wspeedavg wdir wsector alarmamber alarmred
FX32 31-12-19 23:52 20 0 155 0 0 0
FX32 31-12-19 23:50 18.99 0 159 0 0 0
FX32 31-12-19 23:48 18.01 0 154 0 0 0
FX32 31-12-19 23:42 18.99 0 152 0 0 0
FX32 31-12-19 23:40 18.99 0 151 0 0 0
FX32 30-12-19 23:30 18.99 0 144 0 0 0
FX32 30-12-19 23:28 18.99 0 147 0 0 0
FX32 30-12-19 23:22 20 0 135 0 0 0
What I have tried:
I tried this code so far , it can read the file and calculate total AVG/Min/Max. Is there a way i can specify to do this day by day ?
package sortdata;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Scanner;
import javax.xml.crypto.Data;
import org.apache.commons.collections.iterators.ObjectArrayListIterator;
public class Main {
@SuppressWarnings({ "rawtypes", "unchecked", "oracle.jdeveloper.java.nested-assignment" })
public static void main(String[] args) throws IOException, CsvDataTypeMismatchException,
CsvRequiredFieldEmptyException {
String fileName = "C:\\JDeveloper\\wdata.csv";
Path myPath = Paths.get(fileName);
ArrayList<String> d = new ArrayList<String>();
convertDegreeToCardinalDirection direction = new convertDegreeToCardinalDirection();
try (BufferedReader br = Files.newBufferedReader(myPath, StandardCharsets.UTF_8)) {
HeaderColumnNameMappingStrategy<Read> strategy = new HeaderColumnNameMappingStrategy<>();
strategy.setType(Read.class);
CsvToBean toBean = new CsvToBeanBuilder(br).withType(Read.class)
.withMappingStrategy(strategy)
.withIgnoreLeadingWhiteSpace(true)
.build();
List<Read> Data = toBean.parse();
final DoubleSummaryStatistics Wind = Data.stream()
.mapToDouble(Read::getWgust)
.summaryStatistics();
final double averageWind = Wind.getAverage();
final double Windmin = Wind.getMin();
final double Windmax = Wind.getMax();
final int Windtotal = (int) Wind.getCount();
final IntSummaryStatistics WindDIR = Data.stream()
.mapToInt(Read::getWdir)
.summaryStatistics();
final double averageWdir = WindDIR.getAverage();
final int maxWdir = WindDIR.getMax();
final int minWdir = WindDIR.getMin();
System.out.println("Total Number of Values : " + Windtotal);
System.out.println("AverageWindSpeed: " + averageWind);
System.out.println("Minimum WIndSpeed: " + Windmin);
System.out.println("Maximum WIndSpeed: " + Windmax);
System.out.println("AverageWindDir: " + averageWdir);
System.out.println("Minimum WIndDir: " + minWdir);
System.out.println("Maximum WIndDir: " + maxWdir);
Writer writer = new FileWriter("Datasorter1.csv");
StatefulBeanToCsv beanToCsv = new StatefulBeanToCsvBuilder(writer).build();
beanToCsv.write(Wind);
writer.close();
}
}
}
Also have a Class Read wich contains the following :
public class Read {
@CsvBindByName(column = "stationid", required = true)
private String stationid;
@CsvBindByName(column = "datestamp", required = true)
@CsvDate("dd-MM-yy HH:mm")
private Date datestamp;
@CsvBindByName(column = "wgust", required = true)
private double wgust;
@CsvBindByName(column = "wdir", required = true)
private int wdir;
private PropertyChangeSupport _propertyChangeSupport = new PropertyChangeSupport(this);
public void setStationid(String stationid) {
String oldStationid = this.stationid;
this.stationid = stationid;
_propertyChangeSupport.firePropertyChange("stationid", oldStationid, stationid);
}
public String getStationid() {
return stationid;
}
public void setDatestamp(Date datestamp) {
Date oldDatestamp = this.datestamp;
this.datestamp = datestamp;
_propertyChangeSupport.firePropertyChange("datestamp", oldDatestamp, datestamp);
}
public Date getDatestamp() {
return datestamp;
}
public void setWgust(double wgust) {
double oldWgust = this.wgust;
this.wgust = wgust;
_propertyChangeSupport.firePropertyChange("wgust", oldWgust, wgust);
}
public double getWgust() {
return wgust;
}
public void setWdir(int wdir) {
int oldWdir = this.wdir;
this.wdir = wdir;
_propertyChangeSupport.firePropertyChange("wdir", oldWdir, wdir);
}
public int getWdir() {
return wdir;
}
public void addPropertyChangeListener(PropertyChangeListener l) {
_propertyChangeSupport.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
_propertyChangeSupport.removePropertyChangeListener(l);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Station{id=")
.append(stationid)
.append(", Date=")
.append(datestamp)
.append(", Wspeed=")
.append(wgust)
.append(", Direction=")
.append(wdir)
.append("}");
return builder.toString();
}
I would appreciate every help (sample code if possible), i do not have lot of experience in coding, and this would be a on of job.