From: <jde...@us...> - 2010-06-23 02:23:09
|
Revision: 22 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=22&view=rev Author: jdempsey Date: 2010-06-23 02:23:03 +0000 (Wed, 23 Jun 2010) Log Message: ----------- ANDSWRON-690 - Add support for attributes with numeric data types Modified Paths: -------------- trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java trunk/src/main/java/au/csiro/netcdf/util/NetCDFUtils.java trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java Modified: trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java 2010-06-23 00:02:13 UTC (rev 21) +++ trunk/src/main/java/au/csiro/netcdf/NcDefineAttributes.java 2010-06-23 02:23:03 UTC (rev 22) @@ -25,7 +25,9 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.GnuParser; @@ -36,6 +38,7 @@ import org.apache.commons.cli.ParseException; import org.apache.log4j.Logger; +import ucar.ma2.DataType; import ucar.nc2.Attribute; import ucar.nc2.NetcdfFileWriteable; import ucar.nc2.Variable; @@ -56,6 +59,8 @@ */ public class NcDefineAttributes implements Command { + private static final String NULL_VALUE = "null"; + /** * The command name */ @@ -96,12 +101,34 @@ * The pattern for accepted file names where multiple files are accepted. */ public static final String FILENAME_PATTERN = "pattern"; + + /** + * The name of the command line option used for specifying the data type of the attribute. + */ + public static final String ATTRIBUTE_DATA_TYPE = "attributeDataType"; /** * Constant that defines the logger to be used. */ private static final Logger LOG = Logger.getLogger(NcDefineAttributes.class.getName()); + + + /** + * The only data types that can be used for attributes in netCDF v3 (classic) files. + */ + private static Set<DataType> netCDF3DataTypes = new HashSet<DataType>(); + static + { + netCDF3DataTypes.add(DataType.DOUBLE); + netCDF3DataTypes.add(DataType.FLOAT); + netCDF3DataTypes.add(DataType.INT); + netCDF3DataTypes.add(DataType.CHAR); + netCDF3DataTypes.add(DataType.SHORT); + netCDF3DataTypes.add(DataType.BYTE); + netCDF3DataTypes.add(DataType.STRING); + } + /** * Command options */ @@ -139,6 +166,8 @@ .getOptionValue(FILENAME_PATTERN) : ""; String variableNameArg = (parsedCommandLine.hasOption(VARIABLE_NAME)) ? parsedCommandLine .getOptionValue(VARIABLE_NAME) : ""; + String attributeDataTypeArg = (parsedCommandLine.hasOption(ATTRIBUTE_DATA_TYPE)) ? parsedCommandLine + .getOptionValue(ATTRIBUTE_DATA_TYPE) : "String"; // check whether large file support is needed if (Util.fileExists(outputFilenameArg) @@ -162,9 +191,12 @@ // value is an empty list. if(!attributesArg.isEmpty()) { + // try getting the variable's data type + DataType attributeDataType = NcDefineAttributes.mapStringToDataType(attributeDataTypeArg); + try { - attributes = NetCDFUtils.mapStringToAttributeValueList(attributesArg); + attributes = NetCDFUtils.mapStringToAttributeValueList(attributesArg, attributeDataType); } catch (IllegalArgumentException iae) { @@ -260,7 +292,7 @@ for(Attribute attribute : attributes) { // remove Attribute if the value is "null" - if ("null".equalsIgnoreCase(attribute.getStringValue())) + if (NULL_VALUE.equalsIgnoreCase(attribute.getStringValue())) { ncfile.deleteGlobalAttribute(attribute.getName()); } @@ -285,7 +317,7 @@ for(Attribute attribute : attributes) { // remove Attribute if the value is "null" - if ("null".equalsIgnoreCase(attribute.getStringValue())) + if (NULL_VALUE.equalsIgnoreCase(attribute.getStringValue())) { variable.removeAttributeIgnoreCase(attribute.getName()); } @@ -444,6 +476,10 @@ + "set to define variable attribute.") .isRequired(false).withLongOpt(VARIABLE_NAME).create("v"); + Option attributeType = OptionBuilder.withArgName("text").hasArg().withDescription( + "8: OPTIONAL, the data type of the attribute(s), e.g. " + NcDefineAttributes.netCDF3DataTypes) + .isRequired(false).withLongOpt(ATTRIBUTE_DATA_TYPE).create("t"); + Options options = new Options(); options.addOption(outputFileName); @@ -453,6 +489,7 @@ options.addOption(largeFileSupport); options.addOption(pattern); options.addOption(variableName); + options.addOption(attributeType); return options; } @@ -492,6 +529,8 @@ boolean isLargeFileSupport = parsedCommandLine.hasOption(IS_LARGE_FILE); String pattern = (parsedCommandLine.hasOption(FILENAME_PATTERN)) ? parsedCommandLine .getOptionValue(FILENAME_PATTERN) : ""; + String variableDataTypeArg = (parsedCommandLine.hasOption(ATTRIBUTE_DATA_TYPE)) ? parsedCommandLine + .getOptionValue(ATTRIBUTE_DATA_TYPE) : "String"; // check whether large file support is needed if (pattern.length() == 0 && Util.fileExists(outputFilenameArg) @@ -537,7 +576,12 @@ + " value is not a comma separated String of attribute-value pairs: " + attributesArg); } - } + } + + + // try getting the variable's data type + NcDefineAttributes.mapStringToDataType(variableDataTypeArg); + } catch (ParseException pe) { @@ -550,4 +594,24 @@ return errorMsg; } + + /** + * Maps a <code>String</code> into a {@link DataType}. + * + * @param variableDataType + * a data type description. + * @return a {@link DataType} + * @throws IllegalArgumentException + * thrown if the <code>String</code> can not be mapped to a {@link DataType}. + */ + public static DataType mapStringToDataType(String variableDataType) throws IllegalArgumentException + { + DataType dataType = DataType.getType(variableDataType); + if (dataType != null && netCDF3DataTypes.contains(dataType)) + { + return dataType; + } + throw new IllegalArgumentException(NcDefineVariable.VARIABLE_DATA_TYPE + " value is not a valid data type: " + + variableDataType + ". Allowed data types are: " + netCDF3DataTypes); + } } Modified: trunk/src/main/java/au/csiro/netcdf/util/NetCDFUtils.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/util/NetCDFUtils.java 2010-06-23 00:02:13 UTC (rev 21) +++ trunk/src/main/java/au/csiro/netcdf/util/NetCDFUtils.java 2010-06-23 02:23:03 UTC (rev 22) @@ -27,6 +27,7 @@ import java.util.List; import ucar.ma2.Array; +import ucar.ma2.DataType; import ucar.nc2.Attribute; import ucar.nc2.Dimension; import ucar.nc2.NetcdfFile; @@ -46,6 +47,7 @@ /** Index value to be returned when the value cannot be found in the array */ public static final int NOT_FOUND = -1; + public static final String NULL_VALUE = "null"; /** * Lookup the index of a value in a dimension. The dimension to be searched must @@ -242,7 +244,7 @@ } /** - * Converts a comma separated <code>String</code> into an {@link Attribute} list. + * Converts a comma separated <code>String</code> into a String {@link Attribute} list. * * @param commaSeparatedAttributeValueString * a list of comma separated attribute-value pairs, e.g. attribute1=value1,attribute2=value2,... @@ -253,6 +255,22 @@ public static List<Attribute> mapStringToAttributeValueList(String commaSeparatedAttributeValueString) throws IllegalArgumentException { + return mapStringToAttributeValueList(commaSeparatedAttributeValueString, DataType.STRING); + } + + /** + * Converts a comma separated <code>String</code> into an {@link Attribute} list. + * + * @param commaSeparatedAttributeValueString + * a list of comma separated attribute-value pairs, e.g. attribute1=value1,attribute2=value2,... + * @param dataType The type of attributes to be created. + * @return an {@link Attribute} list. + * @throws IllegalArgumentException + * thrown if the <code>String</code> can not be converted into an {@link Attribute} list. + */ + public static List<Attribute> mapStringToAttributeValueList(String commaSeparatedAttributeValueString, DataType dataType) + throws IllegalArgumentException + { List<Attribute> attributeValues = new ArrayList<Attribute>(); List<String> attributePairs = Util.tokeniseCommaSeparatedString(commaSeparatedAttributeValueString); @@ -264,8 +282,90 @@ if (keyValuePair.length == 2 && !keyValuePair[0].isEmpty() && !keyValuePair[1].isEmpty()) { String key = keyValuePair[0].replaceAll("\\\\,", ",").replaceAll("\\\\=", "="); - attributeValues.add(new Attribute(key.trim(), - keyValuePair[1].replaceAll("\\\\,", ",").replaceAll("\\\\=", "="))); + key = key.trim(); + String strVal = keyValuePair[1].replaceAll("\\\\,", ",").replaceAll("\\\\=", "="); + Attribute attrib; + + if (NULL_VALUE.equals(strVal) || dataType == DataType.STRING || dataType == DataType.CHAR) + { + attrib = new Attribute(key, strVal); + } + else if (dataType == DataType.FLOAT) + { + Float value; + try + { + value = Float.parseFloat(strVal); + } + catch (NumberFormatException e) + { + throw new IllegalArgumentException("Invalid float value '" + strVal + "' for key " + + key + "."); + } + attrib = new Attribute(key, value); + } + else if (dataType == DataType.DOUBLE) + { + Double value; + try + { + value = Double.parseDouble(strVal); + } + catch (NumberFormatException e) + { + throw new IllegalArgumentException("Invalid double value '" + strVal + "' for key " + + key + "."); + } + attrib = new Attribute(key, value); + } + else if (dataType == DataType.INT) + { + Integer value; + try + { + value = Integer.parseInt(strVal); + } + catch (NumberFormatException e) + { + throw new IllegalArgumentException("Invalid integer value '" + strVal + "' for key " + + key + "."); + } + attrib = new Attribute(key, value); + } + else if (dataType == DataType.SHORT) + { + Short value; + try + { + value = Short.parseShort(strVal); + } + catch (NumberFormatException e) + { + throw new IllegalArgumentException("Invalid short value '" + strVal + "' for key " + + key + "."); + } + attrib = new Attribute(key, value); + } + else if (dataType == DataType.BYTE) + { + Byte value; + try + { + value = Byte.parseByte(strVal); + } + catch (NumberFormatException e) + { + throw new IllegalArgumentException("Invalid byte value '" + strVal + "' for key " + + key + "."); + } + attrib = new Attribute(key, value); + } + else + { + throw new IllegalArgumentException( + "Unexpected datatype of " + dataType + " supplied to mapStringToAttributeValueList."); + } + attributeValues.add(attrib); } else { Modified: trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java =================================================================== --- trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java 2010-06-23 00:02:13 UTC (rev 21) +++ trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java 2010-06-23 02:23:03 UTC (rev 22) @@ -33,10 +33,11 @@ import ucar.nc2.NetcdfFile; import au.csiro.netcdf.cli.Command; import au.csiro.netcdf.util.NetCDFUtils; + /** * This class is a unit test suite to verify that the NcDefineAttributes command operates correctly. * <p> - * Copyright 2010, CSIRO Australia All rights reserved. + * Copyright 2010, CSIRO Australia * * @author James Dempsey on 23/03/2010 * @version $Revision: 7130 $ $Date: 2010-06-09 14:23:51 +1000 (Wed, 09 Jun 2010) $ @@ -74,6 +75,12 @@ /** Data for the second Attribute */ private static final String ATTR2_DATA = "Murray Darling Basin Sustainable Yields (MDBSY)"; + + /** Name of the third, float, Attribute */ + private static final String ATTR3_NAME = "Float Val"; + + /** Data for the third, float, Attribute */ + private static final String ATTR3_DATA = "17.5f"; /** Name for history Attribute */ private static final String ATTR_HISTORY = "history"; @@ -299,8 +306,141 @@ } } + /** + * Test a valid command creates a netCDF file with a float attribute + * @throws Exception If there is an error running the test + */ + public final void testExecuteValidFloatAttr() throws Exception + { + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + "-" + ATTRIBUTES, ATTR3_NAME + "=" + ATTR3_DATA, + "-t", "float"}; + File ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + ncDefineAttr.execute(args); + assertTrue("The nc file was not created: " + NC_FILE_NAME, ncFile.exists()); + + NetcdfFile netcdfFile = NetcdfFile.open(NC_FILE_NAME); + Attribute attr = netcdfFile.findGlobalAttribute(ATTR3_NAME); + assertTrue("The Attribute was not defined: " + ATTR3_NAME, (attr != null)); + assertEquals("The Attrbute " + ATTR3_NAME + " had a string value. ", + null, attr.getStringValue()); + assertEquals("The Attrbute had the wrong data: " + ATTR3_NAME, + Float.parseFloat(ATTR3_DATA), (Float)attr.getNumericValue(), 0.01f); + + netcdfFile.close(); + ncFile.delete(); + } + /** + * Test a valid command creates a netCDF file with a short attribute + * @throws Exception If there is an error running the test + */ + public final void testExecuteValidShortAttr() throws Exception + { + final String shortAttrName = "Short"; + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + "-" + ATTRIBUTES, shortAttrName + "=57", + "-t", "short"}; + + File ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + ncDefineAttr.execute(args); + assertTrue("The nc file was not created: " + NC_FILE_NAME, ncFile.exists()); + + NetcdfFile netcdfFile = NetcdfFile.open(NC_FILE_NAME); + Attribute attr = netcdfFile.findGlobalAttribute(shortAttrName); + assertTrue("The Attribute was not defined: " + shortAttrName, (attr != null)); + assertEquals("The Attrbute " + shortAttrName + " had a string value. ", + null, attr.getStringValue()); + assertEquals("The Attrbute had the wrong data: " + shortAttrName, + (short) 57, (Short)attr.getNumericValue(), 0); + + netcdfFile.close(); + ncFile.delete(); + } + + /** + * Test a valid command creates a netCDF file with an integer attribute + * @throws Exception If there is an error running the test + */ + public final void testExecuteValidIntAttr() throws Exception + { + final String intAttrName = "Integer"; + final int intAttrValue = 67991082; + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + "-" + ATTRIBUTES, intAttrName + "=" + String.valueOf(intAttrValue), + "-t", "int"}; + + File ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + ncDefineAttr.execute(args); + assertTrue("The nc file was not created: " + NC_FILE_NAME, ncFile.exists()); + + NetcdfFile netcdfFile = NetcdfFile.open(NC_FILE_NAME); + Attribute attr = netcdfFile.findGlobalAttribute(intAttrName); + assertTrue("The Attribute was not defined: " + intAttrName, (attr != null)); + assertEquals("The Attrbute " + intAttrName + " had a string value. ", + null, attr.getStringValue()); + assertEquals("The Attrbute had the wrong data: " + intAttrName, + intAttrValue, (Integer)attr.getNumericValue(), 0); + + netcdfFile.close(); + ncFile.delete(); + } + + /** + * Test a valid command creates a netCDF file with an integer attribute + * @throws Exception If there is an error running the test + */ + public final void testExecuteInvalidType() throws Exception + { + final String intAttrName = "Integer"; + final int intAttrValue = 67991082; + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + "-" + ATTRIBUTES, intAttrName + "=" + String.valueOf(intAttrValue), + "-t", "integer"}; + + File ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + try + { + ncDefineAttr.execute(args); + ncFile.delete(); + fail("Type of integer should have been rejected."); + } + catch (IllegalArgumentException e) + { + // We can only test the start of the message as the supported types is a set and thus the order is not deterministic + assertTrue("Incorrect error message", e.getMessage().startsWith("variableDataType value is not a valid data type: integer. " + + "Allowed data types are: [")); + } + + } + + + /** * Test that required options are reported if missing */ public final void testMissingRequiredOption() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jde...@us...> - 2010-07-08 09:35:52
|
Revision: 70 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=70&view=rev Author: jdempsey Date: 2010-07-08 09:35:45 +0000 (Thu, 08 Jul 2010) Log Message: ----------- ANDSWRON-737 - Add CRS metadata to comply with CF Conventions Modified Paths: -------------- trunk/src/main/java/au/csiro/netcdf/NcDefineVariable.java trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioAConverter.java trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioCConverter.java trunk/src/test/java/au/csiro/netcdf/TestNcDefineVariable.java Modified: trunk/src/main/java/au/csiro/netcdf/NcDefineVariable.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/NcDefineVariable.java 2010-07-08 09:32:13 UTC (rev 69) +++ trunk/src/main/java/au/csiro/netcdf/NcDefineVariable.java 2010-07-08 09:35:45 UTC (rev 70) @@ -459,8 +459,8 @@ .isRequired(false).withLongOpt(VARIABLE_ATTRIBUTES).create("a"); Option dimensionNames = OptionBuilder.withArgName("text").hasArg().withDescription( - "7: a comma separated list of the variable's dimensions, e.g. date,latitude,longitude.").isRequired( - true).withLongOpt(DIMENSION_NAMES).create("d"); + "7: OPTIONAL, a comma separated list of the variable's dimensions, e.g. date,latitude,longitude.") + .isRequired(false).withLongOpt(DIMENSION_NAMES).create("d"); Option largeFileSupport = OptionBuilder.withDescription( "8: OPTIONAL, set if more than 2 GB of data will need to be stored in this file.").isRequired(false) Modified: trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioAConverter.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioAConverter.java 2010-07-08 09:32:13 UTC (rev 69) +++ trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioAConverter.java 2010-07-08 09:35:45 UTC (rev 70) @@ -133,6 +133,7 @@ DataType.FLOAT, DataType.FLOAT, DataType.FLOAT, DataType.FLOAT, DataType.FLOAT, DataType.FLOAT, DataType.FLOAT }; static final int numVariables = variableNames.length; + private static final String GRID_MAPPING = "crs"; // time constants private static final String TIME = "time"; @@ -190,6 +191,12 @@ private static String REPORTING_REGION_LONG_NAME = "MDB Reporting Region Id"; private static final float REPORTING_REGION_MISSING_VALUE = 0f; + // crs constants + private static String CRS_GRID_MAPPING_NAME = "latitude_longitude"; + private static float CRS_LONGITUDE_OF_PRIME_MERIDIAN = 0.0f; + private static float CRS_SEMI_MAJOR_AXIS = 6378137.0f; + private static float CRS_INVERSE_FLATTENING = 298.257222101f; + // cell id private static final int CELL_ID_COLUMN_INDEX = 0; @@ -601,6 +608,11 @@ TIME_BOUNDS)), TIME, false /* isLargeFileSupport */, false/* fillValue */); command.execute(new File(outputFileName), TIME_BOUNDS, DataType.INT, new ArrayList<Attribute>(), TIME + " " + NV, false /* isLargeFileSupport */, false/* fillValue */); + command.execute(new File(outputFileName), GRID_MAPPING, DataType.INT, Arrays.asList(new Attribute( + "grid_mapping_name", CRS_GRID_MAPPING_NAME), new Attribute("longitude_of_prime_meridian", + CRS_LONGITUDE_OF_PRIME_MERIDIAN), new Attribute("semi_major_axis", CRS_SEMI_MAJOR_AXIS), + new Attribute("inverse_flattening", CRS_INVERSE_FLATTENING)), "", + false /* isLargeFileSupport */, true/* fillValue */); } /** @@ -618,13 +630,16 @@ String spatialDim = LAT + " " + LONG; command.execute(new File(outputFileName), ELEVATION, DataType.FLOAT, Arrays.asList(new Attribute("units", ELEVATION_UNITS), new Attribute("standard_name", ELEVATION_STANDARD_NAME), new Attribute("long_name", - ELEVATION_LONG_NAME), new Attribute("_FillValue", ELEVATION_MISSING_VALUE)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); + ELEVATION_LONG_NAME), new Attribute("_FillValue", ELEVATION_MISSING_VALUE), new Attribute( + "grid_mapping", GRID_MAPPING)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); command .execute(new File(outputFileName), CATCHMENT_ID, DataType.FLOAT, Arrays.asList(new Attribute( - "long_name", CATCHMENT_ID_LONG_NAME), new Attribute("_FillValue", CATCHMENT_ID_MISSING_VALUE)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); + "long_name", CATCHMENT_ID_LONG_NAME), new Attribute("_FillValue", CATCHMENT_ID_MISSING_VALUE), new Attribute( + "grid_mapping", GRID_MAPPING)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); command .execute(new File(outputFileName), REPORTING_REGION, DataType.FLOAT, Arrays.asList(new Attribute( - "long_name", REPORTING_REGION_LONG_NAME), new Attribute("_FillValue", REPORTING_REGION_MISSING_VALUE)), spatialDim, false /* isLargeFileSupport */, + "long_name", REPORTING_REGION_LONG_NAME), new Attribute("_FillValue", REPORTING_REGION_MISSING_VALUE), new Attribute( + "grid_mapping", GRID_MAPPING)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); } @@ -647,7 +662,8 @@ .valueOf(variableMissingValues[variableIndex])), new Attribute("_FillValue", Float .valueOf(variableFillValues[variableIndex])), new Attribute("valid_min", Float .valueOf(variableMinValues[variableIndex])), new Attribute("valid_max", Float - .valueOf(variableMaxValues[variableIndex]))), dimensions, + .valueOf(variableMaxValues[variableIndex])), new Attribute("grid_mapping", + GRID_MAPPING)), dimensions, false /* isLargeFileSupport */, false/* fillValue */); } Modified: trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioCConverter.java =================================================================== --- trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioCConverter.java 2010-07-08 09:32:13 UTC (rev 69) +++ trunk/src/main/java/au/csiro/netcdf/wron/MdbsyScenarioCConverter.java 2010-07-08 09:35:45 UTC (rev 70) @@ -123,6 +123,7 @@ private static final String[] variableMissingValues = variableFillValues; private static final DataType[] variableDataTypes = new DataType[] { DataType.FLOAT, DataType.FLOAT }; static final int numVariables = variableNames.length; + private static final String GRID_MAPPING = "crs"; // time constants private static final String TIME = "time"; @@ -180,6 +181,12 @@ private static String REPORTING_REGION_LONG_NAME = "MDB Reporting Region Id"; private static final float REPORTING_REGION_MISSING_VALUE = 0f; + // crs constants + private static String CRS_GRID_MAPPING_NAME = "latitude_longitude"; + private static float CRS_LONGITUDE_OF_PRIME_MERIDIAN = 0.0f; + private static float CRS_SEMI_MAJOR_AXIS = 6378137.0f; + private static float CRS_INVERSE_FLATTENING = 298.257222101f; + // cell id private static final int CELL_ID_COLUMN_INDEX = 0; @@ -595,6 +602,11 @@ TIME_BOUNDS)), TIME, false /* isLargeFileSupport */, false/* fillValue */); command.execute(new File(outputFileName), TIME_BOUNDS, DataType.INT, new ArrayList<Attribute>(), TIME + " " + NV, false /* isLargeFileSupport */, false/* fillValue */); + command.execute(new File(outputFileName), GRID_MAPPING, DataType.INT, Arrays.asList(new Attribute( + "grid_mapping_name", CRS_GRID_MAPPING_NAME), new Attribute("longitude_of_prime_meridian", + CRS_LONGITUDE_OF_PRIME_MERIDIAN), new Attribute("semi_major_axis", CRS_SEMI_MAJOR_AXIS), + new Attribute("inverse_flattening", CRS_INVERSE_FLATTENING)), "", + false /* isLargeFileSupport */, true/* fillValue */); } /** @@ -612,13 +624,16 @@ String spatialDim = LAT + " " + LONG; command.execute(new File(outputFileName), ELEVATION, DataType.FLOAT, Arrays.asList(new Attribute("units", ELEVATION_UNITS), new Attribute("standard_name", ELEVATION_STANDARD_NAME), new Attribute("long_name", - ELEVATION_LONG_NAME), new Attribute("_FillValue", ELEVATION_MISSING_VALUE)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); + ELEVATION_LONG_NAME), new Attribute("_FillValue", ELEVATION_MISSING_VALUE), new Attribute( + "grid_mapping", GRID_MAPPING)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); command .execute(new File(outputFileName), CATCHMENT_ID, DataType.FLOAT, Arrays.asList(new Attribute( - "long_name", CATCHMENT_ID_LONG_NAME), new Attribute("_FillValue", CATCHMENT_ID_MISSING_VALUE)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); + "long_name", CATCHMENT_ID_LONG_NAME), new Attribute("_FillValue", CATCHMENT_ID_MISSING_VALUE), new Attribute( + "grid_mapping", GRID_MAPPING)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); command .execute(new File(outputFileName), REPORTING_REGION, DataType.FLOAT, Arrays.asList(new Attribute( - "long_name", REPORTING_REGION_LONG_NAME), new Attribute("_FillValue", REPORTING_REGION_MISSING_VALUE)), spatialDim, false /* isLargeFileSupport */, + "long_name", REPORTING_REGION_LONG_NAME), new Attribute("_FillValue", REPORTING_REGION_MISSING_VALUE), new Attribute( + "grid_mapping", GRID_MAPPING)), spatialDim, false /* isLargeFileSupport */, false/* fillValue */); } @@ -638,7 +653,8 @@ Arrays.asList(new Attribute("units", variableUnits[variableIndex]), new Attribute("long_name", variableLongNames[variableIndex]), new Attribute("missing_value", Float .valueOf(variableMissingValues[variableIndex])), new Attribute("_FillValue", Float - .valueOf(variableFillValues[variableIndex]))), dimensions, false /* isLargeFileSupport */, + .valueOf(variableFillValues[variableIndex])), new Attribute("grid_mapping", + GRID_MAPPING)), dimensions, false /* isLargeFileSupport */, false/* fillValue */); } Modified: trunk/src/test/java/au/csiro/netcdf/TestNcDefineVariable.java =================================================================== --- trunk/src/test/java/au/csiro/netcdf/TestNcDefineVariable.java 2010-07-08 09:32:13 UTC (rev 69) +++ trunk/src/test/java/au/csiro/netcdf/TestNcDefineVariable.java 2010-07-08 09:35:45 UTC (rev 70) @@ -441,9 +441,6 @@ errors = ncDefineVariable.validCommand(invalidVariableAttributesArgs); assertTrue("Command with invalid variableAttributes option should return an error", !errors.isEmpty()); - - errors = ncDefineVariable.validCommand(missingDimensionNamesArgs); - assertTrue("Command without dimensionNames option should return an error", !errors.isEmpty()); errors = ncDefineVariable.validCommand(invalidVariableDataTypeArgs); assertTrue("Command with invalid variableDataType option should return an error", !errors.isEmpty()); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |