import numpy as np from matplotlib import cm from netCDF4 import Dataset meshpath='/gfs1/work/hbkqtang/input/CORE2_final/' mesh=pf.load_mesh(meshpath,usepickle=False) m = Basemap(projection='robin',lon_0=0, resolution='c') x, y = m(mesh.x2, mesh.y2) # Read the observation data fl_obs=Dataset('/gfs1/work/hbkqtang/temporary/obs_SST.nc') # Read the model forecast from the free run fl_f=Dataset('/gfs1/work/hbkqtang/output/cpl_work_2014_3/thetao_fesom_20140101.nc') for i in range(730,1096): level_data_obs, elem_no_nan = pf.get_data(fl_obs.variables['sst'][i-730,:],mesh,000) level_data_f, elem_no_nan_dummy = pf.get_data(fl_f.variables['thetao'][i,:],mesh,000) # Calculate the differnece level_data=level_data_obs-level_data_f # Plot the figure fig=plt.figure(figsize=(10,7)) m.drawmapboundary(fill_color='0.9') m.drawcoastlines() levels = np.arange(-10., 10., 0.05) plt.tricontourf(x, y, elem_no_nan[::], level_data, levels = levels, cmap=cm.RdBu_r, extend='both') cbar = plt.colorbar(orientation='horizontal', pad=0.03); plt.title('SST_obs-fore'+str(i+1)) plt.tight_layout()
mesh = pf.load_mesh(meshpath, usepickle=False) dataset = Dataset( '/home/h/hbkqtang/fesom_echam6_oasis3-mct/run_scripts/hlrn3/work/cpl_work_8sst_testcase/fesom.2016.oce.daexp1.nc' ) temp_std = dataset.variables['temp_ini'][0:1, 0:126859] print(temp_std.min()) print(temp_std.max()) temp_std_mean = np.mean(temp_std) temp_std_std = np.sqrt(temp_std_mean) print(temp_std_std) m = Basemap(projection='robin', lon_0=0, resolution='c') x, y = m(mesh.x2, mesh.y2) level_data, elem_no_nan = pf.get_data(dataset.variables['temp_ini'][0, :], mesh, 000) fig = plt.figure(figsize=(10, 7)) m.drawmapboundary(fill_color='0.9') m.drawcoastlines() levels = np.arange(0., 2., .05) plt.tricontourf(x, y, elem_no_nan[::], level_data, levels=levels, cmap=cm.Spectral_r, extend='both') cbar = plt.colorbar(orientation='horizontal', pad=0.03) plt.title('SST_var_model_2') plt.tight_layout()
def showfile(ifile, variable, depth, meshpath, box, res, influence, timestep, levels, quiet, ofile, mapproj, abg, clim, cmap, interp, ptype, k): ''' meshpath - Path to the folder with FESOM1.4 mesh files. ifile - Path to FESOM1.4 netCDF file. variable - The netCDF variable to be plotted. ''' if not quiet: click.secho('Mesh: {}'.format(meshpath)) click.secho('File: {}'.format(ifile)) click.secho('Variable: {}'.format(variable), fg='red') click.secho('Depth: {}'.format(depth), fg='red') click.secho('BOX: {}'.format(box)) click.secho('Resolution: {}'.format(res)) click.secho('Influence raduis: {} meters'.format(influence), fg='red') click.secho('Timestep: {}'.format(timestep)) if levels: click.secho('Levels: {}'.format(levels), fg='red') else: click.secho('Levels: auto', fg='red') if cmap: if cmap in cmo.cmapnames: colormap = cmo.cmap_d[cmap] elif cmap in plt.cm.datad: colormap = plt.get_cmap(cmap) else: raise ValueError( 'Get unrecognised name for the colormap `{}`. Colormaps should be from standard matplotlib set of from cmocean package.' .format(cmap)) else: if clim: colormap = cmo.cmap_d['balance'] else: colormap = plt.get_cmap('Spectral_r') sstep = timestep radius_of_influence = influence left, right, down, up = box lonNumber, latNumber = res mesh = pf.load_mesh(meshpath, abg=abg, usepickle=False, usejoblib=True) flf = Dataset(ifile) lonreg = np.linspace(left, right, lonNumber) latreg = np.linspace(down, up, latNumber) lonreg2, latreg2 = np.meshgrid(lonreg, latreg) dind = (abs(mesh.zlevs - depth)).argmin() realdepth = mesh.zlevs[dind] level_data, nnn = pf.get_data(flf.variables[variable][sstep], mesh, realdepth) if interp == 'nn': ofesom = pf.fesom2regular(level_data, mesh, lonreg2, latreg2, radius_of_influence=radius_of_influence) elif interp == 'idist': ofesom = pf.fesom2regular(level_data, mesh, lonreg2, latreg2, radius_of_influence=radius_of_influence, how='idist', k=k) elif interp == 'linear': points = np.vstack((mesh.x2, mesh.y2)).T qh = qhull.Delaunay(points) ofesom = LinearNDInterpolator(qh, level_data)((lonreg2, latreg2)) elif interp == 'cubic': points = np.vstack((mesh.x2, mesh.y2)).T qh = qhull.Delaunay(points) ofesom = CloughTocher2DInterpolator(qh, level_data)((lonreg2, latreg2)) if clim: if variable == 'temp': climvar = 'T' elif variable == 'salt': climvar = 'S' else: raise ValueError( 'You have selected --clim/-c option, but variable `{}` is not in climatology. Acceptable values are `temp` and `salt` only.' .format(variable)) #os.path.join(os.path.dirname(__file__), "../") pathToClim = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../data/") print(pathToClim) w = pf.climatology(pathToClim, clim) xx, yy, oclim = pf.clim2regular( w, climvar, lonreg2, latreg2, levels=[realdepth], radius_of_influence=radius_of_influence) oclim = oclim[0, :, :] data = ofesom - oclim else: data = ofesom if mapproj == 'merc': ax = plt.subplot(111, projection=ccrs.Mercator()) elif mapproj == 'pc': ax = plt.subplot(111, projection=ccrs.PlateCarree()) elif mapproj == 'np': ax = plt.subplot(111, projection=ccrs.NorthPolarStereo()) elif mapproj == 'sp': ax = plt.subplot(111, projection=ccrs.SouthPolarStereo()) elif mapproj == 'rob': ax = plt.subplot(111, projection=ccrs.Robinson()) ax.set_extent([left, right, down, up], crs=ccrs.PlateCarree()) if levels: mmin, mmax, nnum = levels nnum = int(nnum) else: mmin = np.nanmin(data) mmax = np.nanmax(data) nnum = 40 data_levels = np.linspace(mmin, mmax, nnum) if ptype == 'cf': mm = ax.contourf(lonreg,\ latreg,\ data, levels = data_levels, transform=ccrs.PlateCarree(), cmap=colormap, extend='both') elif ptype == 'pcm': data_cyc, lon_cyc = add_cyclic_point(data, coord=lonreg) mm = ax.pcolormesh(lon_cyc,\ latreg,\ data_cyc, vmin = mmin, vmax = mmax, transform=ccrs.PlateCarree(), cmap=colormap, ) else: raise ValueError('Inknown plot type {}'.format(ptype)) ax.coastlines(resolution='50m', lw=0.5) ax.add_feature( cfeature.GSHHSFeature(levels=[1], scale='low', facecolor='lightgray')) cb = plt.colorbar(mm, orientation='horizontal', pad=0.03) cb.set_label(flf.variables[variable].units) plt.title('{} at {}m.'.format(variable, realdepth)) plt.tight_layout() if ofile: plt.savefig(ofile, dpi=100) else: plt.show()
sys.path.append("/home/h/hbkqtang/pyfesom.git/trunk/") import pyfesom as pf import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from matplotlib.colors import LinearSegmentedColormap import numpy as np from matplotlib import cm from netCDF4 import Dataset meshpath = '/gfs1/work/hbkqtang/input/CORE2_final/' mesh = pf.load_mesh(meshpath, usepickle=False) fl = Dataset('/gfs1/work/hbkqtang/temporary/obs_SST.nc') m = Basemap(projection='robin', lon_0=0, resolution='c') x, y = m(mesh.x2, mesh.y2) for i in range(0, 365): level_data, elem_no_nan = pf.get_data(fl.variables['sst'][i, :], mesh, 000) fig = plt.figure(figsize=(10, 7)) m.drawmapboundary(fill_color='0.9') m.drawcoastlines() levels = np.arange(-5., 35., .1) plt.tricontourf(x, y, elem_no_nan[::], level_data, levels=levels, cmap=cm.Spectral_r, extend='both') cbar = plt.colorbar(orientation='horizontal', pad=0.03) cbar.set_label("SST") plt.title('SST_obs_' + str(i + 1))
def showo(meshpath, ifile, variable, depth, box, timestep, minmax, mapproj, quiet, ofile): ''' meshpath - Path to the folder with FESOM1.4 mesh files. ifile - Path to FESOM1.4 netCDF file. variable - The netCDF variable to be plotted. ''' if not quiet: click.secho('Mesh: {}'.format(meshpath)) click.secho('File: {}'.format(ifile)) click.secho('Variable: {}'.format(variable), fg='red') click.secho('Depth: {}'.format(depth), fg='red') click.secho('BOX: {}'.format(box)) click.secho('Timestep: {}'.format(timestep)) if minmax: click.secho('Min/max: {}'.format(minmax), fg='red') else: click.secho('Min/max: auto', fg='red') mesh = pf.load_mesh(meshpath) flf = Dataset(ifile) left, right, down, up = box elem_nonan, no_nan_tri = pf.cut_region(mesh, [left, right, down, up], 0) if variable == 'topo': level_data = mesh.topo else: level_data, nnn = pf.get_data(flf.variables[variable][timestep, :], mesh, depth) if minmax: mmin, mmax = minmax else: mmin = level_data[elem_nonan].min() mmax = level_data[elem_nonan].max() plt.figure(figsize=(10, 10)) if mapproj == 'merc': ax = plt.subplot(111, projection=ccrs.Mercator()) elif mapproj == 'pc': ax = plt.subplot(111, projection=ccrs.PlateCarree()) elif mapproj == 'np': ax = plt.subplot(111, projection=ccrs.NorthPolarStereo()) elif mapproj == 'sp': ax = plt.subplot(111, projection=ccrs.SouthPolarStereo()) elif mapproj == 'rob': ax = plt.subplot(111, projection=ccrs.Robinson()) ax.set_extent(box, crs=ccrs.PlateCarree()) mm = ax.tripcolor(mesh.x2, mesh.y2, elem_nonan, level_data, transform=ccrs.PlateCarree(), edgecolors='k', cmap=cm.Spectral_r, vmin=mmin, vmax=mmax) ax.coastlines(lw=0.5, resolution='10m') plt.colorbar( mm, orientation='horizontal', pad=0.03, ) plt.title('{} at {}'.format(variable, depth), size=20) if ofile: plt.savefig(ofile, dpi=200) else: plt.show()
import pyfesom as pf import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from matplotlib.colors import LinearSegmentedColormap import numpy as np from matplotlib import cm from netCDF4 import Dataset meshpath='/gfs1/work/hbkqtang/input/CORE2_final/' mesh=pf.load_mesh(meshpath,usepickle=False) fl=Dataset('/gfs1/work/hbkqtang/AWI-CM-PDAF/cpl_work_23profile_temp_r106_08/fesom.2016.oce.daexp1.nc') m = Basemap(projection='robin',lon_0=0, resolution='c') x, y = m(mesh.x2, mesh.y2) depth=100 for i in range(3,4): level_data, elem_no_nan = pf.get_data(fl.variables['salt_f'][i,:],mesh,depth) fig=plt.figure(figsize=(10,7)) m.drawmapboundary(fill_color='0.9') m.drawcoastlines() levels = np.arange(31., 38., .01) plt.tricontourf(x, y, elem_no_nan[::], level_data, levels = levels, cmap=cm.Spectral_r, extend='both') cbar = plt.colorbar(orientation='horizontal', pad=0.03); #plt.title('SST_ini_'+str(i+1)) #plt.title('temp_50_'+str(i+1)) #read observation file en4=Dataset("/gfs1/work/hbkqtang/temporary/EN.4.2.1.f.profiles.g10.201601.nc",format="NETCDF3_64BIT_OFFSET") # find out the number of profiles for one day n_prof=en4.dimensions['N_PROF'] n_prof=n_prof.size lon=en4.variables['LONGITUDE'][:]