Search in sources :

Example 21 with Array

use of com.beanit.openiec61850.Array in project risky by amsa-code.

the class NetCdfWriterTest method test.

@Test
public void test() throws IOException, InvalidRangeException {
    File file = new File("target/temp.nc");
    file.delete();
    NetCdfWriter n = new NetCdfWriter(file, "0.1");
    Var<Long> v = n.addVariable("time", Long.class).longName("time in epoch milliseconds").units("epoch milliseconds").numRecords(2).build();
    v.add(100L);
    v.add(200L);
    n.close();
    // now read the file just written and assert
    NetcdfFile nc = NetcdfFile.open(file.getCanonicalPath());
    List<Attribute> attributes = nc.findGroup(null).getAttributes();
    System.out.println(attributes);
    assertFalse(attributes.isEmpty());
    System.out.println(nc.getDimensions());
    {
        Array array = nc.readSection("time");
        assertEquals(100, array.getLong(0));
        assertEquals(200, array.getLong(1));
        assertEquals(2, array.getSize());
    }
}
Also used : NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) Attribute(ucar.nc2.Attribute) NetcdfFile(ucar.nc2.NetcdfFile) File(java.io.File) Test(org.junit.Test)

Example 22 with Array

use of com.beanit.openiec61850.Array in project Protocol-Adapter-IEC61850 by OSGP.

the class NodeContainer method writeFloatArray.

public void writeFloatArray(final SubDataAttribute child, final Float[] values) throws NodeWriteException {
    final Array array = (Array) this.parent.getChild(child.getDescription());
    if (array.size() != values.length) {
        throw new NodeWriteException(String.format("Invalid array size %d. Size on device is %d", values.length, array.size()));
    }
    for (int i = 0; i < values.length; i++) {
        final BdaFloat32 bdaFloat = (BdaFloat32) array.getChild(i);
        bdaFloat.setFloat(values[i]);
        this.writeNode(bdaFloat);
    }
// Unfortunately writing an array using "this.writeNode(array);"
// doesn't seem to work...
// Therefore the items are written in individual calls...
}
Also used : Array(org.openmuc.openiec61850.Array) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) BdaFloat32(org.openmuc.openiec61850.BdaFloat32)

Example 23 with Array

use of com.beanit.openiec61850.Array in project Protocol-Adapter-IEC61850 by OSGP.

the class NodeContainer method writeDateArray.

public void writeDateArray(final SubDataAttribute child, final Date[] values) throws NodeWriteException {
    final Array array = (Array) this.parent.getChild(child.getDescription());
    if (array.size() != values.length) {
        throw new NodeWriteException(String.format("Invalid array size %d. Size on device is %d", values.length, array.size()));
    }
    for (int i = 0; i < values.length; i++) {
        final BdaTimestamp bdaTimestamp = (BdaTimestamp) array.getChild(i);
        bdaTimestamp.setDate(values[i]);
        this.writeNode(bdaTimestamp);
    }
// Unfortunately writing an array using "this.writeNode(array);"
// doesn't seem to work...
// Therefore the items are written in individual calls...
}
Also used : Array(org.openmuc.openiec61850.Array) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) BdaTimestamp(org.openmuc.openiec61850.BdaTimestamp)

Example 24 with Array

use of com.beanit.openiec61850.Array in project sis by apache.

the class VariableWrapper method read.

/**
 * Reads a sub-sampled sub-area of the variable.
 *
 * @param  areaLower    index of the first value to read along each dimension.
 * @param  areaUpper    index after the last value to read along each dimension.
 * @param  subsampling  sub-sampling along each dimension. 1 means no sub-sampling.
 * @return the data as an array of a Java primitive type.
 */
@Override
public Vector read(final int[] areaLower, final int[] areaUpper, final int[] subsampling) throws IOException, DataStoreException {
    final int[] size = new int[areaUpper.length];
    for (int i = 0; i < size.length; i++) {
        size[i] = areaUpper[i] - areaLower[i];
    }
    final Array array;
    try {
        array = variable.read(new Section(areaLower, size, subsampling));
    } catch (InvalidRangeException e) {
        throw new DataStoreContentException(e);
    }
    return Vector.create(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
}
Also used : Array(ucar.ma2.Array) DataStoreContentException(org.apache.sis.storage.DataStoreContentException) InvalidRangeException(ucar.ma2.InvalidRangeException) Section(ucar.ma2.Section)

Example 25 with Array

use of com.beanit.openiec61850.Array in project sis by apache.

the class VariableWrapper method readArray.

/**
 * Reads the data from this variable and returns them as an array of a Java primitive type.
 * Multi-dimensional variables are flattened as a one-dimensional array (wrapped in a vector).
 * This method may replace fill/missing values by NaN values and caches the returned vector.
 *
 * @param  area         indices of cell values to read along each dimension, in "natural" order.
 * @param  subsampling  subsampling along each dimension, or {@code null} if none.
 * @return the data as an array of a Java primitive type.
 *
 * @see #read()
 * @see #read(GridExtent, int[])
 */
private Object readArray(final GridExtent area, final int[] subsampling) throws IOException, DataStoreException {
    int n = area.getDimension();
    final int[] lower = new int[n];
    final int[] size = new int[n];
    final int[] sub = (subsampling != null) ? new int[n] : null;
    n--;
    for (int i = 0; i <= n; i++) {
        final int j = (n - i);
        lower[j] = Math.toIntExact(area.getLow(i));
        size[j] = Math.toIntExact(area.getSize(i));
        if (sub != null) {
            sub[j] = subsampling[i];
        }
    }
    final Array array;
    try {
        array = variable.read(sub != null ? new Section(lower, size, sub) : new Section(lower, size));
    } catch (InvalidRangeException e) {
        throw new DataStoreException(e);
    }
    return get1DJavaArray(array);
}
Also used : Array(ucar.ma2.Array) DataStoreException(org.apache.sis.storage.DataStoreException) InvalidRangeException(ucar.ma2.InvalidRangeException) Section(ucar.ma2.Section)

Aggregations

Array (ucar.ma2.Array)30 IOException (java.io.IOException)17 Attribute (ucar.nc2.Attribute)17 Variable (ucar.nc2.Variable)15 Index (ucar.ma2.Index)14 ArrayFloat (ucar.ma2.ArrayFloat)11 Dimension (ucar.nc2.Dimension)10 NetcdfFile (ucar.nc2.NetcdfFile)10 File (java.io.File)9 InvalidRangeException (ucar.ma2.InvalidRangeException)9 WritableRaster (java.awt.image.WritableRaster)8 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)8 ArrayList (java.util.ArrayList)7 DataType (ucar.ma2.DataType)6 ArrayDouble (ucar.ma2.ArrayDouble)5 ArrayShort (ucar.ma2.ArrayShort)5 Array (com.beanit.openiec61850.Array)4 Array (org.openmuc.openiec61850.Array)4 ArrayInt (ucar.ma2.ArrayInt)4 Range (ucar.ma2.Range)4