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 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))); }
/** * 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; }