private void checkIfAxis(Variable v) {
   String name = v.getName();
   if (name.equalsIgnoreCase("Longitude"))
     v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
   else if (name.equalsIgnoreCase("Latitude"))
     v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
 }
  /**
   * Wrap the given Variable, making it into a VariableDS. Delegate data reading to the original
   * variable. Take all metadata from original variable. Does not share cache, iosp.
   *
   * @param g logical container, if null use orgVar's group
   * @param orgVar the original Variable to wrap. The original Variable is not modified. Must not be
   *     a Structure, use StructureDS instead.
   * @param enhance if true, use NetcdfDataset.defaultEnhanceMode to define what enhancements are
   *     made. Note that this can change DataType and data values. You can also call enhance()
   *     later. If orgVar is VariableDS, then enhance is inherited from there, and this parameter is
   *     ignored.
   */
  public VariableDS(Group g, Variable orgVar, boolean enhance) {
    super(orgVar);
    if (g != null)
      this.group =
          g; // otherwise super() sets group; this affects the long name and the dimensions.
    setDimensions(getDimensionsString()); // reset the dimensions

    if (orgVar instanceof Structure)
      throw new IllegalArgumentException(
          "VariableDS must not wrap a Structure; name=" + orgVar.getName());

    // dont share cache, iosp : all IO is delegated
    this.ncfile = null;
    this.spiObject = null;
    createNewCache();

    this.orgVar = orgVar;
    this.orgDataType = orgVar.getDataType();

    if (orgVar instanceof VariableDS) {
      VariableDS ncVarDS = (VariableDS) orgVar;
      this.enhanceProxy = ncVarDS.enhanceProxy;
      this.scaleMissingProxy = ncVarDS.scaleMissingProxy;
      this.enhanceMode = ncVarDS.enhanceMode;

    } else {
      this.enhanceProxy = new EnhancementsImpl(this);
      if (enhance) {
        enhance(NetcdfDataset.getDefaultEnhanceMode());
      } else {
        this.scaleMissingProxy = new EnhanceScaleMissingImpl();
      }
    }
  }
Beispiel #3
0
 public static String getRasterName(Variable variable) {
   Attribute attribute = variable.findAttribute(Constants.ORIG_NAME_ATT_NAME);
   if (attribute != null) {
     return attribute.getStringValue();
   } else {
     return variable.getName();
   }
 }
  public static void doVar(NetcdfDataset netcdfdataset, String varS, String limits) {
    System.out.println("\r---------\r\r");
    ucar.nc2.Variable var = netcdfdataset.findVariable(varS);
    System.out.println(var.getName() + " " + var.getSize());

    Array arr;
    try {
      arr = var.read(limits);
      NCdump.printArray(arr, "array", System.out, null);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InvalidRangeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  /**
   * Generate a list of ViewVariables direct from a dataset. The list will be filtered to only
   * include variables with the specified name
   *
   * @param ds
   * @param variableNameFilter if not null, all ViewVariables in the response will have the name
   *     variableNameFilter
   * @return
   * @throws IOException
   */
  public static AbstractViewVariable[] fromNetCDFDataset(
      NetcdfDataset ds, String variableNameFilter) throws IOException {
    List<AbstractViewVariable> result = new ArrayList<>();

    for (Variable var : ds.getVariables()) {

      if (variableNameFilter != null) {
        if (!var.getName().equals(variableNameFilter)) {
          continue;
        }
      }

      AbstractViewVariable parsedViewVar = parseVariableRecursive(var);
      if (parsedViewVar != null) result.add(parsedViewVar);
    }

    return result.toArray(new AbstractViewVariable[result.size()]);
  }
 /**
  * Recursively parses attribute and variable paths, filling <code>attributeList</code> and <code>
  * variableList</code>.
  *
  * @param groups List of groups to recursively parse.
  */
 private void parseAttributesAndVariables(List<Group> groups) {
   for (Group group : groups) {
     String groupName = group.getName();
     List<Attribute> attributes = group.getAttributes();
     for (Attribute attribute : attributes) {
       String attributeName = attribute.getName();
       if (!groupName.endsWith("/")) attributeName = "/" + attributeName;
       attributeList.add(groupName + attributeName);
     }
     List<Variable> variables = group.getVariables();
     for (Variable variable : variables) {
       String variableName = variable.getName();
       if (!groupName.endsWith("/")) variableName = "/" + variableName;
       variableList.add(variableName);
     }
     groups = group.getGroups();
     parseAttributesAndVariables(groups);
   }
 }
  /**
   * Make a new VariableDS, delegate data reading to the original variable, but otherwise dont take
   * any info from it. This is used by NcML explicit mode.
   *
   * @param group the containing group; may not be null
   * @param parent parent Structure, may be null
   * @param shortName variable shortName, must be unique within the Group
   * @param orgVar the original Variable to wrap. The original Variable is not modified. Must not be
   *     a Structure, use StructureDS instead.
   */
  public VariableDS(Group group, Structure parent, String shortName, Variable orgVar) {
    super(null, group, parent, shortName);
    setDimensions(getDimensionsString()); // reset the dimensions

    if (orgVar instanceof Structure)
      throw new IllegalArgumentException(
          "VariableDS must not wrap a Structure; name=" + orgVar.getName());

    // dont share cache, iosp : all IO is delegated
    this.ncfile = null;
    this.spiObject = null;
    createNewCache();

    this.orgVar = orgVar;
    this.orgDataType = orgVar.getDataType();

    this.enhanceProxy = new EnhancementsImpl(this);
    this.scaleMissingProxy = new EnhanceScaleMissingImpl();
  }
Beispiel #8
0
 public static Variable[] getVariables(List<Variable> variables, String[] names) {
   if (variables == null || names == null) {
     return null;
   }
   if (variables.size() < names.length) {
     return null;
   }
   final Variable[] result = new Variable[names.length];
   for (int i = 0; i < names.length; i++) {
     final String name = names[i];
     for (Variable variable : variables) {
       if (variable.getName().equalsIgnoreCase(name)) {
         result[i] = variable;
         break;
       }
     }
     if (result[i] == null) {
       return null;
     }
   }
   return result;
 }
  /**
   * 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;
    }
  }
  /**
   * Add this coord as a variable in the netCDF file
   *
   * @param ncfile netCDF file to add to
   * @param g group in file
   */
  void addToNetcdfFile(NetcdfFile ncfile, Group g) {
    if (dontUseVertical) {
      typicalRecord = null;
      return;
    }

    if (g == null) {
      g = ncfile.getRootGroup();
    }

    // coordinate axis
    Variable v = new Variable(ncfile, g, null, getVariableName());
    v.setDataType(DataType.DOUBLE);

    String desc = lookup.getLevelDescription(typicalRecord);
    if (lookup instanceof Grib2GridTableLookup && usesBounds) {
      desc = "Layer between " + desc;
    }

    v.addAttribute(new Attribute("long_name", desc));
    v.addAttribute(new Attribute("units", lookup.getLevelUnit(typicalRecord)));

    // positive attribute needed for CF-1 Height and Pressure
    if (positive != null) {
      v.addAttribute(new Attribute("positive", positive));
    }

    if (units != null) {
      AxisType axisType;
      if (SimpleUnit.isCompatible("millibar", units)) {
        axisType = AxisType.Pressure;
      } else if (SimpleUnit.isCompatible("m", units)) {
        axisType = AxisType.Height;
      } else {
        axisType = AxisType.GeoZ;
      }

      if (lookup instanceof Grib2GridTableLookup || lookup instanceof Grib1GridTableLookup) {
        v.addAttribute(
            new Attribute("GRIB_level_type", Integer.toString(typicalRecord.getLevelType1())));
      } else {
        v.addAttribute(
            new Attribute("level_type", Integer.toString(typicalRecord.getLevelType1())));
      }
      v.addAttribute(new Attribute(_Coordinate.AxisType, axisType.toString()));
    }

    if (coordValues == null) {
      coordValues = new double[levels.size()];
      for (int i = 0; i < levels.size(); i++) {
        LevelCoord lc = (LevelCoord) levels.get(i);
        coordValues[i] = lc.mid;
      }
    }
    Array dataArray = Array.factory(DataType.DOUBLE, new int[] {coordValues.length}, coordValues);

    v.setDimensions(getVariableName());
    v.setCachedData(dataArray, true);

    ncfile.addVariable(g, v);

    if (usesBounds) {
      String boundsDimName = "bounds_dim";
      if (g.findDimension(boundsDimName) == null) {
        ncfile.addDimension(g, new Dimension(boundsDimName, 2, true));
      }

      String bname = getVariableName() + "_bounds";
      v.addAttribute(new Attribute("bounds", bname));
      v.addAttribute(new Attribute(_Coordinate.ZisLayer, "true"));

      Variable b = new Variable(ncfile, g, null, bname);
      b.setDataType(DataType.DOUBLE);
      b.setDimensions(getVariableName() + " " + boundsDimName);
      b.addAttribute(new Attribute("long_name", "bounds for " + v.getName()));
      b.addAttribute(new Attribute("units", lookup.getLevelUnit(typicalRecord)));

      Array boundsArray = Array.factory(DataType.DOUBLE, new int[] {coordValues.length, 2});
      ucar.ma2.Index ima = boundsArray.getIndex();
      for (int i = 0; i < coordValues.length; i++) {
        LevelCoord lc = (LevelCoord) levels.get(i);
        boundsArray.setDouble(ima.set(i, 0), lc.value1);
        boundsArray.setDouble(ima.set(i, 1), lc.value2);
      }
      b.setCachedData(boundsArray, true);

      ncfile.addVariable(g, b);
    }

    if (factors != null) {
      // check if already created
      if (g == null) {
        g = ncfile.getRootGroup();
      }
      if (g.findVariable("hybrida") != null) return;
      v.addAttribute(new Attribute("standard_name", "atmosphere_hybrid_sigma_pressure_coordinate"));
      v.addAttribute(new Attribute("formula_terms", "ap: hybrida b: hybridb ps: Pressure"));
      // create  hybrid factor variables
      // add hybrida variable
      Variable ha = new Variable(ncfile, g, null, "hybrida");
      ha.setDataType(DataType.DOUBLE);
      ha.addAttribute(new Attribute("long_name", "level_a_factor"));
      ha.addAttribute(new Attribute("units", ""));
      ha.setDimensions(getVariableName());
      // add data
      int middle = factors.length / 2;
      double[] adata;
      double[] bdata;
      if (levels.size() < middle) { // only partial data wanted
        adata = new double[levels.size()];
        bdata = new double[levels.size()];
      } else {
        adata = new double[middle];
        bdata = new double[middle];
      }
      for (int i = 0; i < middle && i < levels.size(); i++) adata[i] = factors[i];
      Array haArray = Array.factory(DataType.DOUBLE, new int[] {adata.length}, adata);
      ha.setCachedData(haArray, true);
      ncfile.addVariable(g, ha);

      // add hybridb variable
      Variable hb = new Variable(ncfile, g, null, "hybridb");
      hb.setDataType(DataType.DOUBLE);
      hb.addAttribute(new Attribute("long_name", "level_b_factor"));
      hb.addAttribute(new Attribute("units", ""));
      hb.setDimensions(getVariableName());
      // add data
      for (int i = 0; i < middle && i < levels.size(); i++) bdata[i] = factors[i + middle];
      Array hbArray = Array.factory(DataType.DOUBLE, new int[] {bdata.length}, bdata);
      hb.setCachedData(hbArray, true);
      ncfile.addVariable(g, hb);

      /*  // TODO: delete next time modifying code
      double[] adata = new double[ middle ];
      for( int i = 0; i < middle; i++ )
        adata[ i ] = factors[ i ];
      Array haArray = Array.factory(DataType.DOUBLE, new int[]{adata.length}, adata);
      ha.setCachedData(haArray, true);
      ncfile.addVariable(g, ha);

      // add hybridb variable
      Variable hb = new Variable(ncfile, g, null, "hybridb");
      hb.setDataType(DataType.DOUBLE);
      hb.addAttribute(new Attribute("long_name",  "level_b_factor" ));
      //hb.addAttribute(new Attribute("standard_name", "atmosphere_hybrid_sigma_pressure_coordinate" ));
      hb.addAttribute(new Attribute("units", ""));
      hb.setDimensions(getVariableName());
      // add data
      double[] bdata = new double[ middle ];
      for( int i = 0; i < middle; i++ )
        bdata[ i ] = factors[ i + middle ];
      Array hbArray = Array.factory(DataType.DOUBLE, new int[]{bdata.length}, bdata);
      hb.setCachedData(hbArray, true);
      ncfile.addVariable(g, hb);
      */
    }
  }
 /**
  * Set the Variable to wrap. Used by NcML explicit mode.
  *
  * @param orgVar original Variable, must not be a Structure
  */
 public void setOriginalVariable(ucar.nc2.Variable orgVar) {
   if (orgVar instanceof Structure)
     throw new IllegalArgumentException(
         "VariableDS must not wrap a Structure; name=" + orgVar.getName());
   this.orgVar = orgVar;
 }
Beispiel #12
0
 /**
  * Constructor
  *
  * @param ncVar: the netcdf Variable
  */
 NcSDFloat64(Variable ncVar) {
   super(ncVar.getName());
   this.ncVar = ncVar;
 }