Exemple #1
0
  public static Map<String, Metadata> readFile(String file) throws Exception {
    NetcdfFile n = NetcdfFile.open(file);
    System.out.println("Opened: " + file);

    /* Determine the size of our grid */
    int xLen = n.findDimension("x").getLength();
    int yLen = n.findDimension("y").getLength();
    System.out.println("Grid size: " + xLen + "x" + yLen);

    /* What time is this set of readings for? */
    Variable timeVar = n.findVariable("time");
    String timeStr = timeVar.getUnitsString().toUpperCase();
    timeStr = timeStr.replace("HOURS SINCE ", "");
    timeStr = timeStr.replace("HOUR SINCE ", "");

    /* Find the base date (the day) the reading was taken */
    Date baseDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(timeStr);

    /* Get the number of hours since the base date this reading was taken */
    int offset = timeVar.read().getInt(0);

    /* Generate the actual date for this reading */
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(baseDate);
    calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + offset);
    System.out.println("Time of collection: " + calendar.getTime());

    /* We'll keep a mapping of geolocations -> Galileo Metadata */
    Map<String, Metadata> metaMap = new HashMap<>();

    /* Determine the lat, lon coordinates for the grid points, and get each
     * reading at each grid point. */
    NetcdfDataset dataset = new NetcdfDataset(n);
    @SuppressWarnings("resource")
    GridDataset gridData = new GridDataset(dataset);
    for (GridDatatype g : gridData.getGrids()) {
      /* Let's look at 3D variables: these have WxH dimensions, plus a
       * single plane.  A 4D variable would contain elevation
       * and multiple planes as a result */
      if (g.getShape().length == 3) {
        convert3DVariable(g, calendar.getTime(), metaMap);
      }
    }

    return metaMap;
  }
  public static void main(String args[]) throws Exception {
    long start = System.currentTimeMillis();
    Map<String, ucar.unidata.geoloc.Station> staHash =
        new HashMap<String, ucar.unidata.geoloc.Station>();

    String location = "R:/testdata/sounding/netcdf/Upperair_20070401_0000.nc";
    NetcdfDataset ncfile = NetcdfDataset.openDataset(location);
    ncfile.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);

    // look through record varibles, for those that have "manLevel" dimension
    // make a StructureData object for those
    StructureMembers sm = new StructureMembers("manLevel");
    Dimension manDim = ncfile.findDimension("manLevel");
    Structure record = (Structure) ncfile.findVariable("record");
    List<Variable> allList = record.getVariables();
    List<VariableSimpleIF> varList = new ArrayList<VariableSimpleIF>();
    for (Variable v : allList) {
      if ((v.getRank() == 1) && v.getDimension(0).equals(manDim)) {
        // public VariableDS(NetcdfDataset ds, Group group, Structure parentStructure, String
        // shortName, DataType dataType,
        // String dims, String units, String desc) {
        varList.add(
            new VariableDS(
                ncfile,
                null,
                null,
                v.getShortName(),
                v.getDataType(),
                "",
                v.getUnitsString(),
                v.getDescription()));
        // (String name, String desc, String units, DataType dtype, int []shape)
        sm.addMember(
            v.getShortName(),
            v.getDescription(),
            v.getUnitsString(),
            v.getDataType(),
            new int[0]); // scalar
      }
    }

    ArrayStructureMA manAS = new ArrayStructureMA(sm, new int[] {manDim.getLength()});

    // need the date units
    Variable time = ncfile.findVariable("synTime");
    String timeUnits = ncfile.findAttValueIgnoreCase(time, "units", null);
    timeUnits = StringUtil.remove(timeUnits, '('); // crappy fsl'ism
    timeUnits = StringUtil.remove(timeUnits, ')');
    DateUnit timeUnit = new DateUnit(timeUnits);

    // extract stations
    int nrecs = 0;
    StructureDataIterator iter = record.getStructureIterator();
    while (iter.hasNext()) {
      StructureData sdata = iter.next();
      String name = sdata.getScalarString("staName");
      ucar.unidata.geoloc.Station s = staHash.get(name);
      if (s == null) {
        float lat = sdata.convertScalarFloat("staLat");
        float lon = sdata.convertScalarFloat("staLon");
        float elev = sdata.convertScalarFloat("staElev");
        s = new StationImpl(name, "", lat, lon, elev);
        staHash.put(name, s);
      }
      nrecs++;
    }
    List<ucar.unidata.geoloc.Station> stnList =
        Arrays.asList(staHash.values().toArray(new ucar.unidata.geoloc.Station[staHash.size()]));
    Collections.sort(stnList);

    // create the writer
    WriterProfileObsDataset writer =
        new WriterProfileObsDataset(location + ".out", "rewrite " + location);
    writer.writeHeader(stnList, varList, nrecs, "prMan");

    // extract records
    iter = record.getStructureIterator();
    while (iter.hasNext()) {
      StructureData sdata = iter.next();
      String name = sdata.getScalarString("staName");
      double timeValue = sdata.convertScalarDouble("synTime");
      Date date = timeUnit.makeDate(timeValue);

      // transfer to the ArrayStructure
      List<String> names = sm.getMemberNames();
      for (String mname : names) {
        manAS.setMemberArray(mname, sdata.getArray(mname));
      }

      // each level is weritten as a seperate structure
      int numMand = sdata.getScalarInt("numMand");
      if (numMand >= manDim.getLength()) continue;

      for (int i = 0; i < numMand; i++) {
        StructureData useData = manAS.getStructureData(i);
        writer.writeRecord(name, date, useData);
      }
    }

    writer.finish();

    long took = System.currentTimeMillis() - start;
    System.out.println("That took = " + took);
  }