mirror of
https://github.com/meteoinfo/MeteoInfo.git
synced 2025-12-08 20:36:05 +00:00
add frombuffer function to create ndarray object
This commit is contained in:
parent
e4004a2a4a
commit
88f4010d86
@ -67,7 +67,7 @@ import java.util.zip.ZipInputStream;
|
||||
public static String getVersion() {
|
||||
String version = GlobalUtil.class.getPackage().getImplementationVersion();
|
||||
if (version == null || version.equals("")) {
|
||||
version = "3.9.1";
|
||||
version = "3.9.2";
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
@ -1,32 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<MeteoInfo File="milconfig.xml" Type="configurefile">
|
||||
<Path OpenPath="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf">
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\meteo\calc"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
|
||||
<Path OpenPath="D:\Working\MIScript\Jython\mis\io\radar">
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\projection"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\radar"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\meteo"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\meteo\interpolation"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\interpolate"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\surf"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\radar\cinrad"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\radar"/>
|
||||
</Path>
|
||||
<File>
|
||||
<OpenedFiles>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\meteo\interpolation\cross_section_1.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice\slice_2d.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\griddata_kriging_lcc.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice\plot_cuace_3d_slice.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\radar\cinrad\test_read_radar_mosic.py"/>
|
||||
</OpenedFiles>
|
||||
<RecentFiles>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\meteo\interpolation\cross_section_1.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice\slice_2d.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\griddata_kriging_lcc.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\3d\jogl\slice\plot_cuace_3d_slice.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\io\radar\cinrad\test_read_radar_mosic.py"/>
|
||||
</RecentFiles>
|
||||
</File>
|
||||
<Font>
|
||||
|
||||
Binary file not shown.
@ -33,9 +33,10 @@ class DataType(object):
|
||||
Data type
|
||||
|
||||
:param name: (*string*) Data type name
|
||||
:param byteorder: (*str*) Byte order. Default to '<'.
|
||||
"""
|
||||
|
||||
def __init__(self, name):
|
||||
def __init__(self, name, byteorder='<'):
|
||||
if name is int:
|
||||
name = 'int'
|
||||
elif name is float:
|
||||
@ -47,6 +48,7 @@ class DataType(object):
|
||||
self._dtype = _dtype_dict[name]
|
||||
else:
|
||||
self._dtype = JDataType.OBJECT
|
||||
self._byteorder=byteorder
|
||||
|
||||
@property
|
||||
def itemsize(self):
|
||||
@ -62,7 +64,15 @@ class DataType(object):
|
||||
A character indicating the byte-order of this data-type object.
|
||||
:return: (*str*) Byte order.
|
||||
"""
|
||||
return '>'
|
||||
return self._byteorder
|
||||
|
||||
@byteorder.setter
|
||||
def byteorder(self, val):
|
||||
"""
|
||||
Set byte order.
|
||||
:param val: (*str*) Byte order ['<' | '>'].
|
||||
"""
|
||||
self._byteorder = val
|
||||
|
||||
@property
|
||||
def num(self):
|
||||
|
||||
@ -9,7 +9,7 @@ import _dtype
|
||||
from ._ndarray import NDArray
|
||||
|
||||
__all__ = [
|
||||
'fromfile'
|
||||
'fromfile','frombuffer'
|
||||
]
|
||||
|
||||
def fromfile(file, dtype=_dtype.float, count=-1, sep='', offset=0):
|
||||
@ -31,4 +31,22 @@ def fromfile(file, dtype=_dtype.float, count=-1, sep='', offset=0):
|
||||
r = ArrayUtil.readASCIIFile(file, dtype._dtype, count, sep)
|
||||
else:
|
||||
r = ArrayUtil.readBinFile(file, dtype._dtype, count, offset)
|
||||
return NDArray(r)
|
||||
return NDArray(r)
|
||||
|
||||
|
||||
def frombuffer(buffer, dtype=_dtype.float, count=-1, offset=0):
|
||||
"""
|
||||
Interpret a buffer as a 1-dimensional array.
|
||||
|
||||
:param buffer: (*buffer-like*) An object that exposes the buffer interface.
|
||||
:param dtype: (*dtype*) Data type. Defaults to float.
|
||||
:param count: (*int*) Number of items to read. -1 means all items.
|
||||
:param offset: (*int*) The offset (in bytes) from the file’s current position. Defaults to 0.
|
||||
:return: (*array*) The array read from the buffer.
|
||||
"""
|
||||
if not isinstance(dtype, _dtype.DataType):
|
||||
dtype = _dtype.DataType(dtype)
|
||||
|
||||
byteorder = 'little_endian' if dtype.byteorder == '<' else 'big_endian'
|
||||
r = ArrayUtil.fromBuffer(buffer, dtype._dtype, count, offset, byteorder)
|
||||
return NDArray(r)
|
||||
|
||||
@ -623,6 +623,92 @@ public class ArrayUtil {
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read array from a buffer byte array
|
||||
*
|
||||
* @param buffer Buffer data
|
||||
* @param dataType Data type
|
||||
* @param count Number of items to read. -1 means all items (i.e., the complete file)
|
||||
* @param offset Offset bytes
|
||||
* @return Result array
|
||||
*/
|
||||
public static Array fromBuffer(byte[] buffer, DataType dataType, int count, int offset) {
|
||||
return fromBuffer(buffer, dataType, count, offset, "little_endian");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read array from a buffer byte array
|
||||
*
|
||||
* @param buffer Buffer data
|
||||
* @param dataType Data type
|
||||
* @param count Number of items to read. -1 means all items (i.e., the complete file)
|
||||
* @param offset Offset bytes
|
||||
* @param byteOrder Byte order
|
||||
* @return Result array
|
||||
*/
|
||||
public static Array fromBuffer(byte[] buffer, DataType dataType, int count, int offset,
|
||||
String byteOrder) {
|
||||
ByteOrder bOrder = ByteOrder.LITTLE_ENDIAN;
|
||||
if (byteOrder.equalsIgnoreCase("big_endian")) {
|
||||
bOrder = ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
if (count < 0) {
|
||||
long size = buffer.length;
|
||||
count = (int)((size - offset) / dataType.getSize());
|
||||
}
|
||||
|
||||
Array r = Array.factory(dataType, new int[]{count});
|
||||
IndexIterator iter = r.getIndexIterator();
|
||||
int idx = offset;
|
||||
byte[] bytes;
|
||||
byte[] db;
|
||||
int start = 0;
|
||||
int n = (int) r.getSize() * dataType.getSize();
|
||||
bytes = Arrays.copyOfRange(buffer, idx, idx + n);
|
||||
switch (dataType) {
|
||||
case BYTE:
|
||||
for (int i = 0; i < r.getSize(); i++) {
|
||||
r.setByte(i, bytes[i]);
|
||||
}
|
||||
break;
|
||||
case SHORT:
|
||||
db = new byte[2];
|
||||
while (iter.hasNext()) {
|
||||
System.arraycopy(bytes, start, db, 0, 2);
|
||||
iter.setShortNext(DataTypeUtil.bytes2Short(db, bOrder));
|
||||
start += 2;
|
||||
}
|
||||
break;
|
||||
case INT:
|
||||
db = new byte[4];
|
||||
while (iter.hasNext()) {
|
||||
System.arraycopy(bytes, start, db, 0, 4);
|
||||
iter.setIntNext(DataTypeUtil.bytes2Int(db, bOrder));
|
||||
start += 4;
|
||||
}
|
||||
break;
|
||||
case FLOAT:
|
||||
db = new byte[4];
|
||||
while (iter.hasNext()) {
|
||||
System.arraycopy(bytes, start, db, 0, 4);
|
||||
iter.setFloatNext(DataTypeUtil.bytes2Float(db, bOrder));
|
||||
start += 4;
|
||||
}
|
||||
break;
|
||||
case DOUBLE:
|
||||
db = new byte[8];
|
||||
while (iter.hasNext()) {
|
||||
System.arraycopy(bytes, start, db, 0, 8);
|
||||
iter.setDoubleNext(DataTypeUtil.bytes2Double(db, bOrder));
|
||||
start += 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
// </editor-fold>
|
||||
// <editor-fold desc="Create">
|
||||
/**
|
||||
|
||||
2
pom.xml
2
pom.xml
@ -35,7 +35,7 @@
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<revision>3.9.1</revision>
|
||||
<revision>3.9.2</revision>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<maven.compiler.release>8</maven.compiler.release>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user