def _create_df_with_sats(time1, time2, ts, satname, satnames, **kw): """ Create a DataFrame with one TS per `satellite`. time1, time2 : integer representation of time: YYYYMMDD Note: i,j = y,x <<<<< important! """ dtime1 = ap.num2date(time1) dtime2 = ap.num2date(time2) dtime_range = full_dtime_range(dtime1, dtime2, **kw) #dtime_range = full_dtime_range(dtime1.min(), dtime2.max(), **kw) df = pn.DataFrame(index=dtime_range, columns=satnames) # empty DF # fill df with 1 `satname` TS at a time if not np.alltrue(np.isnan(ts)): for sat in satnames: ind, = np.where(sat == satname) df[sat] = pn.Series(ts[ind], index=dtime2[ind]) return df
def create_df_with_ts_by_first(time1, time2, ts, **kw): """ Create a DataFrame with all the different time1-TS. time1, time2 : integer representation of time: YYYYMMDD ts : time series for 1-grid-cell/1-sat/all-times """ dtime1 = ap.num2date(time1) dtime2 = ap.num2date(time2) dtime_range = full_dtime_range(dtime1, dtime2, **kw) df = pn.DataFrame(index=dtime_range, columns=dtime_range) # empty DF # fill df with 1 `reftime-TS` at a time if not np.alltrue(np.isnan(ts)): for dt_ref in dtime_range: ind, = np.where(dtime1 == dt_ref) df[dt_ref] = pn.Series(ts[ind], index=dtime2[ind]) df[dt_ref][dt_ref] = np.nan # diagonal ''' if not np.alltrue(df[dt_ref].isnull().values): df[dt_ref][dt_ref] = 0. ''' return df
def create_df_with_sats(time, ts, satname, satnames, **kw): """ Create a DataFrame with one TS per `satellite`. time1, time2 : integer representation of time: YYYYMMDD Note1: i,j = y,x <<<<< important! Note2: `satenames` is needed to preserve the order. """ dtime = ap.num2date(time) dtime_range = full_dtime_range2(dtime) df = pn.DataFrame(index=dtime_range, columns=satnames) # empty DF # fill df with 1 `satname` TS at a time if not np.alltrue(np.isnan(ts)): for sat in satnames: ind, = np.where(sat == satname) df[sat] = pn.Series(ts[ind], index=dtime[ind]) return df
''' t = ap.year2date(f1.root.time[:]) lon = f1.root.lon[:] lat = f1.root.lat[:] h = f1.root.h[:] df_gps = pd.DataFrame({'lon': lon, 'lat': lat, 'h': h}, index=t) print df_gps.head() del t, lon, lat, h ''' # altimetry h0 = f2.root.dh_mean_all[:] #h1 = f2.root.dh_mean_corr_short_all[:] #h2 = f2.root.h_firn[:] #g = f2.root.dg_mean_all[:] t0 = ap.num2date(f2.root.time_all[:]) #t2 = ap.num2date(f2.root.time_firn[:]) lon = f2.root.x_edges[:] lat = f2.root.y_edges[:] ''' # maps grid indexes to coordinates ii, jj = xrange(len(lat)), xrange(len(lon)) ij2ll = dict([((i,j),(la,lo)) for i,j,la,lo in zip(ii,jj,lat,lon)]) df_h0 = pd.Panel(h0, items=t0, major_axis=ii, minor_axis=jj ).to_frame(filter_observations=False).T del h0, t0, lon, lat print df_h0 sys.exit() '''
def main(): fname_in = sys.argv[1] din = GetData(fname_in, 'a') time = ap.num2date(getattr(din, T_NAME)[:]) lon = din.lon[:] lat = din.lat[:] nrows = len(time) nt, ny, nx = getattr(din, H_NAME).shape # i,j,k = t,y,x RR = np.empty((nt,ny,nx), 'f8') * np.nan SS = np.empty((nt,ny,nx), 'f8') * np.nan if TINT: intervals = [ap.year2date(tt) for tt in INTERVALS] if TINT: print 'using time-interval correlation' elif TVAR: print 'using time-variable correlation' else: print 'using constant correlation' print 'processing time series:' isfirst = True # iterate over every grid cell (all times): i,j = y,x #----------------------------------------------------------------- for i in xrange(ny): for j in xrange(nx): print 'time series of grid-cell:', i, j dh = getattr(din, H_NAME)[:nrows,i,j] dg = getattr(din, G_NAME)[:nrows,i,j] if np.alltrue(np.isnan(dh)): continue #--------------------------------------------------------- if TINT: # satellite-dependent R and S dh_cor, RR[:,i,j], SS[:,i,j] = \ ap.backscatter_corr3(dh, dg, time, intervals, diff=DIFF, robust=True) elif TVAR: # time-varying R and S dh_cor, RR[:,i,j], SS[:,i,j] = \ ap.backscatter_corr2(dh, dg, diff=DIFF, robust=True, npts=NPTS) else: # constant R and S dh_cor, RR[:,i,j], SS[:,i,j] = \ ap.backscatter_corr(dh, dg, diff=DIFF, robust=True) #--------------------------------------------------------- # plot figures if PLOT_TS: dh_cor = ap.referenced(dh_cor, to='first') dh = ap.referenced(dh, to='first') dg = ap.referenced(dg, to='first') k, = np.where(~np.isnan(RR[:,i,j])) r = np.mean(RR[k,i,j]) s = np.mean(SS[k,i,j]) fig = plot_rs(time, RR[:,i,j], SS[:,i,j]) fig = plot_ts(time, lon[j], lat[i], dh_cor, dh, dg, r, s, diff=DIFF) if fig is None: continue plt.show() # save one TS per grid cell at a time #--------------------------------------------------------- if not SAVE_TO_FILE: continue if isfirst: # open or create output file isfirst = False atom = tb.Atom.from_type('float64', dflt=np.nan) filters = tb.Filters(complib='zlib', complevel=9) c1 = din.file.createCArray('/', SAVE_AS_NAME, atom, (nt,ny,nx), '', filters) c2 = din.file.createCArray('/', R_NAME, atom, (nt,ny,nx), '', filters) c3 = din.file.createCArray('/', S_NAME, atom, (nt,ny,nx), '', filters) c1[:,i,j] = dh_cor if SAVE_TO_FILE: c2[:] = RR c3[:] = SS if PLOT_MAP: if TVAR: # 3D -> 2D RR = np.mean(RR[~np.isnan(RR)], axis=0) SS = np.mean(SS[~np.isnan(SS)], axis=0) plot_map(lon, lat, np.abs(RR), BBOX, MFILE, mres=1, vmin=0, vmax=1) plt.title('Correlation Coefficient, R') plt.savefig('map_r.png') plot_map(lon, lat, SS, BBOX, MFILE, mres=1, vmin=-0.2, vmax=0.7) plt.title('Correlation Gradient, S') plt.savefig('map_s.png') plt.show() din.file.close() if SAVE_TO_FILE: print 'out file -->', fname_in
import seaborn as sns import altimpy as ap #fname = '/Volumes/RADARALT/fpaolo/data/shelves/all_19920716_20111015_shelf_tide_grids_mts.h5' fname = '/Users/fpaolo/data/shelves/all_19920716_20111015_shelf_tide_grids_mts.h5.ice_oce' def gradient(y, dt=.25): return np.gradient(y.values, dt) with tb.open_file(fname) as f: alt0 = f.root.dh_mean_xcal[:] #alt0 = f.root.dh_mean_xcal_short_const[:] alt1 = f.root.dg_mean_xcal[:] firn = f.root.h_firn[:] t_alt = ap.num2date(f.root.time_xcal[:]) t_firn = ap.num2date(f.root.time_firn[:]) lon = f.root.lon[:] lat = f.root.lat[:] # map grid indexes to coordinates ii, jj = xrange(len(lat)), xrange(len(lon)) ij2ll = dict([((i,j),(la,lo)) for i,j,la,lo in zip(ii,jj,lat,lon)]) df_alt0 = pd.Panel(alt0, items=t_alt, major_axis=ii, minor_axis=jj ).to_frame(filter_observations=False).T df_alt1 = pd.Panel(alt1, items=t_alt, major_axis=ii, minor_axis=jj ).to_frame(filter_observations=False).T df_firn = pd.Panel(firn, items=t_firn, major_axis=ii, minor_axis=jj ).to_frame().T
lats1 = d1["lat"][:] # 2d ism = d1["ism"][:] lsm = d1["lsm"][:] dt1 = ap.year2date(year1) # dt1 = year1 f2 = tb.openFile(FILE2) elev = f2.root.elev[:] time2 = f2.root.time[:] lon2 = f2.root.lon[:] # 1d lat2 = f2.root.lat[:] # 1d lon2 = ap.lon_180_360(lon2, inverse=True) lons2, lats2 = np.meshgrid(lon2, lat2) points2 = np.column_stack((lons2.ravel(), lats2.ravel())) # points2 = (lons2[6,4], lats2[6,4]) dt2 = ap.num2date(time2) # dt2 = ap.num2year(time2) """ f3 = tb.openFile(FILE3) d3 = f3.root.data[:] y3, x3 = d3[:,0], d3[:,1] x3 = ap.lon_180_360(x3, inverse=True) f4 = tb.openFile(FILE4) d4 = f4.root.data[:] y4, x4 = d4[:,0], d4[:,1] x4 = ap.lon_180_360(x4, inverse=True) """ # crop firn grid
#fname = '/Volumes/RADARALT/fpaolo/data/shelves/all_19920716_20111015_shelf_tide_grids_mts.h5' fname = '/Users/fpaolo/data/shelves/all_19920716_20111015_shelf_tide_grids_mts.h5.ice_oce' def gradient(y, dt=.25): return np.gradient(y.values, dt) with tb.open_file(fname) as f: alt0 = f.root.dh_mean_xcal[:] #alt0 = f.root.dh_mean_xcal_short_const[:] alt1 = f.root.dg_mean_xcal[:] firn = f.root.h_firn[:] t_alt = ap.num2date(f.root.time_xcal[:]) t_firn = ap.num2date(f.root.time_firn[:]) lon = f.root.lon[:] lat = f.root.lat[:] # map grid indexes to coordinates ii, jj = xrange(len(lat)), xrange(len(lon)) ij2ll = dict([((i, j), (la, lo)) for i, j, la, lo in zip(ii, jj, lat, lon)]) df_alt0 = pd.Panel(alt0, items=t_alt, major_axis=ii, minor_axis=jj).to_frame(filter_observations=False).T df_alt1 = pd.Panel(alt1, items=t_alt, major_axis=ii, minor_axis=jj).to_frame(filter_observations=False).T df_firn = pd.Panel(firn, items=t_firn, major_axis=ii, minor_axis=jj).to_frame().T
def main(): fname_in = sys.argv[1] din = GetData(fname_in, 'a') time = ap.num2date(getattr(din, T_NAME)[:]) lon = din.lon[:] lat = din.lat[:] sat = din.satname[:] nrows = len(time) nt, ny, nx = getattr(din, H_NAME).shape # i,j,k = t,y,x RR = np.empty((nt,ny,nx), 'f8') * np.nan SS = np.empty((nt,ny,nx), 'f8') * np.nan print 'processing time series:' isfirst = True # iterate over every grid cell (all times): i,j = y,x #----------------------------------------------------------------- for i in xrange(ny): for j in xrange(nx): print 'time series of grid-cell:', i, j dh = getattr(din, H_NAME)[:nrows,i,j] dg = getattr(din, G_NAME)[:nrows,i,j] dh_cor = np.zeros_like(dh) if np.alltrue(np.isnan(dh)): continue #--------------------------------------------------------- # pull and correct a chunk of the array at a time for s in np.unique(sat): k = np.where(sat == s) dh_cor[k], R, S = ap.backscatter_corr(dh[k], dg[k], diff=DIFF, robust=True) RR[k,i,j] = R SS[k,i,j] = S #--------------------------------------------------------- if PLOT_TS: dh_cor = ap.referenced(dh_cor, to='first') dh = ap.referenced(dh, to='first') dg = ap.referenced(dg, to='first') fig = plot_rs(time, RR[:,i,j], SS[:,i,j]) for s in np.unique(sat): k = np.where(sat == s) r, s = np.mean(RR[k,i,j]), np.mean(SS[k,i,j]) try: fig = plot_ts(time[k], lon[j], lat[i], dh_cor[k], dh[k], dg[k], r, s, diff=DIFF) except: print 'something wrong with ploting!' print 'dh:', dh print 'dg:', dg if fig is None: continue plt.show() # save one TS per grid cell at a time #--------------------------------------------------------- if not SAVE_TO_FILE: continue if isfirst: # open or create output file isfirst = False atom = tb.Atom.from_type('float64', dflt=np.nan) filters = tb.Filters(complib='zlib', complevel=9) try: c1 = din.file.create_carray('/', SAVE_AS_NAME, atom, (nt,ny,nx), '', filters) except: c1 = din.file.getNode('/', SAVE_AS_NAME) c2 = din.file.create_carray('/', R_NAME, atom, (nt,ny,nx), '', filters) c3 = din.file.create_carray('/', S_NAME, atom, (nt,ny,nx), '', filters) c1[:,i,j] = dh_cor if SAVE_TO_FILE: c2[:] = RR c3[:] = SS if PLOT_MAP: RR = RR[0] # change accordingly SS = SS[0] plot_map(lon, lat, np.abs(RR), BBOX, MASK_FILE, mres=1, vmin=0, vmax=1) plt.title('Correlation Coefficient, R') plt.savefig('map_r.png') plot_map(lon, lat, SS, BBOX, MASK_FILE, mres=1, vmin=-0.2, vmax=0.7) plt.title('Correlation Gradient, S') plt.savefig('map_s.png') plt.show() din.file.close() if SAVE_TO_FILE: print 'out file -->', fname_in
def main(): fname_in = sys.argv[1] din = GetData(fname_in, 'a') time = ap.num2date(getattr(din, T_NAME)[:]) lon = din.lon[:] lat = din.lat[:] nrows = len(time) nt, ny, nx = getattr(din, H_NAME).shape # i,j,k = t,y,x RR = np.empty((nt, ny, nx), 'f8') * np.nan SS = np.empty((nt, ny, nx), 'f8') * np.nan if TINT: intervals = [ap.year2date(tt) for tt in INTERVALS] if TINT: print 'using time-interval correlation' elif TVAR: print 'using time-variable correlation' else: print 'using constant correlation' print 'processing time series:' isfirst = True # iterate over every grid cell (all times): i,j = y,x #----------------------------------------------------------------- for i in xrange(ny): for j in xrange(nx): print 'time series of grid-cell:', i, j dh = getattr(din, H_NAME)[:nrows, i, j] dg = getattr(din, G_NAME)[:nrows, i, j] if np.alltrue(np.isnan(dh)): continue #--------------------------------------------------------- if TINT: # satellite-dependent R and S dh_cor, RR[:,i,j], SS[:,i,j] = \ ap.backscatter_corr3(dh, dg, time, intervals, diff=DIFF, robust=True) elif TVAR: # time-varying R and S dh_cor, RR[:,i,j], SS[:,i,j] = \ ap.backscatter_corr2(dh, dg, diff=DIFF, robust=True, npts=NPTS) else: # constant R and S dh_cor, RR[:,i,j], SS[:,i,j] = \ ap.backscatter_corr(dh, dg, diff=DIFF, robust=True) #--------------------------------------------------------- # plot figures if PLOT_TS: dh_cor = ap.referenced(dh_cor, to='first') dh = ap.referenced(dh, to='first') dg = ap.referenced(dg, to='first') k, = np.where(~np.isnan(RR[:, i, j])) r = np.mean(RR[k, i, j]) s = np.mean(SS[k, i, j]) fig = plot_rs(time, RR[:, i, j], SS[:, i, j]) fig = plot_ts(time, lon[j], lat[i], dh_cor, dh, dg, r, s, diff=DIFF) if fig is None: continue plt.show() # save one TS per grid cell at a time #--------------------------------------------------------- if not SAVE_TO_FILE: continue if isfirst: # open or create output file isfirst = False atom = tb.Atom.from_type('float64', dflt=np.nan) filters = tb.Filters(complib='zlib', complevel=9) c1 = din.file.createCArray('/', SAVE_AS_NAME, atom, (nt, ny, nx), '', filters) c2 = din.file.createCArray('/', R_NAME, atom, (nt, ny, nx), '', filters) c3 = din.file.createCArray('/', S_NAME, atom, (nt, ny, nx), '', filters) c1[:, i, j] = dh_cor if SAVE_TO_FILE: c2[:] = RR c3[:] = SS if PLOT_MAP: if TVAR: # 3D -> 2D RR = np.mean(RR[~np.isnan(RR)], axis=0) SS = np.mean(SS[~np.isnan(SS)], axis=0) plot_map(lon, lat, np.abs(RR), BBOX, MFILE, mres=1, vmin=0, vmax=1) plt.title('Correlation Coefficient, R') plt.savefig('map_r.png') plot_map(lon, lat, SS, BBOX, MFILE, mres=1, vmin=-0.2, vmax=0.7) plt.title('Correlation Gradient, S') plt.savefig('map_s.png') plt.show() din.file.close() if SAVE_TO_FILE: print 'out file -->', fname_in