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

The following examples show how to use ucar.nc2.Variable#getDataType() . 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: CFPointWriterUtils.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static Array getArrayFromMember(Variable var, Member m) {

    // DataType m_dt = writer.findVariable(m.getName()).getDataType();
    DataType v_dt = var.getDataType();
    // DataType m_dt = m.getDataType();

    // Writes one single data
    // int[] shape = writer.findVariable(m.getName()).getShape();
    int[] shape = var.getShape();

    // Set the shape we really want
    for (int i = 0; i < shape.length; i++) {
      shape[i] = 1;
    }

    Array arr = Array.factory(v_dt, shape);
    setDataArray(v_dt, arr, m);

    return arr;

  }
 
Example 2
Source File: NetcdfFormatWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Write String data to a CHAR variable.
 *
 * @param v variable to write to
 * @param origin offset to start writing, ignore the strlen dimension.
 * @param values write this array; must be ArrayObject of String
 * @throws IOException if I/O error
 * @throws InvalidRangeException if values Array has illegal shape
 */
public void writeStringDataToChar(Variable v, int[] origin, Array values) throws IOException, InvalidRangeException {
  if (values.getElementType() != String.class)
    throw new IllegalArgumentException("values must be an ArrayObject of String ");

  if (v.getDataType() != DataType.CHAR)
    throw new IllegalArgumentException("variable " + v.getFullName() + " is not type CHAR");

  int rank = v.getRank();
  int strlen = v.getShape(rank - 1);

  // turn it into an ArrayChar
  ArrayChar cvalues = ArrayChar.makeFromStringArray((ArrayObject) values, strlen);

  int[] corigin = new int[rank];
  System.arraycopy(origin, 0, corigin, 0, rank - 1);

  write(v, corigin, cvalues);
}
 
Example 3
Source File: DataImportExample.java    From tablestore-examples with Apache License 2.0 6 votes vote down vote up
/**
 * read data from netcdf file and write data to table store.
 * @param meta
 * @param ncFileName
 * @throws Exception
 */
public void importFromNcFile(GridDataSetMeta meta, String ncFileName) throws Exception {
    GridDataWriter writer = tableStoreGrid.getDataWriter(meta);
    NetcdfFile ncFile = NetcdfFile.open(ncFileName);
    List<Variable> variables = ncFile.getVariables();
    for (Variable variable : variables) {
        if (meta.getVariables().contains(variable.getShortName())) {
            for (int t = 0; t < meta.gettSize(); t++) {
                for (int z = 0; z < meta.getzSize(); z++) {
                    Array array = variable.read(new int[]{t, z, 0, 0}, new int[]{1, 1, meta.getxSize(), meta.getySize()});
                    Grid2D grid2D = new Grid2D(array.getDataAsByteBuffer(), variable.getDataType(),
                            new int[] {0, 0}, new int[] {meta.getxSize(), meta.getySize()});
                    writer.writeGrid2D(variable.getShortName(), t, z, grid2D);
                }
            }
        }
    }
}
 
Example 4
Source File: RecordDatasetHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Set extra information used by station obs datasets.
 * Use stnIdVName or stnIndexVName.
 *
 * @param stnIdVName the obs variable that is used to find the station in the stnHash; may be type int or a String
 *        (char).
 * @param stnDescVName optional station var containing station description
 */
public void setStationInfo(String stnIdVName, String stnDescVName, String stnIndexVName,
    StationHelper stationHelper) {
  this.stnIdVName = stnIdVName;
  this.stnDescVName = stnDescVName;
  this.stnIndexVName = stnIndexVName;
  this.stationHelper = stationHelper;

  if (stnIdVName != null) {
    Variable stationVar = ncfile.findVariable(stnIdVName);
    stationIdType = stationVar.getDataType();
  }
}
 
Example 5
Source File: FslRaob.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, TableConfig childTable, String outerDin,
    String innerDim) {
  Dimension parentDim = ds.findDimension(outerDin);
  Dimension childDim = ds.findDimension(innerDim);

  // divide up the variables between the parent and the child
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if ((dim1 != null) && dim1.equals(childDim))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  childTable.vars = obsVars;
}
 
Example 6
Source File: BufrFeatureDatasetFactory.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
List<VariableSimpleIF> makeDataVariables(BufrCdmIndex index, Structure obs) {
  this.sdataName = obs.getShortName() + "Munged";

  List<Variable> members = obs.getVariables();
  List<VariableSimpleIF> result = new ArrayList<>(members.size());

  List<BufrCdmIndexProto.Field> flds = index.root.getFldsList();
  int count = 0;
  for (Variable v : members) {
    BufrCdmIndexProto.Field fld = flds.get(count++);
    if (fld.getAction() != null && fld.getAction() != BufrCdmIndexProto.FldAction.none) {
      needed = true;
      Action act = new Action(fld.getAction());
      actions.put(v.getShortName(), act);

      if (fld.getAction() == BufrCdmIndexProto.FldAction.remove) {
        continue; // skip

      } else if (fld.getAction() == BufrCdmIndexProto.FldAction.asMissing) {
        // promote the children
        Structure s = (Structure) v;
        for (Variable child : s.getVariables()) {
          result.add(child);
          vars.put(child.getShortName(), (VariableDS) child); // track ones we may have to create missing values for
        }
        continue;
      }
    }

    if (v.getDataType() == DataType.SEQUENCE)
      continue;
    result.add(v);
  }
  return result;
}
 
Example 7
Source File: TestSimpleGeometryCSAll.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testSimpleGeometryBuildCS() throws IOException, InvalidRangeException {
  NetcdfDataset data =
      NetcdfDataset.openDataset(TestDir.cdmLocalTestDataDir + "dataset/SimpleGeos/hru_soil_moist_3hru_5timestep.nc");
  List<CoordinateSystem> csl = data.getCoordinateSystems();
  SimpleGeometryCSBuilder builder = new SimpleGeometryCSBuilder(data, csl.get(0), null);
  Variable hru_test = data.findVariable("hru_soil_moist");

  SimpleGeometryFeature sgc =
      new SimpleGeometryFeature(hru_test.getFullNameEscaped(), hru_test.getDataType(), hru_test.attributes(),
          csl.get(0).getName(), hru_test.getUnitsString(), hru_test.getDescription(), null, GeometryType.POLYGON);
  sgc.setCoordSys(builder.makeCoordSys());

  // Test retrieval of axis
  Assert.assertEquals("catchments_x", sgc.getXAxis().getFullNameEscaped());
  Assert.assertEquals("catchments_y", sgc.getYAxis().getFullNameEscaped());
  Assert.assertEquals("hruid", sgc.getIDAxis().getFullNameEscaped());
  Assert.assertEquals(null, sgc.getZAxis());

  // Now test reading a geometry.
  SimpleGeometry testGeometry = sgc.readGeometry(0);
  boolean testInstancePolygon = false;

  if (testGeometry instanceof Polygon)
    testInstancePolygon = true;
  Assert.assertEquals(true, testInstancePolygon);

  Polygon polygonTestGeometry = (Polygon) testGeometry;
  Assert.assertEquals(6233, polygonTestGeometry.getPoints().size());
}
 
Example 8
Source File: PointObsDatasetImpl.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected double getTime(Variable timeVar, StructureData sdata) throws ParseException {
  if (timeVar == null)
    return 0.0;

  if ((timeVar.getDataType() == DataType.CHAR) || (timeVar.getDataType() == DataType.STRING)) {
    String time = sdata.getScalarString(timeVar.getShortName());
    if (null == formatter)
      formatter = new DateFormatter();
    Date date = formatter.getISODate(time);
    return date.getTime() / 1000.0;
  } else {
    return sdata.convertScalarFloat(timeVar.getShortName());
  }
}
 
Example 9
Source File: N3headerWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void writeVars(List<Variable> vars, boolean largeFile, Formatter fout) throws IOException {
  int n = vars.size();
  if (n == 0) {
    raf.writeInt(0);
    raf.writeInt(0);
  } else {
    raf.writeInt(MAGIC_VAR);
    raf.writeInt(n);
  }

  for (Variable var : vars) {
    writeString(var.getShortName());

    // dimensions
    long vsize = var.getDataType().getSize(); // works for all netcdf-3 data types
    List<Dimension> dims = var.getDimensions();
    raf.writeInt(dims.size());
    for (Dimension dim : dims) {
      int dimIndex = findDimensionIndex(ncfile, dim);
      raf.writeInt(dimIndex);

      if (!dim.isUnlimited())
        vsize *= dim.getLength();
    }
    long unpaddedVsize = vsize;
    vsize += padding(vsize);

    // variable attributes
    long varAttsPos = raf.getFilePointer();
    writeAtts(var.attributes(), fout);

    // data type, variable size, beginning file position
    DataType dtype = var.getDataType();
    int type = getType(dtype);
    raf.writeInt(type);

    int vsizeWrite = (vsize < MAX_UNSIGNED_INT) ? (int) vsize : -1;
    raf.writeInt(vsizeWrite);
    long pos = raf.getFilePointer();
    if (largeFile)
      raf.writeLong(0); // come back to this later
    else
      raf.writeInt(0); // come back to this later

    // From nc3 file format specification
    // (https://www.unidata.ucar.edu/software/netcdf/docs/netcdf.html#NetCDF-Classic-Format):
    // Note on padding: In the special case of only a single record variable of character,
    // byte, or short type, no padding is used between data values.
    // 2/15/2011: we will continue to write the (incorrect) padded vsize into the header, but we will use the unpadded
    // size to read/write
    if (uvars.size() == 1 && uvars.get(0) == var) {
      if ((dtype == DataType.CHAR) || (dtype == DataType.BYTE) || (dtype == DataType.SHORT)) {
        vsize = unpaddedVsize;
      }
    }
    var.setSPobject(new N3headerNew.Vinfo(var.getShortName(), vsize, pos, var.isUnlimited(), varAttsPos));
  }
}
 
Example 10
Source File: NetcdfCopier.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Copies data from {@code oldVar} to {@code newVar}. The writes are done in a series of chunks no larger than
 * {@code maxChunkSize}
 * bytes.
 *
 * @param oldVar a variable from the original file to copy data from.
 * @param newVar a variable from the original file to copy data from.
 * @param maxChunkSize the size, <b>in bytes</b>, of the largest chunk to write.
 * @param cancel allow user to cancel, may be null.
 * @throws IOException if an I/O error occurs.
 */
private void copySome(NetcdfFormatWriter ncwriter, Variable oldVar, Variable newVar, long maxChunkSize,
    CancelTask cancel) throws IOException {
  long maxChunkElems = maxChunkSize / oldVar.getElementSize();
  long byteWriteTotal = 0;

  ChunkingIndex index = new ChunkingIndex(oldVar.getShape());
  while (index.currentElement() < index.getSize()) {
    try {
      int[] chunkOrigin = index.getCurrentCounter();
      int[] chunkShape = index.computeChunkShape(maxChunkElems);

      cancel.setProgress(
          "Reading chunk " + new Section(chunkOrigin, chunkShape) + " from variable: " + oldVar.getShortName(), -1);

      Array data = oldVar.read(chunkOrigin, chunkShape);
      if (!getOutputFormat().isNetdf4format() && oldVar.getDataType() == DataType.STRING) {
        data = convertDataToChar(newVar, data);
      }

      if (data.getSize() > 0) { // zero when record dimension = 0
        cancel.setProgress(
            "Writing chunk " + new Section(chunkOrigin, chunkShape) + " from variable: " + oldVar.getShortName(), -1);

        ncwriter.write(newVar, chunkOrigin, data);
        if (debugWrite) {
          System.out.println(" write " + data.getSize() + " bytes at " + new Section(chunkOrigin, chunkShape));
        }

        byteWriteTotal += data.getSize();
      }

      index.setCurrentCounter(index.currentElement() + (int) Index.computeSize(chunkShape));
      if (cancel.isCancel()) {
        return;
      }

    } catch (InvalidRangeException e) {
      e.printStackTrace();
      throw new IOException(e.getMessage());
    }
  }
}
 
Example 11
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Array reallyRead(Variable mainv, Section section, CancelTask cancelTask)
    throws IOException, InvalidRangeException {
  Vstate1D vstate = (Vstate1D) mainv.getSPobject();

  // read the original type - if its been promoted to a new type, the conversion happens after this read
  DataType dtype = (mainv instanceof VariableDS) ? ((VariableDS) mainv).getOriginalDataType() : mainv.getDataType();

  Array allData = Array.factory(dtype, section.getShape());
  int destPos = 0;

  // assumes the first dimension is time: LOOK: what about ensemble ??
  List<Range> ranges = section.getRanges();
  Range timeRange = ranges.get(0);
  List<Range> innerSection = ranges.subList(1, ranges.size());

  // keep track of open files - must be local variable for thread safety
  HashMap<String, NetcdfDataset> openFilesRead = new HashMap<>();

  try {

    // iterate over the desired forecast times
    for (int timeIdx : timeRange) {
      Array result = null;

      // find the inventory for this grid, runtime, and hour
      TimeInventory.Instance timeInv = vstate.timeInv.getInstance(vstate.gridLite, timeIdx);
      if (timeInv == null) {
        if (logger.isDebugEnabled())
          logger.debug("Missing Inventory timeInx=" + timeIdx + " for " + mainv.getFullName() + " in "
              + state.lite.collectionName);
        // vstate.timeInv.getInstance(vstate.gridLite, timeIdx); // allow debugger
      } else if (timeInv.getDatasetLocation() != null) {
        if (debugRead)
          System.out.printf("HIT %s%n", timeInv);
        result = read(timeInv, mainv.getFullNameEscaped(), innerSection, openFilesRead); // may return null
        result = MAMath.convert(result, dtype); // just in case it need to be converted
      }

      // may have missing data
      if (result == null) {
        int[] shape = new Section(innerSection).getShape();
        result = ((VariableDS) mainv).getMissingDataArray(shape); // fill with missing values
        if (debugRead)
          System.out.printf("MISS %d ", timeIdx);
      }

      if (debugRead)
        System.out.printf("%d reallyRead %s %d bytes start at %d total size is %d%n", timeIdx, mainv.getFullName(),
            result.getSize(), destPos, allData.getSize());

      Array.arraycopy(result, 0, allData, destPos, (int) result.getSize());
      destPos += result.getSize();
    }
    return allData;

  } finally {
    // close any files used during this operation
    closeAll(openFilesRead);
  }
}
 
Example 12
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void readMemberVariable(Group.Builder groupBuilder, StructureDS.Builder<?> parentStructure,
    @Nullable Structure refParentStructure, Element varElem) {
  String name = varElem.getAttributeValue("name");
  if (name == null) {
    errlog.format("NcML Variable name is required (%s)%n", varElem);
    return;
  }
  String nameInFile = Optional.ofNullable(varElem.getAttributeValue("orgName")).orElse(name);

  DataType dtype = null;
  String typeS = varElem.getAttributeValue("type");
  if (typeS != null) {
    dtype = DataType.getType(typeS);
  }

  // see if it already exists
  Variable refv = (refParentStructure == null) ? null : refParentStructure.findVariable(nameInFile);
  if (refv == null) { // new
    if (dtype == null) {
      errlog.format("NcML Variable dtype is required for new (nested) variable (%s)%n", name);
      return;
    }
    if (dtype == DataType.STRUCTURE || dtype == DataType.SEQUENCE) {
      parentStructure.addMemberVariable(readStructureNew(groupBuilder, varElem));
    } else {
      parentStructure.addMemberVariable(readVariableNew(groupBuilder, dtype, varElem));
    }
    return;
  }

  // refv exists
  if (dtype == null) {
    dtype = refv.getDataType();
  }

  if (dtype == DataType.STRUCTURE || dtype == DataType.SEQUENCE) {
    readStructureExisting(groupBuilder, parentStructure, dtype, (Structure) refv, varElem)
        .ifPresent(parentStructure::addMemberVariable);
  } else {
    readVariableExisting(groupBuilder, parentStructure, dtype, refv, varElem)
        .ifPresent(parentStructure::addMemberVariable);
  }
}
 
Example 13
Source File: Evaluator.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static boolean isEffectivelyScaler(Variable v) {
  return (v.getRank() == 0) || (v.getRank() == 1 && v.getDataType() == DataType.CHAR);
}
 
Example 14
Source File: CFpointObs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Identify ragged array representations for double nests (timeSeries profile, timeSeries trajectory)
 * <p/>
 * This uses the contiguous ragged array representation for each profile (9.5.43.3), and the indexed ragged array
 * representation to organise the profiles into time series (9.3.54). The canonical use case is when writing real-time
 * data streams that contain profiles from many stations, arriving randomly, with the data for each entire profile
 * written all at once.
 *
 * @param ds in this dataset
 * @param info put info here
 * @param errlog error go here
 * @return EncodingInfo if ragged array representations is found
 */
protected boolean identifyDoubleRaggeds(NetcdfDataset ds, EncodingInfo info, Formatter errlog) {
  // the timeseries are stored as ragged index
  Evaluator.VarAtt varatt = Evaluator.findVariableWithAttribute(ds, CF.INSTANCE_DIMENSION);
  if (varatt == null)
    varatt = Evaluator.findVariableWithAttribute(ds, CF.RAGGED_PARENTINDEX);
  if (varatt == null)
    return false;

  Variable ragged_parentIndex = varatt.var;
  String instanceDimName = varatt.att.getStringValue();
  Dimension stationDim = ds.findDimension(instanceDimName);

  if (stationDim == null) {
    errlog.format(
        "CFpointObs: Indexed ragged array representation: parent_index variable has illegal value for %s = %s%n",
        CF.INSTANCE_DIMENSION, instanceDimName);
    return false;
  }

  if (ragged_parentIndex.getDataType() != DataType.INT) {
    errlog.format("CFpointObs: Indexed ragged array representation: parent_index variable must be of type integer%n");
    return false;
  }

  if (ragged_parentIndex.getRank() != 1 && info.childStruct == null) {
    errlog.format("CFpointObs: Indexed ragged array representation: parent_index variable %s must be 1D %n",
        ragged_parentIndex);
    return false;
  }
  Dimension profileDim = (info.childDim != null) ? info.childDim : ragged_parentIndex.getDimension(0);

  // onto the profiles, stored contiguously
  varatt = Evaluator.findVariableWithAttribute(ds, CF.SAMPLE_DIMENSION);
  if (varatt == null)
    varatt = Evaluator.findVariableWithAttribute(ds, CF.RAGGED_ROWSIZE);
  if (varatt == null)
    return false;

  Variable ragged_rowSize = varatt.var;
  String obsDimName = varatt.att.getStringValue();
  Dimension obsDim = ds.findDimension(obsDimName);

  if (obsDimName == null) {
    errlog.format(
        "CFpointObs: Contiguous ragged array representation: parent_index variable has illegal value for %s = %s%n",
        CF.SAMPLE_DIMENSION, obsDimName);
    return false;
  }

  if (!obsDimName.equals(info.grandChildDim.getShortName())) {
    errlog.format(
        "CFpointObs: Contiguous ragged array representation: row_size variable has obs dimension %s must be %s%n",
        obsDimName, info.childDim);
    return false;
  }

  if (ragged_rowSize.getDataType() != DataType.INT) {
    errlog.format("CFpointObs: Contiguous ragged array representation: row_size variable must be of type integer%n");
    return false;
  }

  if (info.childDim == null) { // nc4 ext
    Dimension profileDim2 = ragged_rowSize.getDimension(0);
    if (profileDim2 != profileDim) {
      errlog.format("CFpointObs: Double ragged array representation dimensions do not agree: %s != %s%n",
          profileDim2.getShortName(), profileDim.getShortName());
      return false;
    }
  }

  info.set(Encoding.raggedIndex, stationDim, profileDim, obsDim);
  info.ragged_parentIndex = ragged_parentIndex;
  info.ragged_rowSize = ragged_rowSize;
  return true;
}
 
Example 15
Source File: CFpointObs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private TableConfig makeMultidimInner(NetcdfDataset ds, TableConfig parentTable, Dimension obsDim, EncodingInfo info,
    Formatter errlog) {
  Dimension parentDim = ds.findDimension(parentTable.dimName);

  Table.Type obsTableType =
      (parentTable.structureType == TableConfig.StructureType.PsuedoStructure) ? Table.Type.MultidimInnerPsuedo
          : Table.Type.MultidimInner;
  // if (info.time.isMemberOfStructure()) obsTableType = Table.Type.Structure;

  TableConfig obsTable = new TableConfig(obsTableType, obsDim.getShortName());

  obsTable.lat = matchAxisTypeAndDimension(ds, AxisType.Lat, parentDim, obsDim);
  obsTable.lon = matchAxisTypeAndDimension(ds, AxisType.Lon, parentDim, obsDim);
  obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Height, parentDim, obsDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Pressure, parentDim, obsDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.GeoZ, parentDim, obsDim);
  obsTable.time = matchAxisTypeAndDimension(ds, AxisType.Time, parentDim, obsDim);

  // divide up the variables between the parent and the obs
  List<String> obsVars;
  List<Variable> vars = ds.getVariables();
  List<String> parentVars = new ArrayList<>(vars.size());
  obsVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    Dimension dim0 = orgV.getDimension(0);
    if ((dim0 != null) && dim0.equals(parentDim)) {
      if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
        parentVars.add(orgV.getShortName());
      } else {
        Dimension dim1 = orgV.getDimension(1);
        if (obsDim.equals(dim1))
          obsVars.add(orgV.getShortName());
      }
    }
  }
  parentTable.vars = parentVars;
  // parentTable.vars = parentTable.isPsuedoStructure ? parentVars : null; // restrict to these if psuedoStruct

  obsTable.structureType = parentTable.structureType;
  obsTable.outerName = parentDim.getShortName();
  obsTable.innerName = obsDim.getShortName();
  obsTable.dimName = (parentTable.structureType == TableConfig.StructureType.PsuedoStructure) ? obsTable.outerName
      : obsTable.innerName;
  obsTable.structName = obsDim.getShortName();
  obsTable.vars = obsVars;

  return obsTable;
}
 
Example 16
Source File: CFpointObs.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private TableConfig makeMultidimInner3D(NetcdfDataset ds, TableConfig outerTable, TableConfig middleTable,
    Dimension innerDim, Formatter errlog) {
  Dimension outerDim = ds.findDimension(outerTable.dimName);
  Dimension middleDim = ds.findDimension(middleTable.innerName);

  Table.Type obsTableType =
      (outerTable.structureType == TableConfig.StructureType.PsuedoStructure) ? Table.Type.MultidimInnerPsuedo3D
          : Table.Type.MultidimInner3D;
  TableConfig obsTable = new TableConfig(obsTableType, innerDim.getShortName());
  obsTable.structureType = TableConfig.StructureType.PsuedoStructure2D;
  obsTable.dimName = outerTable.dimName;
  obsTable.outerName = middleTable.innerName;
  obsTable.innerName = innerDim.getShortName();
  obsTable.structName = innerDim.getShortName();

  obsTable.lat = matchAxisTypeAndDimension(ds, AxisType.Lat, outerDim, middleDim, innerDim);
  obsTable.lon = matchAxisTypeAndDimension(ds, AxisType.Lon, outerDim, middleDim, innerDim);
  obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Height, outerDim, middleDim, innerDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.Pressure, middleDim, innerDim);
  if (obsTable.elev == null)
    obsTable.elev = matchAxisTypeAndDimension(ds, AxisType.GeoZ, middleDim, innerDim);
  obsTable.time = matchAxisTypeAndDimension(ds, AxisType.Time, outerDim, middleDim, innerDim);

  // divide up the variables between the 3 tables
  List<Variable> vars = ds.getVariables();
  List<String> outerVars = new ArrayList<>(vars.size());
  List<String> middleVars = new ArrayList<>(vars.size());
  List<String> innerVars = new ArrayList<>(vars.size());
  for (Variable orgV : vars) {
    if (orgV instanceof Structure)
      continue;

    if ((orgV.getRank() == 1) || ((orgV.getRank() == 2) && orgV.getDataType() == DataType.CHAR)) {
      if (outerDim.equals(orgV.getDimension(0)))
        outerVars.add(orgV.getShortName());

    } else if (orgV.getRank() == 2) {
      if (outerDim.equals(orgV.getDimension(0)) && middleDim.equals(orgV.getDimension(1)))
        middleVars.add(orgV.getShortName());

    } else if (orgV.getRank() == 3) {
      if (outerDim.equals(orgV.getDimension(0)) && middleDim.equals(orgV.getDimension(1))
          && innerDim.equals(orgV.getDimension(2)))
        innerVars.add(orgV.getShortName());
    }
  }
  outerTable.vars = outerVars;
  middleTable.vars = middleVars;
  obsTable.vars = innerVars;

  return obsTable;
}
 
Example 17
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read the NcML variable element, and nested elements.
 *
 * @param groupBuilder put dimension into this group
 * @param refGroup parent Group in referenced dataset, may be null
 * @param varElem ncml variable element
 */
private void readVariable(Group.Builder groupBuilder, @Nullable Group refGroup, Element varElem) {
  String name = varElem.getAttributeValue("name");
  if (name == null) {
    errlog.format("NcML Variable name is required (%s)%n", varElem);
    return;
  }
  String nameInFile = Optional.ofNullable(varElem.getAttributeValue("orgName")).orElse(name);

  DataType dtype = null;
  String typeS = varElem.getAttributeValue("type");
  if (typeS != null) {
    dtype = DataType.getType(typeS);
  }

  // see if it already exists
  Variable refv = (refGroup == null) ? null : refGroup.findVariableLocal(nameInFile);
  Optional<Variable.Builder<?>> addedFromAgg = groupBuilder.findVariableLocal(nameInFile);
  if (refv == null && !addedFromAgg.isPresent()) { // new
    if (dtype == null) {
      errlog.format("NcML Variable dtype is required for new variable (%s)%n", name);
      return;
    }
    if (dtype == DataType.STRUCTURE || dtype == DataType.SEQUENCE) {
      groupBuilder.addVariable(readStructureNew(groupBuilder, varElem));
    } else {
      groupBuilder.addVariable(readVariableNew(groupBuilder, dtype, varElem));
    }
    return;
  }

  // refv exists
  if (refv != null) {
    if (dtype == null) {
      dtype = refv.getDataType();
    }
    if (dtype == DataType.STRUCTURE || dtype == DataType.SEQUENCE) {
      readStructureExisting(groupBuilder, null, dtype, (Structure) refv, varElem)
          .ifPresent(groupBuilder::addVariable);
    } else {
      readVariableExisting(groupBuilder, null, dtype, refv, varElem).ifPresent(groupBuilder::addVariable);
    }
    return;
  }

  // refv does not exist, but addedFromAgg may be present
  DataType finalDtype = dtype;
  addedFromAgg.ifPresent(agg -> {
    if (agg instanceof VariableDS.Builder<?>) {
      VariableDS.Builder<?> aggDs = (VariableDS.Builder<?>) agg;
      aggDs.setOriginalName(nameInFile);
    }
    DataType reallyFinalDtype = finalDtype != null ? finalDtype : agg.dataType;
    augmentVariableNew(agg, reallyFinalDtype, varElem);
  });
}
 
Example 18
Source File: NcmlWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Element makeVariableElement(Variable var, boolean showValues) {
  boolean isStructure = var instanceof Structure;

  Element varElem = new Element("variable", namespace);
  varElem.setAttribute("name", var.getShortName());

  StringBuilder buff = new StringBuilder();
  List dims = var.getDimensions();
  for (int i = 0; i < dims.size(); i++) {
    Dimension dim = (Dimension) dims.get(i);
    if (i > 0)
      buff.append(" ");
    if (dim.isShared())
      buff.append(dim.getShortName());
    else if (dim.isVariableLength())
      buff.append("*");
    else
      buff.append(dim.getLength());
  }
  // if (buff.length() > 0)
  varElem.setAttribute("shape", buff.toString());

  DataType dt = var.getDataType();
  if (dt != null) {
    varElem.setAttribute("type", dt.toString());
    if (dt.isEnum())
      varElem.setAttribute("typedef", var.getEnumTypedef().getShortName());
  }


  // attributes
  for (Attribute att : var.attributes()) {
    varElem.addContent(makeAttributeElement(att));
  }

  if (isStructure) {
    Structure s = (Structure) var;
    for (Variable variable : s.getVariables()) {
      varElem.addContent(makeVariableElement(variable, showValues));
    }
  } else if (showValues) {
    try {
      varElem.addContent(makeValuesElement(var, true));
    } catch (IOException e) {
      String message = String.format("Couldn't read values for %s. Omitting <values> element.%n\t%s",
          var.getFullName(), e.getMessage());
      log.warn(message);
    }
  }

  return varElem;
}
 
Example 19
Source File: TestGini.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testGiniRead() throws IOException {
  try (NetcdfFile ncfile = NetcdfFiles.open(TestDir.cdmUnitTestDir + "formats/gini/" + fname)) {
    Variable v = ncfile.findVariable(varName);

    // Make sure we can get the expected variable and that it is at-least 2D
    Assert.assertNotNull(v);
    Assert.assertNotNull(v.getDimension(0));
    Assert.assertNotNull(v.getDimension(1));

    // Make sure the variable has a grid mapping set and that it points
    // to a valid variable
    Attribute grid_mapping = v.findAttribute("grid_mapping");
    Assert.assertNotNull(grid_mapping);
    Variable proj_var = ncfile.findVariable(grid_mapping.getStringValue());
    Assert.assertNotNull(proj_var);
    Assert.assertNotNull(proj_var.findAttribute("grid_mapping_name"));

    // Check size
    Assert.assertEquals(ySize, v.getDimension(v.findDimensionIndex("y")).getLength());
    Assert.assertEquals(xSize, v.getDimension(v.findDimensionIndex("x")).getLength());

    // Check projection info
    Assert.assertEquals(min_lon, ncfile.findAttribute("@geospatial_lon_min").getNumericValue().doubleValue(), 1e-6);
    Assert.assertEquals(max_lon, ncfile.findAttribute("@geospatial_lon_max").getNumericValue().doubleValue(), 1e-6);
    Assert.assertEquals(min_lat, ncfile.findAttribute("@geospatial_lat_min").getNumericValue().doubleValue(), 1e-6);
    Assert.assertEquals(max_lat, ncfile.findAttribute("@geospatial_lat_max").getNumericValue().doubleValue(), 1e-6);

    // Read the array and check that its size matches the variable's
    Array a = v.read();
    Assert.assertNotNull(a);
    Assert.assertEquals(v.getSize(), a.getSize());

    // For byte data, make sure it is specified as unsigned and
    // check that the actual number of bytes is proper
    if (v.getDataType() == DataType.BYTE) {
      byte[] arr = (byte[]) a.getStorage();
      Assert.assertEquals(v.getSize(), arr.length);
      Assert.assertNotNull(v.findAttribute(CDM.UNSIGNED));
    }
  }
}
 
Example 20
Source File: Evaluator.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Does this dataset have a record structure? netcdf-3 specific
 * 
 * @param ds in this dataset
 * @return true if record structure exists
 */
public static boolean hasNetcdf3RecordStructure(NetcdfDataset ds) {
  Variable v = ds.findVariable("record");
  return (v != null) && (v.getDataType() == DataType.STRUCTURE);
}