def gdxyval(uuin, vvin, x, y, nij, grtyp, refparam, xyAxis, hasAxis, ij0): """Interpolate scalar or vector fields to scattered (x, y) points vararg = Fstdc.gdxyval(uuin, vvin, x, y, (ni, nj), grtyp, (grref, ig1, ig2, ig3, ig4), (xs, ys), hasAxis, (i0, j0)) @param (uuin, vvin): Fields to interpolate from; if vvin is None then perform scalar interpolation @param x, y: X and Y-coordinates for interpolation @param ni ... j0: grid definition parameters @return Zout or (UU, VV): scalar or tuple of grid-directed, vector-interpolated fields, as appropriate """ try: gid = _getGridHandle(nij[0], nij[1], grtyp, refparam[0], refparam[1], refparam[2], refparam[3], refparam[4], ij0[0], ij0[1], xyAxis[0], xyAxis[1]) except: raise error("gdxyval: Invalid Grid Desc") try: if vvin == None: return _rmn.gdxysval(gid, x, y, uuin) else: return _rmn.gdxyvval(gid, x, y, uuin, vvin) except: raise error("gdxyval: Problem interpolating")
def data_from_key(key, file_id, lonarr, latarr): meta = rmn.fstprm(key) meta['iunit'] = file_id data = rmn.fstluk(key)['d'] grid = rmn.ezqkdef(meta) xypos = rmn.gdxyfll(grid, latarr, lonarr) val = rmn.gdxysval(grid, xypos['x'], xypos['y'], data) return meta, grid, xypos, val
def test_gdxysval(self): gp1 = self.getGridParams_L() gid1 = rmn.ezqkdef(gp1) self.assertTrue(gid1>=0) zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN') for x in xrange(gp1['ni']): zin[:,x] = x xx = np.array([1.5],dtype=np.float32,order='FORTRAN') yy = np.array([1.5],dtype=np.float32,order='FORTRAN') zout = rmn.gdxysval(gid1,xx,yy,zin) self.assertEqual(xx.shape,zout.shape) self.assertTrue(abs((zin[0,0]+zin[1,1])/2. - zout[0]) < self.epsilon) rmn.gdrls(gid1)
def test_gdxysval(self): gp1 = self.getGridParams_L() gid1 = rmn.ezqkdef(gp1) self.assertTrue(gid1>=0) zin = np.empty(gp1['shape'],dtype=np.float32,order='FORTRAN') for x in range(gp1['ni']): zin[:,x] = x xx = np.array([1.5],dtype=np.float32,order='FORTRAN') yy = np.array([1.5],dtype=np.float32,order='FORTRAN') zout = rmn.gdxysval(gid1,xx,yy,zin) self.assertEqual(xx.shape,zout.shape) self.assertTrue(abs((zin[0,0]+zin[1,1])/2. - zout[0]) < self.epsilon) rmn.gdrls(gid1)
def get_model(overpass, lonArray, latArray, fst_dir, ctrl_dir=None, var='AF', threshold=4, filestart=00): if filestart == 12: file_time = (overpass if overpass.minute < 30 else overpass + datetime.timedelta(hours=1)) - datetime.timedelta( hours=12) file_path = "{0}/{1}".format(os.path.abspath(fst_dir), file_time.strftime('%Y%m%d12_0%H')) elif filestart == 0: file_time = (overpass if overpass.minute < 30 else overpass + datetime.timedelta(hours=1)) file_path = "{0}/{1}".format(os.path.abspath(fst_dir), file_time.strftime('%Y%m%d00_0%H')) rmn.fstopt(rmn.FSTOP_MSGLVL, rmn.FSTOPI_MSG_CATAST) rmn.ezsetopt(rmn.EZ_OPT_INTERP_DEGREE, rmn.EZ_INTERP_NEAREST) fid = rmn.fstopenall(file_path, rmn.FST_RO) lonarr = np.array(lonArray, dtype='float32') latarr = np.array(latArray, dtype='float32') keylist = rmn.fstinl(fid, nomvar=var) dir = os.path.abspath(fst_dir) ctrl_dir = os.path.abspath(ctrl_dir) if ctrl_dir else None # assuming this for now ref_lvl = 'sea' height = [float('-inf')] * len(lonarr) value = [float('-inf')] * len(lonarr) points = [{} for _ in lonarr] iplist = [] for key in keylist: meta = rmn.fstprm(key) iplist.append( rmn.DecodeIp(meta['ip1'], meta['ip2'], meta['ip3'])[0].v1) sorted_keylist = (x for _, x in sorted(zip(iplist, keylist), reverse=True)) next(sorted_keylist, None) before_val = [float('inf')] * len(lonarr) cur_meta, cur_grid, cur_xypos, cur_val = data_from_key( next(sorted_keylist), fid, lonarr, latarr) if ctrl_dir: if filestart == 12: ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir), file_time.strftime('%Y%m%d12_0%H')) elif filestart == 0: ctrl_path = "{0}/{1}".format(os.path.abspath(ctrl_dir), file_time.strftime('%Y%m%d00_0%H')) ctrl_fid = rmn.fstopenall(ctrl_path, rmn.FST_RO) ctrl_keylist = rmn.fstinl(ctrl_fid, nomvar=var) sorted_ctrl_keylist = ( x for _, x in sorted(zip(iplist, ctrl_keylist), reverse=True)) next(sorted_ctrl_keylist, None) _, _, _, ctrl_val = data_from_key(next(sorted_ctrl_keylist), ctrl_fid, lonarr, latarr) cur_val -= ctrl_val for progress_ind, after_key in enumerate(sorted_keylist): after_meta, after_grid, after_xypos, after_val = data_from_key( after_key, fid, lonarr, latarr) if ctrl_dir: after_ctrl_key = next(sorted_ctrl_keylist) _, _, _, after_ctrl_val = data_from_key(after_ctrl_key, ctrl_fid, lonarr, latarr) after_val -= after_ctrl_val for ind, val in enumerate(cur_val): if ((val > before_val[ind]) and (val > after_val[ind]) and (val >= threshold) and (val > value[ind])): try: if int(ind) <= 20: print( 'Updating GZ, val: {}, existing val: {}'.format( val, value[ind]), ind, cur_meta['ip1']) gzkey = rmn.fstinf(fid, nomvar='GZ', ip1=cur_meta['ip1'])['key'] gzdata = rmn.fstluk(gzkey)['d'] meta = rmn.fstprm(gzkey) meta['iunit'] = fid gz_grid = rmn.ezqkdef(meta) heightList = rmn.gdxysval(gz_grid, cur_xypos['x'], cur_xypos['y'], gzdata) * 10 height[ind] = float(heightList[ind]) value[ind] = float(val) #print (height[ind], ind) print(height, value) except TypeError: continue before_val = cur_val cur_meta, cur_grid, cur_xypos, cur_val = after_meta, after_grid, after_xypos, after_val print height rmn.fstcloseall(fid) if ctrl_dir: rmn.fstcloseall(ctrl_fid) #print(height) return height, value