Example #1
0
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()
Example #2
0
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()