Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmerz committed Jan 28, 2016
2 parents 2b52ad0 + 62f20a0 commit a5ddc5f
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 18 deletions.
26 changes: 15 additions & 11 deletions java/src/edu/ucdavis/watershed/CMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ public class CMD {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
CommandLineInput input = mapper.readValue(new File(args[0]), CommandLineInput.class);
//try {
HecDss dssFile = Dss.open(input.getPath());
for( Config config: input.getData() ) {
CsvData data = Csv.parseCsv(config.getCsvFilePath(), config.getType());
Dss.write(config, data, dssFile, input.getPath());

HecDss dssFile = Dss.open(input.getPath());

if( input.isExport() ) {
String root = "";
if( input.getExportRoot() != null ) {
root = input.getExportRoot();
}

//} catch (Exception e) {
// printError(e.getMessage());
// return;
//}
Dss.exportJson(dssFile, root, input.getRegex());
return;
}

for( Config config: input.getData() ) {
CsvData data = Csv.parseCsv(config.getCsvFilePath(), config.getType());
Dss.write(config, data, dssFile, input.getPath());
}

printSuccess();
}
Expand Down
29 changes: 29 additions & 0 deletions java/src/edu/ucdavis/watershed/CommandLineInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

public class CommandLineInput {

public boolean export = false; // are we exporting;
public String exportRoot = "";
public String path = ""; // dss file path
public LinkedList<Config> data = null;
public String regex = "";

public CommandLineInput() {}

Expand All @@ -24,4 +27,30 @@ public LinkedList<Config> getData() {
public void setData(LinkedList<Config> data) {
this.data = data;
}

public boolean isExport() {
return export;
}

public void setExport(boolean export) {
this.export = export;
}

public String getExportRoot() {
return exportRoot;
}

public void setExportRoot(String exportRoot) {
this.exportRoot = exportRoot;
}

public String getRegex() {
return regex;
}

public void setRegex(String regex) {
this.regex = regex;
}


}
14 changes: 14 additions & 0 deletions java/src/edu/ucdavis/watershed/CwsContainer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.ucdavis.watershed;

import java.util.LinkedList;

import hec.io.PairedDataContainer;
import hec.io.TimeSeriesContainer;

public class CwsContainer {

public TimeSeriesContainer timeSeriesContainer = null;
public PairedDataContainer pairedDataContainer = null;
public LinkedList<String> dates = new LinkedList<String>();

}
96 changes: 95 additions & 1 deletion java/src/edu/ucdavis/watershed/Dss.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package edu.ucdavis.watershed;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Vector;

import com.fasterxml.jackson.databind.ObjectMapper;

import hec.data.meta.Catalog;
import hec.dssgui.CombinedDataManager;
import hec.heclib.dss.CondensedReference;
import hec.heclib.dss.HecDSSUtilities;
import hec.heclib.dss.HecDss;
import hec.io.DataContainer;
import hec.io.PairedDataContainer;
import hec.io.TimeSeriesContainer;

import java.io.File;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -115,7 +123,93 @@ public static void writeTimeSeriesData(Config config, CsvData csv, HecDss dssFil
dssFile.put(ts);
}

@SuppressWarnings("deprecation")
public static void exportJson(HecDss dssFile, String directory, String regex) throws Exception {
int count = 0;

HashMap<String, HashMap<String,Integer>> map = new HashMap<String, HashMap<String,Integer>>();
Vector<CondensedReference> v = dssFile.getCondensedCatalog();
ObjectMapper mapper = new ObjectMapper();

cleanDirectory(new File(directory));
new File(directory).mkdir();

for( CondensedReference path: v) {

CwsContainer ts = new CwsContainer();
DataContainer dc;
String name;
String parameter;

try {
String hecpath = path.getNominalPathname();

if( !regex.contentEquals("") && regex != null ) {
if( !hecpath.matches(regex) ) continue;
}

dc = dssFile.get(hecpath, true);

try {


ts.timeSeriesContainer = (TimeSeriesContainer) dc;
name = ts.timeSeriesContainer.getLocationName();
parameter = ts.timeSeriesContainer.getParameterName();

if( ts.timeSeriesContainer.times == null ) {
System.out.println("Ignoring: "+path.getNominalPathname());
continue;
}

for( int i = 0; i < ts.timeSeriesContainer.times.length; i++ ) {
ts.dates.add(Dss.calcDate(ts.timeSeriesContainer.times[i]));
}
} catch(Exception e) {
try {
ts.pairedDataContainer = (PairedDataContainer) dc;
parameter = "pairedData";
name = ts.pairedDataContainer.location;
} catch(Exception e2) {
continue;
}
}
} catch(Exception e2) {
continue;
}

mapper.writeValue(new File(directory+File.separatorChar+count+".json"), ts);

if( !map.containsKey(name) ) {
map.put(name, new HashMap<String, Integer>());
}
map
.get(name)
.put(parameter, count);

count += 1;
}

mapper.writeValue(new File(directory+File.separatorChar+"index.json"), map);
}

public static boolean cleanDirectory(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = cleanDirectory(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}

public static String calcDate(int time) {
LocalDateTime ldt = EPOCH.plusMinutes((long) time);
return ldt.toString();
}

public static int calcTime(String date, boolean start) {
String[] parts = date.split("-");

Expand Down
26 changes: 20 additions & 6 deletions java/src/edu/ucdavis/watershed/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,38 @@ public static void main(String[] args) throws Exception {
int t= 0;
HecDss dssFile = Dss.open("E:\\S09I05\\S09I05TS.dss");

Dss.exportJson(dssFile, "E:\\S09I05", "");

// JM
//HecDss dssFile = Dss.open("E:\\wy2002_jTS.dss");

/*Vector<String> v = dssFile.getPathnameList();
/* Vector<String> v = dssFile.getPathnameList();
//Vector<String> v = dssFile.getCatalogedPathnames();
for( String path: v) {
System.out.println(path);
// TimeSeriesContainer ts = (TimeSeriesContainer) dssFile.get(path);
t++;
CwsTimeSeriesContainer ts = new CwsTimeSeriesContainer();
ts.timeSeriesContainer = (TimeSeriesContainer) dssFile.get(path);
for( int i = 0; i < ts.timeSeriesContainer.times.length; i++ ) {
ts.dates.add(Dss.calcDate(ts.timeSeriesContainer.times[i]));
}
String jsonInString = mapper.writeValueAsString(ts);
System.out.println(jsonInString);
mapper.writeValue(new File("E:\\S09I05\\test.json"), ts);
break;
//t = 1;
}
System.out.print(t); */
System.out.print(t);*/
//TimeSeriesContainer ts = (TimeSeriesContainer) dssFile.get("/HEXT2014/C131_SINK G12/FLOW_LOSS(KAF)/01JAN1920/1MON/CALSIM - D889/",true);
//TimeSeriesContainer ts = (TimeSeriesContainer) dssFile.get("/S09I04/GW-01/STOR/01JAN1920 - 01JAN2000/1MON//",true);

// JM
//TimeSeriesContainer ts = (TimeSeriesContainer) dssFile.get("/INIT/SR_BLB/STOR/01JAN1920 - 01JAN2000/1MON//",true);
TimeSeriesContainer ts = (TimeSeriesContainer) dssFile.get("//C29/FLOW_LOC(KAF)/01JAN1920 - 01JAN2000/1MON/DEFAULT/",true);
System.out.print(dssFile.getStartTime());
//TimeSeriesContainer ts = (TimeSeriesContainer) dssFile.get("//C29/FLOW_LOC(KAF)/01JAN1920 - 01JAN2000/1MON/DEFAULT/",true);
//System.out.print(dssFile.getStartTime());
/*for( int i = 0; i < ts.times.length; i++ ) {
System.out.println(ts.times[i]);
}*/
Expand Down

0 comments on commit a5ddc5f

Please sign in to comment.