From: <rob...@us...> - 2010-07-13 00:09:56
|
Revision: 71 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=71&view=rev Author: robertbridle Date: 2010-07-13 00:09:49 +0000 (Tue, 13 Jul 2010) Log Message: ----------- ANDSWRON-703 - Review fix - Modified code to obtain EPOC dateTime from netCDF file instead of from a hard coded constant. Modified Paths: -------------- trunk/src/main/java/au/csiro/netcdf/wron/MdbsyNetCDF2CSVConverter.java Modified: trunk/src/main/java/au/csiro/netcdf/wron/MdbsyNetCDF2CSVConverter.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/wron/MdbsyNetCDF2CSVConverter.java 2010-07-08 09:35:45 UTC (rev 70) +++ trunk/src/main/java/au/csiro/netcdf/wron/MdbsyNetCDF2CSVConverter.java 2010-07-13 00:09:49 UTC (rev 71) @@ -1,3 +1,19 @@ +/** + * Copyright 2010, CSIRO Australia. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package au.csiro.netcdf.wron; import java.io.BufferedWriter; @@ -23,6 +39,7 @@ import ucar.ma2.Array; import ucar.ma2.InvalidRangeException; +import ucar.nc2.Attribute; import ucar.nc2.NetcdfFile; import ucar.nc2.Variable; import ucar.nc2.units.DateFormatter; @@ -41,8 +58,6 @@ */ public class MdbsyNetCDF2CSVConverter { - private static final String EPOC = "1895-01-01"; - /** * Constant that defines the logger to be used. */ @@ -114,6 +129,11 @@ * A string containing the date column text to be written out as the first column in all the csv files. */ private static String DATE_COLUMN_TEXT = null; + + /** + * The attribute name used to store a variable's type. + */ + private static final String UNITS = "units"; /** * @param args @@ -187,18 +207,26 @@ public void execute(String inputDir, String outputDir) throws IOException, InvalidRangeException { - // perform an initial conversion from netCDF to csv, the csv file will look like this: + // perform an initial conversion from netCDF to csv, the csv files will look like this: // Date, 1985-01-01, 1895-01-02, 1985-01-03, ... // APET, 0.1, 0.3, 0.0, .... // rainfall,0.0, 0.4, 0.1, ... + System.out.println("Starting intial convert of NetCDF 2 CSV."); + LOG.info("Starting intial convert of NetCDF 2 CSV."); writeCSVFiles(inputDir, outputDir); + System.out.println("Finished intial convert of NetCDF 2 CSV."); + LOG.info("Finished intial convert of NetCDF 2 CSV."); - // transpose the csv files so the will look like this: + // transpose the csv files so they will look like this: // Date, APET, rainfall // 1895-01-01, 0.1, 0.0 // 1895-01-02, 0.3, 0.4 // 1895-01-03, 0.0, 0.1 + System.out.println("Started transposition of CSV."); + LOG.info("Started transposition of CSV."); transposeCSVFiles(outputDir); + System.out.println("Finished transposition of CSV."); + LOG.info("Finished transposition of CSV."); } /** @@ -255,6 +283,8 @@ String filePath = outputDir + System.getProperty("file.separator") + fileName + ".csv"; + + LOG.info("\tWorking on file: " + filePath); // if the csv file already exists, then append the next variable. if (Util.fileExists(filePath)) @@ -293,7 +323,7 @@ try { - out.println(writeOutDateColumn(nc)); + out.println(writeOutDateColumn(nc)); // note: we only write out the date column when a new csv file is created. writeOutVariable(nc, out, variablesThatCanBeWrittenOut, @@ -569,12 +599,16 @@ private Array getLongitudeValues(NetcdfFile nc) throws IOException, IllegalArgumentException { - for (Variable v : nc.getVariables()) + List<Variable> variables = nc.getVariables(); + if(variables != null) { - if (v.isCoordinateVariable() - && LONGITUDE_VARIABLE_NAME.equals(v.getName())) + for (Variable v : variables) { - return v.read(); + if (v.isCoordinateVariable() + && LONGITUDE_VARIABLE_NAME.equals(v.getName())) + { + return v.read(); + } } } LOG.error("Could not find coordinate variable: " @@ -595,12 +629,16 @@ private Array getLatitudeValues(NetcdfFile nc) throws IOException, IllegalArgumentException { - for (Variable v : nc.getVariables()) + List<Variable> variables = nc.getVariables(); + if(variables != null) { - if (v.isCoordinateVariable() - && LATITUDE_VARIABLE_NAME.equals(v.getName())) + for (Variable v : variables) { - return v.read(); + if (v.isCoordinateVariable() + && LATITUDE_VARIABLE_NAME.equals(v.getName())) + { + return v.read(); + } } } LOG.error("Could not find coordinate variable: " @@ -620,12 +658,16 @@ private static Variable getTimeVariable(NetcdfFile nc) throws IllegalArgumentException { - for (Variable v : nc.getVariables()) + List<Variable> variables = nc.getVariables(); + if(variables != null) { - if (v.isCoordinateVariable() - && TIME_VARIABLE_NAME.equals(v.getName())) + for (Variable v : variables) { - return v; + if (v.isCoordinateVariable() + && TIME_VARIABLE_NAME.equals(v.getName())) + { + return v; + } } } LOG.error("Could not find coordinate variable: " + TIME_VARIABLE_NAME); @@ -648,16 +690,21 @@ StringBuffer strBuf = new StringBuffer(); DateFormatter dateFormatter = new DateFormatter(); - Date epoc; + Date epocDate; try { - epoc = dateFormatter.dateOnlyFormat(EPOC); + Variable time = getTimeVariable(nc); + + String timeUnits = getVariablesUnits(time); + String epocString = timeUnits.substring(timeUnits.indexOf("days since") + 11); // we expect the time value to be in units of: "days since yyyy-mm-dd h:m:s" + + epocDate = dateFormatter.dateOnlyFormat(epocString); strBuf.append("Date,"); - long timeVariableSize = getTimeVariable(nc).getSize(); + long timeVariableSize = time.getSize(); for (int i = 0; i < timeVariableSize; i++) { - strBuf.append(dateFormatter.toDateOnlyString(new Date(epoc + strBuf.append(dateFormatter.toDateOnlyString(new Date(epocDate .getTime() + (i * MILLISECONDS_IN_A_DAY))).trim()); @@ -674,8 +721,34 @@ pe.printStackTrace(); LOG.error("Error parsing date", pe); } - } return DATE_COLUMN_TEXT; } + + /** + * Finds the string value of the attribute that is used to store a variable's type. + * + * @param variable + * a variable {@link Variable} + * @return the string value of the variable's type. + */ + private static String getVariablesUnits(Variable variable) + { + List<Attribute> attributes = variable.getAttributes(); + if(attributes != null) + { + for(Attribute attribute : attributes) + { + if(UNITS.equals(attribute.getName())) + { + return attribute.getStringValue(); + } + } + } + LOG + .error("Could not find the value for the variable: " + variable.getName() + " -\"" + UNITS + + "\" attribute."); + throw new IllegalStateException("Could not find the value for the variable: " + variable.getName() + " -\"" + + UNITS + "\" attribute."); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |