def get_area_cells(grid, lon, lat): """Calculate the area for each cell in the grid. lon/lat are coordinates in decimal degrees. Parameters ---------- grid : 2d-array A rectangular grid. x, y : 1d-arrays The coordinates of the cells or nodes (edges). If x and y are of the same lengh as grid dimensions, then cell-centered coordinates are assumed, otherwise edges are assumed. Returns ------- out : 2d-array Same shape as 'grid' with the values of the area on each cell. Notes ----- Grid-cell area is in km**2. """ ny, nx = grid.shape area = np.zeros_like(grid) if len(lat) == ny: lon, lat = cell2node(lon, lat) # convert cells -> nodes for i in xrange(ny): for j in xrange(nx): a = haversine(lon[j], lat[i], lon[j], lat[i+1]) b = haversine(lon[j], lat[i], lon[j+1], lat[i]) area[i,j] = a * b return area
f = tb.open_file(FILE_MSK, 'r') x_msk = f.root.x[:] y_msk = f.root.y[:] msk = f.root.mask[:] f.close() print 'done' if 1: # 2d -> 1d, x/y -> lon/lat x_msk, y_msk = np.meshgrid(x_msk, y_msk) # 1d -> 2d x_msk, y_msk = x_msk.ravel(), y_msk.ravel() # 2d -> 1d msk = msk.ravel() lon_msk, lat_msk = ap.xy2ll(x_msk, y_msk, units='m') lon_msk = ap.lon_180_360(lon_msk) del x_msk, y_msk lon_nodes, lat_nodes = ap.cell2node(lon, lat) area = np.full(data[0].shape, np.nan) cells_idx = [] # count number of mask cells falling into each data cell for i in xrange(len(lat)): for j in xrange(len(lon)): i_cells, = np.where((lon_nodes[j] <= lon_msk) & \ (lon_msk <= lon_nodes[j+1]) & \ (lat_nodes[i] <= lat_msk) & \ (lat_msk <= lat_nodes[i+1]) & \ (msk == 4)) # 4 = ice shelf area[i,j] = len(i_cells) # each mask cell is 1 km**2 cells_idx = np.append(cells_idx, i_cells) cells_idx = [int(i) for i in cells_idx]
if 1: # print values f = poly_rate[~np.isnan(poly_rate)].round(2) print 'poly rate (m/yr): ', f f = line_rate[~np.isnan(line_rate)].round(2) print 'line rate (m/yr): ', f f = dpoly_rate[~np.isnan(dpoly_rate)].round(2) print 'dpoly acce (m/yr2): ', f #--------------------------------------------------------------------- # save data #--------------------------------------------------------------------- # spherical -> cartesian xx, yy = np.meshgrid(lon, lat) xed, yed = ap.cell2node(lon, lat) xed2d, yed2d = np.meshgrid(xed, yed) xyz = np.column_stack(ap.sph2xyz(xed2d.ravel(), yed2d.ravel())) if 1: print('saving data...') fout = tb.open_file(DIR + FILE_OUT, 'w') try: fout.create_array('/', 'time', time) except: pass try: fout.create_array('/', 'lon', lon) fout.create_array('/', 'lat', lat) except: pass
data = as_frame(data, dt, lat, lon) data = data.apply(peak_filt, x=time, n_std=3, max_deg=3, iterative=True, plot=False, raw=True) # (NO) filter ts with std > max_std if 0: print 'std filtering...' data = as_frame(data, dt, lat, lon) data = data.apply(std_filt, x=time, max_std=2, max_deg=3, plot=False, raw=False) # (yes) filter out uncomplete time series if 1: print 'percent filtering...' data = data.apply(percent_filt, min_perc=0.7, plot=False, raw=True) # spherical -> cartesian xed, yed = ap.cell2node(lon, lat) xed2d, yed2d = np.meshgrid(xed, yed) xyz = np.column_stack(ap.sph2xyz(xed2d.ravel(), yed2d.ravel())) data = as_frame(data, dt, lat, lon) data = data.apply(ap.referenced, to='mean', raw=True) # to mean !!! data = as_array(data) error[np.isnan(data)] = np.nan #--------------------------------------------------------------------- if PLOT: # plot time series data = as_array(data) j, = ap.find_nearest(lon, LON) i, = ap.find_nearest(lat, LAT) i, j = i[0], j[0]