ToOMS_Reformat_V1_0.java [src/java/m/prms/conv] Revision: Date:
package m.prms.conv;
import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ws.rs.Path;
import oms3.io.CSProperties;
import oms3.io.CSTable;
import oms3.io.DataIO;
/**
* PRMS/OMS Resource conversion.
*
* @author od
*/
@Name("PRMS")
@Description("Parameter Reformatting: Curly Brackets to Tables. Converter. OMS -> PRMS")
@State(State.UNSTABLE)
@Author(org = "USGS")
@Author(org = "CSU")
@Category("Hydrology")
@Path("m/oms_to_oms/params/format/1.0")
public class ToOMS_Reformat_V1_0 extends ModelDataService {
static final String FILE = "file";
@Override
protected void doProcess() throws Exception {
File f = formdata().getFile(FILE);
String name = f.getName();
if (name.endsWith(".csv")) {
File result = new File(f.getParentFile(), name.substring(0, name.indexOf('.')) + "-tables.csv");
CSProperties p = readParameter(f);
writeParameterFile(result, p);
results().put(result);
} else {
throw new ServiceException("Error: cannot convert: " + f);
}
}
static void writeParameterFile(File file, CSProperties p) throws FileNotFoundException {
CSProperties out = DataIO.properties();
out.setName("Parameter");
out.getInfo().putAll(p.getInfo());
out.getInfo().put(DataIO.KEY_MODIFIED_AT, new Date().toString());
List<CSTable> t = new ArrayList<>();
for (String key : p.keySet()) {
// System.out.println("key " + key);
if (DataIO.playsRole(p, key, "dimension")) {
// System.out.println("Dim " + key);
CSTable ta = DataIO.toTable(p, key);
if (ta != null) {
t.add(ta);
}
}
int dim = DataIO.getBound(p, key);
switch (dim) {
case 0:
DataIO.copyProperty(key, p, out);
break;
// they will go into a table
case 1:
case 2:
}
}
List<String> keys2D = DataIO.keysForBounds(p, 2);
for (String key : keys2D) {
CSTable t2 = DataIO.as2DTable(p, key);
t.add(t2);
}
PrintWriter w = new PrintWriter(file);
DataIO.print(out, w);
w.println();
for (CSTable tb : t) {
DataIO.print(tb, w);
w.println();
}
w.close();
}
static CSProperties readParameter(File file) throws IOException {
CSProperties p = DataIO.properties();
if (DataIO.propertyExists("Parameter", file)) {
// original properties.
p.putAll(DataIO.properties(new FileReader(file), "Parameter"));
}
// check for tables in the file.
List<String> tables = DataIO.tables(file);
if (!tables.isEmpty()) {
for (String name : tables) {
CSTable t = DataIO.table(file, name);
// convert them to Properties.
CSProperties prop = DataIO.toProperties(t);
//prop.().put("file", f);
p.putAll(prop);
}
}
return p;
}
public static void main(String[] args) throws IOException, ParseException {
// String b = "/od/oms/oms_examples/oms3.prj.prms/data/mixed_params.csv";
// String b1 = "/tmp/mixed_params.param";
//
// CSProperties p = readParameter(new File(b));
// writeParamFile(p, new File(b1));
}
}