def extract_box(a, region=None): """ extract a subregion a : DimArray instance region: lon_ll, lat_ll, lon_ur, lat_ur or Region instance """ # interactive drawing of a region if region is None: region = regmod.drawbox() # check the arguments: initialize a BoxRegion if not yet a region regobj = regmod.check(region) i, j = regobj.box_idx(a.lon, a.lat) return a.take({'lat': i, 'lon': j}, indexing='position')
def extract_box(a, region=None): """ extract a subregion a : DimArray instance region: lon_ll, lat_ll, lon_ur, lat_ur or Region instance """ # interactive drawing of a region if region is None: region = regmod.drawbox() # check the arguments: initialize a BoxRegion if not yet a region regobj = regmod.check(region) i, j = regobj.box_idx(a.lon, a.lat) return a.take({'lat':i, 'lon':j}, indexing='position')
def regional_mean(a, region=None): """ Average along lon/lat input: - a: DimArray instance including lat, lon axis - region, optional: [lon, lat]: Point [lon_ll, lat_ll, lon_ur, lat_ur]: BoxRegion or Region instance output: - GeoArray instance, regional average Notes: ------ Will adjust longitude axis over [0, 360] or [-180, 180] as necessary Examples: --------- >>> lon = [0, 50., 100.] >>> lat = [0, 50.] >>> a = GeoArray([[1, 2, 4],[4, 5, 6]], lon=lon, lat=lat) >>> rm = regional_mean(a) >>> rm 3.3767428905946684 Which just weights data with cos(lat) >>> lon2, lat2 = np.meshgrid(lon, lat) >>> w = np.cos(np.radians(lat2)) >>> rm2 = np.sum(a.values*w)/np.sum(w) >>> round(rm2, 4) 3.3767 GeoArray detects the weights automatically with name "lat" >>> rm3 = a.mean() >>> round(rm3,4) 3.3767 >>> a.mean(weights=None) 3.6666666666666665 Also works for multi-dimensional data shaped in any kind of order >>> a = a.newaxis('z',[0,1,2]) >>> a = a.transpose(('lon','z','lat')) >>> regional_mean(a) geoarray: 3 non-null elements (0 null) dimensions: 'z' 0 / z (3): 0 to 2 array([ 3.37674289, 3.37674289, 3.37674289]) """ regobj = regmod.check(region) dims = ('lat', 'lon') a = GeoArray(a) # make it a GeoArray to check lon, lat if not set(dims).issubset(a.dims): raise ValueError("does not have lon, lat axes: {}".format(a)) # rearrange dimensions with dims first flatdims = [ax.name for ax in a.axes if ax.name not in dims] newdims = dims + tuple(flatdims) if a.dims != newdims: a = a.transpose(newdims) # If 2-D return a scalar if a.dims == ('lat', 'lon'): return regobj.mean(a.lon, a.lat, a.values) # flatten all except lat, lon, and make it the first axis agrp = a.group(flatdims, insert=0) grpaxis = agrp.axes[0] # grouped axis # iterate over the first, grouped axis results = [] for i in range(grpaxis.size): res = regobj.mean(a.lon, a.lat, agrp.values[i]) results.append(res) # flat object grp_ave = a._constructor(results, [grpaxis]) average = grp_ave.ungroup() # now ungroup return average
def regional_mean(a, region=None): """ Average along lon/lat input: - a: DimArray instance including lat, lon axis - region, optional: [lon, lat]: Point [lon_ll, lat_ll, lon_ur, lat_ur]: BoxRegion or Region instance output: - GeoArray instance, regional average Notes: ------ Will adjust longitude axis over [0, 360] or [-180, 180] as necessary Examples: --------- >>> lon = [0, 50., 100.] >>> lat = [0, 50.] >>> a = GeoArray([[1, 2, 4],[4, 5, 6]], lon=lon, lat=lat) >>> rm = regional_mean(a) >>> rm 3.3767428905946684 Which just weights data with cos(lat) >>> lon2, lat2 = np.meshgrid(lon, lat) >>> w = np.cos(np.radians(lat2)) >>> rm2 = np.sum(a.values*w)/np.sum(w) >>> round(rm2, 4) 3.3767 GeoArray detects the weights automatically with name "lat" >>> rm3 = a.mean() >>> round(rm3,4) 3.3767 >>> a.mean(weights=None) 3.6666666666666665 Also works for multi-dimensional data shaped in any kind of order >>> a = a.newaxis('z',[0,1,2]) >>> a = a.transpose(('lon','z','lat')) >>> regional_mean(a) geoarray: 3 non-null elements (0 null) dimensions: 'z' 0 / z (3): 0 to 2 array([ 3.37674289, 3.37674289, 3.37674289]) """ regobj = regmod.check(region) dims = ('lat','lon') a = GeoArray(a) # make it a GeoArray to check lon, lat if not set(dims).issubset(a.dims): raise ValueError("does not have lon, lat axes: {}".format(a)) # rearrange dimensions with dims first flatdims = [ax.name for ax in a.axes if ax.name not in dims] newdims = dims + tuple(flatdims) if a.dims != newdims: a = a.transpose(newdims) # If 2-D return a scalar if a.dims == ('lat', 'lon'): return regobj.mean(a.lon,a.lat, a.values) # flatten all except lat, lon, and make it the first axis agrp = a.group(flatdims, insert=0) grpaxis = agrp.axes[0] # grouped axis # iterate over the first, grouped axis results = [] for i in range(grpaxis.size): res = regobj.mean(a.lon,a.lat, agrp.values[i]) results.append(res) # flat object grp_ave = a._constructor(results, [grpaxis]) average = grp_ave.ungroup() # now ungroup return average