Exemplo n.º 1
1
    // create from a dataset
    public VariableBean(Variable vs) {
      this.vs = vs;
      // vs = (v instanceof VariableEnhanced) ? (VariableEnhanced) v : new VariableStandardized( v);

      setName(vs.getShortName());
      setDescription(vs.getDescription());
      setUnits(vs.getUnitsString());
      setDataType(vs.getDataType().toString());

      // Attribute csAtt = vs.findAttribute("_coordSystems");
      // if (csAtt != null)
      //  setCoordSys( csAtt.getStringValue());

      // collect dimensions
      StringBuilder lens = new StringBuilder();
      StringBuilder names = new StringBuilder();
      java.util.List dims = vs.getDimensions();
      for (int j = 0; j < dims.size(); j++) {
        ucar.nc2.Dimension dim = (ucar.nc2.Dimension) dims.get(j);
        if (j > 0) {
          lens.append(",");
          names.append(",");
        }
        String name = dim.isShared() ? dim.getName() : "anon";
        names.append(name);
        lens.append(dim.getLength());
      }
      setDimensions(names.toString());
      setShape(lens.toString());
    }
Exemplo n.º 2
0
  private Variable hasUnits(NetcdfDataset ds, String unitList) {
    List<Variable> varList = ds.getVariables();
    StringTokenizer stoker = new StringTokenizer(unitList, ",");
    while (stoker.hasMoreTokens()) {
      String unit = stoker.nextToken();

      for (Variable ve : varList) {
        String hasUnit = ve.getUnitsString();
        if (hasUnit == null) continue;
        if (hasUnit.equalsIgnoreCase(unit)) return ve;
      }
    }
    return null;
  }
Exemplo n.º 3
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);
  }
Exemplo n.º 5
0
  /**
   * Parses a variable recursively into appropriate ViewVariable implementations
   *
   * @throws IOException
   * @throws
   */
  private static AbstractViewVariable parseVariableRecursive(Variable var) throws IOException {
    List<Dimension> dimensions = var.getDimensions();

    // A single dimension means we can parse a SimpleAxis
    if (dimensions.size() == 1) {
      SimpleAxis axis =
          new SimpleAxis(var.getName(), var.getDataType().name(), var.getUnitsString(), null, null);
      Dimension d = dimensions.get(0);

      axis.setDimensionBounds(new SimpleBounds(0, d.getLength()));

      // Read our first and last values
      Array first = null, last = null;
      try {
        first = var.read(new int[] {0}, new int[] {1});
        last = var.read(new int[] {d.getLength() - 1}, new int[] {1});
      } catch (InvalidRangeException ex) {
        throw new IllegalArgumentException(
            String.format("Unable to read variable ranges '%1$s'", var), ex);
      }

      axis.setValueBounds(new SimpleBounds(first.getDouble(0), last.getDouble(0)));

      return axis;
      // Otherwise we have a multi dimensional variable that we can parse as a grid
    } else if (dimensions.size() > 0) {
      SimpleGrid grid =
          new SimpleGrid(var.getName(), var.getDataType().name(), var.getUnitsString(), null);
      List<AbstractViewVariable> childAxes = new ArrayList<>();

      // Recursively parse each dimension (which should map to a variable in the parent group)
      for (Dimension d : dimensions) {
        Variable mappedVariable = d.getGroup().findVariable(d.getName());
        if (mappedVariable == null) {
          // If the dimension doesn't map to a variable, we can't pull much information out of it
          // So instead we'll have to introduce an axis that only includes dimension bounds
          log.warn(
              String.format(
                  "Dimension '%1$s' has no matching variable in parent group '%2$s'",
                  d, d.getGroup()));

          SimpleAxis axis = new SimpleAxis(d.getName(), DataType.FLOAT.name(), "????", null, null);
          axis.setDimensionBounds(new SimpleBounds(0, d.getLength() - 1));
          childAxes.add(axis);
        } else {
          AbstractViewVariable parsedVar = parseVariableRecursive(mappedVariable);

          if (parsedVar != null) childAxes.add(parsedVar);
        }
      }

      if (childAxes.size() > 0) {
        grid.setAxes(childAxes.toArray(new AbstractViewVariable[childAxes.size()]));
        return grid;
      } else {
        return null;
      }
    } else {
      // Currently unsupported...
      log.warn(
          String.format("Variables with 0 dimensions are currently unsupported. var='%1$s'", var));
      return null;
    }
  }