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

The following examples show how to use ucar.ma2.Array#factory() . 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: AggregationOuter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected Array read(AggDatasetOuter dset, NetcdfFile ncfile) {
  Array data = getData(dset.getId());
  if (data != null)
    return data;

  List<Object> vals = new ArrayList<>();
  for (String gattName : gattNames) {
    Attribute att = ncfile.findGlobalAttribute(gattName);
    if (att == null)
      throw new IllegalArgumentException("Unknown attribute name= " + gattName);
    vals.add(att.getValue(0));
  }

  Formatter f = new Formatter();
  f.format(format, vals.toArray());
  String result = f.toString();

  Array allData = Array.factory(dtype, new int[] {dset.ncoord});
  for (int i = 0; i < dset.ncoord; i++)
    allData.setObject(i, result);
  putData(dset.getId(), allData);
  return allData;
}
 
Example 2
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create a scalar numeric-valued Attribute, possibly unsigned.
 *
 * @param name name of Attribute
 * @param val value of Attribute
 * @param isUnsigned if value is unsigned, used only for integer types.
 */
public Attribute(String name, Number val, boolean isUnsigned) {
  super(name);
  if (name == null)
    throw new IllegalArgumentException("Trying to set name to null on " + this);

  int[] shape = new int[1];
  shape[0] = 1;
  DataType dt = DataType.getType(val.getClass(), isUnsigned);
  this.dataType = dt;
  Array vala = Array.factory(dt, shape);
  Index ima = vala.getIndex();
  vala.setObject(ima.set0(0), val);
  setValues(vala); // make private
  setImmutable();
}
 
Example 3
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Array convertEnums(Map<Integer, String> map, DataType dataType, Array values) {
  Array result = Array.factory(DataType.STRING, values.getShape());
  IndexIterator ii = result.getIndexIterator();
  values.resetLocalIterator();
  while (values.hasNext()) {
    int ival;
    if (dataType == DataType.ENUM1)
      ival = (int) DataType.unsignedByteToShort(values.nextByte());
    else if (dataType == DataType.ENUM2)
      ival = DataType.unsignedShortToInt(values.nextShort());
    else
      ival = values.nextInt();
    String sval = map.get(ival);
    if (sval == null)
      sval = "Unknown enum value=" + ival;
    ii.setObjectNext(sval);
  }
  return result;
}
 
Example 4
Source File: AggregationOuter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected Array read(AggDatasetOuter dset, NetcdfFile ncfile) {
  Array data = getData(dset.getId());
  if (data != null)
    return data;

  Attribute att = ncfile.findGlobalAttribute(gattName);
  if (att == null)
    throw new IllegalArgumentException("Unknown attribute name= " + gattName);
  data = att.getValues();
  if (dtype == null)
    dtype = DataType.getType(data);

  if (dset.ncoord == 1) // LOOK ??
    putData(dset.getId(), data);
  else {
    // duplicate the value to each of the coordinates
    Array allData = Array.factory(dtype, new int[] {dset.ncoord});
    for (int i = 0; i < dset.ncoord; i++)
      Array.arraycopy(data, 0, allData, i, 1); // LOOK generalize to vectors ??
    putData(dset.getId(), allData);
    data = allData;
  }
  return data;
}
 
Example 5
Source File: H5headerNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Array findReferenceObjectNames(Array data) throws IOException {
  IndexIterator ii = data.getIndexIterator();

  Array newData = Array.factory(DataType.STRING, data.getShape());
  IndexIterator ii2 = newData.getIndexIterator();
  while (ii.hasNext()) {
    long objId = ii.getLongNext();
    DataObject dobj = getDataObject(objId, null);
    if (dobj == null) {
      log.warn("readReferenceObjectNames cant find obj= {}", objId);
    } else {
      if (debugReference) {
        log.debug(" Referenced object= {}", dobj.who);
      }
      ii2.setObjectNext(dobj.who);
    }
  }
  return newData;
}
 
Example 6
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private VariableDS makeOffsetCoordinate(NetcdfDataset result, Group group, String dimName, CalendarDate base,
    double[] values) {
  DataType dtype = DataType.DOUBLE;
  VariableDS timeVar = new VariableDS(result, group, null, dimName + "_offset", dtype, dimName, null, null); // LOOK
                                                                                                             // could
                                                                                                             // just
                                                                                                             // make a
                                                                                                             // CoordinateAxis1D
  timeVar.addAttribute(new Attribute(CDM.LONG_NAME, "offset hour from start of run for coordinate = " + dimName));
  timeVar.addAttribute(new ucar.nc2.Attribute("standard_name", "forecast_period"));
  timeVar.addAttribute(new ucar.nc2.Attribute(CF.CALENDAR, base.getCalendar().name()));
  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.UNITS, "hours since " + base));
  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.MISSING_VALUE, Double.NaN));

  // construct the values
  int ntimes = values.length;
  ArrayDouble.D1 timeCoordVals = (ArrayDouble.D1) Array.factory(DataType.DOUBLE, new int[] {ntimes}, values);
  timeVar.setCachedData(timeCoordVals);
  group.addVariable(timeVar);

  return timeVar;
}
 
Example 7
Source File: GeotiffWriter.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Replace missing values with dataMinMax.min - 1.0; return a floating point data array.
 *
 * @param grid GridDatatype
 * @param data input data array
 * @return floating point data array with missing values replaced.
 */
private ArrayFloat replaceMissingValues(IsMissingEvaluator grid, Array data, MAMath.MinMax dataMinMax) {
  float minValue = (float) (dataMinMax.min - 1.0);

  ArrayFloat floatArray = (ArrayFloat) Array.factory(DataType.FLOAT, data.getShape());
  IndexIterator dataIter = data.getIndexIterator();
  IndexIterator floatIter = floatArray.getIndexIterator();
  while (dataIter.hasNext()) {
    float v = dataIter.getFloatNext();
    if (grid.isMissing((double) v)) {
      v = minValue;
    }
    floatIter.setFloatNext(v);
  }

  return floatArray;
}
 
Example 8
Source File: RadialDatasetTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void showImage(SweepBean bean) {
  if (bean == null)
    return;

  if (imageWindow == null) {
    imageWindow = new IndependentWindow("Image Viewer", BAMutil.getImage("nj22/ImageData"));
    imageView = new ImageViewPanel(null);
    imageWindow.setComponent(new JScrollPane(imageView));
    // imageWindow.setComponent( imageView);
    Rectangle b = (Rectangle) prefs.getBean(ImageViewer_WindowSize, new Rectangle(99, 33, 700, 900));
    // System.out.println("bounds in = "+b);
    imageWindow.setBounds(b);
  }

  float[] data;
  try {
    data = bean.sweep.readData();
    int[] shape = {bean.getNumRadial(), bean.getNumGates()};
    Array arrData = Array.factory(DataType.FLOAT, shape, data);

    imageView.setImage(ImageArrayAdapter.makeGrayscaleImage(arrData, null));
    imageWindow.show();

  } catch (IOException e) {
    logger.warn("sweep read data failed", e);
  }
}
 
Example 9
Source File: TimeAxis2DSwath.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Array getCoordBoundsAsArray() {
  double[] values = getValues();
  int[] shapeB = new int[3];
  System.arraycopy(shape, 0, shapeB, 0, 2);
  shapeB[2] = 2;
  return Array.factory(DataType.DOUBLE, shapeB, values);
}
 
Example 10
Source File: Grib1DataTable.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void showData(Grib1RecordBean bean, GribData.InterpolationMethod method, Formatter f) {
  float[] data;
  try {
    data = bean.readData(method);
  } catch (IOException e) {
    f.format("IOException %s", e.getMessage());
    return;
  }

  if (method == GribData.InterpolationMethod.none && bean.gds.isThin()) {
    int[] lines = bean.gdss.getNptsInLine();
    int count = 0;
    for (int line = 0; line < lines.length; line++) {
      f.format(" %3d: ", line);
      for (int i = 0; i < lines[line]; i++)
        f.format("%f,", data[count++]);
      f.format("%n");
    }

  } else {
    int[] shape = {bean.gdss.getNy(), bean.gdss.getNx()};
    Array dataA = Array.factory(DataType.FLOAT, shape, data);
    f.format("%s%n", Ncdump.printArray(dataA));
  }

  float max = -Float.MAX_VALUE;
  float min = Float.MAX_VALUE;
  for (float fd : data) {
    if (Float.isNaN(fd))
      continue;
    max = Math.max(fd, max);
    min = Math.min(fd, min);
  }
  f.format("max = %f%n", max);
  f.format("min = %f%n", min);
}
 
Example 11
Source File: TestGrib1Unpack.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private float[] getData(ucar.unidata.io.RandomAccessFile raf, Grib1Record gr, GribData.InterpolationMethod method,
    int lineno) throws IOException {
  float[] data;
  data = gr.readData(raf, method);

  System.out.printf("%s%n", method);
  Grib1Gds gds = gr.getGDS();
  if (method == GribData.InterpolationMethod.none) {
    int[] lines = gds.getNptsInLine();
    int count = 0;
    for (int line = 0; line < lines.length; line++) {
      if (line != lineno)
        continue;
      System.out.printf(" %3d: ", line);
      for (int i = 0; i < lines[line]; i++)
        System.out.printf("%f,", data[count++]);
      System.out.printf("%n");
    }

  } else {
    int[] shape = new int[] {gds.getNy(), gds.getNx()};
    Array dataA = Array.factory(DataType.FLOAT, shape, data);
    Array lineA = dataA.slice(0, lineno);
    logger.debug("{}", Ncdump.printArray(lineA));
  }
  System.out.printf("%s%n", method);

  return data;
}
 
Example 12
Source File: BytePaddingTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void checkPaddingOnWriteReadTwoDimByteArrayOnlyRecordVar() throws IOException, InvalidRangeException {
  File tmpDataDir = tempFolder.newFolder();
  File testFile = new File(tmpDataDir, "file.nc");

  NetcdfFormatWriter.Builder writerb = NetcdfFormatWriter.createNewNetcdf3(testFile.getPath());
  writerb.addDimension(Dimension.builder().setName("v").setIsUnlimited(true).build());
  writerb.addDimension(Dimension.builder("s", 3).build());

  Variable.Builder v = writerb.addVariable("v", DataType.BYTE, "v s");
  assertEquals(1, v.getElementSize());
  writerb.addVariable("v2", DataType.BYTE, "v");

  byte[] data = {1, 2, 3, 11, 12, 13, 21, 22, 23, -1, -2, -3};

  try (NetcdfFormatWriter ncfWriteable = writerb.build()) {
    Variable var = ncfWriteable.findVariable("v");
    Array dataArray = Array.factory(DataType.BYTE, new int[] {4, 3}, data);
    ncfWriteable.write(var.getFullNameEscaped(), dataArray);
  }

  try (NetcdfFile ncf = NetcdfFiles.open(testFile.getPath())) {
    Variable readVar = ncf.findVariable("v");
    assertEquals(readVar.getDataType(), DataType.BYTE);
    assertEquals(1, readVar.getElementSize());

    int[] org = {0, 0};
    byte[] readdata = (byte[]) readVar.read(org, readVar.getShape()).copyTo1DJavaArray();

    assertArrayEquals(data, readdata);
  }
}
 
Example 13
Source File: BytePaddingTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void checkPaddingOnWriteReadOneDimCharArrayOneOfTwoRecordVars() throws IOException, InvalidRangeException {
  File tmpDataDir = tempFolder.newFolder();
  File testFile = new File(tmpDataDir, "file.nc");

  NetcdfFormatWriter.Builder writerb = NetcdfFormatWriter.createNewNetcdf3(testFile.getPath());
  writerb.addDimension(Dimension.builder().setName("v").setIsUnlimited(true).build());
  writerb.addDimension(Dimension.builder("s", 3).build());

  Variable.Builder v = writerb.addVariable("v", DataType.CHAR, "v s");
  assertEquals(1, v.getElementSize());
  writerb.addVariable("v2", DataType.CHAR, "v");

  char[] data = {1, 2, 3, 40, 41, 42, 50, 51, 52, 60, 61, 62};

  try (NetcdfFormatWriter ncfWriteable = writerb.build()) {
    Variable var = ncfWriteable.findVariable("v");

    Array dataArray = Array.factory(DataType.CHAR, new int[] {4, 3}, data);
    ncfWriteable.write(var.getFullNameEscaped(), dataArray);
  }

  try (NetcdfFile ncf = NetcdfFiles.open(testFile.getPath())) {
    Variable readVar = ncf.findVariable("v");
    assertEquals(readVar.getDataType(), DataType.CHAR);
    assertEquals(1, readVar.getElementSize());
    Variable readVar2 = ncf.findVariable("v2");
    assertEquals(readVar2.getDataType(), DataType.CHAR);
    assertEquals(1, readVar2.getElementSize());

    int[] org = {0, 0};
    char[] readdata = (char[]) readVar.read(org, readVar.getShape()).copyTo1DJavaArray();

    assertArrayEquals(data, readdata);
  }
}
 
Example 14
Source File: Attribute.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * A copy constructor using a ucar.unidata.util.Parameter.
 * Need to do this so ucar.unidata.geoloc package doesnt depend on ucar.nc2 library
 *
 * @param param copy info from here.
 */
public Attribute(Parameter param) {
  this(param.getName());

  if (param.isString()) {
    setStringValue(param.getStringValue());

  } else {
    double[] values = param.getNumericValues();
    int n = values.length;
    Array vala = Array.factory(DataType.DOUBLE, new int[] {n}, values);
    setValues(vala); // make private
  }
  setImmutable();
}
 
Example 15
Source File: NcStream.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Variable.Builder decodeVar(NcStreamProto.Variable var) {
  DataType varType = convertDataType(var.getDataType());
  Variable.Builder ncvar = Variable.builder().setName(var.getName()).setDataType(varType);

  if (varType.isEnum()) {
    ncvar.setEnumTypeName(var.getEnumType());
  }

  // The Dimensions are stored redunantly in the Variable.
  // If shared, they must also exist in a parent Group. However, we dont yet have the Groups wired together,
  // so that has to wait until build().
  List<Dimension> dims = new ArrayList<>(6);
  Section.Builder section = Section.builder();
  for (ucar.nc2.stream.NcStreamProto.Dimension dim : var.getShapeList()) {
    dims.add(decodeDim(dim));
    section.appendRange((int) dim.getLength());
  }
  ncvar.addDimensions(dims);

  for (ucar.nc2.stream.NcStreamProto.Attribute att : var.getAttsList())
    ncvar.addAttribute(decodeAtt(att));

  if (!var.getData().isEmpty()) {
    // LOOK may mess with ability to change var size later.
    ByteBuffer bb = ByteBuffer.wrap(var.getData().toByteArray());
    Array data = Array.factory(varType, section.build().getShape(), bb);
    ncvar.setCachedData(data, true);
  }

  return ncvar;
}
 
Example 16
Source File: FmrcDataset.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private VariableDS makeRunTimeCoordinate(NetcdfDataset result, Group group, String dimName, CalendarDate base,
    double[] values) {
  DataType dtype = DataType.DOUBLE;
  VariableDS timeVar = new VariableDS(result, group, null, dimName + "_run", dtype, dimName, null, null); // LOOK
                                                                                                          // could
                                                                                                          // just make
                                                                                                          // a
                                                                                                          // CoordinateAxis1D
  timeVar.addAttribute(new Attribute(CDM.LONG_NAME, "run times for coordinate = " + dimName));
  timeVar.addAttribute(new ucar.nc2.Attribute("standard_name", "forecast_reference_time"));
  timeVar.addAttribute(new ucar.nc2.Attribute(CF.CALENDAR, base.getCalendar().name()));
  // timeVar.addAttribute(new ucar.nc2.Attribute(CDM.UNITS, "hours since " + base));
  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.UNITS, "hours since " + base.getTimeUnits()));

  timeVar.addAttribute(new ucar.nc2.Attribute(CDM.MISSING_VALUE, Double.NaN));
  timeVar.addAttribute(new ucar.nc2.Attribute(_Coordinate.AxisType, AxisType.RunTime.toString())); // if theres
                                                                                                   // already a time
                                                                                                   // coord, dont put
                                                                                                   // in coordSys -
                                                                                                   // too complicated

  // construct the values
  int ntimes = values.length;
  ArrayDouble.D1 timeCoordVals = (ArrayDouble.D1) Array.factory(DataType.DOUBLE, new int[] {ntimes}, values);
  timeVar.setCachedData(timeCoordVals);
  group.addVariable(timeVar);

  return timeVar;
}
 
Example 17
Source File: BytePaddingTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void checkPaddingOnWriteReadTwoDimByteArrayOneOfTwoRecordVars() throws IOException, InvalidRangeException {
  File tmpDataDir = tempFolder.newFolder();
  File testFile = new File(tmpDataDir, "file.nc");

  NetcdfFormatWriter.Builder writerb = NetcdfFormatWriter.createNewNetcdf3(testFile.getPath());
  writerb.addDimension(Dimension.builder().setName("v").setIsUnlimited(true).build());
  writerb.addDimension(Dimension.builder("s", 3).build());

  Variable.Builder v = writerb.addVariable("v", DataType.BYTE, "v s");
  assertEquals(1, v.getElementSize());
  writerb.addVariable("v2", DataType.BYTE, "v");

  byte[] data = {1, 2, 3, 11, 12, 13, 21, 22, 23, -1, -2, -3};

  try (NetcdfFormatWriter ncfWriteable = writerb.build()) {
    Variable var = ncfWriteable.findVariable("v");
    Array dataArray = Array.factory(DataType.BYTE, new int[] {4, 3}, data);
    ncfWriteable.write(var.getFullNameEscaped(), dataArray);
  }

  try (NetcdfFile ncf = NetcdfFiles.open(testFile.getPath())) {
    Variable readVar = ncf.findVariable("v");
    assertEquals(readVar.getDataType(), DataType.BYTE);
    assertEquals(1, readVar.getElementSize());
    Variable readVar2 = ncf.findVariable("v2");
    assertEquals(readVar2.getDataType(), DataType.BYTE);
    assertEquals(1, readVar2.getElementSize());

    int[] org = {0, 0};
    byte[] readdata = (byte[]) readVar.read(org, readVar.getShape()).copyTo1DJavaArray();

    assertArrayEquals(data, readdata);
  }
}
 
Example 18
Source File: GridHorizCoordSys.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void addLatLon2D(NetcdfFile ncfile, double[] xData, double[] yData) {

    Variable latVar = new Variable(ncfile, g, null, "lat");
    latVar.setDataType(DataType.DOUBLE);
    latVar.setDimensions("y x");
    latVar.addAttribute(new Attribute("units", "degrees_north"));
    latVar.addAttribute(new Attribute("long_name", "latitude coordinate"));
    latVar.addAttribute(new Attribute("standard_name", "latitude"));
    latVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));

    Variable lonVar = new Variable(ncfile, g, null, "lon");
    lonVar.setDataType(DataType.DOUBLE);
    lonVar.setDimensions("y x");
    lonVar.addAttribute(new Attribute("units", "degrees_east"));
    lonVar.addAttribute(new Attribute("long_name", "longitude coordinate"));
    lonVar.addAttribute(new Attribute("standard_name", "longitude"));
    lonVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));

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

    // create the data
    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++) {
        ProjectionPoint projPoint = ProjectionPoint.create(xData[j], yData[i]);
        LatLonPoint latlonPoint = proj.projToLatLon(projPoint);
        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);

    ncfile.addVariable(g, latVar);
    ncfile.addVariable(g, lonVar);
  }
 
Example 19
Source File: CFGridWriter2.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private long 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<>();
  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
  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++) {
      ProjectionPoint projPoint = ProjectionPoint.create(xData[j], yData[i]);
      LatLonPoint latlonPoint = proj.projToLatLon(projPoint);
      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);

  long size = 0;
  size += latVar.getSize() * latVar.getElementSize();
  size += lonVar.getSize() * lonVar.getElementSize();
  return size;
}
 
Example 20
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 {
  FmrcInvLite.Gridset.Grid gridLite = (FmrcInvLite.Gridset.Grid) 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 two dimensions are runtime and time: LOOK: ensemble ??
  List<Range> ranges = section.getRanges();
  Range runRange = ranges.get(0);
  Range timeRange = ranges.get(1);
  List<Range> innerSection = ranges.subList(2, ranges.size());

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

    // iterate over the desired runs
    for (int runIdx : runRange) {
      // Date runDate = vstate.runTimes.get(runIdx);

      // 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 = gridLite.getInstance(runIdx, timeIdx);
        if (timeInv != null) {
          if (debugRead)
            System.out.printf("HIT %d %d ", runIdx, timeIdx);
          result = read(timeInv, gridLite.name, innerSection, openFilesRead); // may return null
          result = MAMath.convert(result, dtype); // just in case it need to be converted
        }

        // 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 %d ", runIdx, timeIdx);
        }

        if (debugRead)
          System.out.printf("%d %d reallyRead %s %d bytes start at %d total size is %d%n", runIdx, 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);
  }
}