@Test /** * Find min/max/average precipitation for a randomly-positioned but fixed-size region from a nc * file output: filename,origin,size key: value:min, max, average * * @throws Exception */ public void testProcessingNASANexDataInNetCDF() throws Exception { final int SIZE = 100; File file = new File(this.getClass().getResource("/ncar.nc").getPath()); byte[] netcdfinbyte = FileUtils.readFileToByteArray(file); // use any dummy filename for file in memory NetcdfFile netCDFfile = NetcdfFile.openInMemory("inmemory.nc", netcdfinbyte); Variable time = netCDFfile.findVariable("time"); ArrayDouble.D1 days = (ArrayDouble.D1) time.read(); Variable lat = netCDFfile.findVariable("lat"); if (lat == null) { logger.error("Cannot find Variable latitude(lat)"); return; } ArrayFloat.D1 absolutelat = (ArrayFloat.D1) lat.read(); Variable lon = netCDFfile.findVariable("lon"); if (lon == null) { logger.error("Cannot find Variable longitude(lon)"); return; } ArrayFloat.D1 absolutelon = (ArrayFloat.D1) lon.read(); Variable pres = netCDFfile.findVariable("pr"); if (pres == null) { logger.error("Cannot find Variable precipitation(pr)"); return; } Random rand = new Random(); int orig_lat = rand.nextInt((int) lat.getSize()); orig_lat = Math.min(orig_lat, (int) (lat.getSize() - SIZE)); int orig_lon = rand.nextInt((int) lon.getSize()); orig_lon = Math.min(orig_lon, (int) (lon.getSize() - SIZE)); int[] origin = new int[] {0, orig_lat, orig_lon}; int[] size = new int[] {1, SIZE, SIZE}; ArrayFloat.D3 data3D = (ArrayFloat.D3) pres.read(origin, size); double max = Double.NEGATIVE_INFINITY; double min = Double.POSITIVE_INFINITY; double sum = 0; for (int j = 0; j < SIZE; j++) { for (int k = 0; k < SIZE; k++) { double current = data3D.get(0, j, k); max = (current > max ? current : max); min = (current < min ? current : min); sum += current; } } logger.info( days + "," + absolutelat.get(orig_lat) + "," + absolutelon.get(orig_lon) + "," + SIZE + ":" + min + "," + max + "," + sum / (SIZE * SIZE)); }
/** * Make the station variables from a representative station * * @param stations list of stations * @param dim station dimension * @return the list of variables */ protected List<Variable> makeStationVars(List<GempakStation> stations, Dimension dim) { int numStations = stations.size(); boolean useSTID = true; for (GempakStation station : stations) { if (station.getSTID().equals("")) { useSTID = false; break; } } List<Variable> vars = new ArrayList<Variable>(); List<String> stnKeyNames = gemreader.getStationKeyNames(); for (String varName : stnKeyNames) { Variable v = makeStationVariable(varName, dim); // use STNM or STID as the name or description Attribute stIDAttr = new Attribute("standard_name", "station_id"); if (varName.equals(GempakStation.STID) && useSTID) { v.addAttribute(stIDAttr); } if (varName.equals(GempakStation.STNM) && !useSTID) { v.addAttribute(stIDAttr); } vars.add(v); } // see if we fill these in completely now if ((dim != null) && (numStations > 0)) { for (Variable v : vars) { Array varArray; if (v.getDataType().equals(DataType.CHAR)) { int[] shape = v.getShape(); varArray = new ArrayChar.D2(shape[0], shape[1]); } else { varArray = get1DArray(v.getDataType(), numStations); } int index = 0; String varname = v.getFullName(); for (GempakStation stn : stations) { String test = ""; if (varname.equals(GempakStation.STID)) { test = stn.getName(); } else if (varname.equals(GempakStation.STNM)) { ((ArrayInt.D1) varArray) .set( index, // (int) (stn.getSTNM() / 10)); (int) (stn.getSTNM())); } else if (varname.equals(GempakStation.SLAT)) { ((ArrayFloat.D1) varArray).set(index, (float) stn.getLatitude()); } else if (varname.equals(GempakStation.SLON)) { ((ArrayFloat.D1) varArray).set(index, (float) stn.getLongitude()); } else if (varname.equals(GempakStation.SELV)) { ((ArrayFloat.D1) varArray).set(index, (float) stn.getAltitude()); } else if (varname.equals(GempakStation.STAT)) { test = stn.getSTAT(); } else if (varname.equals(GempakStation.COUN)) { test = stn.getCOUN(); } else if (varname.equals(GempakStation.STD2)) { test = stn.getSTD2(); } else if (varname.equals(GempakStation.SPRI)) { ((ArrayInt.D1) varArray).set(index, stn.getSPRI()); } else if (varname.equals(GempakStation.SWFO)) { test = stn.getSWFO(); } else if (varname.equals(GempakStation.WFO2)) { test = stn.getWFO2(); } if (!test.equals("")) { ((ArrayChar.D2) varArray).setString(index, test); } index++; } v.setCachedData(varArray, false); } } return vars; }
public static void main2(String args[]) throws Exception { final int NLVL = 2; final int NLAT = 6; final int NLON = 12; final int NumberOfRecords = 2; final float SAMPLE_PRESSURE = 900.0f; final float SAMPLE_TEMP = 9.0f; final float START_LAT = 25.0f; final float START_LON = -125.0f; // Create the file. String filename = "pres_temp_4D.nc"; NetcdfFileWriteable dataFile = null; try { // Create new netcdf-3 file with the given filename dataFile = NetcdfFileWriteable.createNew(filename, false); // add dimensions where time dimension is unlimit Dimension lvlDim = dataFile.addDimension("level", NLVL); Dimension latDim = dataFile.addDimension("latitude", NLAT); Dimension lonDim = dataFile.addDimension("longitude", NLON); Dimension timeDim = dataFile.addDimension("time", 1000); // should // not be // need // second // argument ArrayList dims = null; // Define the coordinate variables. dataFile.addVariable("latitude", DataType.FLOAT, new Dimension[] {latDim}); dataFile.addVariable("longitude", DataType.FLOAT, new Dimension[] {lonDim}); // Define units attributes for data variables. dataFile.addVariableAttribute("latitude", "units", "degrees_north"); dataFile.addVariableAttribute("longitude", "units", "degrees_east"); // Define the netCDF variables for the pressure and temperature // data. dims = new ArrayList(); dims.add(timeDim); dims.add(lvlDim); dims.add(latDim); dims.add(lonDim); dataFile.addVariable("pressure", DataType.FLOAT, dims); dataFile.addVariable("temperature", DataType.FLOAT, dims); // Define units attributes for data variables. dataFile.addVariableAttribute("pressure", "units", "hPa"); dataFile.addVariableAttribute("temperature", "units", "celsius"); // Create some pretend data. If this wasn't an example program, we // would have some real data to write for example, model output. ArrayFloat.D1 lats = new ArrayFloat.D1(latDim.getLength()); ArrayFloat.D1 lons = new ArrayFloat.D1(lonDim.getLength()); int i, j; for (i = 0; i < latDim.getLength(); i++) { lats.set(i, START_LAT + 5.f * i); } for (j = 0; j < lonDim.getLength(); j++) { lons.set(j, START_LON + 5.f * j); } // Create the pretend data. This will write our surface pressure and // surface temperature data. ArrayFloat.D4 dataTemp = new ArrayFloat.D4( NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength()); ArrayFloat.D4 dataPres = new ArrayFloat.D4( NumberOfRecords, lvlDim.getLength(), latDim.getLength(), lonDim.getLength()); for (int record = 0; record < NumberOfRecords; record++) { i = 0; for (int lvl = 0; lvl < NLVL; lvl++) for (int lat = 0; lat < NLAT; lat++) for (int lon = 0; lon < NLON; lon++) { dataPres.set(record, lvl, lat, lon, SAMPLE_PRESSURE + i); dataTemp.set(record, lvl, lat, lon, SAMPLE_TEMP + i++); } } // Create the file. At this point the (empty) file will be written // to disk dataFile.create(); // A newly created Java integer array to be initialized to zeros. int[] origin = new int[4]; dataFile.write("latitude", lats); dataFile.write("longitude", lons); dataFile.write("pressure", origin, dataPres); dataFile.write("temperature", origin, dataTemp); dataFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } catch (InvalidRangeException e) { e.printStackTrace(System.err); } finally { // if (dataFile != null) { // try { // dataFile.close(); // } catch (IOException ioe) { // ioe.printStackTrace(); // } // } } System.out.println("*** SUCCESS writing example file " + filename); }