private void writeAll() { List<MessageBean> beans = messageTable.getBeans(); HashMap<Integer, Message> map = new HashMap<Integer, Message>(2 * beans.size()); for (MessageBean mb : beans) { map.put(mb.m.hashCode(), mb.m); } if (fileChooser == null) fileChooser = new FileManager(null, null, null, (PreferencesExt) prefs.node("FileManager")); String defloc = (raf.getLocation() == null) ? "." : raf.getLocation(); String dirName = fileChooser.chooseDirectory(defloc); if (dirName == null) return; try { int count = 0; for (Message m : map.values()) { String header = m.getHeader(); if (header != null) { header = header.split(" ")[0]; } else { header = Integer.toString(Math.abs(m.hashCode())); } File file = new File(dirName + "/" + header + ".bufr"); FileOutputStream fos = new FileOutputStream(file); WritableByteChannel wbc = fos.getChannel(); wbc.write(ByteBuffer.wrap(m.getHeader().getBytes())); byte[] raw = scan.getMessageBytes(m); wbc.write(ByteBuffer.wrap(raw)); wbc.close(); count++; } JOptionPane.showMessageDialog( BufrMessageViewer.this, count + " successfully written to " + dirName); } catch (IOException e1) { JOptionPane.showMessageDialog(BufrMessageViewer.this, e1.getMessage()); e1.printStackTrace(); } }
public void writeRecord(String stnName, Date obsDate, StructureData sdata) throws IOException { StationTracker stnTracker = stationMap.get(stnName); ProfileTracker proTracker = stnTracker.profileMap.get(obsDate); if (proTracker == null) { proTracker = new ProfileTracker(profileIndex); stnTracker.profileMap.put(obsDate, proTracker); stnTracker.link.add(profileIndex); stnTracker.lastChild = profileIndex; stnTracker.numChildren++; try { originTime[0] = profileIndex; // 2d index timeArray.set(0, dateFormatter.toDateTimeStringISO(obsDate)); parentArray.set(0, stnTracker.parent_index); ncfile.writeStringData(timeName, originTime, timeArray); ncfile.write(parentStationIndex, originTime, parentArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } profileIndex++; } // needs to be wrapped as an ArrayStructure, even though we are only writing one at a time. ArrayStructureW sArray = new ArrayStructureW(sdata.getStructureMembers(), new int[] {1}); sArray.setStructureData(sdata, 0); // track the min and max date if ((minDate == null) || minDate.after(obsDate)) minDate = obsDate; if ((maxDate == null) || maxDate.before(obsDate)) maxDate = obsDate; timeArray.set(0, dateFormatter.toDateTimeStringISO(obsDate)); parentArray.set(0, proTracker.parent_index); proTracker.link.add(recno); proTracker.lastChild = recno; // not currently used proTracker.numChildren++; // write the recno record origin[0] = recno; // 1d index originTime[0] = recno; // 2d index try { ncfile.write("record", origin, sArray); ncfile.write(parentProfileIndex, originTime, parentArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } recno++; }
private void dumpDDS() { List<MessageBean> beans = messageTable.getBeans(); HashMap<Integer, Message> map = new HashMap<Integer, Message>(2 * beans.size()); for (MessageBean mb : beans) { map.put(mb.m.hashCode(), mb.m); } if (fileChooser == null) fileChooser = new FileManager(null, null, null, (PreferencesExt) prefs.node("FileManager")); String defloc = (raf.getLocation() == null) ? "." : raf.getLocation(); int pos = defloc.lastIndexOf("."); if (pos > 0) defloc = defloc.substring(0, pos); String filename = fileChooser.chooseFilenameToSave(defloc + ".txt"); if (filename == null) return; try { File file = new File(filename); FileOutputStream fos = new FileOutputStream(file); int count = 0; for (Message m : map.values()) { Formatter f = new Formatter(fos); m.dump(f); f.flush(); count++; } fos.close(); JOptionPane.showMessageDialog( BufrMessageViewer.this, count + " successfully written to " + filename); } catch (IOException e1) { JOptionPane.showMessageDialog(BufrMessageViewer.this, e1.getMessage()); e1.printStackTrace(); } }
private void writeStationData(List<ucar.unidata.geoloc.Station> stnList) throws IOException { this.stnList = stnList; int nstns = stnList.size(); stationMap = new HashMap<String, StationTracker>(2 * nstns); if (debug) System.out.println("stationMap created"); // now write the station data ArrayDouble.D1 latArray = new ArrayDouble.D1(nstns); ArrayDouble.D1 lonArray = new ArrayDouble.D1(nstns); ArrayDouble.D1 altArray = new ArrayDouble.D1(nstns); ArrayObject.D1 idArray = new ArrayObject.D1(String.class, nstns); ArrayObject.D1 descArray = new ArrayObject.D1(String.class, nstns); ArrayObject.D1 wmoArray = new ArrayObject.D1(String.class, nstns); for (int i = 0; i < stnList.size(); i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); stationMap.put(stn.getName(), new StationTracker(i)); latArray.set(i, stn.getLatitude()); lonArray.set(i, stn.getLongitude()); if (useAlt) altArray.set(i, stn.getAltitude()); idArray.set(i, stn.getName()); descArray.set(i, stn.getDescription()); if (useWmoId) wmoArray.set(i, stn.getWmoId()); } try { ncfile.write(latName, latArray); ncfile.write(lonName, lonArray); if (useAlt) ncfile.write(altName, altArray); ncfile.writeStringData(idName, idArray); ncfile.writeStringData(descName, descArray); if (useWmoId) ncfile.writeStringData(wmoName, wmoArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } }
private void writeDataFinish() throws IOException { // finish global variables ArrayInt.D0 totalArray = new ArrayInt.D0(); totalArray.set(profileIndex); try { ncfile.write(numProfilesTotalName, totalArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } // finish the station data int nstns = stnList.size(); ArrayInt.D1 firstProfileArray = new ArrayInt.D1(nstns); ArrayInt.D1 numProfileArray = new ArrayInt.D1(nstns); ArrayInt.D1 nextProfileArray = new ArrayInt.D1(nprofiles); for (int i = 0; i < stnList.size(); i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); StationTracker tracker = stationMap.get(stn.getName()); numProfileArray.set(i, tracker.numChildren); int first = (tracker.link.size() > 0) ? tracker.link.get(0) : -1; firstProfileArray.set(i, first); if (tracker.link.size() > 0) { // construct forward link List<Integer> nextList = tracker.link; for (int j = 0; j < nextList.size() - 1; j++) { Integer curr = nextList.get(j); Integer next = nextList.get(j + 1); nextProfileArray.set(curr, next); } Integer curr = nextList.get(nextList.size() - 1); nextProfileArray.set(curr, -1); } } try { ncfile.write(firstProfileName, firstProfileArray); ncfile.write(numProfilesName, numProfileArray); ncfile.write(nextProfileName, nextProfileArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } // finish the profile data ArrayInt.D1 nextObsArray = new ArrayInt.D1(recno); ArrayInt.D1 firstObsArray = new ArrayInt.D1(nprofiles); ArrayInt.D1 numObsArray = new ArrayInt.D1(nprofiles); for (int i = 0; i < stnList.size(); i++) { ucar.unidata.geoloc.Station stn = stnList.get(i); StationTracker stnTracker = stationMap.get(stn.getName()); Set<Date> dates = stnTracker.profileMap.keySet(); for (Date date : dates) { ProfileTracker proTracker = stnTracker.profileMap.get(date); int trackerIndex = proTracker.parent_index; numObsArray.set(trackerIndex, proTracker.numChildren); int first = (proTracker.link.size() > 0) ? proTracker.link.get(0) : -1; firstObsArray.set(trackerIndex, first); if (proTracker.link.size() > 0) { // construct forward link List<Integer> nextList = proTracker.link; for (int j = 0; j < nextList.size() - 1; j++) { Integer curr = nextList.get(j); Integer next = nextList.get(j + 1); nextObsArray.set(curr, next); } Integer curr = nextList.get(nextList.size() - 1); nextObsArray.set(curr, -1); } } } try { ncfile.write(firstObsName, firstObsArray); ncfile.write(numObsName, numObsArray); ncfile.write(nextObsName, nextObsArray); } catch (InvalidRangeException e) { e.printStackTrace(); throw new IllegalStateException(e); } // finish the obs data ncfile.updateAttribute( null, new Attribute("time_coverage_start", dateFormatter.toDateTimeStringISO(minDate))); ncfile.updateAttribute( null, new Attribute("time_coverage_end", dateFormatter.toDateTimeStringISO(maxDate))); }
public VariableSimpleIF getDataVariable(String name) { return ((VariableSimpleIF) variablesMap.get(name)); }
/** * Setup needed for all SingleTrajectoryObsDatatypes. Can only be called once. * * <p>Units of time varible must be udunits time units. Units of latitude variable must be * convertible to "degrees_north" by udunits. Units of longitude variable must be convertible to * "degrees_east" by udunits. Units of altitude variable must be convertible to "meters" by * udunits. * * @throws IllegalArgumentException if units of time, latitude, longitude, or altitude variables * are not as required. * @throws IllegalStateException if this method has already been called. */ public void setTrajectoryInfo(Config trajConfig) throws IOException { if (timeDim != null) throw new IllegalStateException("The setTrajectoryInfo() method can only be called once."); this.trajectoryId = trajConfig.getTrajectoryId(); this.timeDim = trajConfig.getTimeDim(); this.timeVar = trajConfig.getTimeVar(); this.latVar = trajConfig.getLatVar(); this.lonVar = trajConfig.getLonVar(); this.elevVar = trajConfig.getElevVar(); trajectoryNumPoint = this.timeDim.getLength(); timeVarUnitsString = this.timeVar.findAttribute("units").getStringValue(); // Check that time, lat, lon, elev units are acceptable. if (DateUnit.getStandardDate(timeVarUnitsString) == null) { throw new IllegalArgumentException( "Units of time variable <" + timeVarUnitsString + "> not a date unit."); } String latVarUnitsString = this.latVar.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(latVarUnitsString, "degrees_north")) { throw new IllegalArgumentException( "Units of lat var <" + latVarUnitsString + "> not compatible with \"degrees_north\"."); } String lonVarUnitsString = this.lonVar.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(lonVarUnitsString, "degrees_east")) { throw new IllegalArgumentException( "Units of lon var <" + lonVarUnitsString + "> not compatible with \"degrees_east\"."); } String elevVarUnitsString = this.elevVar.findAttribute("units").getStringValue(); if (!SimpleUnit.isCompatible(elevVarUnitsString, "meters")) { throw new IllegalArgumentException( "Units of elev var <" + elevVarUnitsString + "> not compatible with \"meters\"."); } try { elevVarUnitsConversionFactor = getMetersConversionFactor(elevVarUnitsString); } catch (Exception e) { throw new IllegalArgumentException( "Exception on getMetersConversionFactor() for the units of elev var <" + elevVarUnitsString + ">."); } if (this.ncfile.hasUnlimitedDimension() && this.ncfile.getUnlimitedDimension().equals(timeDim)) { Object result = this.ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE); if ((result != null) && (Boolean) result) this.recordVar = (Structure) this.ncfile.getRootGroup().findVariable("record"); else this.recordVar = new StructurePseudo(this.ncfile, null, "record", timeDim); } else { this.recordVar = new StructurePseudo(this.ncfile, null, "record", timeDim); } // @todo HACK, HACK, HACK - remove once addRecordStructure() deals with ncd attribute changes. Variable elevVarInRecVar = this.recordVar.findVariable(this.elevVar.getFullNameEscaped()); if (!elevVarUnitsString.equals(elevVarInRecVar.findAttribute("units").getStringValue())) { elevVarInRecVar.addAttribute(new Attribute("units", elevVarUnitsString)); } trajectoryVarsMap = new HashMap(); // for ( Iterator it = this.recordVar.getVariables().iterator(); it.hasNext(); ) for (Iterator it = this.ncfile.getRootGroup().getVariables().iterator(); it.hasNext(); ) { Variable curVar = (Variable) it.next(); if (curVar.getRank() > 0 && !curVar.equals(this.timeVar) && !curVar.equals(this.latVar) && !curVar.equals(this.lonVar) && !curVar.equals(this.elevVar) && (this.recordVar == null ? true : !curVar.equals(this.recordVar))) { MyTypedDataVariable typedVar = new MyTypedDataVariable(new VariableDS(null, curVar, true)); dataVariables.add(typedVar); trajectoryVarsMap.put(typedVar.getShortName(), typedVar); } } trajectory = new SingleTrajectory( this.trajectoryId, trajectoryNumPoint, this.timeVar, timeVarUnitsString, this.latVar, this.lonVar, this.elevVar, dataVariables, trajectoryVarsMap); startDate = trajectory.getTime(0); endDate = trajectory.getTime(trajectoryNumPoint - 1); ((SingleTrajectory) trajectory).setStartDate(startDate); ((SingleTrajectory) trajectory).setEndDate(endDate); }