update array's astype function

This commit is contained in:
wyq 2024-07-24 22:56:03 +08:00
parent 4de1cef0ac
commit ee78df31cc
8 changed files with 169 additions and 83 deletions

View File

@ -1624,31 +1624,27 @@ public class Axis extends Artist implements Cloneable {
}
//Time label - left
if (this.drawTickLabel) {
DateTimeFormatter format;
if (this instanceof TimeAxis) {
TimeAxis tAxis = (TimeAxis) this;
if (tAxis.isVarFormat()) {
drawStr = null;
int idx = this.inverse ? this.getTickValues().length - 1 : 0;
LocalDateTime cDate = JDateUtil.fromOADate(this.getTickValues()[idx]);
DateTimeFormatter format = null;
switch (tAxis.getTimeUnit()) {
case MONTH:
format = DateTimeFormatter.ofPattern("yyyy");
LocalDateTime cdate = JDateUtil.fromOADate(this.getTickValues()[0]);
drawStr = format.format(cdate);
break;
case DAY:
format = DateTimeFormatter.ofPattern("yyyy-MM");
cdate = JDateUtil.fromOADate(this.getTickValues()[0]);
drawStr = format.format(cdate);
break;
case HOUR:
case MINUTE:
case SECOND:
format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
cdate = JDateUtil.fromOADate(this.getTickValues()[0]);
drawStr = format.format(cdate);
break;
}
if (drawStr != null) {
if (format != null) {
drawStr = format.format(cDate);
labx = (float) minx;
laby = laby + this.tickSpace;
Draw.drawString(g, labx, laby, drawStr, XAlign.LEFT, YAlign.TOP, true);

View File

@ -1,14 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\plot_types\contour">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart\axis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\topology"/>
<Path OpenPath="D:\Working\MIScript\Jython\mis\traj">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\meteo\eof"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\meteo\calc"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\meteo"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\stats"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\chart\subplot"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\scatter"/>
@ -16,17 +13,20 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\map\projection"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\contour"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\traj"/>
</Path>
<File>
<OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\plot\plot_cdata_3.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\contour\conout_negtive_value.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\plot_types\contour\conoutm_clabel_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\array\astype_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\traj\plot_traj_bd.py"/>
</OpenedFiles>
<RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\plot\plot_cdata_3.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\contour\conout_negtive_value.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\plot_types\contour\conoutm_clabel_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\array\astype_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\traj\plot_traj_bd.py"/>
</RecentFiles>
</File>
<Font>

View File

@ -7,16 +7,25 @@ __all__ = [
_dtype_dict = dict(byte = JDataType.BYTE,
char = JDataType.CHAR,
bool = JDataType.BOOLEAN,
boolean = JDataType.BOOLEAN,
int = JDataType.INT,
int32 = JDataType.INT,
integer = JDataType.INT,
uint = JDataType.UINT,
short = JDataType.SHORT,
int16 = JDataType.SHORT,
long = JDataType.LONG,
int64 = JDataType.LONG,
float = JDataType.FLOAT,
float32 = JDataType.FLOAT,
double = JDataType.DOUBLE,
float64 = JDataType.DOUBLE,
str = JDataType.STRING,
string = JDataType.STRING,
complex = JDataType.COMPLEX,
date = JDataType.DATE,
datetime = JDataType.DATE,
object = JDataType.OBJECT)
class DataType(object):

View File

@ -717,17 +717,18 @@ class NDArray(object):
if self.dtype == dtype:
return self.copy()
if dtype.kind == 'i':
r = NDArray(ArrayUtil.toInteger(self._array))
elif dtype.kind == 'f':
if dtype.name == 'float':
r = NDArray(ArrayUtil.toFloat(self._array))
else:
r = NDArray(ArrayUtil.toDouble(self._array))
elif dtype.kind == 'b':
r = NDArray(ArrayUtil.toBoolean(self._array))
else:
r = self
# if dtype.kind == 'i':
# r = NDArray(ArrayUtil.toInteger(self._array))
# elif dtype.kind == 'f':
# if dtype.name == 'float':
# r = NDArray(ArrayUtil.toFloat(self._array))
# else:
# r = NDArray(ArrayUtil.toDouble(self._array))
# elif dtype.kind == 'b':
# r = NDArray(ArrayUtil.toBoolean(self._array))
# else:
# r = self
r = NDArray(ArrayUtil.convertToDataType(self._array, dtype._dtype))
return r
@property

View File

@ -32,6 +32,8 @@
*/
package org.meteoinfo.ndarray;
import org.meteoinfo.common.util.JDateUtil;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.time.LocalDateTime;
@ -266,9 +268,13 @@ public class ArrayDouble extends Array {
throw new ForbiddenConversionException();
}
public LocalDateTime getDate(Index i) { throw new ForbiddenConversionException(); }
public LocalDateTime getDate(Index i) {
return JDateUtil.fromOADate(storageD[i.currentElement()]);
}
public void setDate(Index i, LocalDateTime value) { throw new ForbiddenConversionException(); }
public void setDate(Index i, LocalDateTime value) {
storageD[i.currentElement()] = JDateUtil.toOADate(value);
}
public Object getObject(Index i) {
return storageD[i.currentElement()];
@ -363,9 +369,13 @@ public class ArrayDouble extends Array {
throw new ForbiddenConversionException();
}
public LocalDateTime getDate(int index) { throw new ForbiddenConversionException(); }
public LocalDateTime getDate(int index) {
return JDateUtil.fromOADate(storageD[index]);
}
public void setDate(int index, LocalDateTime value) { throw new ForbiddenConversionException(); }
public void setDate(int index, LocalDateTime value) {
storageD[index] = JDateUtil.toOADate(value);
}
public Object getObject(int index) {
return getDouble(index);

View File

@ -191,122 +191,122 @@ public class ArrayString extends Array {
}
/**
* set the value at the sepcified index.
* set the value at the specified index.
*/
public void set(Index i, String value) {
storage[i.currentElement()] = value;
}
/**
* not legal, throw ForbiddenConversionException
* get the double value at the specified index.
*/
public double getDouble(Index i) {
throw new ForbiddenConversionException();
return Double.parseDouble(storage[i.currentElement()]);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setDouble(Index i, double value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
/**
* not legal, throw ForbiddenConversionException
* get the float value at the specified index.
*/
public float getFloat(Index i) {
throw new ForbiddenConversionException();
return Float.parseFloat(storage[i.currentElement()]);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setFloat(Index i, float value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
/**
* not legal, throw ForbiddenConversionException
* get the long value at the specified index.
*/
public long getLong(Index i) {
throw new ForbiddenConversionException();
return Long.parseLong(storage[i.currentElement()]);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setLong(Index i, long value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
/**
* not legal, throw ForbiddenConversionException
* get the integer value at the specified index.
*/
public int getInt(Index i) {
throw new ForbiddenConversionException();
return Integer.parseInt(storage[i.currentElement()]);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setInt(Index i, int value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
/**
* not legal, throw ForbiddenConversionException
* get the short value at the specified index.
*/
public short getShort(Index i) {
throw new ForbiddenConversionException();
return Short.parseShort(storage[i.currentElement()]);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setShort(Index i, short value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
/**
* not legal, throw ForbiddenConversionException
* get the byte value at the specified index.
*/
public byte getByte(Index i) {
throw new ForbiddenConversionException();
return Byte.parseByte(storage[i.currentElement()]);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setByte(Index i, byte value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
/**
* not legal, throw ForbiddenConversionException
* get the boolean value at the specified index.
*/
public boolean getBoolean(Index i) {
throw new ForbiddenConversionException();
return Boolean.parseBoolean(storage[i.currentElement()]);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setBoolean(Index i, boolean value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
/**
* not legal, throw ForbiddenConversionException
* get the char value at the specified index.
*/
public char getChar(Index i) {
throw new ForbiddenConversionException();
return storage[i.currentElement()].charAt(0);
}
/**
* not legal, throw ForbiddenConversionException
* set the value at the specified index.
*/
public void setChar(Index i, char value) {
throw new ForbiddenConversionException();
storage[i.currentElement()] = String.valueOf(value);
}
public String getString(Index i) {
@ -339,67 +339,67 @@ public class ArrayString extends Array {
// package private : mostly for iterators
public double getDouble(int index) {
throw new ForbiddenConversionException();
return Double.parseDouble(storage[index]);
}
public void setDouble(int index, double value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);
}
public float getFloat(int index) {
throw new ForbiddenConversionException();
return Float.parseFloat(storage[index]);
}
public void setFloat(int index, float value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);
}
public long getLong(int index) {
throw new ForbiddenConversionException();
return Long.parseLong(storage[index]);
}
public void setLong(int index, long value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);
}
public int getInt(int index) {
throw new ForbiddenConversionException();
return Integer.parseInt(storage[index]);
}
public void setInt(int index, int value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);
}
public short getShort(int index) {
throw new ForbiddenConversionException();
return Short.parseShort(storage[index]);
}
public void setShort(int index, short value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);;
}
public byte getByte(int index) {
throw new ForbiddenConversionException();
return Byte.parseByte(storage[index]);
}
public void setByte(int index, byte value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);
}
public char getChar(int index) {
throw new ForbiddenConversionException();
return storage[index].charAt(0);
}
public void setChar(int index, char value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);
}
public boolean getBoolean(int index) {
throw new ForbiddenConversionException();
return Boolean.parseBoolean(storage[index]);
}
public void setBoolean(int index, boolean value) {
throw new ForbiddenConversionException();
storage[index] = String.valueOf(value);
}
public String getString(int index) {
@ -411,11 +411,11 @@ public class ArrayString extends Array {
}
public Complex getComplex(int index) {
throw new ForbiddenConversionException();
return Complex.parse(storage[index]);
}
public void setComplex(int index, Complex value) {
throw new ForbiddenConversionException();
storage[index] = value.toString();
}
public LocalDateTime getDate(int index) { throw new ForbiddenConversionException(); }

View File

@ -1900,6 +1900,76 @@ public class ArrayUtil {
}
}
/**
* Convert array to another data type
* @param a The input array
* @param dataType The data type to be converted
* @return Converted array
*/
public static Array convertToDataType(Array a, DataType dataType) {
if (a.getDataType() == dataType) {
return a;
} else {
Array r = Array.factory(dataType, a.getShape());
IndexIterator iterA = a.getIndexIterator();
IndexIterator iterR = r.getIndexIterator();
switch (dataType) {
case BYTE:
while (iterR.hasNext()) {
iterR.setByteNext(iterA.getByteNext());
}
break;
case BOOLEAN:
while (iterR.hasNext()) {
iterR.setBooleanNext(iterA.getBooleanNext());
}
break;
case SHORT:
while (iterR.hasNext()) {
iterR.setShortNext(iterA.getShortNext());
}
break;
case INT:
while (iterR.hasNext()) {
iterR.setIntNext(iterA.getIntNext());
}
break;
case LONG:
while (iterR.hasNext()) {
iterR.setLongNext(iterA.getLongNext());
}
break;
case FLOAT:
while (iterR.hasNext()) {
iterR.setFloatNext(iterA.getFloatNext());
}
break;
case DOUBLE:
while (iterR.hasNext()) {
iterR.setDoubleNext(iterA.getDoubleNext());
}
break;
case COMPLEX:
while (iterR.hasNext()) {
iterR.setComplexNext(iterA.getComplexNext());
}
break;
case STRING:
while (iterR.hasNext()) {
iterR.setStringNext(iterA.getStringNext());
}
break;
case DATE:
while (iterR.hasNext()) {
iterR.setDateNext(iterA.getDateNext());
}
break;
}
return r;
}
}
/**
* Convert array to integer type
*