Merge "Omaha #4704 Add NumpyJavaEnforcer support for smartInits and procedures" into omaha_16.2.1

Former-commit-id: 812f3d324f07b44d0259f016c1ab5c53f713ce47
This commit is contained in:
Nate Jensen 2015-08-17 09:36:24 -05:00 committed by Gerrit Code Review
commit 7aa53d8e2b
2 changed files with 46 additions and 27 deletions

View file

@ -67,6 +67,8 @@
# Fixed createGrid to accept a DatabaseID for model
# Apr 23, 2015 4259 njensen Updated for new JEP API
# Jul 17, 2015 4575 njensen callSmartTool() and callProcedure() send HashMap for varDict
# Aug 13, 2015 4704 randerso Added NumpyJavaEnforcer support in createGrids and decodeEditArea
# additional code cleanup
########################################################################
import types, string, time, sys
from math import *
@ -80,6 +82,7 @@ import BaseTool, Exceptions
import DatabaseID, TimeRange, AbsTime, ParmID
import GridInfo
import JUtil
import NumpyJavaEnforcer
from java.util import ArrayList
from java.util import Date
@ -112,13 +115,22 @@ class SmartScript(BaseTool.BaseTool):
#self.__pythonGrids = []
self.__accessTime = 0
self.__gridLoc = self.__parmMgr.compositeGridLocation()
self.__gridShape = (self.__gridLoc.getNy().intValue(), self.__gridLoc.getNx().intValue())
self.__topoGrid = None
self.__toolType = "numeric"
self._empty = zeros(self.getGridShape(), float32)
self._minus = self._empty - 1
self._empty = self.empty()
self._minus = self.newGrid(-1)
self._handlers = dict()
def empty(self, dtype=float32):
"""Return a grid filled with 0"""
return zeros(self.getGridShape(), dtype)
def newGrid(self, initialValue, dtype=float32):
"""Return a grid filled with initialValue"""
return full(self.getGridShape(), initialValue, dtype)
##
## Call ProcessVariableList to obtain values from the user
##
@ -1244,11 +1256,10 @@ class SmartScript(BaseTool.BaseTool):
auxJavaGrid = None
javaOldKeys = None
if elementType == "DISCRETE" or elementType == "WEATHER":
ngZero = numericGrid[0]
ngZero = NumpyJavaEnforcer.checkdTypes(numericGrid[0], int8)
dimx = ngZero.shape[1]
dimy = ngZero.shape[0]
# Use createGrid() to get around Jep problems with 3-arg ctor.
ngZero = ngZero.astype('int8')
javaGrid = Grid2DByte.createGrid(dimx, dimy, ngZero)
oldKeys = numericGrid[1]
javaOldKeys = ArrayList()
@ -1263,12 +1274,11 @@ class SmartScript(BaseTool.BaseTool):
# FIXME: add oldKey[0] to the ArrayList for tuple types
javaOldKeys.add(str(oldKey))
elif elementType == "SCALAR":
if numericGrid.dtype.name != 'float32':
numericGrid = numericGrid.astype('float32')
numericGrid = NumpyJavaEnforcer.checkdTypes(numericGrid, float32)
javaGrid = Grid2DFloat.createGrid(numericGrid.shape[1], numericGrid.shape[0], numericGrid)
elif elementType == "VECTOR":
ngZero = numericGrid[0].astype(float32)
ngOne = numericGrid[1].astype(float32)
ngZero = NumpyJavaEnforcer.checkdTypes(numericGrid[0], float32)
ngOne = NumpyJavaEnforcer.checkdTypes(numericGrid[1], float32)
javaGrid = Grid2DFloat.createGrid(ngZero.shape[1], ngZero.shape[0], ngZero)
auxJavaGrid = Grid2DFloat.createGrid(ngOne.shape[1], ngOne.shape[0], ngOne)
else:
@ -2174,7 +2184,7 @@ class SmartScript(BaseTool.BaseTool):
if editArea.isQuery():
editArea = self.__refSetMgr.evaluateQuery(editArea.getQuery())
return editArea.getGrid().getNDArray().astype(numpy.bool8)
return editArea.getGrid().getNDArray().astype(bool)
def decodeEditArea(self, mask):
# Returns a refData object for the given mask
@ -2183,7 +2193,7 @@ class SmartScript(BaseTool.BaseTool):
gridLoc = self.getGridLoc()
nx = gridLoc.getNx().intValue()
ny = gridLoc.getNy().intValue()
bytes = mask.astype('int8')
bytes = NumpyJavaEnforcer.checkdTypes(mask, int8)
grid = Grid2DBit.createBitGrid(nx, ny, bytes)
return ReferenceData(gridLoc, ReferenceID("test"), grid)
@ -2244,9 +2254,7 @@ class SmartScript(BaseTool.BaseTool):
# @return: The number of data points in the X and Y directions.
# @rtype: 2-tuple of int
def getGridShape(self):
gridLoc = self.__parmMgr.compositeGridLocation()
gridShape = (gridLoc.getNy().intValue(), gridLoc.getNx().intValue())
return gridShape
return self.__gridShape
#########################################################################
## Procedure methods ##

View file

@ -33,10 +33,14 @@
# Apr 23, 2015 4259 njensen Updated for new JEP API
# 08/06/2015 4718 dgilling Prevent numpy 1.9 from wasting memory by
# upcasting scalars too high when using where.
# Aug 13, 2015 4704 randerso Added NumpyJavaEnforcer support for smartInits
# additional code cleanup
#
##
import string, sys, re, time, types, getopt, fnmatch, LogStream, DatabaseID, JUtil, AbsTime, TimeRange
import SmartInitParams
import NumpyJavaEnforcer
from numpy import *
pytime = time
@ -356,8 +360,22 @@ class Forecaster(GridUtilities):
# self.__stopo = self.__topo
self._editAreas = self._client.getEditAreaNames()
self._empty = self.__topo * 0
self._minus = self._empty - 1
self.__gridShape = topo.shape
self._empty = self.empty();
self._minus = self.newGrid(-1)
def getGridShape(self):
"""Return a tuple containing the grid shape"""
return self.__gridShape
def empty(self, dtype=float32):
"""Return a grid filled with 0"""
return zeros(self.getGridShape(), dtype)
def newGrid(self, initialValue, dtype=float32):
"""Return a grid filled with initialValue"""
return full(self.getGridShape(), initialValue, dtype)
#--------------------------------------------------------------------------
# Returns a string that corresponds to the specified time range.
@ -448,7 +466,7 @@ class Forecaster(GridUtilities):
def getAreas(self, pbot, tbot, ptop, ttop):
maxm = maximum(tbot, ttop)
minm = minimum(tbot, ttop)
freeze = self._empty + 273.15
freeze = self.newGrid(273.15)
crosses = logical_and(less(minm, freeze), greater(maxm, freeze))
crossp = self.linear(pbot, ptop, tbot, ttop, freeze)
crosst = freeze
@ -1302,26 +1320,19 @@ class IFPIO:
wrongType = None
saved = False
if type(grid) is ndarray:
if grid.dtype != dtype('float32'):
grid = grid.astype('float32')
grid = NumpyJavaEnforcer.checkdTypes(grid, float32)
# scalar save
newwe.setItemScalar(newwe.getTimeRange(tr[0]), grid)
saved = True
elif (type(grid) is list or type(grid) is tuple) and len(grid) == 2:
if type(grid[0]) is ndarray and type(grid[1]) is ndarray:
magGrid = grid[0]
dirGrid = grid[1]
if magGrid.dtype != dtype('float32'):
magGrid = magGrid.astype('float32')
if dirGrid.dtype != dtype('float32'):
dirGrid = dirGrid.astype('float32')
magGrid = NumpyJavaEnforcer.checkdTypes(grid[0], float32)
dirGrid = NumpyJavaEnforcer.checkdTypes(grid[1], float32)
# vector save
newwe.setItemVector(newwe.getTimeRange(tr[0]), magGrid, dirGrid)
saved = True
elif type(grid[0]) is ndarray and type(grid[1]) is list:
bgrid = grid[0]
if bgrid.dtype != dtype('byte'):
bgrid = bgrid.astype('byte')
bgrid = NumpyJavaEnforcer.checkdTypes(grid[0], int8)
if gridType == "DISCRETE":
newwe.setItemDiscrete(newwe.getTimeRange(tr[0]), bgrid, str(grid[1]))