def example(ncNameFile): #Input file is a list of each output file on its own line. #Paraview has a hard time animating the 0 time step .nc files. Maybe it's an issue with the MPAS reader? #We can get around it by creating vtk files of the fields of interest named fnameN.vtk where N is an integer that indicates time. #store the names of the files we want in order in a file #ncNameFile = 'ncNames.txt' fp = open(ncNameFile,'r') #output the files to outNameBase = 'june' # i = 0 for line in fp: ncfname = line.rstrip('\n') #do we have to strip off any characters like \n? if (i==0): #reference field for orientation vtkfname = outNameBase+'_ref'+'.vtk' data = output_data.open_netcdf_data(ncfname) #header and mesh info fvtk = output_data.write_vtk_header_polydata(vtkfname, ncfname) nNodes = output_data.write_vtk_xyzNodes(fvtk, data) nCells = output_data.write_vtk_polygons(fvtk, data) fvtk.write('\nCELL_DATA '+str(nCells)+'\n') output_data.write_vtk_staticGeoFields(f,data,nCells) fvtk.close() data.close() # vtkfname = outNameBase+str(i)+'.vtk' data = output_data.open_netcdf_data(ncfname) #header and mesh info fvtk = output_data.write_vtk_header_polydata(vtkfname, ncfname) nNodes = output_data.write_vtk_xyzNodes(fvtk, data) nCells = output_data.write_vtk_polygons(fvtk, data) #write cell data fvtk.write('\nCELL_DATA '+str(nCells)+'\n') time = 0 vLevel = 18 output_data.write_vtk_cellCenterVelocity(fvtk, data, time, vLevel, nCells) output_data.write_vtk_var_timeLevelCells(fvtk, 'pv_cell', data, vLevel, time, nCells) i = i+1 #close files data.close() fvtk.close() fp.close()
def example(): # file properties ncfname = "/arctic1/nick/cases/cfsr/output.2006-08-07_12.00.00.nc" # ncfname = '/home/nickszap/research/mpas/output.2010-10-23_00:00:00.nc' #input file vtkfname = "plotTest.vtk" # output file data = output_data.open_netcdf_data(ncfname) # open the output vtk file and write header. ------------------- fvtk = output_data.write_vtk_header_polydata(vtkfname, ncfname) # write nodes and cells nNodes = output_data.write_vtk_xyzNodes(fvtk, data) nCells = output_data.write_vtk_polygons(fvtk, data) nLevels = len(data.dimensions["nVertLevels"]) # write some cell data -------------------- fvtk.write("\nCELL_DATA " + str(nCells) + "\n") output_data.write_vtk_staticGeoFields(fvtk, data, nCells) # time dependent stuff goes in different files time = 0 output_data.write_vtk_pressureHeights(fvtk, data, nCells, time, nLevels, 50000) # 500mb = 50,000Pa # write some node data # close the .nc and vtk files data.close() fvtk.close()
def driver_ncGeo2vtk(ncfname): ''' given .nc (MPAS NETCDF) file, output the surface geography data into classic vtk format. The MPAS NetCDF reader in Paraview loads only the "critical" netcdf variables, ie with time and vertLevels. Rather than edit that reader, we can create a file of the vars we care about on the scvt mesh rather than the dual. I think they have to use the dual for the volume mesh since the elements need to be of supported VTK type (eg prism, hex,...) ''' #file properties #ncfname = '/home/nickszap/research/mpas/output.2010-10-23_00:00:00.nc' #input file vtkfname = 'geoTest.vtk' #output file data = output_data.open_netcdf_data(ncfname) #open the output vtk file and write header. fvtk = output_data.write_vtk_header_polydata(vtkfname, ncfname) #write nodes and cells nNodes = output_data.write_vtk_xyzNodes(fvtk, data) nCells = output_data.write_vtk_polygons(fvtk, data) vLevels = len(data.dimensions['nVertLevels']) nTimes = len(data.dimensions['Time']) #write some geographic cell data fvtk.write('\nCELL_DATA '+str(nCells)+'\n') write_vtk_staticGeoFields(fvtk,data,nCells) write_vtk_timeGeoFields(fvtk,data,nCells, nTimes) #write node data (none for geog) #close the .nc and vtk files data.close() fvtk.close()
def example_2pvu(): # fpath = '/arctic1/nick/cases/v1.0/x4/august/kf/v1.1/x4.kf.output.2006-08-01_00.00.00.nc' fpath = "/arctic1/nick/cases/v1.0/x4/august/tiedtke/v1.1/x4.t.output.2006-08-08_00.00.00.nc" # fpath = '/arctic1/nick/cases/v1.0/x4/august/kf/v1.1/x4.kf.output.2006-08-08_00.00.00.nc' # fnames = searchFiles() # for iFile, fpath in enumerate(fnames): data = output_data.open_netcdf_data(fpath) for timeInd in xrange(0, 28, 4): # for timeInd in [0]: # open the output vtk file and write header, nodes, and cells vtkfname = "x4_t_2006-08-01_1day." + str(28 + timeInd) + ".vtk" # vtkfname = 'x4_cfsr_2006-07-25_1day.'+str(iFile)+'.vtk' fvtk = output_data.write_vtk_header_polydata(vtkfname, fpath) # fvtk = open(vtkfname,'w'); nCells = 163842 nNodes = output_data.write_vtk_xyzNodes(fvtk, data) nCells = output_data.write_vtk_polygons(fvtk, data) # write some cell dataa fvtk.write("\nCELL_DATA " + str(nCells) + "\n") # calc values # timeInd = 0 epv_ht, theta_trop = calc_height_theta_2PVU(data, timeInd) output_data.write_levelData_float("ht_2pvu", fvtk, epv_ht, nCells) output_data.write_levelData_float("theta_2pvu", fvtk, theta_trop, nCells) fvtk.close() data.close()
def derivedSfcs(ncfname, vtkfname): #write some derived surfaces to file data = output_data.open_netcdf_data(ncfname) #header info fvtk = output_data.write_vtk_header_polydata(vtkfname, ncfname) nNodes = output_data.write_vtk_xyzNodes(fvtk, data) nCells = output_data.write_vtk_polygons(fvtk, data) nLevels = len(data.dimensions['nVertLevels']) #cell data fvtk.write('\nCELL_DATA '+str(nCells)+'\n') #geo for reference output_data.write_vtk_staticGeoFields(f,data,nCells) time = 0 #500 mb output_data.write_vtk_pressureHeights(fvtk, data, nCells, time, vLevels, 50000.) #theta on dynamic tropopause pv = np.empty((nCells,nLevels), dtype=float) for hcell in range(nCells): for l in range(nLevels): pv[hcell,l] = vars.calc_ertelPV(data, 'theta', time, hcell, l, nLevels) # pvuVal = 2. thetaVal = np.empty(nCells) for hcell in range(nCells): (l,dl) = output_data.calcIndexOfValue(pvuVal,pv[hcell,:], nLevels) thetaVal[hcell] = output_data.calcValueOfIndex(l,dl,data.variables['theta'][time,hcell,:]) output_data.write_levelData_float('theta_pv', fvtk, thetaVal, nCells) #slp slp = np.empty(nCells) for hcell in range(nCells): slp[hcell] = vars.calc_slp(data, hcell, nLevels, time) output_data.write_levelData_float('slp', fvtk, slp, nCells) #close da files fvtk.close() data.close()
def example_vars(): # for every 6 hours of the IC CFSR data, # write a vtk file with slp, 500mb heights, and theta on 2 pv surface t0 = dt.datetime(2006, 6, 1, 0) # tf = dt.datetime(2006,9,29,18) tf = dt.datetime(2006, 6, 1, 13) h6 = dt.timedelta(hours=6) cfsrPath = "/arctic1/nick/cases/cfsr/" vtkBase = "cfsrIC." i = 1 t = t0 while t <= tf: # since increment t after check, don't do # open the .nc data file for this datetime tString = cfsr.form_cfsrTimeString(t) ncName = "vert_sfc." + tString + ".nc" # initial condition netcdf file ncName = cfsrPath + ncName data = output_data.open_netcdf_data(ncName) # open the output vtk file and write header. ------------------- vtkfname = vtkBase + str(i - 1) + ".vtk" fvtk = output_data.write_vtk_header_polydata(vtkfname, ncName) # write nodes and cells nNodes = output_data.write_vtk_xyzNodes(fvtk, data) nCells = output_data.write_vtk_polygons(fvtk, data) nLevels = len(data.dimensions["nVertLevels"]) # write some cell data -------------------- fvtk.write("\nCELL_DATA " + str(nCells) + "\n") timeInd = 0 # data.close() fvtk.close() # increment day t = t0 + i * h6 i = i + 1
def driver_arctic(): #plot epv on a polar cap ncfname = '/arctic1/nick/cases/163842/testDuda/x1.163842.output.2006-07-15_00.00.00.nc' #ncfname = '/arctic1/mduda/60km/x1.163842.output.2006-07-08_00.00.00.nc' #ncfname = '/home/nickszap/research/mpas/output.2010-10-23_00:00:00.nc' data = netCDF4.Dataset(ncfname,'r') nCellsTotal = len(data.dimensions['nCells']) nVerticesTotal = len(data.dimensions['nVertices']); nLevels = len(data.dimensions['nVertLevels']) nEdgesOnCell = data.variables['nEdgesOnCell'][:]; cellsOnCell = data.variables['cellsOnCell'][:]-1; latThresh = 45.*np.pi/180. #latThresh = 70.*np.pi/180. latCell = data.variables['latCell'][:] cells = conn.gatherArcticCells(latCell, nCellsTotal, latThresh) nCells = len(cells) #open the output vtk file and write header. vtkfname = 'test.arctic.pv_approx.vtk' fvtk = output_data.write_vtk_header_polydata(vtkfname, ncfname) #write nodes and cells output_data.write_vtk_polyHorizConn_domain(data, fvtk, cells, nEdgesOnCell,nVerticesTotal) #cell values and connectivity for this domain haloCells = conn.get_arcticHalo(cells, latCell, latThresh, cellsOnCell, nEdgesOnCell) g2lCell = conn.make_global2localMap(cells, haloCells, nCellsTotal) c2c = conn.make_localDomainNbrs(nCells, cellsOnCell[cells,:], nEdgesOnCell[cells], g2lCell) neededCells = cells.tolist(); neededCells.extend(haloCells) #has to be domain then halo (as in g2l map) #I'm having memory errors. #gc.collect() #load data for domain and halo ----------------------------- timeInd = 0 print "Loading data for domain {0} with {1} cells\n".format('arctic', len(neededCells)) #print neededCells state = loadFields(data, timeInd, neededCells, nLevels) #compute derived variables ------------------------------- #theta on dynamic tropopause pv = np.empty((nCells,nLevels), dtype=float) #make_localDomainNbrs(nCells, cellsOnCell_local, nEdgesOnCell_local, g2lMap) for hCell in xrange(nCells): hNbrs = c2c[hCell,0:nEdgesOnCell[cells[hCell]]] pvColumn = driverErtel(state, hCell, hNbrs, nLevels) #pvColumn = driverErtel_column(state, hCell, hNbrs, nLevels) for l in range(nLevels): pv[hCell,l] = pvColumn[l] # pvuVal = 2.; #pv = np.abs(pv) #don't need questionable hack for southern hemisphere thetaVal = np.empty(nCells) for hCell in xrange(nCells): (l,dl) = output_data.calcIndexOfValue(pvuVal,pv[hCell,:], nLevels) thetaVal[hCell] = output_data.calcValueOfIndex(l,dl,state.theta[hCell,:]) #write some cell data ---------------- fvtk.write('\nCELL_DATA '+str(nCells)+'\n') output_data.write_levelData_float('theta_2pvu', fvtk, thetaVal, nCells) fvtk.close() data.close()
def driver_domains(nSeeds): # ncfname = '/arctic1/mduda/60km/x1.163842.output.2006-07-09_12.00.00.nc' #ncfname = '/home/nickszap/research/mpas/output.2010-10-23_00:00:00.nc' data = netCDF4.Dataset(ncfname,'r') nCellsTotal = len(data.dimensions['nCells']) nVerticesTotal = len(data.dimensions['nVertices']); nLevels = len(data.dimensions['nVertLevels']) nEdgesOnCell = data.variables['nEdgesOnCell'][:]; cellsOnCell = data.variables['cellsOnCell'][:]-1; seed0 = 0 #seed0 = np.argmax(data.variables['meshDensity'][:]) #seems like a decent heuristic cell2Site,seeds = conn.partition_max(seed0, cellsOnCell, nEdgesOnCell,nCellsTotal, nSeeds) for domainInd in xrange(nSeeds): #output domain mesh ------------------------ cells = np.array(xrange(nCellsTotal))[cell2Site==seeds[domainInd]] nCells = len(cells) #open the output vtk file and write header. vtkfname = 'test'+str(domainInd)+'.vtk' fvtk = output_data.write_vtk_header_polydata(vtkfname, ncfname) #write nodes and cells output_data.write_vtk_polyHorizConn_domain(data, fvtk, cells, nEdgesOnCell,nVerticesTotal) #cell values and connectivity for this domain haloCells = conn.getHalo(seeds[domainInd], cell2Site, cellsOnCell, nEdgesOnCell, nCellsTotal) g2lCell = conn.make_global2localMap(cells, haloCells, nCellsTotal) c2c = conn.make_localDomainNbrs(nCells, cellsOnCell[cells,:], nEdgesOnCell[cells], g2lCell) neededCells = cells.tolist(); neededCells.extend(haloCells) #has to be domain then halo (as in g2l map) #I'm having memory errors. #gc.collect() #load data for domain and halo ----------------------------- timeInd = 0 print "Loading data for domain {0} with {1} cells\n".format(domainInd, len(neededCells)) #print neededCells state = loadFields(data, timeInd, neededCells, nLevels) #compute derived variables ------------------------------- #theta on dynamic tropopause pv = np.empty((nCells,nLevels), dtype=float) #make_localDomainNbrs(nCells, cellsOnCell_local, nEdgesOnCell_local, g2lMap) for hCell in xrange(nCells): hNbrs = c2c[hCell,0:nEdgesOnCell[cells[hCell]]] pvColumn = driverErtel(state, hCell, hNbrs, nLevels) for l in range(nLevels): pv[hCell,l] = pvColumn[l] # pvuVal = 2.; pv = np.abs(pv) #questionable hack for southern hemisphere thetaVal = np.empty(nCells) for hCell in range(nCells): (l,dl) = output_data.calcIndexOfValue(pvuVal,pv[hCell,:], nLevels) thetaVal[hCell] = output_data.calcValueOfIndex(l,dl,state.theta[hCell,:]) #write some cell data ---------------- fvtk.write('\nCELL_DATA '+str(nCells)+'\n') output_data.write_levelData_float('theta_2pvu', fvtk, thetaVal, nCells) fvtk.close() data.close()
def example_difference_vtk(): # For this, we have mpas outputs and CFSR ICs every 6 hours. # The files are on the same horizontal and vertical mesh t0 = dt.datetime(2006, 7, 15, 0) mpasFile = mpasOutputName(t0) mpasData = netCDF4.Dataset(mpasFile, "r") # fields: config_frames_per_outfile tells us when we need to get new file # config_output_interval time separation between values nTimeInds = 28 h6 = dt.timedelta(hours=6) s = cfsr.form_cfsrTimeString(t0) diffFile = "diff." + s + ".6hr.vtk" fvtk = output_data.write_vtk_header_polydata(diffFile, s) # write nodes and cells nNodes = output_data.write_vtk_xyzNodes(fvtk, mpasData) nCells = output_data.write_vtk_polygons(fvtk, mpasData) nLevels = len(mpasData.dimensions["nVertLevels"]) fieldKeys = [ "uReconstructX", "uReconstructY", "uReconstructZ", "uReconstructZonal", "uReconstructMeridional", "w", "theta", ] # ,'qv','qc','qr'] for i in xrange(nTimeInds): # for i in xrange(4,5,1): t = t0 + i * h6 initFile = loc_initFile(t) initData = netCDF4.Dataset(initFile, "r") for key in fieldKeys: varInit = initData.variables[key][0, :, :] varMPAS = mpasData.variables[key][i, :, :] varMPAS -= varInit s = "Statistics for MPAS-Init for variable {0} at timInd {1}\n".format(key, i) m = np.mean(varMPAS) dev = np.std(varMPAS) maxd = np.max(np.abs(varMPAS)) s += "{0},\t{1},\t{2}\n".format(m, dev, maxd) print s s = "" # diffData.variables[key][i,:,:] = varMPAS[:,:] # histograms varMPAS.flatten() # else each column is separate dataset nBins = 100 plt2.hist(varMPAS, bins=nBins, normed=False) # save instead of displaying figName = key + ".t" + str(i) plt2.title(figName) # plt2.savefig(figName+'.png') plt2.autoscale(enable=True, axis="x", tight=True) plt2.savefig(figName + ".png") plt2.clf() initData.close() mpasData.close()