SciEnergyResultSet.java [src/usda/weru/weps/reports/query] Revision: default Date:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package usda.weru.weps.reports.query;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Types;
import java.util.logging.Logger;
import usda.weru.util.ConversionCalculator;
import usda.weru.util.ConversionCalculator.ConversionNotFoundException;
import usda.weru.util.ConversionCalculator.UnitNotFoundException;
import usda.weru.util.Util;
/**
*
* @author joelevin
*/
public class SciEnergyResultSet extends WepsResultSet {
private static final Logger LOGGER = Logger.getLogger(SciEnergyResultSet.class.getName());
// Names of the table and columns in the SQL data model.
public static final String NAME = "sci_energy";
public static final String OUTPUT_FILE = "sci_energy.out";
public static final String COLUMN_PREFIX = NAME + ".";
public static final String COLUMN_SCI = COLUMN_PREFIX + "sci";
public static final String COLUMN_ENERGY = COLUMN_PREFIX + "energy";
public static final String COLUMN_OMFACTOR = COLUMN_PREFIX + "omfactor";
public static final String COLUMN_ERFACTOR = COLUMN_PREFIX + "erfactor";
public static final String COLUMN_FOFACTOR = COLUMN_PREFIX + "fofactor";
public static final String COLUMN_RENNEROM = COLUMN_PREFIX + "rennerom";
public static final String COLUMN_RENNERWATER = COLUMN_PREFIX + "rennerwater";
public static final String COLUMN_RENNERWIND = COLUMN_PREFIX + "rennerwind";
public static final String COLUMN_RENNERSTIR = COLUMN_PREFIX + "rennerstir";
public static final String COLUMN_AVGBIOMASS = COLUMN_PREFIX + "avgbiomass";
public static final String COLUMN_WINDEROS = COLUMN_PREFIX + "winderos";
public static final String COLUMN_WATEREROS = COLUMN_PREFIX + "watereros";
public static final String COLUMN_AVGALLSTIR = COLUMN_PREFIX + "avgallstir";
public static final String COLUMN_TEXTUREMULT = COLUMN_PREFIX + "texturemult";
// private final WepsConnection c_con;
// private boolean c_filled;
// New member variables
public String m_sDirectory;
public String m_sUnits;
public SciEnergyResultSet(WepsConnection con, String sDirectory, String sUnits) {
super(con);
m_sDirectory = sDirectory;
m_sUnits = sUnits;
// c_con = con;
// addColumn(RunsResultSet.COLUMN_RUNID, Types.INTEGER, 10, 0);
//Columns expected in the sci file
addColumn(COLUMN_SCI, Types.DOUBLE, 10, 5);
addColumn(COLUMN_ENERGY, Types.DOUBLE, 10, 5);
addColumn(COLUMN_OMFACTOR, Types.DOUBLE, 10, 5);
addColumn(COLUMN_ERFACTOR, Types.DOUBLE, 10, 5);
addColumn(COLUMN_FOFACTOR, Types.DOUBLE, 10, 5);
addColumn(COLUMN_RENNEROM, Types.DOUBLE, 10, 5);
addColumn(COLUMN_RENNERWATER, Types.DOUBLE, 10, 5);
addColumn(COLUMN_RENNERWIND, Types.DOUBLE, 10, 5);
addColumn(COLUMN_RENNERSTIR, Types.DOUBLE, 10, 5);
addColumn(COLUMN_AVGBIOMASS, Types.DOUBLE, 10, 5);
addColumn(COLUMN_WINDEROS, Types.DOUBLE, 10, 5);
addColumn(COLUMN_WATEREROS, Types.DOUBLE, 10, 5);
addColumn(COLUMN_AVGALLSTIR, Types.DOUBLE, 10, 5);
addColumn(COLUMN_TEXTUREMULT, Types.DOUBLE, 10, 5);
}
public String getName() {
return NAME;
}
protected boolean isUSUnits(){
return Util.USUnits.equals(m_sUnits);
}
public synchronized void fill() {
// if (c_filled) {
// return;
// }
// File[] files = c_con.getRunFiles();
// //loop over all the runs
int i = 0;
// for (int i = 0; i < files.length; i++) {
File runDir = new File(m_sDirectory);
File output = new File(runDir, OUTPUT_FILE);
if (!output.exists()) {
//no sci_energy file, might be an old run
assert(false): "error: missing file" + OUTPUT_FILE ;
// continue;
}
Object[] row = createNewRow(false);
// setRowValue(row, RunsResultSet.COLUMN_RUNID, i);
//Stir energy file specific
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(output));
String line;
String[] values;
//first line
line = getLine(reader);
if (line == null){
return;
}
values = line.split("\\|", -1);
if (values.length == 2){
try{
Double sci = Double.valueOf(values[0].trim());
setRowValue(row, COLUMN_SCI, sci);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse sci value in file: " + output.getAbsolutePath());
}
try{
Double energy = Double.valueOf(values[1].trim());
if (isUSUnits()){
energy = ConversionCalculator.convert(energy, "L/ha", "gal/ac");
}
setRowValue(row, COLUMN_ENERGY, energy);
}
catch(UnitNotFoundException unf){
LOGGER.warning("Unable to convert energy value in file: " + output.getAbsolutePath());
}
catch(ConversionNotFoundException cnf){
LOGGER.warning("Unable to convert energy value in file: " + output.getAbsolutePath());
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse energy value in file: " + output.getAbsolutePath());
}
}
else{
LOGGER.severe("Unexpected number of values on line 1 in file: " + output.getAbsolutePath());
}
//second line
line = getLine(reader);
values = line.split("\\|", -1);
if (values.length == 3){
try{
Double om = Double.valueOf(values[0].trim());
setRowValue(row, COLUMN_OMFACTOR, om);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_OMFACTOR + " value in file: " + output.getAbsolutePath());
}
try{
Double er = Double.valueOf(values[1].trim());
setRowValue(row, COLUMN_ERFACTOR, er);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_ERFACTOR + " value in file: " + output.getAbsolutePath());
}
try{
Double fo = Double.valueOf(values[2].trim());
setRowValue(row, COLUMN_FOFACTOR, fo);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_FOFACTOR + " value in file: " + output.getAbsolutePath());
}
}
else{
LOGGER.severe("Unexpected number of values on line 2 in file: " + output.getAbsolutePath());
}
//third line
line = getLine(reader);
values = line.split("\\|", -1);
if (values.length == 4){
try{
Double rennerom = Double.valueOf(values[0].trim());
setRowValue(row, COLUMN_RENNEROM, rennerom);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_RENNEROM + " value in file: " + output.getAbsolutePath());
}
try{
Double rennerwater = Double.valueOf(values[1].trim());
setRowValue(row, COLUMN_RENNERWATER, rennerwater);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_RENNERWATER + " value in file: " + output.getAbsolutePath());
}
try{
Double rennerwind = Double.valueOf(values[2].trim());
setRowValue(row, COLUMN_RENNERWIND, rennerwind);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_RENNERWIND + " value in file: " + output.getAbsolutePath());
}
try{
Double rennerstir = Double.valueOf(values[3].trim());
setRowValue(row, COLUMN_RENNERSTIR, rennerstir);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_RENNERSTIR + " value in file: " + output.getAbsolutePath());
}
}
else{
LOGGER.severe("Unexpected number of values on line 3 in file: " + output.getAbsolutePath());
}
//fourth line
line = getLine(reader);
values = line.split("\\|", -1);
if (values.length == 4){
try{
Double avgbiomass = Double.valueOf(values[0].trim());
setRowValue(row, COLUMN_AVGBIOMASS, avgbiomass);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_AVGBIOMASS + " value in file: " + output.getAbsolutePath());
}
try{
Double winderos = Double.valueOf(values[1].trim());
if (isUSUnits()){
winderos = ConversionCalculator.convert(winderos, "kg/m^2", "t/ac");
}
setRowValue(row, COLUMN_WINDEROS, winderos);
}
catch(UnitNotFoundException unf){
LOGGER.warning("Unable to convert " + COLUMN_WINDEROS + " value in file: " + output.getAbsolutePath());
}
catch(ConversionNotFoundException cnf){
LOGGER.warning("Unable to convert " + COLUMN_WINDEROS + " value in file: " + output.getAbsolutePath());
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_WINDEROS + " value in file: " + output.getAbsolutePath());
}
try{
Double watereros = Double.valueOf(values[2].trim());
if (isUSUnits()){
watereros = ConversionCalculator.convert(watereros, "kg/m^2", "t/ac");
}
setRowValue(row, COLUMN_WATEREROS, watereros);
}
catch(UnitNotFoundException unf){
LOGGER.warning("Unable to convert " + COLUMN_WINDEROS + " value in file: " + output.getAbsolutePath());
}
catch(ConversionNotFoundException cnf){
LOGGER.warning("Unable to convert " + COLUMN_WINDEROS + " value in file: " + output.getAbsolutePath());
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_WATEREROS + " value in file: " + output.getAbsolutePath());
}
try{
Double avgallstir = Double.valueOf(values[3].trim());
setRowValue(row, COLUMN_AVGALLSTIR, avgallstir);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse " + COLUMN_AVGALLSTIR + " value in file: " + output.getAbsolutePath());
}
}
else{
LOGGER.severe("Unexpected number of values on line 4 in file: " + output.getAbsolutePath());
}
//fifth line
line = getLine(reader);
values = line.split("\\|", -1);
if (values.length == 1){
try{
Double texturemult = Double.valueOf(values[0].trim());
setRowValue(row, COLUMN_TEXTUREMULT, texturemult);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse sci value in file: " + output.getAbsolutePath());
}
}
else{
LOGGER.severe("Unexpected number of values on line 5 in file: " + output.getAbsolutePath());
}
} catch (IOException ioe) {
LOGGER.severe("Error reading file: " + output.getAbsolutePath() + ioe.getMessage());
// throw new SQLException("Error reading file: " + output.getAbsolutePath(), ioe);
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
LOGGER.severe("Error closing file: " + output.getAbsolutePath() + e.getMessage());
}
}
// c_filled = true;
}
// }
addRow(row);
}
//Skip comments and blank lines
private String getLine(BufferedReader in) throws IOException {
String temp;
while ((temp = in.readLine()) != null) {
temp = temp.trim();
if (temp.length() == 0) {
//blank line
continue;
}
if (temp.charAt(0) != '#') {
//not a comment
return temp;
}
}
return null;
}
public static String getDisplayUnits(String columnName, Boolean us){
if (COLUMN_ENERGY.equals(columnName)){
if (us){
return "gal diesel/ac";
}
else{
return "L diesel/ha";
}
}
else if (COLUMN_WATEREROS.equals(columnName) || COLUMN_WINDEROS.equals(columnName)){
if (us){
return "t/ac";
}
else{
return "kg/m\u00B2";
}
}
return null;
}
}