def import_dem(filein): # grid point lists # open netCDF file # nc_file = "/home/sberton2/Downloads/sresa1b_ncar_ccsm3-example.nc" nc_file = filein dem_xarr = xr.open_dataset(nc_file) lats = np.deg2rad(dem_xarr.lat.values) + np.pi / 2. lons = np.deg2rad(dem_xarr.lon.values) # -np.pi data = dem_xarr.z.values print("test") # Exclude last column because only 0<=lat<pi # and 0<=lon<pi are accepted (checked that lon=0 has same values) # print(data[:,0]==data[:,-1]) # kx=ky=1 required not to mess up results!!!!!!!!!! Higher interp orders mess up... if True: interp_spline = RectBivariateSpline(lats[:-1], lons[:-1], data[:-1, :-1], kx=1, ky=1) pickleIO.save(interp_spline, tmpdir + "interp_dem.pkl") else: interp_spline = pickleIO.load(tmpdir + "interp_dem.pkl") return interp_spline
def run(seed,sub_len=100): vecopts = {} xov_ = xov(vecopts) xov_ = xov_.load(tmpdir+"dKX_clean.pkl") print("Loaded...") # dR absolute value taken xov_.xovers['dR_orig'] = xov_.xovers.dR xov_.xovers['dR'] = xov_.xovers.dR.abs() start = time.time() # get list of all orbA-orbB giving xov at low lats lowlat_xov = xov_.xovers.loc[xov_.xovers.LAT < 50] xov_occ = list(zip(lowlat_xov.orbA.values,lowlat_xov.orbB.values)) xov_occ_str = [a+'-'+b for a, b in xov_occ] # build up random combinations of N orbA-orbB orbs = pd.DataFrame([xov_.xovers['orbA'].value_counts(), xov_.xovers['orbB'].value_counts()]).T.fillna(0).sum(axis=1) print(orbs.index) orbs = orbs.index.values nxov_old = 0 np.random.seed(seed) for i in range(1000000): orb_sel = np.random.choice(orbs, sub_len) s = [(a,b) for a,b in list( itert.product(orb_sel, orb_sel)) if a != b] sampl_str = [a+'-'+b for a, b in s] # print(np.array(sampl_str)) # intersect = intersect1d_searchsorted(sampl_str,xov_occ_str,assume_unique=True) nxov = len(intersection(sampl_str, xov_occ_str)) if nxov >= nxov_old: # good number based on full database # nxov_old: s_max = s nxov_old = nxov print("New max = ", nxov_old, " @ sample ",i) save([np.array(s_max),nxov_old],tmpdir+'bestROItracks'+str(sub_len)+'_'+str(nxov_old)+'-'+str(i)+'.pkl') # print(load(tmpdir+'bestROItracks.pkl')) # print(nxov_old) # print(np.array(s_max)) end = time.time() print("Got it in ", str(end-start), " seconds!")
def select_from_stats(sol ='KX1', subexp ='0res_1amp', outfil='bestROItracks.pkl'): from src.accumxov.Amat import Amat from examples.MLA.options import outdir, vecopts # import numpy as np subfolder = '' # sol = 'KX1_0' # r4_1' # subexp = '0res_1amp' tmp = Amat(vecopts) tmp = tmp.load(outdir + 'sim/' + subfolder + sol + '/' + subexp + '/Abmat_sim_' + sol.split('_')[0] + '_' + str( int(sol.split('_')[-1]) + 1) + '_' + subexp + '.pkl') xov_df = tmp.xov.xovers # remove very large dR (>1km) xov_df = xov_df.loc[xov_df['dR'].abs() < 1.e3] print(xov_df.columns) # print(tmp.weights) # exit() hilat_xov = xov_df.loc[xov_df.LAT > 60] print(hilat_xov[['dR', 'weights', 'huber']].abs().max()) print(hilat_xov[['dR', 'weights', 'huber']].abs().min()) print(hilat_xov[['dR', 'weights', 'huber']].abs().mean()) print(hilat_xov[['dR', 'weights', 'huber']].abs().median()) to_keep = 1. - 8.e5 / len(hilat_xov) to_keep_hilat = hilat_xov.loc[hilat_xov['weights'] > hilat_xov['weights'].quantile(to_keep)].xOvID.values lolat_xov = xov_df.loc[xov_df.LAT < 60] to_keep_lolat = lolat_xov.loc[lolat_xov['weights'] > lolat_xov['weights'].quantile(0.1)].xOvID.values # select very good xovers at LAT>60N OR decent xovers at low latitudes selected = xov_df.loc[(xov_df.xOvID.isin(to_keep_hilat)) | (xov_df.xOvID.isin(to_keep_lolat))] print(len(selected)) print(selected[['dR', 'weights', 'huber', 'dist_min_mean']].abs().max()) print(selected[['dR', 'weights', 'huber', 'dist_min_mean']].abs().min()) print(selected[['dR', 'weights', 'huber', 'dist_min_mean']].abs().median()) print(selected[['dR', 'weights', 'huber', 'dist_min_mean']].abs().mean()) # set of orbits giving orbs = list(set(np.hstack([selected.orbA.values, selected.orbB.values]))) print(orbs) print(len(orbs), "orbits giving the 'best'", len(selected), "xovers out of", len(xov_df)) save([np.array(orbs)], tmpdir + outfil) return orbs
def plot_topo(df): fig = plt.figure(figsize=(8, 6), edgecolor='w') # m = Basemap(projection='moll', resolution=None, # lat_0=0, lon_0=0) m = Basemap(projection='npstere', boundinglat=10, lon_0=0, resolution='l') x, y = m(df.LON.values, df.LAT.values) map = m.scatter(x, y, c=df['R'].values, s=0.1, cmap='Reds') # afmhot') # , marker=',', s=3**piv.count(), plt.colorbar(map) draw_map(m) fig.savefig(tmpdir + 'mla_altres_' + sol + '_' + subexp + '.png') plt.clf() plt.close() deg_step = 1 to_bin = lambda x: np.floor(x / deg_step) * deg_step df["latbin"] = df.LAT.map(to_bin) df["lonbin"] = df.LON.map(to_bin) groups = df.groupby(["latbin", "lonbin"]) tmpdf = groups.R.apply(lambda x: np.median(x)).reset_index() # piv = pd.pivot_table(tmpdf, values="R", index=["latbin"], columns=["lonbin"], fill_value=0) # lats = np.deg2rad(piv.index.values) + np.pi / 2. # # TODO not sure why sometimes it has to be rescaled # lons = np.deg2rad(piv.columns.values) + np.pi # data = piv.values # print("Done groupby and medians") # Exclude last column because only 0<=lat<pi # and 0<=lon<pi are accepted (checked that lon=0 has same values) # print(data[:,0]==data[:,-1]) # kx=ky=1 required not to mess up results!!!!!!!!!! Higher interp orders mess up... # interp_spline = RectBivariateSpline(lats[:-1], # lons[:-1], # data[:-1, :-1], kx=2, ky=2) # interp_spline = RectBivariateSpline(tmpdf.latbin.values, # tmpdf.lonbin.values, # tmpdf.R.values, kx=2, ky=2) start = time.time() import scipy.interpolate as interp x, y = project_stereographic(tmpdf.lonbin.values, tmpdf.latbin.values, 0, 90, R=vecopts['PLANETRADIUS']) def euclidean_norm_numpy(x1, x2): return np.linalg.norm(x1 - x2, axis=0) zfun_smooth_rbf = interp.Rbf(x, y, tmpdf.R.values, function='gaussian', norm=euclidean_norm_numpy, smooth=0) end = time.time() print('----- Runtime interp = ' + str(end - start) + ' sec -----' + str((end - start) / 60.) + ' min -----') # zfun_smooth_rbf = interp.Rbf(x, y, tmpdf.R.values, function='cubic', # smooth=0) # default smooth=0 for interpolation pickleIO.save(zfun_smooth_rbf, tmpdir + "interp_R_" + sol + ".pkl") # new_lats = np.deg2rad(np.arange(0, 180, 1)) # new_lons = np.deg2rad(np.arange(0, 360, 1)) start = time.time() xx, yy = np.mgrid[-2000:2000:100j, -2000:2000:100j] import dask.array as da n1 = xx.shape[1] ix = da.from_array(xx, chunks=(1, n1)) iy = da.from_array(yy, chunks=(1, n1)) iz = da.map_blocks(zfun_smooth_rbf, ix, iy) z_dense_smooth_rbf = iz.compute() # z_dense_smooth_rbf = zfun_smooth_rbf(new_lats, # new_lons) # not really a function, but a callable class instance fig, ax1 = plt.subplots(nrows=1) im = ax1.imshow(z_dense_smooth_rbf, origin='lower', cmap="RdBu") # vmin=1,vmax=20,cmap="RdBu") fig.colorbar(im, ax=ax1, orientation='horizontal') fig.savefig(tmpdir + 'test_interp_' + sol + '.png') end = time.time() print('----- Runtime eval = ' + str(end - start) + ' sec -----' + str((end - start) / 60.) + ' min -----') # exit() # # z_dense_smooth_griddata = interp.griddata(np.array([tmpdf.latbin.values.ravel()+90.,tmpdf.lonbin.values.ravel()+180.]).T, # tmpdf.R.values.ravel(), # (new_lats, new_lons), method='cubic') # fig, ax1 = plt.subplots(nrows=1) # im = ax1.imshow(z_dense_smooth_griddata, origin='lower', cmap="RdBu") # vmin=1,vmax=20,cmap="RdBu") # fig.colorbar(im, ax=ax1, orientation='horizontal') # fig.savefig(tmpdir + 'test_interp_' + sol + '.png') # exit() # pickleIO.save(interp_spline, tmpdir+"interp_R_"+sol+".pkl") # print("Done interp") # # new_lats = np.deg2rad(np.arange(0, 180, 1)) # new_lons = np.deg2rad(np.arange(0, 360, 1)) # new_lats, new_lons = np.meshgrid(new_lats, new_lons) # ev = interp_spline.ev(new_lats.ravel(), new_lons.ravel()).reshape((360, 180)).T # fig, ax1 = plt.subplots(nrows=1) # im = ax1.imshow(ev, origin='lower', cmap="RdBu") # vmin=1,vmax=20,cmap="RdBu") # fig.colorbar(im, ax=ax1, orientation='horizontal') # fig.savefig(tmpdir + 'test_interp_' + sol + '.png') return interp_spline
def interpolate(self): # Read required trajectories from spice and interpolate startSpInterp = time.time() self.MGRx = interp_obj('MGRx') self.MGRv = interp_obj('MGRv') self.MGRa = interp_obj('MGRa') self.MERx = interp_obj('MERx') self.MERv = interp_obj('MERv') self.SUNx = interp_obj('SUNx') tstep = 1 # Define call times for the SPICE try: t_spc = self.ladata_df['ET_TX'].values # t_spc = np.array( # [x for x in np.arange(self.ladata_df['ET_TX'].min(), self.ladata_df['ET_TX'].max(), tstep)]) # add 1000s to each side of track to avoid boundary effects t_spc = np.hstack([ t_spc[0] + np.arange(-1000, -1, 1), t_spc, t_spc[-1] + np.arange(1, 1000, 1) ]) except: print("*** ground_track.py: Issue interpolating ..." + self.name) print(self.ladata_df) print(self.ladata_df['ET_TX'].min()) print(self.ladata_df['ET_TX'].max()) exit(2) # print("Start spkezr MGR") # trajectory xv_spc = np.array([ spice.spkezr(self.vecopts['SCNAME'], t, self.vecopts['INERTIALFRAME'], 'NONE', self.vecopts['INERTIALCENTER']) for t in t_spc ])[:, 0] xv_spc = np.reshape(np.concatenate(xv_spc), (-1, 6)) # print("Start pxform MGR") # attitude pxform_array = np.frompyfunc(spice.pxform, 3, 1) cmat = pxform_array('MSGR_SPACECRAFT', self.vecopts['INERTIALFRAME'], t_spc) # m2q_array = np.frompyfunc(spice.m2q, 1, 1) # quat = m2q_array(cmat) # quat = np.reshape(np.concatenate(quat), (-1, 4)) # print("Start MGR interpolation") self.MGRx.interp([xv_spc[:, i] for i in range(0, 3)], t_spc) self.MGRv.interp([xv_spc[:, i] for i in range(3, 6)], t_spc) self.MGRa.interpCmat(cmat, t_spc) # print("Start spkezr MER") xv_pla = np.array([ spice.spkezr(self.vecopts['PLANETNAME'], t, self.vecopts['INERTIALFRAME'], 'NONE', self.vecopts['INERTIALCENTER']) for t in t_spc ])[:, 0] xv_pla = np.reshape(np.concatenate(xv_pla), (-1, 6)) # print("Start MER interpolation") self.MERx.interp([xv_pla[:, i] for i in range(0, 3)], t_spc) self.MERv.interp([xv_pla[:, i] for i in range(3, 6)], t_spc) # print("Start spkezr SUN") xv_sun = np.array([ spice.spkezr('SUN', t, self.vecopts['INERTIALFRAME'], 'NONE', self.vecopts['INERTIALCENTER']) for t in t_spc ])[:, 0] xv_sun = np.reshape(np.concatenate(xv_sun), (-1, 6)) # print("Start SUN interpolation") self.SUNx.interp([xv_sun[:, i] for i in range(0, 3)], t_spc) # save to orbit-wise file self.SpObj = { 'MGRx': self.MGRx, 'MGRv': self.MGRv, 'MGRa': self.MGRa, 'MERx': self.MERx, 'MERv': self.MERv, 'SUNx': self.SUNx } if not os.path.exists(auxdir + spauxdir): os.mkdir(auxdir + spauxdir) pickleIO.save(self.SpObj, auxdir + spauxdir + 'spaux_' + self.name + '.pkl') endSpInterp = time.time() if (debug): print('----- Runtime SpInterp = ' + str(endSpInterp - startSpInterp) + ' sec -----' + str((endSpInterp - startSpInterp) / 60.) + ' min -----')