Java Code Examples for ucar.nc2.Variable#addAttribute()

The following examples show how to use ucar.nc2.Variable#addAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataToCDM.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array createVar(DataCursor data) throws DapException {
  Array array = null;
  DapVariable d4var = (DapVariable) data.getTemplate();
  switch (d4var.getBaseType().getTypeSort()) {
    default: // atomic var
      array = createAtomicVar(data);
      break;
    case Sequence:
      array = createSequence(data);
      break;
    case Structure:
      array = createStructure(data);
      break;
  }
  if (d4var.isTopLevel() && this.dsp.getChecksumMode().enabled(dsp.getChecksumMode())) {
    // transfer the checksum attribute
    int csum = d4var.getChecksum();
    String scsum = String.format("0x%08x", csum);
    Variable cdmvar = (Variable) nodemap.get(d4var);
    Attribute acsum = new Attribute(DapUtil.CHECKSUMATTRNAME, scsum);
    cdmvar.addAttribute(acsum);
  }
  return array;
}
 
Example 2
Source File: Suomi.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void augmentDataset(NetcdfDataset ds, CancelTask cancelTask) {
  String start_date = ds.findAttValueIgnoreCase(null, "start_date", null);
  if (start_date == null)
    return;

  SimpleDateFormat df = new SimpleDateFormat("yyyy.DDD.HH.mm.ss"); // "2006.105.00.00.00"
  DateFormatter dfo = new DateFormatter();

  Date start;
  try {
    start = df.parse(start_date);
  } catch (ParseException e) {
    throw new RuntimeException("Cant read start_date=" + start_date);
  }

  Variable v = ds.findVariable("time_offset");
  v.addAttribute(new Attribute(CDM.UNITS, "seconds since " + dfo.toDateTimeString(start)));

  Group root = ds.getRootGroup();
  root.addAttribute(new Attribute(CDM.CONVENTIONS, "Suomi-Station-CDM"));
  ds.finish();
}
 
Example 3
Source File: FslWindProfiler.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void augmentDataset(NetcdfDataset ds, CancelTask cancelTask) {
  for (Variable v : ds.getVariables()) {
    switch (v.getShortName()) {
      case "staName":
        v.addAttribute(new Attribute("standard_name", "station_name"));
        break;
      case "staLat":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
        break;
      case "staLon":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
        break;
      case "staElev":
      case "levels":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Height.toString()));
        break;
      case "timeObs":
        v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
        break;
    }
  }
  ds.finish();
}
 
Example 4
Source File: AreaReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Set the area directory attributes on the variable
 *
 * @param v the variable to set them on
 */
private void setAreaDirectoryAttributes(Variable v) {
  if ((dirBlock == null) || (ad == null)) {
    return;
  }
  for (int i = 1; i < 14; i++) {
    if (i == 7) {
      continue;
    }
    v.addAttribute(new Attribute(getADDescription(i), dirBlock[i]));
  }
}
 
Example 5
Source File: AreaReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Set the navigation block attributes on the variable
 *
 * @param v the variable to set them on
 */
private void setNavBlockAttributes(Variable v) {
  if ((navBlock == null) || (ad == null)) {
    return;
  }
  v.addAttribute(new Attribute("navigation_type", McIDASUtil.intBitsToString(navBlock[0])));
}
 
Example 6
Source File: GtopoIosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {
  super.open(raf, ncfile, cancelTask);

  readHDR();

  ncfile.addDimension(null, new Dimension("lat", nlats));
  ncfile.addDimension(null, new Dimension("lon", nlons));

  Variable elev = new Variable(ncfile, null, null, "elevation");
  elev.setDataType(DataType.SHORT);
  elev.setDimensions("lat lon");

  elev.addAttribute(new Attribute(CDM.UNITS, "m"));
  elev.addAttribute(new Attribute("units_desc", "meters above sea level"));
  elev.addAttribute(new Attribute(CDM.LONG_NAME, "digital elevation in meters above mean sea level"));
  elev.addAttribute(new Attribute(CDM.MISSING_VALUE, (short) -9999));
  ncfile.addVariable(null, elev);

  Variable lat = new Variable(ncfile, null, null, "lat");
  lat.setDataType(DataType.FLOAT);
  lat.setDimensions("lat");
  lat.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS));
  ncfile.addVariable(null, lat);
  Array data = Array.makeArray(DataType.FLOAT, nlats, starty, -incr);
  lat.setCachedData(data, false);

  Variable lon = new Variable(ncfile, null, null, "lon");
  lon.setDataType(DataType.FLOAT);
  lon.setDimensions("lon");
  lon.addAttribute(new Attribute(CDM.UNITS, CDM.LON_UNITS));
  ncfile.addVariable(null, lon);
  Array lonData = Array.makeArray(DataType.FLOAT, nlons, startx, incr);
  lon.setCachedData(lonData, false);

  ncfile.addAttribute(null, new Attribute(CDM.CONVENTIONS, "CF-1.0"));
  ncfile.addAttribute(null, new Attribute("History", "Direct read by Netcdf-Java CDM library"));
  ncfile.addAttribute(null, new Attribute("Source", "http://eros.usgs.gov/products/elevation/gtopo30.html"));

  ncfile.finish();
}
 
Example 7
Source File: GradsBinaryGridServiceProvider.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Add the appropriate attributes for a Z dimension
 *
 * @param zDim The GrADS Z dimension
 * @param v the variable to augment
 */
private void addZAttributes(GradsDimension zDim, Variable v) {
  if (zDim.getUnit().contains("Pa")) {
    v.addAttribute(new Attribute(CF.POSITIVE, CF.POSITIVE_DOWN));
    v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Pressure.toString()));
  } else {
    v.addAttribute(new Attribute(CF.POSITIVE, CF.POSITIVE_UP));
    v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Height.toString()));
  }
}
 
Example 8
Source File: Nimbus.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean setAxisType(NetcdfDataset ds, String varName, AxisType atype) {
  Variable v = ds.findVariable(varName);
  if (v == null)
    return false;

  v.addAttribute(new Attribute(_Coordinate.AxisType, atype.toString()));
  return true;
}
 
Example 9
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void augmentDataset(NetcdfDataset ds, CancelTask cancelTask) throws IOException {
  if (null != ds.findVariable("xCoord"))
    return; // check if its already been done - aggregating enhanced datasets.

  parseInfo.format("IFPS augmentDataset %n");

  // Figure out projection info. Assume the same for all variables
  VariableDS lonVar = (VariableDS) ds.findVariable("longitude");
  lonVar.setUnitsString(CDM.LON_UNITS);
  lonVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
  VariableDS latVar = (VariableDS) ds.findVariable("latitude");
  latVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
  latVar.setUnitsString(CDM.LAT_UNITS);

  projVar = latVar;
  String projName = ds.findAttValueIgnoreCase(projVar, "projectionType", null);
  if ("LAMBERT_CONFORMAL".equals(projName)) {
    Projection proj = makeLCProjection(ds);
    makeXYcoords(ds, proj, latVar, lonVar);
  }

  // figure out the time coordinate for each data variable
  // LOOK : always seperate; could try to discover if they are the same
  List<Variable> vars = ds.getVariables();
  for (Variable ncvar : vars) {
    // variables that are used but not displayable or have no data have DIM_0, also don't want history, since those
    // are just how the person edited the grids
    if ((!ncvar.getDimension(0).getShortName().equals("DIM_0")) && !ncvar.getShortName().endsWith("History")
        && (ncvar.getRank() > 2) && !ncvar.getShortName().startsWith("Tool")) {
      createTimeCoordinate(ds, ncvar);
    } else if (ncvar.getShortName().equals("Topo")) {
      // Deal with Topography variable
      ncvar.addAttribute(new Attribute(CDM.LONG_NAME, "Topography"));
      ncvar.addAttribute(new Attribute(CDM.UNITS, "ft"));
    }
  }

  ds.finish();
}
 
Example 10
Source File: AreaReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Set the long name and units for the calibration type
 *
 * @param image image variable
 * @param calType calibration type
 */
private void setCalTypeAttributes(Variable image, int calType) {
  String longName = "image values";
  // String unit = "";
  switch (calType) {

    case Calibrator.CAL_ALB:
      longName = "albedo";
      // unit = "%";
      break;

    case Calibrator.CAL_BRIT:
      longName = "brightness values";
      break;

    case Calibrator.CAL_TEMP:
      longName = "temperature";
      // unit = "K";
      break;

    case Calibrator.CAL_RAD:
      longName = "pixel radiance values";
      // unit = "mW/m2/sr/cm-1";
      break;

    case Calibrator.CAL_RAW:
      longName = "raw image values";
      break;

    default:
      break;
  }
  image.addAttribute(new Attribute("long_name", longName));
  if (calUnit != null) {
    image.addAttribute(new Attribute(CDM.UNITS, calUnit));
  }
  if (calScale != 1.f) {
    image.addAttribute(new Attribute("scale_factor", calScale));
  }

}
 
Example 11
Source File: CFGridWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void convertAttribute(Variable ctv, Attribute att, double scalef) {
  if (att == null)
    return;
  double val = scalef * att.getNumericValue().doubleValue();
  ctv.addAttribute(new Attribute(att.getShortName(), val));
}
 
Example 12
Source File: CFGridWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void addLatLon2D(NetcdfFile ncfile, List<Variable> varList, Projection proj, CoordinateAxis xaxis,
    CoordinateAxis yaxis) throws IOException {

  double[] xData = (double[]) xaxis.read().get1DJavaArray(double.class);
  double[] yData = (double[]) yaxis.read().get1DJavaArray(double.class);

  List<Dimension> dims = new ArrayList<Dimension>();
  dims.add(yaxis.getDimension(0));
  dims.add(xaxis.getDimension(0));

  Variable latVar = new Variable(ncfile, null, null, "lat");
  latVar.setDataType(DataType.DOUBLE);
  latVar.setDimensions(dims);
  latVar.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS));
  latVar.addAttribute(new Attribute(CDM.LONG_NAME, "latitude coordinate"));
  latVar.addAttribute(new Attribute(CF.STANDARD_NAME, "latitude"));
  latVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));

  Variable lonVar = new Variable(ncfile, null, null, "lon");
  lonVar.setDataType(DataType.DOUBLE);
  lonVar.setDimensions(dims);
  lonVar.addAttribute(new Attribute(CDM.UNITS, CDM.LON_UNITS));
  lonVar.addAttribute(new Attribute(CDM.LONG_NAME, "longitude coordinate"));
  lonVar.addAttribute(new Attribute(CF.STANDARD_NAME, "longitude"));
  lonVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));

  int nx = xData.length;
  int ny = yData.length;

  // create the data
  ProjectionPointImpl projPoint = new ProjectionPointImpl();
  LatLonPointImpl latlonPoint = new LatLonPointImpl();
  double[] latData = new double[nx * ny];
  double[] lonData = new double[nx * ny];
  for (int i = 0; i < ny; i++) {
    for (int j = 0; j < nx; j++) {
      projPoint.setLocation(xData[j], yData[i]);
      proj.projToLatLon(projPoint, latlonPoint);
      latData[i * nx + j] = latlonPoint.getLatitude();
      lonData[i * nx + j] = latlonPoint.getLongitude();
    }
  }
  Array latDataArray = Array.factory(DataType.DOUBLE, new int[] {ny, nx}, latData);
  latVar.setCachedData(latDataArray, false);

  Array lonDataArray = Array.factory(DataType.DOUBLE, new int[] {ny, nx}, lonData);
  lonVar.setCachedData(lonDataArray, false);

  varList.add(latVar);
  varList.add(lonVar);
}
 
Example 13
Source File: CEDRICRadarConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void augmentDataset(NetcdfDataset ncDataset, CancelTask cancelTask) throws IOException {
  /*
   * float lat = 40.45f;
   * float lon = -104.64f;
   * ProjectionImpl projection = new FlatEarth(lat, lon);
   * 
   * Variable ct = new Variable( ncDataset, null, null, projection.getClassName());
   * ct.setDataType( DataType.CHAR);
   * ct.setDimensions( "");
   * 
   * ct.addAttribute( new Attribute("grid_mapping_name", "flat_earth"));
   * ct.addAttribute( new Attribute(_Coordinate.TransformType, "Projection"));
   * ct.addAttribute( new Attribute(_Coordinate.Axes, "GeoX GeoY"));
   * ncDataset.addVariable(null, ct);
   */
  NcMLReader.wrapNcMLresource(ncDataset, CoordSysBuilder.resourcesDir + "CEDRICRadar.ncml", cancelTask);
  Variable lat = ncDataset.findVariable("radar_latitude");
  Variable lon = ncDataset.findVariable("radar_longitude");
  float latv = (float) lat.readScalarDouble();
  float lonv = (float) lon.readScalarDouble();
  Variable pv = ncDataset.findVariable("Projection");
  pv.addAttribute(new Attribute("longitude_of_projection_origin", lonv));
  pv.addAttribute(new Attribute("latitude_of_projection_origin", latv));

  Variable tvar = ncDataset.findVariable("time");
  /*
   * Date dt = null;
   * Variable sdate = ncDataset.findVariable("start_date");
   * Variable stime = ncDataset.findVariable("start_time");
   * String dateStr = sdate.readScalarString();
   * String timeStr = stime.readScalarString();
   * try {
   * dt = DateUtil.parse(dateStr + " " + timeStr);
   * } catch (Exception e) {}
   */

  int nt = 1;

  ArrayDouble.D1 data = new ArrayDouble.D1(nt);

  // fake
  data.setDouble(0, 0);
  // data.setDouble(0, dt.getTime()/1000);

  tvar.setCachedData(data, false);

  super.augmentDataset(ncDataset, cancelTask);
}
 
Example 14
Source File: Nimbus.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void augmentDataset(NetcdfDataset ds, CancelTask cancelTask) throws IOException {
  ds.addAttribute(null, new Attribute("cdm_data_type", ucar.nc2.constants.FeatureType.TRAJECTORY.name())); // deprecated
  ds.addAttribute(null, new Attribute(CF.FEATURE_TYPE, ucar.nc2.constants.FeatureType.TRAJECTORY.name()));

  if (!setAxisType(ds, "LATC", AxisType.Lat))
    if (!setAxisType(ds, "LAT", AxisType.Lat))
      setAxisType(ds, "GGLAT", AxisType.Lat);

  if (!setAxisType(ds, "LONC", AxisType.Lon))
    if (!setAxisType(ds, "LON", AxisType.Lon))
      setAxisType(ds, "GGLON", AxisType.Lon);

  if (!setAxisType(ds, "PALT", AxisType.Height))
    setAxisType(ds, "GGALT", AxisType.Height);

  boolean hasTime = setAxisType(ds, "Time", AxisType.Time);
  if (!hasTime)
    hasTime = setAxisType(ds, "time", AxisType.Time);

  // do we need to version this ?
  // String version = ds.findAttValueIgnoreCase(null, "version", null);

  if (!hasTime) {
    Variable time = ds.findVariable("time_offset");
    if (time != null) {
      Variable base = ds.findVariable("base_time");
      int base_time = base.readScalarInt();
      try {
        DateUnit dunit = new DateUnit("seconds since 1970-01-01 00:00");
        String time_units = "seconds since " + dunit.makeStandardDateString(base_time);
        time.addAttribute(new Attribute(CDM.UNITS, time_units));
        time.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.name()));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  // look for coordinates
  String coordinates = ds.findAttValueIgnoreCase(null, "coordinates", null);
  if (coordinates != null) {
    String[] vars = coordinates.split(" ");
    for (String vname : vars) {
      Variable v = ds.findVariable(vname);
      if (v != null) {
        AxisType atype = getAxisType(ds, (VariableEnhanced) v);
        if (atype != null)
          v.addAttribute(new Attribute(_Coordinate.AxisType, atype.name()));
      }
    }
  }

}
 
Example 15
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void createTimeCoordinate(NetcdfDataset ds, Variable ncVar) {
  // Time coordinate is stored in the attribute validTimes
  // One caveat is that the times have two bounds an upper and a lower

  // get the times values
  Attribute timesAtt = ncVar.findAttribute("validTimes");
  if (timesAtt == null)
    return;
  Array timesArray = timesAtt.getValues();

  // get every other one LOOK this is awkward
  try {
    int n = (int) timesArray.getSize();
    List<Range> list = new ArrayList<>();
    list.add(new Range(0, n - 1, 2));
    timesArray = timesArray.section(list);
  } catch (InvalidRangeException e) {
    throw new IllegalStateException(e);
  }

  // make sure it matches the dimension
  DataType dtype = DataType.getType(timesArray);
  int nTimesAtt = (int) timesArray.getSize();

  // create a special dimension and coordinate variable
  Dimension dimTime = ncVar.getDimension(0);
  int nTimesDim = dimTime.getLength();
  if (nTimesDim != nTimesAtt) {
    parseInfo.format(" **error ntimes in attribute (%d) doesnt match dimension length (%d) for variable %s%n",
        nTimesAtt, nTimesDim, ncVar.getFullName());
    return;
  }

  // add the dimension
  String dimName = ncVar.getFullName() + "_timeCoord";
  Dimension newDim = new Dimension(dimName, nTimesDim);
  ds.addDimension(null, newDim);

  // add the coordinate variable
  String units = "seconds since 1970-1-1 00:00:00";
  String desc = "time coordinate for " + ncVar.getFullName();

  CoordinateAxis1D timeCoord = new CoordinateAxis1D(ds, null, dimName, dtype, dimName, units, desc);
  timeCoord.setCachedData(timesArray, true);
  timeCoord.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
  ds.addCoordinateAxis(timeCoord);

  parseInfo.format(" added coordinate variable %s%n", dimName);

  // now make the original variable use the new dimension
  List<Dimension> dimsList = new ArrayList(ncVar.getDimensions());
  dimsList.set(0, newDim);
  ncVar.setDimensions(dimsList);

  // better to explicitly set the coordinate system
  ncVar.addAttribute(new Attribute(_Coordinate.Axes, dimName + " yCoord xCoord"));

  // fix the attributes
  Attribute att = ncVar.findAttribute("fillValue");
  if (att != null)
    ncVar.addAttribute(new Attribute(CDM.FILL_VALUE, att.getNumericValue()));
  att = ncVar.findAttribute("descriptiveName");
  if (null != att)
    ncVar.addAttribute(new Attribute(CDM.LONG_NAME, att.getStringValue()));

  // ncVar.enhance();
}
 
Example 16
Source File: UnidataObsConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void addAxisType(Variable v, AxisType a) {
  v.addAttribute(new Attribute(_Coordinate.AxisType, a.toString()));
}