def apply_mask(resolution, surface, mask_filename=None, path=None): if path is None: path = './fortran/data/src' if resolution == 0.25: mask = read_surface('mask_glbl_qrtd.dat', path) surface = surface + mask return surface elif resolution == 0.5: mask = read_surface('mask_glbl_hlfd.dat', path) surface = surface + mask return surface else: print("Mask with correct resolution not found.") return surface
def easy_plot(dat_file, dat_path, product, bds, figs_dir, figname, log=True): surface = read_surface('MPI-ESM1-2-HR_cs.dat', cs) fig = plot(surface, bds=bds, product=product) fig.savefig(figs_dir+figname+'_cs', dpi=300) plt.close() surface = read_surface('MPI-ESM1-2-HR_cs.dat', cs) fig = plot(surface, bds=bds, product=product, extent='gs') fig.savefig(figs_dir+figname+'_gs', dpi=300) plt.close() surface = read_surface('MPI-ESM1-2-HR_cs.dat', cs) fig = plot(surface, bds=bds, product=product, extent='ag') fig.savefig(figs_dir+figname+'_ag', dpi=300) plt.close() if log: surface = read_surface('MPI-ESM1-2-HR_cs.dat', cs) fig = plot(surface, bds=bds, product=product, log=True) fig.savefig(figs_dir+figname+'_cs_log', dpi=300) plt.close() surface = read_surface('MPI-ESM1-2-HR_cs.dat', cs) fig = plot(surface, bds=bds, product=product, extent='gs', log=True) fig.savefig(figs_dir+figname+'_gs_log', dpi=300) plt.close() surface = read_surface('MPI-ESM1-2-HR_cs.dat', cs) fig = plot(surface, bds=bds, product=product, extent='ag', log=True) fig.savefig(figs_dir+figname+'_ag_log', dpi=300) plt.close() return surface
def main(): mdts = '../a_mdt_data/computations/mdts/' mss = '../a_mdt_data/computations/mss/' geoids = '../a_mdt_data/computations/geoids/' cs = '../a_mdt_data/computations/currents/' masks = '../a_mdt_data/computations/masks/' figs_dir = './figs/' mask = read_surface('mask_rr0004.dat', '../a_mdt_data/computations/masks/') prior_dir = '../a_mdt_data/computations/currents/prior_geodetic_gauss' regions = '../a_mdt_data/cdae_testing_data_128/cs/geodetic/' geoid = read_surface(f'GO_CONS_GCF_2_TIM_R6_do0280_rr0004.dat', geoids) print(np.mean(geoid), np.max(geoid), np.min(geoid), np.std(geoid), np.shape(geoid)) geoid = (geoid - geoid.mean())/geoid.std() print(np.mean(geoid), np.max(geoid), np.min(geoid), np.std(geoid), np.shape(geoid)) nemo = read_surface(f'orca0083_12th_cs_band20.dat', cs) # plot(nemo, product='cs', coastlines=True, bds=1.4)#, extent='ku') cmip = read_surface(f'MPI-ESM1-2-HR_cs.dat', cs) # plot(cls, product='cs', coastlines=True, bds=2)#, extent='gs') dtu18_eigen = read_surface(f'dtu18_eigen-6c4_do0280_rr0004_cs_band20.dat', cs) # plot(dtu18_eigen, product='cs', coastlines=True, bds=2)#, extent='gs') dtu18_gtim6 = read_surface(f'dtu18_GTIM_R6_do0280_rr0004_cs.dat', cs) # plot(dtu18_gtim6, product='cs', coastlines=True, bds=2)#, extent='gs') dtu18_gtim5 = read_surface(f'dtu18_GTIM_R5_do0280_rr0004_cs.dat', cs) # plot(dtu18_gtim5, product='cs', coastlines=True, bds=2)#, extent='gs') dtu18_gtim5[dtu18_gtim5<1.4] = 0 # plot(dtu18_gtim5, product='cs', coastlines=True, bds=2) test = dtu18_gtim6 - dtu18_eigen test[test<1.4] = 0
filenames = glob.glob((os.path.join(directory, '*.dat'))) # filenames2 = glob.glob((os.path.join(directory2, 'cls18_cs_band10.dat'))) # filenames = glob.glob((os.path.join(directory, '*.dat'))) # filenames = [os.path.split(fname)[-1] for fname in filenames] # filenames = [fname[:len(fname)-4] for fname in filenames] # cls18 = read_surface(filenames2[0]) # cls18[cls18==0] = np.nan # cls18=apply_gaussian(cls18, sigma=6) # <----- take out # cls18[np.isnan(cls18)] = 0 # cls18 = bound_arr(cls18, -20, 20) # cls18 = resize(cls18, (720, 1440), order=2) for fname in filenames: arr = read_surface(fname) arr[arr == 0] = np.nan # arr=apply_gaussian(arr, sigma=6) # <----- take out arr[np.isnan(arr)] = 0 arr = bound_arr(arr, -20, 20) arr = resize(arr, (720, 1440), order=2) fname = os.path.split(fname)[-1] fname = fname[:len(fname) - 4] print('running') for x, y in zip(x_coords, y_coords): # print(x,y) region = arr[y:y + size, x:x + size] if mdt: nans = np.isnan(arr) arr[nans] = 0 # if (np.count_nonzero(region==0)/(region.size) < 0.25):
def main(): mdts = '../a_mdt_data/computations/mdts/' gauss_filtered = '../a_mdt_data/computations/mdts/gauss_filtered/' cs_path = '../a_mdt_data/computations/currents/' gauss_cs_path = '../a_mdt_data/computations/currents/gauss_filtered/' masks = '../a_mdt_data/computations/masks/' mask = read_surface('mask_rr0008.dat', masks) mask_4 = read_surface('mask_rr0004.dat', masks) fig_dir = 'figs/mdt_plots/dtu18_gtim5_filtered/' dtu18_gtim5_4 = read_surface('sh_mdt_DTU18_GTIM5_L280.dat', mdts) dtu18_gtim5 = read_surface('dtu18_gtim5_do0280_rr0008.dat', mdts) # dtu18_gtim5 = resize(dtu18_gtim5, (1440, 2880), order=3) dtu18_gtim5[np.isnan(dtu18_gtim5)] = 0 # dtu18_gtim5 = np.clip(dtu18_gtim5, -1.4, 1.4) # dtu18_gtim5 = norm(dtu18_gtim5) # filt8 = apply_filter(dtu18_gtim5_4, 'boxcar', 8) # filt12 = apply_filter(dtu18_gtim5_4, 'boxcar', 12) # filt16 = apply_filter(dtu18_gtim5_4, 'boxcar', 16) # filt20 = apply_filter(dtu18_gtim5_4, 'boxcar', 20) # multi_plot([filt8, filt12, filt16, filt20], extent='na', coastlines=True) cls_mdt = read_surface('cls18_mdt.dat', mdts) cls_mdt[np.isnan(cls_mdt)] = 0 cls_downscaled = rescale(cls_mdt, 0.5) # cls_downscaled = np.clip(cls_downscaled, -1.4, 1.4) # cls_downscaled = norm(cls_downscaled) nemo_mdt = read_surface('orca0083_mdt.dat', mdts) nemo_mdt[np.isnan(nemo_mdt)] = 0 nemo_downscaled = resize(nemo_mdt, (720, 1440)) # nemo_downscaled = np.clip(nemo_downscaled, -1.4, 1.4) # nemo_downscaled = norm(nemo_downscaled) # # Gaussian filter filter_widths = [] gauss_mdts = [] gauss_mdt_plots = [] for i in range(32): k = (i + 1) / 4 mdt = gaussian_filter(dtu18_gtim5, sigma=k) mdt[np.isnan(mdt)] = 0 gauss_mdts.append(mdt) r = int((int((4 * k) + 0.5)) / 4 * 111) filter_widths.append(r) if r in (138, 249, 360, 471): # resized_mdt = resize(mdt, (1440, 2880), order=3) gauss_mdt_plots.append(mdt) # multi_plot(gauss_mdt_plots, extent='na', coastlines=True) res1 = dtu18_gtim5 - gauss_mdt_plots[0] res2 = dtu18_gtim5 - gauss_mdt_plots[1] res3 = dtu18_gtim5 - gauss_mdt_plots[2] res4 = dtu18_gtim5 - gauss_mdt_plots[3] multi_plot([res1, res2, res3, res4], product='resid', extent='na', coastlines=True) plt.show() # Plot Gaussian filter widths: for r = 5, 9, 13, 17 # gauss_plots = [] # for i in range(4): # k = ((i+1)*4) # image = resize(gauss_mdts[k], (1440, 2880), order=3) + mask # gauss_plots.append(image) # plot(image, product='mdt', extent='na') # plt.show() # plt.close() # # Load gaussian filtered currents # gauss_cs = [] # gauss_cs_plots = [] # for i in range(32): # k = (i+1)/4 # r = int((int((4*k)+0.5))/4 * 111) # cs = read_surface(f'dtu18_gtim5_gauss_{r}km_cs.dat', gauss_cs_path) # cs[np.isnan(cs)] = 0 # gauss_cs.append(cs) # if r in (138, 249, 360, 471): # resized_cs = resize(cs, (1440, 2880), order=3) + mask # gauss_cs_plots.append(resized_cs) # multi_plot(gauss_cs_plots, product='cs', extent='na', coastlines=True) # multi_plot(gauss_cs_plots, product='cs', extent='na', coastlines=True) # pmf_0 = read_surface('dtu18_gtim5_land0_pmf_300i_16k_02g.dat', mdts) # pmf_1 = read_surface('dtu18_gtim5_land0_pmf_500i_16k_02g.dat', mdts) # pmf_2 = read_surface('dtu18_gtim5_land0_pmf_700i_16k_02g.dat', mdts) # pmf_3 = read_surface('dtu18_gtim5_land0_pmf_900i_16k_02g.dat', mdts) # pmf_0[np.isnan(pmf_0)] = 0 # pmf_1[np.isnan(pmf_1)] = 0 # pmf_2[np.isnan(pmf_2)] = 0 # pmf_3[np.isnan(pmf_3)] = 0 # pmf_0 = resize(pmf_0, (1440, 2880), order=3) # pmf_1 = resize(pmf_1, (1440, 2880), order=3) # pmf_2 = resize(pmf_2, (1440, 2880), order=3) # pmf_3 = resize(pmf_2, (1440, 2880), order=3) # multi_plot([pmf_0, pmf_1, pmf_2, pmf_3], product='mdt', extent='na', coastlines=True) # plt.show() # PMF Filter pmf_mdts, iterations = pmf(dtu18_gtim5, iterations=900) extent = 'na' # write_surface('dtu18_gtim5_land0_pmf_30i_16k_05g.dat', pmf_mdts[29], mdts) # write_surface('dtu18_gtim5_land0_pmf_50i_16k_05g.dat', pmf_mdts[49], mdts)# overwrite=True) # write_surface('dtu18_gtim5_land0_pmf_70i_16k_05g.dat', pmf_mdts[69], mdts)# overwrite=True) # write_surface('dtu18_gtim5_land0_pmf_90i_16k_05g.dat', pmf_mdts[89], mdts)# overwrite=True) res1 = dtu18_gtim5 - pmf_mdts[299] res2 = dtu18_gtim5 - pmf_mdts[499] res3 = dtu18_gtim5 - pmf_mdts[699] res4 = dtu18_gtim5 - pmf_mdts[899] multi_plot([res1, res2, res3, res4], product='resid', extent='na', coastlines=True) plt.show() # get pmf from interior na pmf_regions = [ extract_region(pmf_mdt, (-60, -40), (10, 30)) for pmf_mdt in pmf_mdts ] # rmse between pmf and gaussian[25] same extent g_region = extract_region(gauss_mdts[25], (-60, -40), (10, 30)) rmses = [] for pmf_region in pmf_regions: rmses.append(np.sqrt(np.nanmean(((g_region - pmf_region)**2)))) plt.plot(iterations, rmses) plt.grid(b=True, which='major', color='#888888', linestyle='-') plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) # plt.xlim([100, 600]) plt.minorticks_on() # plt.ylim([-0.05, 0.5]) # plt.title('') plt.xlabel('Iterations') plt.ylabel('RMSE (m)') # plt.yscale("log") plt.show() # Original MDT plot(resize(dtu18_gtim5, (1440, 2880), order=3) + mask, product='mdt', extent=extent, coastlines=True) plt.show() # # Gaussian mdt filtered 200km radius # image = resize(gauss_mdts[6], (1440, 2880), order=3) + mask # plot(image, product='mdt', extent=extent) # plt.show() # Gaussian: Residual from original mdt gauss_residual = dtu18_gtim5 - gauss_mdts[12] image = resize(gauss_residual, (1440, 2880), order=3) + mask plot(image, product='mdt', extent=extent, low_bd=-0.15, up_bd=0.15, coastlines=True) plt.show() # PMF filtered mdt 350 iterations image = resize(pmf_mdts[349], (1440, 2880), order=3) + mask plot(image, product='mdt', extent=extent, coastlines=True) plt.show() # PMF: Residual from original mdt pmf_residual = dtu18_gtim5 - pmf_mdts[349] image = resize(pmf_residual, (1440, 2880), order=3) + mask plot(image, product='mdt', extent=extent, low_bd=-0.15, up_bd=0.15, coastlines=True) plt.show() # PMF - gaussian MDT residual pmf_gauss = pmf_mdts[349] - gauss_mdts[12] image = resize(pmf_gauss, (1440, 2880), order=3) + mask plot(image, product='mdt', extent=extent, low_bd=-0.1, up_bd=0.1, coastlines=True) plt.show() lon_range, lat_range = EXTENTS['na'] gauss_mdts = [ extract_region(mdt, lon_range, lat_range) for mdt in gauss_mdts ] cls_downscaled = extract_region(cls_downscaled, lon_range, lat_range) nemo_downscaled = extract_region(nemo_downscaled, lon_range, lat_range) # RMSE rmse_cls = [] ss_cls = [] for mdt in gauss_mdts: # print(mdt.shape, cls_downscaled.shape) rmse = np.sqrt(np.nanmean(((mdt - cls_downscaled)**2))) rmse_cls.append(rmse) ss = ssim(mdt, cls_downscaled) ss_cls.append(ss) rmse_cls = np.array(rmse_cls) rmse_cls = norm(rmse_cls) #+ 0.01 ss_cls = np.array(ss_cls) rmse_nemo = [] ss_nemo = [] for mdt in gauss_mdts: rmse = np.sqrt(np.nanmean(((mdt - nemo_downscaled)**2))) rmse_nemo.append(rmse) ss = ssim(mdt, nemo_downscaled) ss_nemo.append(ss) rmse_nemo = np.array(rmse_nemo) rmse_nemo = norm(rmse_nemo) #+ 0.01 ss_nemo = np.array(ss_nemo) rmse_summed = rmse_cls + rmse_nemo rmse_summed = norm(rmse_summed) + 0.01 fig, axs = plt.subplots(1, 2) axs[0].plot(filter_widths, rmse_cls) axs[0].plot(filter_widths, rmse_nemo) # axs[0].plot(filter_widths, rmse_summed) axs[0].grid(b=True, which='major', color='#888888', linestyle='-') # Show the minor grid lines with very faint and almost transparent grey lines axs[0].grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) # axs[0].set_xlim([0, 900]) axs[0].minorticks_on() # axs[0].set_ylim([-0.05, 1]) # axs[0].title('') # axs[0].set_xlabel('Gaussian kernel half-width radius (km)') # axs[0].set_ylabel('RMSE') # axs[0].yscale("log") axs[1].plot(filter_widths, ss_cls) axs[1].plot(filter_widths, ss_nemo) # axs[1].plot(filter_widths, ss_summed) axs[1].grid(b=True, which='major', color='#888888', linestyle='-') # Show the minor grid lines with very faint and almost transparent grey lines axs[1].grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) # axs[1].set_xlim([0, 900]) axs[1].minorticks_on() # axs[1].ylim([-0.05, 1]) # axs[1].title('') axs[1].set_xlabel('Gaussian kernel half-width radius (km)') # axs[1].set_ylabel('Structural Similarity Index Measurement') # axs[1].yscale("log") plt.show() plt.close() plt.plot(filter_widths, rmse_cls) plt.plot(filter_widths, rmse_nemo) # plt.plot(filter_widths, rmse_summed) plt.grid(b=True, which='major', color='#888888', linestyle='-') # Show the minor grid lines with very faint and almost transparent grey lines plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) # plt.xlim([0, 900]) plt.minorticks_on() # plt.ylim([-0.05, 0.6]) # plt.title('') plt.xlabel('Gaussian filter half-width radius (km)') plt.ylabel('RMSE') # plt.yscale("log") plt.show() plt.plot(filter_widths, ss_cls) plt.plot(filter_widths, ss_nemo) # plt.plot(filter_widths, ss_summed) plt.grid(b=True, which='major', color='#888888', linestyle='-') # Show the minor grid lines with very faint and almost transparent grey lines plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) # plt.xlim([100, 600]) plt.minorticks_on() plt.ylim([-0.05, 0.5]) # plt.title('') plt.xlabel('Gaussian kernel half-width radius (km)') plt.ylabel('Structural Similarity Index Measurement') # plt.yscale("log") plt.show() rmse_pmf = [] for mdt in pmf_mdts: rmse = np.sqrt(np.nanmean(((mdt - cls_mdt)**2))) rmse_pmf.append(rmse) rmse_pmf = np.array(rmse_pmf) rmse_pmf = norm(rmse_pmf) plt.plot(iterations, rmse_pmf) plt.show()
region = extract_region(arr, (tile_pt[0], tile_pt[0] + tile_size), (tile_pt[1], tile_pt[1] + tile_size)) if valid_region(region): region[np.isnan(region)] = 0 regions.append(region) # plt.imshow(region) # plt.show() return regions, tile_pts cmip6_file = 'cmip6_historical_mdts_yr5_meta.txt' cmip6_path = '../a_mdt_data/datasets/cmip6/' mdt_path = "../a_mdt_data/computations/mdts/" cs_path = "../a_mdt_data/computations/currents/" masks = '../a_mdt_data/computations/masks/' mask = read_surface('mask_rr0004.dat', masks) training_fnames = [ 'dtu18_GO_CONS_GCF_2_DIR_R5_do0280_rr0004.dat', #300- 'dtu18_GO_CONS_GCF_2_TIM_R6_do0280_rr0004.dat', #300- 'dtu18_GO_CONS_GCF_2_SPW_R5_do0280_rr0004.dat', #330- 'dtu18_GO_CONS_GCF_2_SPW_R4_do0280_rr0004.dat', #280- 'dtu18_goco06s_do0280_rr0004.dat', #300- # gdir6 moves to testing 'dtu18_GO_CONS_GCF_2_TIM_R5_do0280_rr0004.dat', #280- 'dtu18_GO_CONS_GCF_2_SPW_R2_do0240_rr0004.dat', #240- 'dtu18_GTIM5_R6e_do0280_rr0004.dat', #300 - not available on home pc # 'dtu18_eigen-6c4_do0280_rr0004.dat', #2190- # 'dtu18_egm2008_do0280_rr0004.dat', #2190- # 'dtu18_geco_do0280_rr0004.dat', #2190- # 'dtu18_IGGT_R1_do0240_rr0004.dat', #240