private void writeCoordinateVariable(VariableEntry variable, NetcdfFileWriteable ncFile) throws IOException, InvalidRangeException { int[] dimensions = {variable.getSize()}; Array array; // so far, everything is a float or an int // way too much code duplication but I'm done fightin java for now if (variable.getType() == DataType.INT) { array = new ArrayInt(dimensions); int tempInt = 0; IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { iter.getIntNext(); iter.setIntCurrent(tempInt); tempInt++; } ncFile.write(variable.getVariableName(), array); } else if (variable.getType() == DataType.FLOAT) { array = new ArrayFloat(dimensions); float tempFloat = 0; IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { iter.getFloatNext(); iter.setFloatCurrent(tempFloat); tempFloat++; } ncFile.write(variable.getVariableName(), array); } else if (variable.getType() == DataType.LONG) { array = new ArrayLong(dimensions); long tempLong = 0; IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { iter.getLongNext(); iter.setLongCurrent(tempLong); tempLong++; } ncFile.write(variable.getVariableName(), array); } // ncFile.write(variable.getVariableName(),array); }
private NetcdfFileWriteable writeNetcdfMetadata( NetcdfFileWriteable ncFile, String variableName, ArrayList<ArrayList<VariableEntry>> variableListList) throws IOException { int suffixInt = 1; for (ArrayList<VariableEntry> variableList : variableListList) { ArrayList<Dimension> dims = new ArrayList<Dimension>(); // add coordinate variables for each dimension for (int i = 0; i < variableList.size(); i++) { VariableEntry temp = variableList.get(i); Dimension tempDim; System.out.println("writing metadata for dim " + temp.getVariableName()); if (temp.isUnlimited()) { // see if there is already an unlimited dimension in this filea, // if so, use it. If not, add it // if ( ncFile.hasUnlimitedDimension() ) { if (this._unlimitedDim == null) { tempDim = ncFile.addUnlimitedDimension(temp.getVariableName()); this._unlimitedDim = tempDim; } else { // tempDim = this._unlimitedDim; dims.add(this._unlimitedDim); continue; } } else { tempDim = ncFile.addDimension(temp.getVariableName(), temp.getSize()); } // add a coordinate variable Dimension[] tempDimArray = {tempDim}; ncFile.addVariable(temp.getVariableName(), temp.getType(), tempDimArray); ncFile.addVariableAttribute(temp.getVariableName(), "units", temp.getUnits()); ncFile.addVariableAttribute(temp.getVariableName(), "long_name", temp.getLongName()); dims.add(tempDim); } // add the main variable System.out.println("adding variable " + (variableName + Integer.toString(suffixInt))); ncFile.addVariable(variableName + Integer.toString(suffixInt), DataType.INT, dims); suffixInt++; } /* ArrayList<Dimension> dims = new ArrayList<Dimension>(); Dimension tempDim; String localName = "dim"; for( int i = 0; i < dimensions.length; i++){ if ( (0 == i) && variableDim) { tempDim = ncFile.addUnlimitedDimension("dim" + Integer.toString(i)); } else { tempDim = ncFile.addDimension("dim" + Integer.toString(i), dimensions[i]); } dims.add( tempDim); } ncFile.addVariable(varName, dataType, dims); */ return ncFile; }