def getGridData(fileID, key): fileMeta = rmn.fstprm(key) fileMeta['iunit'] = fileID gridMeta = rmn.ezqkdef(fileMeta) gridData = rmn.gdll(gridMeta) lonData = gridData['lon'].tolist() latData = gridData['lat'].tolist() return gridData, lonData, latData
def test_ezqkdef_gdll(self): gp = self.getGridParams_L() gid1 = rmn.ezqkdef(gp) self.assertTrue(gid1>=0) gll = rmn.gdll(gid1) self.assertEqual(gp['shape'],gll['lat'].shape) self.assertEqual(gp['shape'],gll['lon'].shape) self.assertEqual(gp['lat0'],gll['lat'][0,0]) self.assertEqual(gp['lon0'],gll['lon'][0,0]) rmn.gdrls(gid1)
def getGrid(dataKey): fileMeta = rmn.fstprm(dataKey) fileMeta['iunit'] = fileID #add to dictionary as key,val pair gridData = rmn.ezqkdef(fileMeta) gridDecode = rmn.decodeGrid(gridData) #use this for information, not really needed if just getting all lat lon values, but can get bottom left corner if needed for something else llGridData = rmn.gdll(gridData) # is a dictionary of two arrays for lat and lon latData = llGridData['lat'] lonData = llGridData['lon'] return latData, lonData
def getGrid(dataKey, fileID): try: fileMeta = rmn.fstprm(dataKey) fileMeta['iunit'] = fileID gridData = rmn.ezqkdef(fileMeta) llGridData = rmn.gdll(gridData) latData = llGridData['lat'].tolist() lonData = llGridData['lon'].tolist() return lonData, latData, gridData except: print('ERROR: Could not get grid. Please try again.')
def getNiNj(lonVal, latVal, gridID): xypos = rmn.gdxyfll(gridID, latVal, lonVal) ni = int(round(xypos['x'])) - 1 #-1 to use 0 as list index nj = int(round(xypos['y'])) - 1 gridData = rmn.gdll(gridID) if ni >= len(gridData['lon']) or nj >= len(gridData['lon'][0]): print( 'lon lat values of station outside of grid domain. Please check your values.' ) return None else: return ni, nj
def getGrid(dataKey, fileID): """ Get the grid details in form of lon/lat of the FST file. """ # Get the file metadata, then add on the keypair for 'iunit' fileMeta = rmn.fstprm(dataKey) fileMeta['iunit'] = fileID # Get the grid data and decode it gridData = rmn.ezqkdef(fileMeta) gridDecode = rmn.decodeGrid(gridData) llGridData = rmn.gdll(gridData) latData = llGridData['lat'] lonData = llGridData['lon'] return {'gridll':llGridData,'gridID':gridData, 'latData':latData,'lonData':lonData}
def ezgetlalo(nij, grtyp, refparam, xyAxis, hasAxis, ij0, doCorners): """Get Lat-Lon of grid points centers and corners (lat, lon, clat, clon) = Fstdc.ezgetlalo((niS, njS), grtypS, (grrefS, ig1S, ig2S, ig3S, ig4S), (xsS, ysS), hasSrcAxis, (i0S, j0S), doCorners) @param ...TODO... @return tuple of (numpy.ndarray) with center lat/lon (lat, lon) and optionally corners lat/lon (clat, clon) @exception TypeError @exception Fstdc.error """ 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("ezgetlalo: Invalid Grid Desc") try: gridLaLo = _rmn.gdll(gid); except: raise error("ezgetlalo: Problem computing lat, lon in ezscint") if not doCorners: return (gridLaLo['lat'], gridLaLo['lon']) xyc = _rmn.gdxyfll(gid, gridLaLo['lat'], gridLaLo['lon']) xc1 = xyc['x'] yc1 = xyc['y'] nij2 = (4, gridLaLo['lat'].shape[0], gridLaLo['lat'].shape[1]) xc4 = _np.empty(nij2, dtype=_np.float32, order='FORTRAN') yc4 = _np.empty(nij2, dtype=_np.float32, order='FORTRAN') ## x = _np.arange(float(nij2[1])) ## y = _np.arange(float(nij2[2])) ## fx = interpolate.interp2d(x, y, xc1, kind='linear', copy=False) ## fy = interpolate.interp2d(x, y, yc1, kind='linear', copy=False) dij_corners = ( (-0.5, -0.5), #SW (-0.5, 0.5), #NW ( 0.5, 0.5), #NE ( 0.5, -0.5) #SE ) for icorner in range(4): di = dij_corners[icorner][0] dj = dij_corners[icorner][1] ## xnew = x.copy('FORTRAN') + dij_corners[icorner][0] ## ynew = y.copy('FORTRAN') + dij_corners[icorner][1] ## xc4[icorner, :, :] = fx(xnew, ynew) ## yc4[icorner, :, :] = fy(xnew, ynew) xc4[icorner, :, :] = xc1[:, :] + dij_corners[icorner][0] yc4[icorner, :, :] = yc1[:, :] + dij_corners[icorner][1] llc = _rmn.gdllfxy(gid, xc4, yc4) return (gridLaLo['lat'], gridLaLo['lon'], llc['lat'], llc['lon'])
def ezgetlalo(nij, grtyp, refparam, xyAxis, hasAxis, ij0, doCorners): """Get Lat-Lon of grid points centers and corners (lat, lon, clat, clon) = Fstdc.ezgetlalo((niS, njS), grtypS, (grrefS, ig1S, ig2S, ig3S, ig4S), (xsS, ysS), hasSrcAxis, (i0S, j0S), doCorners) @param ...TODO... @return tuple of (numpy.ndarray) with center lat/lon (lat, lon) and optionally corners lat/lon (clat, clon) @exception TypeError @exception Fstdc.error """ 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("ezgetlalo: Invalid Grid Desc") try: gridLaLo = _rmn.gdll(gid); except: raise error("ezgetlalo: Problem computing lat, lon in ezscint") if not doCorners: return (gridLaLo['lat'], gridLaLo['lon']) xyc = _rmn.gdxyfll(gid, gridLaLo['lat'], gridLaLo['lon']) xc1 = xyc['x'] yc1 = xyc['y'] nij2 = (4, gridLaLo['lat'].shape[0], gridLaLo['lat'].shape[1]) xc4 = _np.empty(nij2, dtype=_np.float32, order='FORTRAN') yc4 = _np.empty(nij2, dtype=_np.float32, order='FORTRAN') ## x = _np.arange(float(nij2[1])) ## y = _np.arange(float(nij2[2])) ## fx = interpolate.interp2d(x, y, xc1, kind='linear', copy=False) ## fy = interpolate.interp2d(x, y, yc1, kind='linear', copy=False) dij_corners = ( (-0.5, -0.5), #SW (-0.5, 0.5), #NW ( 0.5, 0.5), #NE ( 0.5, -0.5) #SE ) for icorner in xrange(4): di = dij_corners[icorner][0] dj = dij_corners[icorner][1] ## xnew = x.copy('FORTRAN') + dij_corners[icorner][0] ## ynew = y.copy('FORTRAN') + dij_corners[icorner][1] ## xc4[icorner, :, :] = fx(xnew, ynew) ## yc4[icorner, :, :] = fy(xnew, ynew) xc4[icorner, :, :] = xc1[:, :] + dij_corners[icorner][0] yc4[icorner, :, :] = yc1[:, :] + dij_corners[icorner][1] llc = _rmn.gdllfxy(gid, xc4, yc4) return (gridLaLo['lat'], gridLaLo['lon'], llc['lat'], llc['lon'])
def getNiNj(lonVal, latVal, gridID): """ Get the ni,nj position in a grid for a given lon/lat value. Function returns None if lon/lat value is out of bounds. """ xypos = rmn.gdxyfll(gridID, latVal, lonVal) ni = int(math.floor(xypos['x'])) - 1 #-1 to use 0 as list index nj = int(math.floor(xypos['y'])) - 1 gridData = rmn.gdll(gridID) if ni >= len(gridData['lon']) or nj >= len(gridData['lon'][0]): print( 'lon lat values of station outside of grid domain. Please check your values.' ) return None else: return ni, nj
def getMeta(filePath, level, spc): try: fileID = rmn.fstopenall(filePath,rmn.FST_RO) dataKey = rmn.fstinf(fileID,nomvar=spc,ip1=level)['key'] dataRec = rmn.fstluk(dataKey) # get the concentration data concData = dataRec['d'] fileMeta = rmn.fstprm(dataKey) fileMeta['iunit'] = fileID gridData = rmn.ezqkdef(fileMeta) gridDecode = rmn.decodeGrid(gridData) # get the lonlat data llGridData = rmn.gdll(gridData) latData = llGridData['lat'] lonData = llGridData['lon'] return concData, lonData.tolist(), latData.tolist() except: print('Could not read file.') pass
def getConc(filePath, level, spc): """ Get the concentration data for an FST file. Returns the data in an array, the datakey as a key and the fileID of the FST file. """ try: fileID = rmn.fstopenall(filePath,rmn.FST_RO) dataKey = rmn.fstinf(fileID,nomvar=spc,ip1=level)['key'] dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] fileMeta = rmn.fstprm(dataKey) fileMeta['iunit']=fileID gridData = rmn.ezqkdef(fileMeta) llGridData = rmn.gdll(gridData) print ('File {} recorded'.format(filePath)) rmn.fstcloseall(fileID) return concData,llGridData except TypeError: # log an error into the log file #logging.warning('nomvar {} and ip1 {} could not be found for file {}.'.format(fileID, spc, level, filePath)) return None
def read_fst(file, var, level, fhour, yy): global field global lat global lon print var print level print fhour print yy try: file = rmn.fstopenall(file, rmn.FST_RO) except: sys.stderr.write("Problem opening the file: %s\n" % file) ###Read in lat/lon data### # Prefered method to get grid lat, lon. Works on any RPNSTD grid except 'x' ##### try: rec = rmn.fstlir(file, nomvar=var) except: sys.stderr.write('Error: Problem reading fields ' + var + ' in file: ' + file + '\n') sys.exit(1) try: rec['iunit'] = file gridid = rmn.ezqkdef(rec) # use ezscint to retreive full grid gridLatLon = rmn.gdll(gridid) lat = gridLatLon['lat'] lon = gridLatLon['lon'] if yy == True: print "This grid is Yin-Yang and the lat/lon arrays need to be constructed from the 2 subgrids" lat1 = gridLatLon['subgrid'][0]['lat'] lat2 = gridLatLon['subgrid'][1]['lat'] lon1 = gridLatLon['subgrid'][0]['lon'] lon2 = gridLatLon['subgrid'][1]['lon'] lat = np.append(lat1, lat2, axis=1) lon = np.append(lon1, lon2, axis=1) except: sys.stderr.write('Error: Problem getting grid info in file') sys.exit(1) ###### ###### try: #Get vertical grid definition from file v = vgd.vgd_read(file) #Get the list of ip1 on thermo levels in this file #tlvl = vgd.vgd_get(v, 'VIPT') tlvl = level # Trim the list of thermo ip1 to actual levels in files for TT # since the vgrid in the file is a super set of all levels # and get their "key" tlvlkeys = [] rshape = None #for ip1 in tlvl: (lval, lkind) = rmn.convertIp(rmn.CONVIP_DECODE, level) key = rmn.fstinf(file, nomvar=var, ip2=fhour, ip1=rmn.ip1_all(lval, lkind)) if key is not None: tlvlkeys.append((level, key['key'])) if rshape is None: rshape = key['shape'] rshape = (rshape[0], rshape[1], len(tlvlkeys)) r2d = {'d': None} r3d = None k = 0 try: r2d = rmn.fstluk(key, dataArray=r2d['d']) if r3d is None: r3d = r2d.copy() r3d['d'] = np.empty(rshape, dtype=r2d['d'].dtype, order='FORTRAN') r3d['d'][:, :, k] = r2d['d'][:, :] k += 1 except: raise except: raise finally: # Close file even if an error occured above rmn.fstcloseall(file) # Add the vgrid and the actual ip1 list in the r3d dict, update shape and nk r3d['vgd'] = v r3d['ip1list'] = [x[0] for x in tlvlkeys] r3d['shape'] = rshape r3d['nk'] = rshape[2] field = r3d['d'] return (field, lat, lon)
###Read in lat/lon data### # Prefered method to get grid lat, lon. Works on any RPNSTD grid except 'x' ##### try: rec = rmn.fstlir(file, nomvar=var) except: sys.stderr.write('Error: Problem reading fields ' + var + ' in file: ' + file + '\n') sys.exit(1) try: rec['iunit'] = file gridid = rmn.ezqkdef(rec) # use ezscint to retreive full grid gridLatLon = rmn.gdll(gridid) lat = gridLatLon['lat'] lon = gridLatLon['lon'] except: sys.stderr.write('Error: Problem getting grid info in file') sys.exit(1) ###### ###### try: #Get vertical grid definition from file v = vgd.vgd_read(file) #Get the list of ip1 on thermo levels in this file #tlvl = vgd.vgd_get(v, 'VIPT') tlvl = level
import os import rpnpy.librmn.all as rmn defaultIp1=76696048 defaultSpc='TO3' defaultFile='2010071000_001' level=defaultIp1 spc=defaultSpc fileName = defaultFile fileID = rmn.fstopenall(fileName, rmn.FST_RO) dataKey = rmn.fstinf(fileID, nomvar=spc, ip1=level)['key'] dataRec = rmn.fstluk(dataKey) concData = dataRec['d'] fileMeta = rmn.fstprm(dataKey) fileMeta['iunit'] = fileID gridData = rmn.ezqkdef(fileMeta) llGridData = rmn.gdll(gridData) latData = llGridData['lat'] lonData = llGridData['lon'] ni = dataRec['ni'] nj = dataRec['nj'] n_pil=0 #crop the number of points from top/bot ## ToDo: # find way to calculate width, height based on data available # Adjust so there are projection options # plt.figure(figsize=(8,8)) # get: max lat,max lon, min lat, min lon # ID lon/lat of corners, and adjust by how many degrees