def plot_one_date(self, plot_date, **kwargs): datestr = plot_date.strftime('%Y%m%dT000000Z') basename = 'field_%s' % datestr prev_date = plot_date - dt.timedelta(1) rf1 = os.path.join( self.root_forecast_dir, prev_date.strftime('%Y%m%d'), 'restart', basename) # restart after 1 day of previous forecast rf2 = os.path.join(self.root_forecast_dir, plot_date.strftime('%Y%m%d'), 'inputs', basename) # restart after assimilation nb1 = NextsimBin(rf1, add_gmsh=True, logfile=os.path.join(self.root_forecast_dir, prev_date.strftime('%Y%m%d'), 'nextsim.log'), **kwargs) nb2 = NextsimBin(rf2) plot_vars = [ 'M_conc', 'M_thick', 'M_snow_thick', 'M_ridge_ratio', 'M_tice_0', 'M_tice_1', 'M_tice_2', 'M_sst', 'M_sss', 'M_tsurf_thin', 'M_h_thin', 'M_hs_thin', 'M_conc_thin', ] innovs = dict() vtypes = dict() for v in plot_vars: v1 = nb1.get_var(v) v2 = nb2.get_var(v) k = 'innov-' + v innovs[k] = v2 - v1 vtypes[k] = 'f' nb1.add_vars(innovs, vtypes) gridded_vars = nb1.interp_vars(list(innovs)) outdir = os.path.join(self.root_forecast_dir, plot_date.strftime('%Y%m%d'), 'innovation_plots') nsl.make_dir(outdir) for v, arr in gridded_vars.items(): fig = nb1.get_gridded_figure(arr.T, land_color=[1, 1, 1], clim=[-.5, .5], cmap='balance') figname = '%s/%s-%s.png' % (outdir, v, datestr) print('Saving %s' % figname) fig.savefig(figname) plt.close(fig) return nb1.mesh_info.gmsh_mesh
def run(args): ''' make the file Parameters: ----------- args : argparse.Namespace ''' nsl.make_dir(args.outdir) outfile = os.path.join(args.outdir, args.date.strftime(NEW_FILEMASK)) # open arome file and ecmwf file for each day of forecast ec_dsets = open_ecmwf(args) dst_data = {v: [] for v in DST_VARS} dst_vecs = [] for i, ec_ds in ec_dsets.items(): ''' loop over each day of forecast: - eg 1st day of forecast is in legacy*.1.nc, 2nd day of forecast is in legacy*.2.nc ''' print('Day %i: Using file %s' % (i, ec_ds.filepath())) in_ec2_time_range = test_ec2_time_range(ec_ds, args.date + dt.timedelta(i)) dst_vecs.append(set_destination_coordinates(ec_ds, in_ec2_time_range)) # fetch, interpolate and blend all variables from ECMWF and AROME for dst_var_name in DST_VARS: # Interpolate data from ECMWF ec_var_name = DST_VARS[dst_var_name] print('Read', ec_var_name) dst_data[dst_var_name].append( get_ec2_var(ec_ds, ec_var_name, in_ec2_time_range)) # set DST_DATA (combine days 1,2,... before exporting) dst_vec = merge_days(dst_data, dst_vecs) # save the output export(outfile, ec_ds, dst_vec)
def run(args): ''' process one day's AROME file Parameters: ----------- args : argparse.Namespace parsed command line arguments ''' ar_ds, ar_ds2, ar_proj, ar_pts = open_arome(args.date) dst_vec, dst_shape = set_destination_coordinates(ar_proj, ar_ds, ar_ds2) # save the output outfile = args.date.strftime(AR_FILEMASK_NEW) outdir = os.path.split(outfile)[0] nsl.make_dir(outdir) print('Exporting %s' % outfile) with Dataset(outfile, 'w') as dst_ds: create_dimensions(ar_ds, dst_ds, dst_vec) create_variables(ar_ds, ar_ds2, dst_ds, dst_vec, dst_shape) for ds in [ar_ds, ar_ds2]: ds.close()
def run(args): ''' make the file Parameters: ----------- args : argparse.Namespace ''' os.makedirs(os.path.dirname(NEW_FILEMASK), exist_ok=True) outfile = args.date.strftime(NEW_FILEMASK) if os.path.exists(outfile): print(f'{outfile} exists - skipping') return # open arome file and ecmwf file ar_ds, ar_proj, ar_pts = open_arome(args.date) ec_ds, ec_pts = open_ecmwf(args.date) in_ec2_time_range = test_ec2_time_range(ec_ds, args.date) if args.plot: # plot the grid if desired figname = outfile.replace('.nc', '.png') plot_destination_grid(figname, ar_proj, ar_pts) # set target coords (dst_vec, dst_ecp, dst_arp, dst_shape) = set_destination_coordinates(ar_proj, ar_ds) # distance to AROME border ar_shp = [len(v) for v in ar_pts] ar_dist = create_distance(ar_shp) dst_ardist_grd = interpolate(ar_dist, ar_pts, dst_arp, dst_shape) #if args.save_grid: # # save the grid # print('Exporting %s' %GRID_FILE) # with Dataset(GRID_FILE, 'w') as dst_ds: # add_grid_to_file(dst_ds, ar_ds, dst_ecp, dst_vec, dst_shape) sz = list(dst_ardist_grd.shape) num_ens_mems = len(dst_vec['ensemble_member']) sz.insert(1, num_ens_mems) for dst_var_name in DST_VARS: DST_DATA[dst_var_name] = np.zeros(sz) # fetch, interpolate and blend all variables from ECMWF and AROME load_transformed_ECMWF(ec_ds, dst_vec, in_ec2_time_range, ec_pts, dst_ecp, dst_shape) for i_ens in range(num_ens_mems): ar_data = load_transformed_AROME(ar_ds, i_ens, dst_vec, ar_proj, ar_shp, ar_pts, dst_arp, dst_shape) # Compute destination product from ECMWF data if 0: # test plots kw = dict(vmin=0, vmax=3e6) for n in range(8): nsl.make_dir('figs0') figname = 'figs0/ec_%s_%i.png' % (dst_var_name, n) print('Saving %s' % figname) plt.imshow(dst_ecd_grd[n, :, :], origin='upper', **kw) plt.colorbar() plt.title(dst_var_name) plt.savefig(figname) plt.close() figname = 'figs0/ar_%s_%i.png' % (dst_var_name, n) print('Saving %s' % figname) plt.imshow(ar_data[dst_var_name][n, :, :], origin='upper', **kw) plt.colorbar() plt.title(dst_var_name) plt.savefig(figname) plt.close() # blend and add to destination data print('- Blending AROME and ECMWF') for dst_var_name in DST_VARS: DST_DATA[dst_var_name][:, i_ens, :, :] = blend( ar_data[dst_var_name], EC_DATA[dst_var_name], dst_ardist_grd) assert (np.all(np.isfinite(DST_DATA[dst_var_name][:, i_ens, :, :]))) #export the files export4d(outfile, ar_ds, dst_ecp, dst_vec, dst_shape)
'2T' : lambda x : x, '2D' : lambda x : x, 'MSL' : lambda x : x, 'SSRD' : accumulate_ecmwf, 'STRD' : accumulate_ecmwf, #'STRD' : lambda x : x, #to test 'TP' : accumulate_ecmwf, } #dst_var = 'air_temperature_2m' #DST_VARS = {dst_var: DST_VARS[dst_var]} if __name__ == '__main__': args = parse_args(sys.argv[1:]) outdir = os.path.split(NEW_FILEMASK)[0] nsl.make_dir(outdir) outfile = os.path.join(outdir, NEW_FILEMASK % args.date) # open arome file and ecmwf file ar_ds, ar_proj, ar_pts = open_arome(args.date) ec_ds, ec_pts = open_ecmwf(args.date) if args.plot: # plot the grid if desired figname = outfile.replace('.nc', '.png') plot_destination_grid(figname, ar_proj, ar_pts) # set target coords (dst_vec, dst_ecp, dst_arp, dst_shape) = set_destination_coordinates(ar_proj, ar_ds)