private int countSeq(Structure recordStruct) throws IOException { int total = 0; int count = 0; int max = 0; StructureDataIterator iter = recordStruct.getStructureIterator(); try { while (iter.hasNext()) { StructureData sdata = iter.next(); ArraySequence seq1 = sdata.getArraySequence("seq1"); int n = seq1.getStructureDataCount(); total += n; count++; max = Math.max(max, n); } } finally { iter.finish(); } double avg = total / count; int wasted = count * max - total; double wp = (double) wasted / (count * max); System.out.println(" Max = " + max + " avg = " + avg + " wasted = " + wasted + " %= " + wp); return max; }
private void copySome(NetcdfFileWriteable ncfile, Variable oldVar, int nelems) throws IOException { String newName = N3iosp.makeValidNetcdfObjectName(oldVar.getShortName()); int[] shape = oldVar.getShape(); int[] origin = new int[oldVar.getRank()]; int size = shape[0]; for (int i = 0; i < size; i += nelems) { origin[0] = i; int left = size - i; shape[0] = Math.min(nelems, left); Array data; try { data = oldVar.read(origin, shape); if (oldVar.getDataType() == DataType.STRING) { data = convertToChar(ncfile.findVariable(newName), data); } if (data.getSize() > 0) { // zero when record dimension = 0 ncfile.write(newName, origin, data); if (debug) System.out.println("write " + data.getSize() + " bytes"); } } catch (InvalidRangeException e) { e.printStackTrace(); throw new IOException(e.getMessage()); } } }
private void init() throws IOException { members = new StructureMembers("stationObs"); // used to convert from adde format calendar = new GregorianCalendar(); calendar.setTimeZone(TimeZone.getTimeZone("GMT")); // time unit timeUnit = DateUnit.getUnixDateUnit(); try { AddePointDataReader reader = callAdde(addeURL); String[] params = reader.getParams(); String[] units = reader.getUnits(); int[] scales = reader.getScales(); scaleFactor = new double[params.length]; if (debugHead) System.out.println(" Param Unit Scale"); for (int paramNo = 0; paramNo < params.length; paramNo++) { // memberNames.add( params[i]); if (debugHead) System.out.println(" " + params[paramNo] + " " + units[paramNo] + " " + scales[paramNo]); if (scales[paramNo] != 0) scaleFactor[paramNo] = 1.0 / Math.pow(10.0, (double) scales[paramNo]); DataType dt = null; if ("CHAR".equals(units[paramNo])) dt = DataType.STRING; else if (scaleFactor[paramNo] == 0) dt = DataType.INT; else dt = DataType.DOUBLE; String unitString = null; if ((units[paramNo] != null) && (units[paramNo].length() > 0)) unitString = visad.jmet.MetUnits.makeSymbol(units[paramNo]); AddeTypedDataVariable tdv = new AddeTypedDataVariable(params[paramNo], unitString, dt); dataVariables.add(tdv); StructureMembers.Member m = members.addMember( tdv.getShortName(), tdv.getDescription(), tdv.getUnitsString(), tdv.getDataType(), tdv.getShape()); m.setDataParam(paramNo); members.addMember(m); } } catch (AddeException e) { e.printStackTrace(); throw new IOException(e.getMessage()); } }
private void createStations(List<ucar.unidata.geoloc.Station> stnList) throws IOException { int nstns = stnList.size(); // see if there's altitude, wmoId for any stations for (int i = 0; i < nstns; i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); // if (!Double.isNaN(stn.getAltitude())) // useAlt = true; if ((stn.getWmoId() != null) && (stn.getWmoId().trim().length() > 0)) useWmoId = true; } /* if (useAlt) ncfile.addGlobalAttribute("altitude_coordinate", altName); */ // find string lengths for (int i = 0; i < nstns; i++) { ucar.unidata.geoloc.Station station = stnList.get(i); name_strlen = Math.max(name_strlen, station.getName().length()); desc_strlen = Math.max(desc_strlen, station.getDescription().length()); if (useWmoId) wmo_strlen = Math.max(wmo_strlen, station.getName().length()); } LatLonRect llbb = getBoundingBox(stnList); ncfile.addGlobalAttribute( "geospatial_lat_min", Double.toString(llbb.getLowerLeftPoint().getLatitude())); ncfile.addGlobalAttribute( "geospatial_lat_max", Double.toString(llbb.getUpperRightPoint().getLatitude())); ncfile.addGlobalAttribute( "geospatial_lon_min", Double.toString(llbb.getLowerLeftPoint().getLongitude())); ncfile.addGlobalAttribute( "geospatial_lon_max", Double.toString(llbb.getUpperRightPoint().getLongitude())); // add the dimensions Dimension recordDim = ncfile.addUnlimitedDimension(recordDimName); recordDims.add(recordDim); Dimension stationDim = ncfile.addDimension(stationDimName, nstns); stationDims.add(stationDim); // add the station Variables using the station dimension Variable v = ncfile.addVariable(latName, DataType.DOUBLE, stationDimName); ncfile.addVariableAttribute(v, new Attribute("units", "degrees_north")); ncfile.addVariableAttribute(v, new Attribute("long_name", "station latitude")); v = ncfile.addVariable(lonName, DataType.DOUBLE, stationDimName); ncfile.addVariableAttribute(v, new Attribute("units", "degrees_east")); ncfile.addVariableAttribute(v, new Attribute("long_name", "station longitude")); if (useAlt) { v = ncfile.addVariable(altName, DataType.DOUBLE, stationDimName); ncfile.addVariableAttribute(v, new Attribute("units", "meters")); ncfile.addVariableAttribute(v, new Attribute("long_name", "station altitude")); } v = ncfile.addStringVariable(idName, stationDims, name_strlen); ncfile.addVariableAttribute(v, new Attribute("long_name", "station identifier")); v = ncfile.addStringVariable(descName, stationDims, desc_strlen); ncfile.addVariableAttribute(v, new Attribute("long_name", "station description")); if (useWmoId) { v = ncfile.addStringVariable(wmoName, stationDims, wmo_strlen); ncfile.addVariableAttribute(v, new Attribute("long_name", "station WMO id")); } v = ncfile.addVariable(numProfilesName, DataType.INT, stationDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "number of profiles in linked list for this station")); v = ncfile.addVariable(firstProfileName, DataType.INT, stationDimName); ncfile.addVariableAttribute( v, new Attribute("long_name", "index of first profile in linked list for this station")); }
public static boolean closeEnoughP(float d1, float d2) { if (Math.abs(d1) < TOLF) return Math.abs(d1 - d2) < TOLF; return Math.abs((d1 - d2) / d1) < TOLF; }
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException { NexradStationDB.init(); volScan = new Cinrad2VolumeScan(raf, cancelTask); if (volScan.hasDifferentDopplarResolutions()) throw new IllegalStateException("volScan.hasDifferentDopplarResolutions"); radialDim = new Dimension("radial", volScan.getMaxRadials()); ncfile.addDimension(null, radialDim); makeVariable( ncfile, Cinrad2Record.REFLECTIVITY, "Reflectivity", "Reflectivity", "R", volScan.getReflectivityGroups()); int velocity_type = (volScan.getDopplarResolution() == Cinrad2Record.DOPPLER_RESOLUTION_HIGH_CODE) ? Cinrad2Record.VELOCITY_HI : Cinrad2Record.VELOCITY_LOW; Variable v = makeVariable( ncfile, velocity_type, "RadialVelocity", "Radial Velocity", "V", volScan.getVelocityGroups()); makeVariableNoCoords( ncfile, Cinrad2Record.SPECTRUM_WIDTH, "SpectrumWidth", "Spectrum Width", v); if (volScan.getStationId() != null) { ncfile.addAttribute(null, new Attribute("Station", volScan.getStationId())); ncfile.addAttribute(null, new Attribute("StationName", volScan.getStationName())); ncfile.addAttribute( null, new Attribute("StationLatitude", new Double(volScan.getStationLatitude()))); ncfile.addAttribute( null, new Attribute("StationLongitude", new Double(volScan.getStationLongitude()))); ncfile.addAttribute( null, new Attribute("StationElevationInMeters", new Double(volScan.getStationElevation()))); double latRadiusDegrees = Math.toDegrees(radarRadius / ucar.unidata.geoloc.Earth.getRadius()); ncfile.addAttribute( null, new Attribute( "geospatial_lat_min", new Double(volScan.getStationLatitude() - latRadiusDegrees))); ncfile.addAttribute( null, new Attribute( "geospatial_lat_max", new Double(volScan.getStationLatitude() + latRadiusDegrees))); double cosLat = Math.cos(Math.toRadians(volScan.getStationLatitude())); double lonRadiusDegrees = Math.toDegrees(radarRadius / cosLat / ucar.unidata.geoloc.Earth.getRadius()); ncfile.addAttribute( null, new Attribute( "geospatial_lon_min", new Double(volScan.getStationLongitude() - lonRadiusDegrees))); ncfile.addAttribute( null, new Attribute( "geospatial_lon_max", new Double(volScan.getStationLongitude() + lonRadiusDegrees))); // add a radial coordinate transform (experimental) Variable ct = new Variable(ncfile, null, null, "radialCoordinateTransform"); ct.setDataType(DataType.CHAR); ct.setDimensions(""); // scalar ct.addAttribute(new Attribute("transform_name", "Radial")); ct.addAttribute(new Attribute("center_latitude", new Double(volScan.getStationLatitude()))); ct.addAttribute(new Attribute("center_longitude", new Double(volScan.getStationLongitude()))); ct.addAttribute(new Attribute("center_elevation", new Double(volScan.getStationElevation()))); ct.addAttribute(new Attribute(_Coordinate.TransformType, "Radial")); ct.addAttribute( new Attribute(_Coordinate.AxisTypes, "RadialElevation RadialAzimuth RadialDistance")); Array data = Array.factory(DataType.CHAR.getPrimitiveClassType(), new int[0], new char[] {' '}); ct.setCachedData(data, true); ncfile.addVariable(null, ct); } DateFormatter formatter = new DateFormatter(); ncfile.addAttribute(null, new Attribute(CDM.CONVENTIONS, _Coordinate.Convention)); ncfile.addAttribute(null, new Attribute("format", volScan.getDataFormat())); ncfile.addAttribute(null, new Attribute(CF.FEATURE_TYPE, FeatureType.RADIAL.toString())); // Date d = Cinrad2Record.getDate(volScan.getTitleJulianDays(), volScan.getTitleMsecs()); // ncfile.addAttribute(null, new Attribute("base_date", formatter.toDateOnlyString(d))); ncfile.addAttribute( null, new Attribute( "time_coverage_start", formatter.toDateTimeStringISO(volScan.getStartDate()))); ; // .toDateTimeStringISO(d))); ncfile.addAttribute( null, new Attribute("time_coverage_end", formatter.toDateTimeStringISO(volScan.getEndDate()))); ncfile.addAttribute( null, new Attribute(CDM.HISTORY, "Direct read of Nexrad Level 2 file into NetCDF-Java 2.2 API")); ncfile.addAttribute(null, new Attribute("DataType", "Radial")); ncfile.addAttribute( null, new Attribute( "Title", "Nexrad Level 2 Station " + volScan.getStationId() + " from " + formatter.toDateTimeStringISO(volScan.getStartDate()) + " to " + formatter.toDateTimeStringISO(volScan.getEndDate()))); ncfile.addAttribute( null, new Attribute( "Summary", "Weather Surveillance Radar-1988 Doppler (WSR-88D) " + "Level II data are the three meteorological base data quantities: reflectivity, mean radial velocity, and " + "spectrum width.")); ncfile.addAttribute( null, new Attribute( "keywords", "WSR-88D; NEXRAD; Radar Level II; reflectivity; mean radial velocity; spectrum width")); ncfile.addAttribute( null, new Attribute( "VolumeCoveragePatternName", Cinrad2Record.getVolumeCoveragePatternName(volScan.getVCP()))); ncfile.addAttribute( null, new Attribute("VolumeCoveragePattern", new Integer(volScan.getVCP()))); ncfile.addAttribute( null, new Attribute( "HorizonatalBeamWidthInDegrees", new Double(Cinrad2Record.HORIZONTAL_BEAM_WIDTH))); ncfile.finish(); }
public static double pdiff(double d1, double d2) { return Math.abs((d1 - d2) / d1); }
public static boolean closeEnough(float d1, float d2) { return Math.abs(d1 - d2) < TOLF; }
public static double diff(double d1, double d2) { return Math.abs(d1 - d2); }
public static boolean closeEnoughP(double d1, double d2, double tol) { if (Math.abs(d1) < tol) return Math.abs(d1 - d2) < tol; return Math.abs((d1 - d2) / d1) < tol; }
public static boolean closeEnough(double d1, double d2, double tol) { return Math.abs(d1 - d2) < tol; }
public static boolean closeEnough(double d1, double d2) { return Math.abs(d1 - d2) < TOL; }
public static boolean closeEnoughP(double d1, double d2) { if (Math.abs(d1) < TOL) return Math.abs(d1 - d2) < TOL; return Math.abs((d1 - d2) / d1) < TOL; }