From: <jde...@us...> - 2010-08-11 02:35:27
|
Revision: 80 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=80&view=rev Author: jdempsey Date: 2010-08-11 02:35:21 +0000 (Wed, 11 Aug 2010) Log Message: ----------- ANDSWRON-703 - Fix up issues reported by checkstyle 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-26 05:51:53 UTC (rev 79) +++ trunk/src/main/java/au/csiro/netcdf/wron/MdbsyNetCDF2CSVConverter.java 2010-08-11 02:35:21 UTC (rev 80) @@ -61,8 +61,7 @@ /** * Constant that defines the logger to be used. */ - private static final Logger LOG = Logger - .getLogger(MdbsyNetCDF2CSVConverter.class.getName()); + private static final Logger LOG = Logger.getLogger(MdbsyNetCDF2CSVConverter.class.getName()); /** * The latitude variable name used in the netCDF files. @@ -129,42 +128,40 @@ * 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 + * Command line entry point for the MdbsyNetCDF2CSVConverter class. Gets the input and output directory names + * from the command line and runs a conversion. + * + * @param args The command line arguments + * @throws ParseException If an error occurs when parsing the arguments. + * @throws IOException If the files cannot be read or written to. + * @throws InvalidRangeException if an invalid range is attempted to be read from a netCDf variable */ @SuppressWarnings("static-access") - public static void main(String[] args) throws ParseException, IOException, - InvalidRangeException, java.text.ParseException + public static void main(String[] args) throws ParseException, IOException, InvalidRangeException { Options options = new Options(); try { - Option inputDirectoryName = OptionBuilder - .withArgName("dir") - .hasArg() - .withDescription( - "1. the directory path containing the netCDF files (split by latitude) to be converted into csv.") + Option inputDirectoryName = OptionBuilder.withArgName("dir").hasArg().withDescription( + "1. the directory path containing the netCDF files (split by latitude) to be converted into csv.") .isRequired(true).withLongOpt(INPUT_DIR).create("i"); - Option outputDirectoryName = OptionBuilder - .withArgName("dir") - .hasArg() - .withDescription( - "2: the directory path to place the new csv files.") - .isRequired(true).withLongOpt(OUTPUT_DIR).create("o"); + Option outputDirectoryName = OptionBuilder.withArgName("dir").hasArg().withDescription( + "2: the directory path to place the new csv files.").isRequired(true).withLongOpt(OUTPUT_DIR) + .create("o"); options.addOption(inputDirectoryName); options.addOption(outputDirectoryName); // parse the command line arguments - CommandLine parsedCommandLine = new GnuParser() - .parse(options, args); + CommandLine parsedCommandLine = new GnuParser().parse(options, args); String inputDir = parsedCommandLine.getOptionValue(INPUT_DIR); String outputDir = parsedCommandLine.getOptionValue(OUTPUT_DIR); @@ -187,8 +184,8 @@ StringWriter sw = new StringWriter(); HelpFormatter formatter = new HelpFormatter(); formatter.setOptionComparator(new CommandLineOptionsComparator()); - formatter.printHelp(new PrintWriter(sw), 80, "-", header, options, - 0, 1, footer); + final int lineWidth = 80; + formatter.printHelp(new PrintWriter(sw), lineWidth, "-", header, options, 0, 1, footer); System.out.println(sw.toString()); } } @@ -202,10 +199,10 @@ * the directory in which the csv file will be placed. * @throws InvalidRangeException * thrown if an invalid range is attempted to be read from a netCDf variable. - * @throws IOException + * @throws IOException + * If the files cannot be read or written to. */ - public void execute(String inputDir, String outputDir) throws IOException, - InvalidRangeException + public void execute(String inputDir, String outputDir) throws IOException, InvalidRangeException { // 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, ... @@ -244,21 +241,21 @@ * thrown if an invalid range is attempted to be read from a netCDf variable. * @throws IOException */ - private void writeCSVFiles(String inputDir, String outputDir) - throws IOException, InvalidRangeException + private void writeCSVFiles(String inputDir, String outputDir) throws IOException, InvalidRangeException { File dir = new File(inputDir); File[] files = dir.listFiles(); - if(files == null) + if (files == null) { System.out.println("The input directory does not exist: " + inputDir); LOG.error("The input directory does not exist: " + inputDir); return; } - + for (int fileIndex = 0; fileIndex < files.length; fileIndex++) { - if (files[fileIndex].isFile() && NETCDF_FILE_EXTENSION.equals(Util.getFileExtension(files[fileIndex].getName()))) + if (files[fileIndex].isFile() + && NETCDF_FILE_EXTENSION.equals(Util.getFileExtension(files[fileIndex].getName()))) { NetcdfFile nc = null; try @@ -268,43 +265,35 @@ Array latitudes = getLatitudeValues(nc); Array longitudes = getLongitudeValues(nc); - LOG.info("latitude coordinate variable size: " - + latitudes.getSize()); - LOG.info("longitude coordinate variable size: " - + longitudes.getSize()); + LOG.info("latitude coordinate variable size: " + latitudes.getSize()); + LOG.info("longitude coordinate variable size: " + longitudes.getSize()); for (int latIndex = 0; latIndex < latitudes.getSize(); latIndex++) { - for (int longIndex = 0; longIndex < longitudes - .getSize(); longIndex++) + for (int longIndex = 0; longIndex < longitudes.getSize(); longIndex++) { - String fileName = createFileNameBasedOnLatLong(nc, - latIndex, longIndex); - String filePath = outputDir - + System.getProperty("file.separator") - + fileName + ".csv"; - + String fileName = createFileNameBasedOnLatLong(nc, latIndex, longIndex); + 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)) { File file = new File(filePath); - PrintWriter out = new PrintWriter( - new BufferedWriter(new FileWriter(file, - true /* append mode */))); + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true /* + * append + * mode + */))); List<String> variablesAlreadyWrittenOut = getVariablesAlreadyWrittenOut(file); List<String> variablesThatCanBeWrittenOut = getVariablesThatCanBeWrittenOut(nc); - variablesThatCanBeWrittenOut - .removeAll(variablesAlreadyWrittenOut); - + variablesThatCanBeWrittenOut.removeAll(variablesAlreadyWrittenOut); + try { - writeOutVariable(nc, out, - variablesThatCanBeWrittenOut, - latIndex, longIndex); + writeOutVariable(nc, out, variablesThatCanBeWrittenOut, latIndex, longIndex); } finally { @@ -315,19 +304,17 @@ else // if the csv file does not exist, create a new csv file and write all available variables. { - PrintWriter out = new PrintWriter(new BufferedWriter( - new FileWriter(new File(filePath), - false /* append mode */))); + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(new File(filePath), + false /* append mode */))); List<String> variablesThatCanBeWrittenOut = getVariablesThatCanBeWrittenOut(nc); try { - out.println(writeOutDateColumn(nc)); // note: we only write out the date column when a new csv file is created. - - writeOutVariable(nc, out, - variablesThatCanBeWrittenOut, - latIndex, longIndex); + out.println(writeOutDateColumn(nc)); // note: we only write out the date column when + // a new csv file is created. + + writeOutVariable(nc, out, variablesThatCanBeWrittenOut, latIndex, longIndex); } finally { @@ -359,16 +346,13 @@ * @throws IOException * */ - private void writeOutVariable(NetcdfFile nc, PrintWriter out, - List<String> variablesThatCanBeWrittenOut, int latIndex, - int longIndex) throws IOException, InvalidRangeException + private void writeOutVariable(NetcdfFile nc, PrintWriter out, List<String> variablesThatCanBeWrittenOut, + int latIndex, int longIndex) throws IOException, InvalidRangeException { for (String variableName : variablesThatCanBeWrittenOut) { - Array subsection = getVariableAcrossTime(nc, variableName, - latIndex, longIndex); - out.println(variableName + "," - + subsection.toString().replaceAll(" ", ",")); + Array subsection = getVariableAcrossTime(nc, variableName, latIndex, longIndex); + out.println(variableName + "," + subsection.toString().replaceAll(" ", ",")); } } @@ -383,7 +367,7 @@ { File dir = new File(outputDir); File[] files = dir.listFiles(); - if(files == null) + if (files == null) { System.out.println("The output directory does not exist: " + outputDir); LOG.error("The output directory does not exist: " + outputDir); @@ -392,14 +376,16 @@ for (int fileIndex = 0; fileIndex < files.length; fileIndex++) { - if (files[fileIndex].isFile() && CSV_FILE_EXTENSION.equals(Util.getFileExtension(files[fileIndex].getName()))) + if (files[fileIndex].isFile() + && CSV_FILE_EXTENSION.equals(Util.getFileExtension(files[fileIndex].getName()))) { String[][] matrix = readLookupFile(files[fileIndex]); // write over-the-top of the csv file. - PrintWriter out = new PrintWriter( - new BufferedWriter(new FileWriter(files[fileIndex], - false /* append mode */))); + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(files[fileIndex], false /* + * append + * mode + */))); // transpose String[][] matrix and write back out to csv String[][] transposed = transpose(matrix); @@ -411,7 +397,7 @@ for (int col = 0; col < transposed[0].length; col++) { out.print(transposed[row][col]); - if(col < (transposed[0].length-1)) + if (col < (transposed[0].length - 1)) { out.print(", "); } @@ -460,8 +446,7 @@ * @return the name of the variables that have already be written out to a csv file. * @throws IOException */ - private List<String> getVariablesAlreadyWrittenOut(File file) - throws IOException + private List<String> getVariablesAlreadyWrittenOut(File file) throws IOException { List<String> variablesAlreadyWrittenOut = new ArrayList<String>(); @@ -487,15 +472,11 @@ * @throws InvalidRangeException * @throws IOException */ - private String createFileNameBasedOnLatLong(NetcdfFile nc, int latIndex, - int longIndex) throws IOException, InvalidRangeException + private String createFileNameBasedOnLatLong(NetcdfFile nc, int latIndex, int longIndex) throws IOException, + InvalidRangeException { - return "region_" - + nc.readSection("lat(" + latIndex + ":" + latIndex + ")") - .toString().trim() - + "_" - + nc.readSection("long(" + longIndex + ":" + longIndex + ")") - .toString().trim(); + return "region_" + nc.readSection("lat(" + latIndex + ":" + latIndex + ")").toString().trim() + "_" + + nc.readSection("long(" + longIndex + ":" + longIndex + ")").toString().trim(); } /** @@ -510,7 +491,7 @@ { RandomAccessFile raf = new RandomAccessFile(file, "r"); List<String[]> lineList = new ArrayList<String[]>(); - + try { String line = raf.readLine(); @@ -533,11 +514,12 @@ /** * Transpose a matrix, e.g. [a][b] -> [b][a] * - * @param values a String[][] matrix. + * @param values + * a String[][] matrix. */ private String[][] transpose(String[][] values) { - if(values.length == 0) + if (values.length == 0) { return new String[0][0]; } @@ -568,21 +550,19 @@ * @throws IOException * @throws InvalidRangeException */ - private Array getVariableAcrossTime(NetcdfFile nc, String variableName, - int latIndex, int longIndex) throws IOException, - InvalidRangeException + private Array getVariableAcrossTime(NetcdfFile nc, String variableName, int latIndex, int longIndex) + throws IOException, InvalidRangeException { try { - return nc.readSection(variableName + "(" + latIndex + ":" - + latIndex + "," + longIndex + ":" + longIndex + ",:)"); + return nc.readSection(variableName + "(" + latIndex + ":" + latIndex + "," + longIndex + ":" + longIndex + + ",:)"); } catch (InvalidRangeException ire) { ire.printStackTrace(); - LOG.error("Could not read section: " + variableName + "(" - + latIndex + ":" + latIndex + "," + longIndex + ":" - + longIndex + ",:)", ire); + LOG.error("Could not read section: " + variableName + "(" + latIndex + ":" + latIndex + "," + longIndex + + ":" + longIndex + ",:)", ire); } return null; } @@ -596,25 +576,21 @@ * @throws IOException * @throws IllegalArgumentException */ - private Array getLongitudeValues(NetcdfFile nc) throws IOException, - IllegalArgumentException + private Array getLongitudeValues(NetcdfFile nc) throws IOException, IllegalArgumentException { List<Variable> variables = nc.getVariables(); - if(variables != null) + if (variables != null) { for (Variable v : variables) { - if (v.isCoordinateVariable() - && LONGITUDE_VARIABLE_NAME.equals(v.getName())) + if (v.isCoordinateVariable() && LONGITUDE_VARIABLE_NAME.equals(v.getName())) { return v.read(); } } } - LOG.error("Could not find coordinate variable: " - + LONGITUDE_VARIABLE_NAME); - throw new IllegalStateException("Could not find coordinate variable: " - + LONGITUDE_VARIABLE_NAME); + LOG.error("Could not find coordinate variable: " + LONGITUDE_VARIABLE_NAME); + throw new IllegalStateException("Could not find coordinate variable: " + LONGITUDE_VARIABLE_NAME); } /** @@ -626,25 +602,21 @@ * @throws IOException * @throws IllegalArgumentException */ - private Array getLatitudeValues(NetcdfFile nc) throws IOException, - IllegalArgumentException + private Array getLatitudeValues(NetcdfFile nc) throws IOException, IllegalArgumentException { List<Variable> variables = nc.getVariables(); - if(variables != null) + if (variables != null) { for (Variable v : variables) { - if (v.isCoordinateVariable() - && LATITUDE_VARIABLE_NAME.equals(v.getName())) + if (v.isCoordinateVariable() && LATITUDE_VARIABLE_NAME.equals(v.getName())) { return v.read(); } } } - LOG.error("Could not find coordinate variable: " - + LATITUDE_VARIABLE_NAME); - throw new IllegalStateException("Could not find coordinate variable: " - + LATITUDE_VARIABLE_NAME); + LOG.error("Could not find coordinate variable: " + LATITUDE_VARIABLE_NAME); + throw new IllegalStateException("Could not find coordinate variable: " + LATITUDE_VARIABLE_NAME); } /** @@ -655,24 +627,21 @@ * @return the time variable. * @throws IllegalArgumentException */ - private static Variable getTimeVariable(NetcdfFile nc) - throws IllegalArgumentException + private static Variable getTimeVariable(NetcdfFile nc) throws IllegalArgumentException { List<Variable> variables = nc.getVariables(); - if(variables != null) + if (variables != null) { for (Variable v : variables) { - if (v.isCoordinateVariable() - && TIME_VARIABLE_NAME.equals(v.getName())) + if (v.isCoordinateVariable() && TIME_VARIABLE_NAME.equals(v.getName())) { return v; } } } LOG.error("Could not find coordinate variable: " + TIME_VARIABLE_NAME); - throw new IllegalStateException("Could not find coordinate variable: " - + TIME_VARIABLE_NAME); + throw new IllegalStateException("Could not find coordinate variable: " + TIME_VARIABLE_NAME); } /** @@ -694,21 +663,23 @@ try { 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" - + 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 = time.getSize(); for (int i = 0; i < timeVariableSize; i++) { - strBuf.append(dateFormatter.toDateOnlyString(new Date(epocDate - .getTime() - + (i * MILLISECONDS_IN_A_DAY))).trim()); - - if(i < (timeVariableSize-1)) + strBuf.append(dateFormatter.toDateOnlyString( + new Date(epocDate.getTime() + (i * MILLISECONDS_IN_A_DAY))).trim()); + + if (i < (timeVariableSize - 1)) { strBuf.append(","); } @@ -735,11 +706,11 @@ private static String getVariablesUnits(Variable variable) { List<Attribute> attributes = variable.getAttributes(); - if(attributes != null) + if (attributes != null) { - for(Attribute attribute : attributes) + for (Attribute attribute : attributes) { - if(UNITS.equals(attribute.getName())) + if (UNITS.equals(attribute.getName())) { return attribute.getStringValue(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |