def toCurveGrid(self, gridid=None): """Convert to a curvilinear grid. 'gridid' is the string identifier of the resulting curvilinear grid object. """ from coord import TransientVirtualAxis, TransientAxis2D from hgrid import TransientCurveGrid lat = self._lataxis_[:] latunits = self._lataxis_.units lon = self._lonaxis_[:] lonunits = self._lonaxis_.units blat, blon = self.getBounds() mask = self.getMask() nlat = len(lat) nlon = len(lon) centerLat = numpy.repeat(lat[:,numpy.newaxis], nlon, axis=1) centerLon = numpy.repeat(lon[numpy.newaxis,:], nlat, axis=0) # Create corner latitudes, ensuring counterclockwise direction clat = numpy.zeros((nlat, 4), numpy.float) if (blat[0,0]<= blat[0,1]): incr = 1 else: incr = 0 clat[:,0] = blat[:,1-incr] clat[:,1] = blat[:,1-incr] clat[:,2] = blat[:,incr] clat[:,3] = blat[:,incr] cornerLat = numpy.repeat(clat[:,numpy.newaxis,:], nlon, axis=1) # Create corner longitudes, ensuring counterclockwise direction clon = numpy.zeros((nlon, 4), numpy.float) if (blon[0,0]<= blon[0,1]): incr = 1 else: incr = 0 clon[:,0] = blon[:,1-incr] clon[:,1] = blon[:,incr] clon[:,2] = blon[:,incr] clon[:,3] = blon[:,1-incr] cornerLon = numpy.repeat(clon[numpy.newaxis,:,:], nlat, axis=0) iaxis = TransientVirtualAxis("i",nlat) jaxis = TransientVirtualAxis("j",nlon) lataxis = TransientAxis2D(centerLat, axes=(iaxis, jaxis), bounds=cornerLat, attributes={'units':latunits}, id="latitude") lonaxis = TransientAxis2D(centerLon, axes=(iaxis, jaxis), bounds=cornerLon, attributes={'units':lonunits}, id="longitude") grid = TransientCurveGrid(lataxis, lonaxis, id=gridid, tempmask=mask) return grid
def readScripCurveGrid(fileobj, dims, whichType, whichGrid): """Read a 'native' SCRIP grid file, returning a transient curvilinear grid. fileobj is an open CDMS dataset or file object. dims is the grid shape. whichType is the type of file, either "grid" or "mapping" if whichType is "mapping", whichGrid is the choice of grid, either "source" or "destination" """ import string from coord import TransientAxis2D if 'S' in fileobj.variables.keys(): if whichType == "grid": gridCornerLatName = 'grid_corner_lat' gridCornerLonName = 'grid_corner_lon' gridMaskName = 'grid_imask' gridCenterLatName = 'grid_center_lat' gridCenterLonName = 'grid_center_lon' titleName = 'title' elif whichGrid == "destination": gridCornerLatName = 'yv_b' gridCornerLonName = 'xv_b' gridMaskName = 'mask_b' gridCenterLatName = 'yc_b' gridCenterLonName = 'xc_b' titleName = 'dest_grid' else: gridCornerLatName = 'yv_a' gridCornerLonName = 'xv_a' gridMaskName = 'mask_a' gridCenterLatName = 'yc_a' gridCenterLonName = 'xc_a' titleName = 'source_grid' else: if whichType == "grid": gridCornerLatName = 'grid_corner_lat' gridCornerLonName = 'grid_corner_lon' gridMaskName = 'grid_imask' gridCenterLatName = 'grid_center_lat' gridCenterLonName = 'grid_center_lon' titleName = 'title' elif whichGrid == "destination": gridCornerLatName = 'dst_grid_corner_lat' gridCornerLonName = 'dst_grid_corner_lon' gridMaskName = 'dst_grid_imask' gridCenterLatName = 'dst_grid_center_lat' gridCenterLonName = 'dst_grid_center_lon' titleName = 'dest_grid' else: gridCornerLatName = 'src_grid_corner_lat' gridCornerLonName = 'src_grid_corner_lon' gridMaskName = 'src_grid_imask' gridCenterLatName = 'src_grid_center_lat' gridCenterLonName = 'src_grid_center_lon' titleName = 'source_grid' vardict = fileobj.variables cornerLat = fileobj(gridCornerLatName) cornerLon = fileobj(gridCornerLonName) ncorners = cornerLat.shape[-1] ni = dims[1] nj = dims[0] gridshape = (ni, nj) boundsshape = (ni, nj, ncorners) if hasattr(cornerLat, 'units') and string.lower( cornerLat.units)[0:6] == 'radian': cornerLat = (cornerLat * (180.0 / numpy.pi)).reshape(boundsshape) cornerLon = (cornerLon * (180.0 / numpy.pi)).reshape(boundsshape) else: cornerLat = cornerLat.reshape(boundsshape) cornerLon = cornerLon.reshape(boundsshape) iaxis = TransientVirtualAxis("i", ni) jaxis = TransientVirtualAxis("j", nj) if vardict.has_key(gridMaskName): # SCRIP convention: 0 for invalid data # numpy.ma convention: 1 for invalid data mask = 1 - fileobj(gridMaskName) mask = mask.reshape(gridshape) else: mask = None if vardict.has_key(gridCenterLatName): centerLat = fileobj(gridCenterLatName).reshape(gridshape) gclat = fileobj[gridCenterLatName] if hasattr(gclat, "units") and string.lower(gclat.units) == 'radians': centerLat *= (180.0 / numpy.pi) else: centerLat = cornerLat[:, :, 0] if vardict.has_key(gridCenterLonName): centerLon = fileobj(gridCenterLonName).reshape(gridshape) gclon = fileobj[gridCenterLonName] if hasattr(gclon, "units") and string.lower(gclon.units) == 'radians': centerLon *= (180.0 / numpy.pi) else: centerLon = cornerLon[:, :, 0] if hasattr(fileobj, titleName): gridid = getattr(fileobj, titleName) gridid = string.replace(string.strip(gridid), ' ', '_') else: gridid = "<None>" lataxis = TransientAxis2D(centerLat, axes=(iaxis, jaxis), bounds=cornerLat, attributes={'units': 'degrees_north'}, id="latitude") lonaxis = TransientAxis2D(centerLon, axes=(iaxis, jaxis), bounds=cornerLon, attributes={'units': 'degrees_east'}, id="longitude") grid = TransientCurveGrid(lataxis, lonaxis, id=gridid, tempmask=mask) return grid
def toCurveGrid(self, gridid=None): """Convert to a curvilinear grid. 'gridid' is the string identifier of the resulting curvilinear grid object. """ from coord import TransientVirtualAxis, TransientAxis2D from hgrid import TransientCurveGrid lat = self._lataxis_[:] lon = self._lonaxis_[:] latunits = '' if hasattr(self._lataxis_, 'units'): latunits = self._lataxis_.units lonunits = '' if hasattr(self._lonaxis_, 'units'): lonunits = self._lonaxis_.units blat, blon = self.getBounds() mask = self.getMask() nlat = len(lat) nlon = len(lon) order = self.getOrder() # Deal with the order of the axes # ax - first index, ay - second index if re.search(order, 'xy', re.I): orderXY = True ax, ay = lat, lon bx, by = blat, blon nx, ny = nlat, nlon else: orderXY = False ax, ay = lon, lat bx, by = blon, blat nx, ny = nlon, nlat centerX = numpy.outer(numpy.ones(ny), ax) centerY = numpy.outer(ay, numpy.ones(nx)) # Create corner latitudes (in yx order), ensuring counterclockwise direction cy = numpy.zeros((ny, 4), numpy.float) if (by[0, 0] <= by[0, 1]): incr = 1 else: incr = 0 cy[:, 0] = by[:, 1 - incr] cy[:, 1] = by[:, 1 - incr] cy[:, 2] = by[:, incr] cy[:, 3] = by[:, incr] cornerY = numpy.repeat(cy[:, numpy.newaxis, :], nx, axis=1) # Create corner longitudes (in yx order), ensuring counterclockwise direction cx = numpy.zeros((nx, 4), numpy.float) if (bx[0, 0] <= bx[0, 1]): incr = 1 else: incr = 0 cx[:, 0] = bx[:, 1 - incr] cx[:, 1] = bx[:, incr] cx[:, 2] = bx[:, incr] cx[:, 3] = bx[:, 1 - incr] cornerX = numpy.repeat(cx[numpy.newaxis, :, :], ny, axis=0) iaxis = TransientVirtualAxis("i", ny) # First axis jaxis = TransientVirtualAxis("j", nx) # Second axis centerLat = centerY centerLon = centerX cornerLat = cornerY cornerLon = cornerX if orderXY: centerLat = centerX centerLon = centerY cornerLat = cornerX cornerLon = cornerY lataxis = TransientAxis2D(centerLat, axes=(iaxis, jaxis), bounds=cornerLat, attributes={'units': latunits}, id="latitude") lonaxis = TransientAxis2D(centerLon, axes=(iaxis, jaxis), bounds=cornerLon, attributes={'units': lonunits}, id="longitude") grid = TransientCurveGrid(lataxis, lonaxis, id=gridid, tempmask=mask) return grid