From: <jde...@us...> - 2010-06-24 00:29:14
|
Revision: 29 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=29&view=rev Author: jdempsey Date: 2010-06-24 00:29:08 +0000 (Thu, 24 Jun 2010) Log Message: ----------- ANDSWRON-657 - Allow defineAttributes to manipulate multiple files - Update to address review feedback and only a single file matching the pattern Modified Paths: -------------- trunk/USAGE.txt trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java trunk/src/main/java/au/csiro/netcdf/util/Util.java Added Paths: ----------- trunk/src/test/java/au/csiro/netcdf/util/UtilTest.java Modified: trunk/USAGE.txt =================================================================== --- trunk/USAGE.txt 2010-06-23 08:29:49 UTC (rev 28) +++ trunk/USAGE.txt 2010-06-24 00:29:08 UTC (rev 29) @@ -1,19 +1,3 @@ -==== - 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. -==== - ncextractcsv Extract a block of data from a CSV file. The data can then be piped into the ncwritevar command to add the data to a variable in a netCDF file. @@ -47,7 +31,7 @@ ------------------------------------- -usage: ncdefineAtt +ncdefineAtt Define an attribute in a netCDF file. -o,--outputFileName <file> 1: the filename of the netCDF file to be created. @@ -69,10 +53,16 @@ -p,--pattern <filename pattern> 6: OPTIONAL, a pattern to match multiple existing files, where multiple files should be processed. If this option is present the - outptuFilename is expected to be a direcotry - containing the files to be processed. + outputFilename is expected to be a directory + containing the files to be processed. The + wildcard characters * and ? are supported but + may need to be escaped by a \ to avoid + processing by the shell. e.g. \*.nc -v,--variable <text> 7: OPTIONAL, the variable to assign attribute(s) to, set to define variable attribute. +-t,--attributeDataType <text> 8: OPTIONAL, the data type of the attribute(s), + e.g. [byte, short, String, float, double, int, + char] Example: ncdefineatt -outputFileName ABC.nc -attributes "netCDF-Java=4.0,Conventions=value with a space,toDelete=null" @@ -93,8 +83,8 @@ that text containing '=' or ',' characters are delimited by a backslash. -v,--variableName <text> 4: the name to be given to the variable. --t,--variableDataType <text> 5: the data type of the variable, e.g. [int, - double, char, float, short, byte] +-t,--variableDataType <text> 5: the data type of the variable, e.g. [byte, + short, float, double, int, char] -a,--variableAttributes <text> 6: a comma separated list of attribute-value pairs, OPTIONAL, e.g. "units=mm", ensure that text containing '=' or ',' characters are Modified: trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java 2010-06-23 08:29:49 UTC (rev 28) +++ trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java 2010-06-24 00:29:08 UTC (rev 29) @@ -168,17 +168,21 @@ .getOptionValue(VARIABLE_NAME) : ""; String attributeDataTypeArg = (parsedCommandLine.hasOption(ATTRIBUTE_DATA_TYPE)) ? parsedCommandLine .getOptionValue(ATTRIBUTE_DATA_TYPE) : "String"; + + List<String> targetFileNames = Util.getListOfTargetFiles(outputFilenameArg, pattern); // check whether large file support is needed - if (Util.fileExists(outputFilenameArg) - && Util.getExistingFile(outputFilenameArg).length() >= MAX_32BIT_OFFSET_FILE_SIZE - && !isLargeFileSupport) + for (String outputFileName : targetFileNames) { - throw new IllegalArgumentException("The netCDF file will be too large, please use " + IS_LARGE_FILE - + " flag."); + if (Util.fileExists(outputFileName) + && Util.getExistingFile(outputFileName).length() >= MAX_32BIT_OFFSET_FILE_SIZE + && !isLargeFileSupport) + { + throw new IllegalArgumentException("The netCDF file " + outputFileName + + " will be too large, please use " + IS_LARGE_FILE + " flag."); + } } - List<String> targetFileNames = Util.getListOfTargetFiles(outputFilenameArg, pattern); // check that if an input file is specified then it actually exists. if (!inputFilenameArg.isEmpty()) @@ -217,6 +221,7 @@ for (String outputFileName : targetFileNames) { + LOG.debug("Processing file : " + outputFileName); this.execute(outputFileName, attributes, isLargeFileSupport, variableNameArg); } } @@ -465,9 +470,10 @@ Option pattern = OptionBuilder.withArgName("filename pattern").hasArg().withDescription( "6: OPTIONAL, a pattern to match multiple existing files, where multiple files should be " - + "processed. If this option is present the outptuFilename is expected to be a direcotry " - + "containing the files to be processed.").isRequired(false).withLongOpt(FILENAME_PATTERN) - .create("p"); + + "processed. If this option is present the outputFilename is expected to be a directory " + + "containing the files to be processed. The wildcard characters * and ? are supported but " + + "may need to be escaped by a \\ to avoid processing by the shell. e.g. \\*.nc ").isRequired( + false).withLongOpt(FILENAME_PATTERN).create("p"); Option variableName = OptionBuilder .withArgName("text") .hasArg() Modified: trunk/src/main/java/au/csiro/netcdf/util/Util.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/util/Util.java 2010-06-23 08:29:49 UTC (rev 28) +++ trunk/src/main/java/au/csiro/netcdf/util/Util.java 2010-06-24 00:29:08 UTC (rev 29) @@ -220,6 +220,7 @@ */ public static List<String> getListOfTargetFiles(String foldername, String pattern) { + String cleanPat = pattern.replaceAll("\\\\\\*", "*").replaceAll("\\\\\\?", "?"); List<String> fileNameList = new ArrayList<String>(); if (pattern == null || pattern.length() == 0) { @@ -233,7 +234,7 @@ return Collections.emptyList(); } - FileFilter patternFilter = new WildcardFileFilter(pattern); + FileFilter patternFilter = new WildcardFileFilter(cleanPat); File[] files = dir.listFiles(patternFilter); for (int i = 0; i < files.length; i++) { Added: trunk/src/test/java/au/csiro/netcdf/util/UtilTest.java =================================================================== --- trunk/src/test/java/au/csiro/netcdf/util/UtilTest.java (rev 0) +++ trunk/src/test/java/au/csiro/netcdf/util/UtilTest.java 2010-06-24 00:29:08 UTC (rev 29) @@ -0,0 +1,79 @@ +/** + * 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.util; + +import java.util.List; + +import junit.framework.TestCase; + +/** + * Check the function of the Util class. + * + * @author James Dempsey on 23/06/2010 + * @version $Revision$ $Date$ + */ +public class UtilTest extends TestCase +{ + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception + { + super.setUp(); + } + + /** + * Test method for {@link au.csiro.netcdf.util.Util#getListOfTargetFiles(java.lang.String, java.lang.String)}. + */ + public final void testGetListOfTargetFiles() + { + List<String> files = Util.getListOfTargetFiles(".", "*.txt"); + assertNotNull("Files should be a non-empty list", files); + assertTrue("Files should be a non-empty list", files.size()>1); + boolean hasUsage = false; + for (String string : files) + { + if (string.endsWith("USAGE.txt")) + { + hasUsage = true; + } + } + assertTrue("Expected to find USAGE.txt in result set.", hasUsage); + + } + + /** + * Test method for {@link au.csiro.netcdf.util.Util#getListOfTargetFiles(java.lang.String, java.lang.String)}. + */ + public final void testGetListOfTargetFilesEscaped() + { + List<String> files = Util.getListOfTargetFiles(".", "\\*.txt"); + assertNotNull("Files should be a non-empty list", files); + assertTrue("Files should be a non-empty list", files.size()>1); + boolean hasUsage = false; + for (String string : files) + { + if (string.endsWith("USAGE.txt")) + { + hasUsage = true; + } + } + assertTrue("Expected to find USAGE.txt in result set.", hasUsage); + + } + +} Property changes on: trunk/src/test/java/au/csiro/netcdf/util/UtilTest.java ___________________________________________________________________ Added: svn:keywords + Author Revision Date Id Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |