def example_nc2vtk_domain(): ''' given .nc (MPAS NETCDF) file, output data into classic vtk format. This way, we can read into VisIt ''' import conn #file properties ncfname = '/home/nickszap/research/mpas/output.2010-10-23_00:00:00.nc' #input file vtkfname = 'domainTest5.vtk' #output file data = open_netcdf_data(ncfname) #partition mesh seed0 = 0; nSeeds = 10 nCellsTotal = len(data.dimensions['nCells']); nVerticesTotal = len(data.dimensions['nVertices']); nEdgesOnCell = data.variables['nEdgesOnCell'][:]; cellsOnCell = data.variables['cellsOnCell'][:]-1; cell2Site,seeds = conn.partition_max(seed0, cellsOnCell, nEdgesOnCell, nCellsTotal, nSeeds) #let's pick out a specific domain domainInd = 5; countThreshold=1 cells = np.array(xrange(nCellsTotal))[cell2Site==seeds[domainInd]] nCells=len(cells) vOnCell = data.variables['verticesOnCell'][cells,:]-1 verts = conn.gatherVerticesInRegion(nCells, vOnCell, nEdgesOnCell[cells,:], nVerticesTotal, countThreshold) #open the output vtk file and write header. fvtk = write_vtk_header_polydata(vtkfname, ncfname) #write nodes and cells nNodes = write_vtk_xyzNodes_domain(fvtk, data, verts) #need local indices for polygon vertices g2lVertex = conn.make_global2localMap(verts, [], nVerticesTotal) vOnCell = conn.make_localDomainNbrs(nCells, vOnCell, nEdgesOnCell[cells,:], g2lVertex) write_vtk_polygons_domain(fvtk, nCells, vOnCell, nEdgesOnCell[cells,:]) #write some cell data fvtk.write('\nCELL_DATA '+str(nCells)+'\n') write_vtk_cellLandType_domain(fvtk, data, cells) #write some node data #close the files data.close() fvtk.close()
def write_vtk_polyHorizConn_domain(data, fvtk, cells, nEdgesOnCell,nVerticesTotal): #write the polygons in this domain to file in legacy vtk format import conn #write nodes and cells nCells = len(cells) vOnCell = data.variables['verticesOnCell'][cells,:]-1 verts = conn.gatherVerticesInRegion(nCells, vOnCell,nEdgesOnCell[cells,:], nVerticesTotal, 1) nNodes = write_vtk_xyzNodes_domain(fvtk, data, verts) #need local indices for polygon vertices g2lVertex = conn.make_global2localMap(verts, [], nVerticesTotal) vOnCell = conn.make_localDomainNbrs(nCells, vOnCell, nEdgesOnCell[cells,:], g2lVertex) write_vtk_polygons_domain(fvtk, nCells, vOnCell, nEdgesOnCell[cells,:])
def test_deriv(): #we'll use analytic functions to test derivs 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; c0 = 0 cells = np.array([c0]) nCells = len(cells) haloCells = cellsOnCell[c0,0:nEdgesOnCell[c0]] #cell values and connectivity for this domain 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) #edit state with analytic functions state.ux[:,:] = state.xyz[:,:,0]; print state.ux state.uy[:,:] = state.xyz[:,:,1] state.uz[:,:] = state.xyz[:,:,2] out = [None]*nCells for hCell in xrange(nCells): grads = [] gInd = cells[hCell] hNbrs = c2c[hCell,0:nEdgesOnCell[gInd]] nHNbrs = len(hNbrs) nVars = 4 #u's and theta vals0 = [None]*nVars vals0[0] = state.ux[hCell,:] vals0[1] = state.uy[hCell,:] vals0[2] = state.uz[hCell,:] vals0[3] = state.theta[hCell,:] xyz0 = state.xyz[hCell,:,:] #.copy() #these get altered! #ht0 = state.ht[hCell,:] valsNbrs = [None]*nVars valsNbrs[0] = state.ux[hNbrs,:] valsNbrs[1] = state.uy[hNbrs,:] valsNbrs[2] = state.uz[hNbrs,:] valsNbrs[3] = state.theta[hNbrs,:] xyzNbrs = state.xyz[hNbrs,:,:] #.copy() #htNbrs = state.ht[hNbrs,:] for l in xrange(nLevels): grads.append(calc_varsGradients_leastSquare(l, nVars, xyz0, xyzNbrs,vals0,valsNbrs,nHNbrs, nLevels)) #grads = calc_varsGradients_leastSquare_column(ht0, htNbrs, nVars, xyz0, xyzNbrs,vals0,valsNbrs,nHNbrs, nLevels) out[hCell] = grads print grads data.close() return out
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()