def test_translate_orient(): cell_size = 0.05 o0 = geo.GeoPoint.from_degrees(-12.0, 112.0) o1 = geo.GeoPoint.from_degrees(-12.15, 112.0) o2 = geo.GeoPoint.from_degrees(-13.0, 112.0) g0 = geo.GeoReference(o0, 20, 20, cell_size, lat_orient=-1) g1 = geo.GeoReference(o1, 4, 5, cell_size, lat_orient=-1) g2 = geo.GeoReference(o2, 18, 10, cell_size, lat_orient=1) e0 = extents.Extent(g0) e1 = extents.Extent(g1) e2 = extents.Extent(g2) e1t = e1.translate(e0.geospatial_reference()) assert ((e1t.latitudes == e1.latitudes).all()) assert (e1.origin == e1t.origin) assert (e1t.geospatial_reference() == e1.geospatial_reference()) assert ((e1t.parent_ref.origin - e1.parent_ref.origin).lat == 0.15) e2.set_mask(np.tri(*e2.shape).astype(bool)) e2c = e2.translate(g0) assert ((e2.areas == e2c.areas[::-1, :]).all()) assert (( e2c.latitudes.to_degrees()[::-1] == e2.latitudes.to_degrees()).all())
def get_from_multiple(source_extents): ''' Get an extent containing all the cells of source_extents +++ This assumes all source extents contain compatible georefs with the same orientation/cell_size etc It needs a bit of a tidy-up! ''' egr = source_extents[0].geospatial_reference() offsets = [ egr.get_offset(e.geospatial_reference()) for e in source_extents ] ncells = [(e.nlats, e.nlons) for e in source_extents] lat_offsets = np.array(offsets)[:, 0] lon_offsets = np.array(offsets)[:, 1] nlats = np.array(ncells)[:, 0] nlons = np.array(ncells)[:, 1] min_lat_offset = np.min(lat_offsets) min_lon_offset = np.min(lon_offsets) shape = np.max(lat_offsets + nlats) - np.min(lat_offsets), np.max( lon_offsets + nlons) - np.min(lon_offsets) m = np.empty(shape, dtype=bool) m[...] = True full_origin = egr.origin + geo.GeoPoint( egr.cell_size * min_lat_offset * egr.lat_orient, egr.cell_size * min_lon_offset * egr.lon_orient) georef = geo.GeoReference(full_origin, shape[0], shape[1], egr.cell_size, egr.lat_orient, egr.lon_orient) for s in source_extents: l_ex = s.translate(georef) m[l_ex.indices][l_ex.mask == False] = False return Extent(georef, mask=m)
def test_orient(): o1 = geo.GeoPoint.from_degrees(-12,112.0) o2 = geo.GeoPoint.from_degrees(-33.95,112.0) cell_size = 0.05 g1 = geo.GeoReference(o1,440,10,cell_size,lat_orient=-1) g2 = geo.GeoReference(o2,440,10,cell_size,lat_orient=1) assert(g2.to_orient(-1) == g1) assert(g1.to_orient(1) == g2) assert(g1 != g2) assert(g1.to_orient(1).to_orient(-1) == g1) assert(g1 == g1.to_mode(geo.GeoReferenceMode.CORNER).to_orient(1).to_mode(geo.GeoReferenceMode.CENTER).to_orient(-1))
def __getitem__(self, k): g = self.f.groups[k] lats = g.variables['latitude'][...] lons = g.variables['longitude'][...] avar = g.variables['area'] areas = avar[...] mask = areas == 0. cell_size = float(avar.getncattr('cell_size')) lat_orient = int(avar.getncattr('lat_orient')) origin = geo.GeoPoint.from_degrees(lats[0], lons[0]) georef = geo.GeoReference(origin, len(lats), len(lons), cell_size, lat_orient) return extents.Extent(georef, mask=mask, areas=areas)
def _getitem_fast(self, k): g = self.f[k] lats = g['latitude'][...] lons = g['longitude'][...] avar = g['area'] areas = avar[...] mask = areas == 0. cell_size = float(avar.attrs['cell_size']) lat_orient = int(avar.attrs['lat_orient']) origin = geo.GeoPoint.from_degrees(lats[0], lons[0]) georef = geo.GeoReference(origin, len(lats), len(lons), cell_size, lat_orient) return extents.Extent(georef, mask=mask, areas=areas)
def get_data(self,coords): origin = geo.GeoPoint.from_degrees(coords['latitude'][0],coords['longitude'][0]) nlats,nlons = len(coords['latitude']), len(coords['longitude']) georef = geo.GeoReference(origin,nlats,nlons,self.cell_size,lat_orient=self.lat_orient) target = extents.Extent(georef) ss,offset,scalefac = get_extent_scale_indices(self.cextent,target) data = self.child.get_data(gen_coordset(coords['time'].index,ss)) dres = downscale(data,scalefac) return dres
def setup_var_coords(): global m_tvar global extent global test_path georef = geo.GeoReference((0,0),1000,1000,0.05) extent = extents.Extent(georef).ioffset[0:10,0:10] period = dt.dates('dec 2000 - jan 25 2001') tvar = mt.Variable('test_var','mm') m_tvar = mt.MappedVariable(tvar,mt.gen_coordset(period,extent),np.float32) test_path = os.path.join(os.path.dirname(__file__),'file_tests') shutil.rmtree(test_path,True)
def get_extent(self, use_mask=True): #from awrams.utils.extents import from_boundary_coords from awrams.utils import geo from awrams.utils.extents import Extent lat, lon = self.get_coord('latitude'), self.get_coord('longitude') if use_mask is True: ref_data = self.awra_var[0] if not hasattr(ref_data, 'mask'): try: ### maybe using h5py which doesn't return an ma mask = np.ma.masked_values( ref_data, self.awra_var.attrs['_FillValue'][0]) mask = mask.mask except AttributeError: mask = False else: mask = ref_data.mask else: mask = False origin = geo.GeoPoint.from_degrees(lat.index[0], lon.index[0]) cell_size = geo.GeoUnit.from_dms(lat.index[-1] - lat.index[0]) / (len(lat.index) - 1) nlats, nlons = len(lat), len(lon) lat_orient = 1 if cell_size > geo.GeoUnit(0) else -1 cell_size = cell_size * lat_orient georef = geo.GeoReference(origin, nlats, nlons, cell_size, lat_orient=lat_orient) if not isinstance(mask, np.ndarray): mask = bool(mask) return Extent(georef, mask=mask)
def geospatial_reference(self): origin = self.parent_ref.geo_for_cell(self.lat_offset, self.lon_offset) return geo.GeoReference(origin, self.nlats, self.nlons, self.cell_size, self.parent_ref.lat_orient, self.parent_ref.lon_orient)