def read1DVariableFromDataset( dimStr, varStr, ds, iLOff=0, iROff=0, cl=1): # iLOff: left offset # iROff: right offset # cl : coarsening level if(varStr in ds.variables.keys()): vs = ds.variables[varStr] dimList = vs.dimensions # return a list of variable dimensions ('time', 'x', 'y', etc.) print(' dimList = {} '.format( dimList )) vdim = partialMatchFromList( dimStr, dimList ) try: print(' Reading variable {} ... '.format(vdim)) if(iROff == 0 or (iROff is None) ): var = ds.variables[vdim][(0 + iLOff):] else: var = ds.variables[vdim][(0 + iLOff):-abs(iROff)] print(' ... done.') except: print(' Cannot read the array of variable: {}.'.format(varStr)) sys.exit(1) else: print(' Variable {} not in list {}.'.format(varStr, ds.variables.keys())) sys.exit(1) return var[::cl], np.shape(var[::cl])
def read3DVariableFromDataset(varStr, ds, iTOff=0, iLOff=0, iROff=0, cl=1, meanOn=False): # iLOff: left offset # iROff: right offset # cl : coarsening level varStr = partialMatchFromList( varStr , ds.variables.keys() ) print(' Reading variable {} ... '.format(varStr)) var, dDict = readVariableFromDataset(varStr, ds, cl=1 ) print(' ... done.') iL = 0 + int(iLOff/cl) iR = int(abs(iROff/cl)) iT = 0 + int(iTOff) if(iR == 0): # Param list (time, z, y, x ) if(meanOn): vo = var[iL:, iL:, iL:] else: vo = var[iT:, iL:, iL:, iL:] else: if(meanOn): vo = var[iL:-iR, iL:-iR, iL:-iR] else: vo = var[iT:, iL:-iR, iL:-iR, iL:-iR] var = None return vo, np.array(vo.shape)
def read3dDataFromNetCDF( fname, varStr, cl=1 ): ''' Establish two boolean variables which indicate whether the created variable is an independent or dependent variable in function createNetcdfVariable(). ''' parameter = True; variable = False ''' Create a NETCDF input dataset (ds), and its associated lists of dependent (varList) and independent (dimList) variables. ''' ds, varList, paramList = netcdfDataset(fname) varStr = partialMatchFromList( varStr , varList ) print(' Extracting {} from dataset in {} ... '.format( varStr, fname )) var, dDict = readVariableFromDataset(varStr, ds, cl ) print(' {}_dims = {}\n Done!'.format(varStr, var.shape )) # Rename the keys in dDict to simplify the future postprocessing for dn in dDict.keys(): idNan = np.isnan(dDict[dn]); dDict[dn][idNan] = 0. if( 'time' in dn and 'time' != dn ): dDict['time'] = dDict.pop( dn ) elif( 'x' == dn[0] and 'x' != dn ): dDict['x'] = dDict.pop( dn ) elif( 'y' == dn[0] and 'y' != dn ): dDict['y'] = dDict.pop( dn ) elif( 'z' == dn[0] and 'z' != dn ): dDict['z'] = dDict.pop( dn ) else: pass # Append the variable into the dict. dDict['v'] = var return dDict
and independent (dimList) variables. ''' ds, varList, paramList = netcdfDataset(filename) ''' Read cell center coordinates and time. Create the output independent variables right away and empty memory. ''' if (meanOn): time = np.array([ 0, ]) time_dims = np.shape(time) else: time, time_dims = read1DVariableFromDataset('time', ds, paramList, 0, 0, 1) uStr = partialMatchFromList('u', varList) x, x_dims = read1DVariableFromDataset('x', uStr, ds, paramList, 0, 0, cl) y, y_dims = read1DVariableFromDataset('y', uStr, ds, paramList, 0, 0, cl) z, z_dims = read1DVariableFromDataset('z', uStr, ds, paramList, 0, 0, cl) # - - - - velocity components - - - - - - - - - - - - sa = '' if (meanOn): sa = 'm' u, u_dims = read3DVariableFromDataset('u', ds, varList, 0, 0, cl, meanOn) v, v_dims = read3DVariableFromDataset('v', ds, varList, 0, 0, cl, meanOn) w, w_dims = read3DVariableFromDataset('w', ds, varList, 0, 0, cl, meanOn) infoStr = ''' time_dims = {} x_dims = {}