Java Code Examples for ucar.ma2.Array#makeArray()

The following examples show how to use ucar.ma2.Array#makeArray() . 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: HdfEosModisConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean addTimeCoordinate(NetcdfDataset ds) {
  // add time coordinate
  CalendarDate cd = parseFilenameForDate(ds.getLocation());
  if (cd == null)
    return false;

  ds.addAttribute(ds.getRootGroup(), new Attribute("_MODIS_Date", cd.toString()));

  // add the time dimension
  int nTimesDim = 1;
  Dimension newDim = new Dimension(TIME_NAME, nTimesDim);
  ds.addDimension(null, newDim);

  // add the coordinate variable
  String units = "seconds since " + cd;
  String desc = "time coordinate";

  Array data = Array.makeArray(DataType.DOUBLE, 1, 0.0, 0.0);

  CoordinateAxis1D timeCoord = new CoordinateAxis1D(ds, null, TIME_NAME, DataType.DOUBLE, "", units, desc);
  timeCoord.setCachedData(data, true);
  timeCoord.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
  ds.addCoordinateAxis(timeCoord);

  return true;
}
 
Example 2
Source File: NetcdfDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Set the data values from a list of Strings.
 *
 * @param v for this variable
 * @param values list of Strings
 * @throws IllegalArgumentException if values array not correct size, or values wont parse to the correct type
 * @deprecated use Variable.setValues()
 */
@Deprecated
public void setValues(Variable v, List<String> values) throws IllegalArgumentException {
  Array data = Array.makeArray(v.getDataType(), values);

  if (data.getSize() != v.getSize())
    throw new IllegalArgumentException("Incorrect number of values specified for the Variable " + v.getFullName()
        + " needed= " + v.getSize() + " given=" + data.getSize());

  if (v.getRank() != 1) // dont have to reshape for rank 1
    data = data.reshape(v.getShape());

  v.setCachedData(data, true);
}
 
Example 3
Source File: TimeStringParsing.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
TimeStringParsing() throws IOException {

    String filename = "file:C:\\Documents and Settings\\caron\\.unidata\\cachePersist\\fileD-godas-singleAgg.ncml";
    Element aggElem = ucar.nc2.util.xml.Parse.readRootElement(filename);

    List<Element> ncList = aggElem.getChildren("netcdf", thredds.client.catalog.Catalog.ncmlNS);
    for (Element netcdfElemNested : ncList) {
      String location = netcdfElemNested.getAttributeValue("location");

      List<Element> cacheElemList = netcdfElemNested.getChildren("cache", thredds.client.catalog.Catalog.ncmlNS);
      for (Element cacheElemNested : cacheElemList) {
        String varName = cacheElemNested.getAttributeValue("varName");
        String sdata = cacheElemNested.getText();
        System.out.println("text size = " + sdata.length());

        long start = System.nanoTime();
        String[] vals = sdata.split(" ");
        double took = .001 * .001 * .001 * (System.nanoTime() - start);
        System.out.println(" split took = " + took + " sec; ");

        start = System.nanoTime();
        Array data = Array.makeArray(DataType.DOUBLE, vals);
        took = .001 * .001 * .001 * (System.nanoTime() - start);
        System.out.println(" makeArray took = " + took + " sec; ");
      }
    }

  }
 
Example 4
Source File: NcMLReader.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Parse the values element
 *
 * @param s JDOM element to parse
 * @return Array with parsed values
 * @throws IllegalArgumentException if string values not parsable to specified data type
 */
public static ucar.ma2.Array readAttributeValues(Element s) throws IllegalArgumentException {
  String valString = s.getAttributeValue("value");

  // can also be element text
  if (valString == null) {
    valString = s.getTextNormalize();
  }

  // no value specified hmm technically this is not illegal !!
  if (valString == null)
    throw new IllegalArgumentException("No value specified");

  String type = s.getAttributeValue("type");
  DataType dtype = (type == null) ? DataType.STRING : DataType.getType(type);
  if (dtype == DataType.CHAR)
    dtype = DataType.STRING;

  // backwards compatibility with deprecated isUnsigned attribute
  String unS = s.getAttributeValue("isUnsigned");
  boolean isUnsignedSet = "true".equalsIgnoreCase(unS);
  if (isUnsignedSet && dtype.isIntegral() && !dtype.isUnsigned()) {
    dtype = dtype.withSignedness(DataType.Signedness.UNSIGNED);
  }

  String sep = s.getAttributeValue("separator");
  if ((sep == null) && (dtype == DataType.STRING)) {
    List<String> list = new ArrayList<>();
    list.add(valString);
    return Array.makeArray(dtype, list);
  }

  if (sep == null)
    sep = " "; // default whitespace separated

  List<String> stringValues = new ArrayList<>();
  StringTokenizer tokn = new StringTokenizer(valString, sep);
  while (tokn.hasMoreTokens())
    stringValues.add(tokn.nextToken());

  return Array.makeArray(dtype, stringValues);
}
 
Example 5
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Parse the values element
 *
 * @param s JDOM element to parse
 * @return Array with parsed values
 * @throws IllegalArgumentException if string values not parsable to specified data type
 */
private static ucar.ma2.Array readAttributeValues(Element s) throws IllegalArgumentException {
  String valString = s.getAttributeValue("value");

  // can also be element text
  if (valString == null) {
    valString = s.getTextNormalize();
  }

  // no value specified hmm technically this is not illegal !!
  if (valString == null) {
    throw new IllegalArgumentException("No value specified");
  }

  String type = s.getAttributeValue("type");
  DataType dtype = (type == null) ? DataType.STRING : DataType.getType(type);
  if (dtype == DataType.CHAR) {
    dtype = DataType.STRING;
  }

  // backwards compatibility with deprecated isUnsigned attribute
  String unS = s.getAttributeValue("isUnsigned");
  boolean isUnsignedSet = "true".equalsIgnoreCase(unS);
  if (isUnsignedSet && dtype.isIntegral() && !dtype.isUnsigned()) {
    dtype = dtype.withSignedness(DataType.Signedness.UNSIGNED);
  }

  String sep = s.getAttributeValue("separator");
  if ((sep == null) && (dtype == DataType.STRING)) {
    List<String> list = new ArrayList<>();
    list.add(valString);
    return Array.makeArray(dtype, list);
  }

  if (sep == null) {
    sep = " "; // default whitespace separated
  }

  List<String> stringValues = new ArrayList<>();
  StringTokenizer tokn = new StringTokenizer(valString, sep);
  while (tokn.hasMoreTokens()) {
    stringValues.add(tokn.nextToken());
  }

  return Array.makeArray(dtype, stringValues);
}
 
Example 6
Source File: TestAttributeBuilder.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testArrayConstructor() {
  Array data = Array.makeArray(DataType.FLOAT, ImmutableList.of("3.14", ".0015"));
  Attribute att = Attribute.builder().setName("name").setValues(data).build();
  assertThat(att).isEqualTo(new Attribute("name", data));
}
 
Example 7
Source File: TestNetcdfFormatWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void testUnsignedAttribute() throws IOException, InvalidRangeException {
  String filename = tempFolder.newFile().getAbsolutePath();

  NetcdfFormatWriter.Builder writerb = NetcdfFormatWriter.createNewNetcdf3(filename);
  writerb.addUnlimitedDimension("time");

  writerb.addVariable("time", DataType.BYTE, "time").addAttribute(new Attribute(CDM.UNSIGNED, "true"))
      .addAttribute(new Attribute(CDM.SCALE_FACTOR, 10.0))
      .addAttribute(new Attribute(CDM.VALID_RANGE, ImmutableList.of(10, 240), false));

  /*
   * byte Band1(y, x);
   * > Band1:_Unsigned = "true";
   * > Band1:_FillValue = -1b; // byte
   */
  writerb.addVariable("Band1", DataType.BYTE, "time").addAttribute(new Attribute(CDM.UNSIGNED, "true"))
      .addAttribute(new Attribute(CDM.FILL_VALUE, (byte) -1)).addAttribute(new Attribute(CDM.SCALE_FACTOR, 1.0));

  /*
   * byte Band2(y, x);
   * > Band2:_Unsigned = "true";
   * > Band2:valid_range = 0s, 254s; // short
   */
  writerb.addVariable("Band2", DataType.BYTE, "time").addAttribute(new Attribute(CDM.UNSIGNED, "true"))
      .addAttribute(new Attribute(CDM.SCALE_FACTOR, 1.0))
      .addAttribute(new Attribute(CDM.VALID_RANGE, ImmutableList.of((short) 0, (short) 254)));

  try (NetcdfFormatWriter writer = writerb.build()) {
    Array timeData = Array.factory(DataType.BYTE, new int[] {1});
    int[] time_origin = new int[] {0};

    for (int time = 0; time < 256; time++) {
      timeData.setInt(timeData.getIndex(), time);
      time_origin[0] = time;
      writer.write("time", time_origin, timeData);
      writer.write("Band1", time_origin, timeData);
      writer.write("Band2", time_origin, timeData);
    }
  }

  Array expected = Array.makeArray(DataType.BYTE, 256, 0, 1);
  try (NetcdfFile ncFile = NetcdfFiles.open(filename)) {
    Array result2 = ncFile.readSection("time");
    CompareNetcdf2.compareData("time", expected, result2);
  }
}
 
Example 8
Source File: NetcdfDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
/**
 * Generate the list of values from a starting value and an increment.
 * Will reshape to variable if needed.
 *
 * @param v for this variable
 * @param npts number of values, must = v.getSize()
 * @param start starting value
 * @param incr increment
 * @deprecated use Variable.setValues()
 */
@Deprecated
public void setValues(Variable v, int npts, double start, double incr) {
  if (npts != v.getSize())
    throw new IllegalArgumentException("bad npts = " + npts + " should be " + v.getSize());
  Array data = Array.makeArray(v.getDataType(), npts, start, incr);
  if (v.getRank() != 1)
    data = data.reshape(v.getShape());
  v.setCachedData(data, true);
}
 
Example 9
Source File: NetcdfDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Make a 1D array from a list of strings.
 *
 * @param dtype data type of the array.
 * @param stringValues list of strings.
 * @return resulting 1D array.
 * @throws NumberFormatException if string values not parssable to specified data type
 * @deprecated use Array#makeArray directly
 */
@Deprecated
public static Array makeArray(DataType dtype, List<String> stringValues) throws NumberFormatException {
  return Array.makeArray(dtype, stringValues);
}