def test_getncmodeldata_timeid(): from dfm_tools.get_nc import get_ncmodeldata file_map1 = os.path.join(dir_testinput,r'DFM_sigma_curved_bend\DFM_OUTPUT_cb_3d\cb_3d_map.nc') data_frommap = get_ncmodeldata(file_nc=file_map1, varname='mesh2d_sa1', timestep=1, layer=5)#, multipart=False) assert (data_frommap.data[0,0,0] - 31. ) < 1E-9
def test_gettimesfromnc(): from dfm_tools.get_nc import get_ncmodeldata #this file seems to have a reconstructable array, but contains gaps, so the time reader falls back to reading all file_nc = os.path.join(dir_testinput,'id1-LICHTELGRE.nc') data_fromhis_time = get_ncmodeldata(file_nc,varname='time',timestep='all')
def test_gethisbottomdata(): """ checks whether time dimension is indexed once, resulted in shape of (10,10,1) """ from dfm_tools.get_nc import get_ncmodeldata file_nc_his = os.path.join(dir_testinput,'MWRA','MB_02_0000_his.nc') moddata_dfm = get_ncmodeldata(file_nc_his,varname='salinity',station='MWRA_F22',timestep=range(10),layer='bottom') assert moddata_dfm.shape == (10, 1, 1)
def test_getmapdata(file_nc, expected_size): """ Checks whether ghost cells are properly taken care of. file_nc = os.path.join(dir_testinput,'DFM_3D_z_Grevelingen','computations','run01','DFM_OUTPUT_Grevelingen-FM','Grevelingen-FM_0000_map.nc') expected_size = (1, 44796) """ from dfm_tools.get_nc import get_ncmodeldata data_nc = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_s1',timestep=2) assert data_nc.shape == expected_size
def test_getncmodeldata_indexcountmetadata(): from dfm_tools.get_nc import get_ncmodeldata #check if retrieving 1 index of data from 1 dimensional variable works (does not work if indices are np.arrays, so conversion to list in get_nc.py) file_his = os.path.join(dir_testinput,r'DFM_fou_RMM\RMM_dflowfm_0000_his.nc') data_statcoord = get_ncmodeldata(file_nc=file_his, varname='station_x_coordinate',station='NM_1005.26_R_HBR-Cl_VCS-Nieuwe-Maas-85m') assert len(data_statcoord.var_stations) == 1 file_his = os.path.join(dir_testinput,r'DFM_sigma_curved_bend\DFM_OUTPUT_cb_3d\cb_3d_his.nc') data_fromhis = get_ncmodeldata(file_nc=file_his, varname='x_velocity', timestep=1, layer=5, station='Innersouth boundary')#, multipart=False) assert len(data_fromhis.var_times) == 1 assert len(data_fromhis.var_layers) == 1 assert len(data_fromhis.var_stations) == 1 #data_fromhis_all = get_ncmodeldata(file_nc=file_his, varname='x_velocity', timestep='all', layer='all', station='all') data_fromhis = get_ncmodeldata(file_nc=file_his, varname='x_velocity', timestep=[1,0,-5,1,4,3,2,0,1,-1], layer=[5,-2,3,0,5], station=[4,-1,0,0]) assert len(data_fromhis.var_times) == 7 assert data_fromhis.var_times.index.tolist() == [0,1,2,3,4,2156,2160] assert len(data_fromhis.var_layers) == 4 assert data_fromhis.var_layers == [0,3,5,8] assert len(data_fromhis.var_stations) == 3 assert data_fromhis.var_stations.index.tolist() == [0,4,5]
def test_getncmodeldata_datetime(): import numpy as np import datetime as dt import pandas as pd from dfm_tools.get_nc import get_ncmodeldata #retrieve all file_nc = os.path.join(dir_testinput,r'DFM_sigma_curved_bend\DFM_OUTPUT_cb_3d\cb_3d_map.nc') data_frommap = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_sa1', timestep='all', layer=5)#, multipart=False) assert data_frommap.shape[0] == len(data_frommap.var_times) #retrieve with numpy datetime array data_frommap = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_sa1', timestep=np.arange(dt.datetime(2001,1,1),dt.datetime(2001,1,2),dt.timedelta(hours=1)), layer=5)#, multipart=False) assert (data_frommap.data[0,0,0] - 31. ) < 1E-9 assert data_frommap.shape[0] == len(data_frommap.var_times) #retrieve with pandas date_range data_frommap = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_sa1', timestep=pd.date_range(dt.datetime(2001,1,1),dt.datetime(2001,1,2),freq='30min'), layer=5)#, multipart=False) assert (data_frommap.data[0,0,0] - 31. ) < 1E-9 assert data_frommap.shape[0] == len(data_frommap.var_times)
def test_getmapbottomdata(file_nc, varname, expected_size): """ Checks whether ghost cells are properly taken care and if the mask comes trough (needed for selecting the bottom layer). It will fail on get_ncmodeldata, the assertions are just extras file_nc = os.path.join(dir_testinput,'DFM_3D_z_Grevelingen','computations','run01','DFM_OUTPUT_Grevelingen-FM','Grevelingen-FM_0000_map.nc') varname = 'mesh2d_sa1' expected_size = (1, 44796, 1) file_nc = os.path.join('p:\\11203850-coastserv\\06-Model\\waq_model\\simulations\\run0_20200319\\DFM_OUTPUT_kzn_waq', 'kzn_waq_0000_map.nc') varname = 'Chlfa' expected_size = (1, 17385, 1) """ from dfm_tools.get_nc import get_ncmodeldata data_fromnc_bot = get_ncmodeldata(file_nc=file_nc, varname=varname, timestep=3, layer='bottom') assert data_fromnc_bot.shape == expected_size assert data_fromnc_bot.mask.shape == expected_size
# # import libraries # from dfm_tools.get_nc import get_netdata, get_ncmodeldata, plot_netmapdata from dfm_tools.get_nc_helpers import get_ncvardimlist, get_timesfromnc, get_hisstationlist import matplotlib.pyplot as plt import xarray as xr import numpy as np fname = 'C:\\Users\\backeber\\OneDrive - Stichting Deltares\\Desktop\\Project-D-HYDRO-Phase-4\\dflowfm\\dflowfm_serial\\DFM_OUTPUT_ocean_eddy_' + expt + '\\ocean_eddy_' + expt + '_map.nc' ugrid_all = get_netdata(file_nc=fname) ds = xr.open_dataset(fname) fig, axs = plt.subplots(1, 1, figsize=(5, 5)) var = get_ncmodeldata(file_nc=fname, varname=varname, timestep=tstep2plot) pc = plot_netmapdata(ugrid_all.verts, values=var[0, :], ax=axs, linewidth=0.5, cmap="jet") #pc.set_clim([0, 0.25]) #axs.plot(x[:i],y[:i],'r.', markersize = 2) axs.set_title(np.datetime_as_string(ds.time.data[tstep2plot], unit='h')) #axs.set_title('t = '+str(tstep2plot)+' hours') axs.set_aspect('equal') xticks = np.linspace(np.min(ds.mesh2d_face_x.data), np.max(ds.mesh2d_face_x.data), num=5, endpoint=True)
plt.close('all') from dfm_tools.get_nc import get_netdata, get_ncmodeldata, plot_netmapdata from dfm_tools.get_nc_helpers import get_ncvardimlist from dfm_tools.regulargrid import scatter_to_regulargrid dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' #RMM foufile met quivers file_nc = os.path.join(dir_testinput, r'DFM_fou_RMM\RMM_dflowfm_0000_fou.nc') vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc) #stations_pd = get_hisstationlist(file_nc,varname='waterlevel') ugrid_all = get_netdata(file_nc=file_nc) ux_mean = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_fourier001_mean') uy_mean = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_fourier002_mean') magn_mean = np.sqrt(ux_mean**2 + uy_mean**2) #uc_mean = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_fourier003_mean') #uc_max = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_fourier004_max') facex = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_face_x') facey = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_face_y') X, Y, U = scatter_to_regulargrid(xcoords=facex, ycoords=facey, ncellx=60, ncelly=35, values=ux_mean) X, Y, V = scatter_to_regulargrid(xcoords=facex, ycoords=facey, ncellx=60,
import os import matplotlib.pyplot as plt plt.close('all') from dfm_tools.get_nc import get_ncmodeldata #from dfm_tools.get_nc_helpers import get_hisstationlist dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' file_nc = os.path.join(dir_testinput, 'KenmerkendeWaarden', 'observations.nc') #station_names = get_hisstationlist(file_nc=file_nc, varname='observation_id') #station_names = get_hisstationlist(file_nc=file_nc, varname='water_level') data_fromsobek = get_ncmodeldata(file_nc=file_nc, varname='water_level', station=['Maasmond', 'HKVHLD', 'MO_1035.00'], timestep='all') fig, ax = plt.subplots() for iL in range(data_fromsobek.shape[1]): ax.plot(data_fromsobek.var_times, data_fromsobek[:, iL], '-', label=data_fromsobek.var_stations.iloc[iL, 0]) ax.legend() plt.savefig( os.path.join( dir_output, '%s_waterlevel' % (os.path.basename(file_nc).replace('.', ''))))
# from dfm_tools.get_nc import get_netdata, get_ncmodeldata, plot_netmapdata from dfm_tools.get_nc_helpers import get_ncvardimlist, get_timesfromnc, get_hisstationlist import matplotlib.pyplot as plt import xarray as xr import numpy as np fname = 'C:\\Users\\backeber\\OneDrive - Stichting Deltares\\Desktop\\Project-D-HYDRO-Phase-4\\dflowfm\\dflowfm_serial\\DFM_OUTPUT_ocean_eddy_' + expt + '\\ocean_eddy_' + expt + '_map.nc' ugrid_all = get_netdata(file_nc=fname) ds = xr.open_dataset(fname) for i in np.arange(0, len(ds.time.data), 1): fig, axs = plt.subplots(1, 1, figsize=(5, 5)) ssh = get_ncmodeldata(file_nc=fname, varname=var2plot, timestep=i) pc = plot_netmapdata(ugrid_all.verts, values=ssh[0, :], ax=axs, linewidth=0.5, cmap="jet") pc.set_clim(cmaplims) #axs.plot(x[:i],y[:i],'r.', markersize = 2) axs.set_title(np.datetime_as_string(ds.time.data[i], unit='h')) #axs.set_title('t = '+str(i)+' hours') axs.set_aspect('equal') xticks = np.linspace(np.min(ds.mesh2d_face_x.data), np.max(ds.mesh2d_face_x.data), num=5, endpoint=True)
from dfm_tools.get_nc import get_ncmodeldata #, get_netdata, plot_netmapdata from dfm_tools.get_nc_helpers import get_ncvardimlist, get_hisstationlist #, get_varname_fromnc from dfm_tools.regulargrid import uva2xymagdeg from dfm_tools.io.polygon import Polygon dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' file_ldb = r'p:\archivedprojects\1220688-lake-kivu\3_modelling\1_FLOW\4_CH4_CO2_included\008\lake_kivu_geo.ldb' data_ldb = Polygon.fromfile(file_ldb, pd_output=True) file_nc = r'p:\archivedprojects\1220688-lake-kivu\3_modelling\1_FLOW\7_heatfluxinhis\062_netcdf\trim-thiery_002_coarse.nc' vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc) data_nc_XZ = get_ncmodeldata(file_nc=file_nc, varname='XZ') data_nc_YZ = get_ncmodeldata(file_nc=file_nc, varname='YZ') data_nc_XCOR = get_ncmodeldata(file_nc=file_nc, varname='XCOR') data_nc_YCOR = get_ncmodeldata(file_nc=file_nc, varname='YCOR') data_nc_ALFAS = get_ncmodeldata( file_nc=file_nc, varname='ALFAS') #contains rotation of all cells wrt real world #data_nc_S1 = get_ncmodeldata(file_nc=file_nc, varname='S1',timestep='all') data_nc_QNET = get_ncmodeldata(file_nc=file_nc, varname='QNET', timestep='all') data_nc_DPV0 = get_ncmodeldata(file_nc=file_nc, varname='DPV0') #data_nc_QEVA = get_ncmodeldata(file_nc=file_nc, varname='QEVA',timestep='all') data_nc_KCU = get_ncmodeldata(file_nc=file_nc, varname='KCU') data_nc_KCV = get_ncmodeldata(file_nc=file_nc, varname='KCV') layno = -2 data_nc_U1 = get_ncmodeldata(file_nc=file_nc,
pol_shp = Polygon(pol_data_nonan) pol_shp_list.append(pol_shp) print('creating geodataframe with cells') newdata = gpd.GeoDataFrame(crs="EPSG:4326") newdata[ 'geometry'] = pol_shp_list #way more time efficient than doing it the loop for iV, varname in enumerate(varlist): newdata[varname] = None for timestep in [6]: #[0,10,20,30]: for iV, varname in enumerate(varlist): try: data_fromnc_all = get_ncmodeldata(file_nc=file_nc, varname=varname, timestep=timestep, layer='all') data_fromnc_bot = get_ncmodeldata(file_nc=file_nc, varname=varname, timestep=timestep, layer='bottom') data_fromnc_top = get_ncmodeldata(file_nc=file_nc, varname=varname, timestep=timestep, layer='top') except: data_fromnc_top = get_ncmodeldata(file_nc=file_nc, varname=varname, timestep=timestep) data_fromnc_nonan = data_fromnc_top[:]
import datetime as dt import numpy as np import matplotlib.pyplot as plt plt.close('all') from dfm_tools.get_nc import get_ncmodeldata #, get_netdata, plot_netmapdata from dfm_tools.get_nc_helpers import get_ncvardimlist, get_hisstationlist #, get_varname_fromnc dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' #MAP ZUNO file_nc = r'p:\1204257-dcsmzuno\2019\DCSMv6\A01\SDS-A01_map.nc' vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc) data_nc_x = get_ncmodeldata(file_nc=file_nc, varname='grid_x') data_nc_y = get_ncmodeldata(file_nc=file_nc, varname='grid_y') data_nc_SEP = get_ncmodeldata(file_nc=file_nc, varname='SEP', timestep='all') #data_nc_VELU = get_ncmodeldata(file_nc=file_nc, varname='VELU',timestep='all') #data_nc_VELV = get_ncmodeldata(file_nc=file_nc, varname='VELV',timestep='all') data_nc_xcen = np.mean(data_nc_x, axis=2) data_nc_ycen = np.mean(data_nc_y, axis=2) fig, ax = plt.subplots() #vel_x, vel_y, vel_magn, direction_naut_deg = uva2xymagdeg(u=data_nc_U1[timestep,90,:,:],v=data_nc_V1[timestep,90,:,:],alpha=data_nc_ALFAS) #pc = ax.pcolor(data_nc_XZ,data_nc_YZ,direction_naut_deg,cmap='jet') #pc.set_clim([0,360]) timestep = 0 pc = ax.pcolor(data_nc_xcen, data_nc_ycen, data_nc_SEP[timestep, :, :],
#calcdist_fromlatlon = None multipart = None #line_array = np.array([[ 104.15421399, 2042.7077107 ], # [2913.47878063, 2102.48057382]]) #val_ylim = None #clim_bl = None #optimize_dist = None #ugrid = get_netdata(file_nc=file_nc, multipart=multipart) #intersect_gridnos, intersect_coords = ugrid.polygon_intersect(line_array, optimize_dist=None) #code from get_xzcoords_onintersection data_nc = Dataset(file_nc) varn_mesh2d_s1 = get_varname_fromnc(data_nc, 'mesh2d_s1', vardim='var') data_frommap_wl3 = get_ncmodeldata(file_nc, varname=varn_mesh2d_s1, timestep=timestep, multipart=multipart) data_frommap_wl3 = data_frommap_wl3[0, :] #data_frommap_wl3_sel = data_frommap_wl3[0,intersect_gridnos] varn_mesh2d_flowelem_bl = get_varname_fromnc(data_nc, 'mesh2d_flowelem_bl', vardim='var') data_frommap_bl = get_ncmodeldata(file_nc, varname=varn_mesh2d_flowelem_bl, multipart=multipart) #data_frommap_bl_sel = data_frommap_bl[intersect_gridnos] dimn_layer = get_varname_fromnc(data_nc, 'nmesh2d_layer', vardim='dim') if dimn_layer is None: #no layers, 2D model nlay = 1 else:
return time_cor if 'Grevelingen-FM_0000' in file_nc: #file_nc = os.path.join(dir_testinput,r'DFM_3D_z_Grevelingen\computations\run01\DFM_OUTPUT_Grevelingen-FM\Grevelingen-FM_0000_his.nc') station = 'all' station_zt = 'GTSO-02' elif 'tttz' in file_nc: #NITHIN #file_nc = os.path.join(dir_testinput,'vanNithin','tttz_0000_his.nc') station = ['Peiraias', 'Ovrios_2','Ovrios','Ovrios','Ortholithi'] station_zt = 'Ortholithi' elif 'impaqt' in file_nc: station = ['MO_TS_MO_ATHOS','MO_TS_MO_LESVO','MO_TS_MO_SKYRO','IOC_thes','farm_impaqt'] station_zt = 'MO_TS_MO_ATHOS' print('plot bedlevel from his') data_fromhis = get_ncmodeldata(file_nc=file_nc, varname='bedlevel', station=station)#, multipart=False) fig, ax = plt.subplots() ax.plot(data_fromhis.var_stations.iloc[:,0],data_fromhis,'-') ax.tick_params('x',rotation=90) fig.savefig(os.path.join(dir_output,'%s_bedlevel'%(os.path.basename(file_nc).replace('.','')))) print('plot waterlevel from his') data_fromhis = get_ncmodeldata(file_nc=file_nc, varname='waterlevel', timestep='all', station=station)#, multipart=False) fig, ax = plt.subplots() ax.plot(data_fromhis.var_times,data_fromhis,'-') fig.savefig(os.path.join(dir_output,'%s_waterlevel'%(os.path.basename(file_nc).replace('.','')))) print('plot salinity from his') data_fromhis = get_ncmodeldata(file_nc=file_nc, varname='salinity', timestep='all', layer=5, station=station)#, multipart=False) data_fromhis_flat = data_fromhis[:,:,0] fig, ax = plt.subplots()
def regularGrid_to_netcdf(fp_in, nx, ny, treg, lreg): dir_output = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', 'output')) if not os.path.exists(dir_output): os.makedirs(dir_output) file_nc = fp_in input_nc = Dataset(file_nc, 'r', format='NetCDF4') time_old = input_nc.variables['time'][:] if treg != 'all': time_old = np.take(time_old, treg) vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc) df = vars_pd key_values = [ 'mesh2d_tem1', 'time', 'mesh2d_s1', 'mesh2d_ucx', 'mesh2d_ucy', 'mesh2d_tem1', 'mesh2d_sa1', 'mesh2d_water_quality_output_17', 'mesh2d_OXY', 'mesh2d_face_x', 'mesh2d_face_y' ] df = df.loc[df['nc_varkeys'].isin(key_values)] """ #################################################################################################################### # Regularise all files with 3 dimensions (time, nFaces, layers). # This will be equal to four dimensions in the regular grid format since nFaces is the x- and y- dimension. #################################################################################################################### """ df2 = df.loc[df['ndims'] == 3] data_frommap_x = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_face_x') data_frommap_y = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_face_y') time = get_ncmodeldata(file_nc=file_nc, varname='time', timestep=treg) outname = '%s_regular.nc' % os.path.split(fileNC)[1][0:-3] file_nc_reg = os.path.join(dir_output, outname) root_grp = Dataset(file_nc_reg, 'w', format='NETCDF4') root_grp.description = 'Example simulation data' first_read = True i = 0 for index, row in df2.iterrows(): if row['dimensions'][1] == 'mesh2d_nEdges': continue data_frommap_var = get_ncmodeldata(file_nc=file_nc, varname=row['nc_varkeys'], timestep=treg, layer=lreg) data_frommap_var = data_frommap_var.filled(np.nan) field_array = np.empty( (data_frommap_var.shape[0], ny, nx, data_frommap_var.shape[-1])) tms = data_frommap_var.shape[0] lrs = data_frommap_var.shape[-1] trange = range(0, tms) lrange = range(0, lrs) A = np.array([ scatter_to_regulargrid(xcoords=data_frommap_x, ycoords=data_frommap_y, ncellx=nx, ncelly=ny, values=data_frommap_var[t, :, l].flatten(), method='linear') for t in trange for l in lrange ]) x_grid = A[0][0] y_grid = A[0][1] A = A[:, 2, :, :] A = np.moveaxis(A, [0], [2]) subs = np.split(A, tms, axis=2) field_array[:, :, :, 0:lrs] = [subs[tn] for tn in trange] field_array = np.ma.masked_invalid(field_array) print('done with variable %s' % row['nc_varkeys']) if first_read: unout = 'seconds since 2015-01-01 00:00:00' lon = x_grid[0, :] lat = y_grid[:, 0] # create dimensions root_grp.createDimension('time', None) root_grp.createDimension('lon', lon.shape[0]) root_grp.createDimension('lat', lat.shape[0]) root_grp.createDimension('layer', lrs) lonvar = root_grp.createVariable('lon', 'float32', 'lon') lonvar.setncattr('axis', 'X') lonvar.setncattr('reference', 'geographical coordinates, WGS84 projection') lonvar.setncattr('units', 'degrees_east') lonvar.setncattr('_CoordinateAxisType', 'Lon') lonvar.setncattr('long_name', 'longitude') lonvar.setncattr('valid_max', '180') lonvar.setncattr('valid_min', '-180') lonvar[:] = lon latvar = root_grp.createVariable('lat', 'float32', 'lat') latvar.setncattr('axis', 'Y') latvar.setncattr('reference', 'geographical coordinates, WGS84 projection') latvar.setncattr('units', 'degrees_north') latvar.setncattr('_CoordinateAxisType', 'Lat') latvar.setncattr('long_name', 'latitude') latvar.setncattr('valid_max', '90') latvar.setncattr('valid_min', '-90') latvar[:] = lat layervar = root_grp.createVariable('layer', 'float32', 'layer') layervar.setncattr('axis', 'Z') layervar.setncattr('reference', 'geographical coordinates, WGS84 projection') layervar.setncattr('units', 'm') layervar.setncattr('_CoordinateZisPositive', 'down') layervar.setncattr('_CoordinateAxisType', 'Height') layervar.setncattr('long_name', 'Depth') layervar[:] = range(0, lrs) timevar = root_grp.createVariable('time', 'float64', 'time') timevar.setncattr('units', unout) timevar.setncattr('calendar', 'standard') timevar.setncattr('long_name', 'time') timevar.setncattr('_CoordinateAxisType', 'Time') timevar[:] = time_old fieldName = row['nc_varkeys'] fieldvar = root_grp.createVariable(fieldName, 'float32', ('time', 'lat', 'lon', 'layer'), fill_value=-999) key = fieldName for ncattr in input_nc.variables[key].ncattrs(): if ncattr != "_FillValue": root_grp.variables[fieldName].setncattr( ncattr, input_nc.variables[key].getncattr(ncattr)) fieldvar[:] = field_array first_read = False i += 1 """ #################################################################################################################### # Regularise all files with 2 dimensions (time, nFaces, layers). # This will be equal to 3 dimensions in the regular grid format since nFaces is the x- and y- dimension. #################################################################################################################### """ print('STARTING 2D') df2 = df.loc[df['ndims'] == 2] excludeList = ['edge', 'face', 'x', 'y'] for index, row in df2.iterrows(): test = any(n in str(row['nc_varkeys']) for n in excludeList) if not test: if row['dimensions'][1] == 'mesh2d_nEdges': continue ntimes = row['shape'][0] data_frommap_var = get_ncmodeldata(file_nc=file_nc, varname=row['nc_varkeys'], timestep=treg) data_frommap_var = data_frommap_var.filled(np.nan) field_array = np.empty((data_frommap_var.shape[0], ny, nx)) trange = range(0, data_frommap_var.shape[0]) tms = data_frommap_var.shape[0] A = np.array([ scatter_to_regulargrid(xcoords=data_frommap_x, ycoords=data_frommap_y, ncellx=nx, ncelly=ny, values=data_frommap_var[t, :].flatten(), method='linear') for t in trange ]) A = A[:, 2, :, :] field_array[:, :, :] = A field_array = np.ma.masked_invalid(field_array) """write data to new netcdf""" fieldName = row['nc_varkeys'] fieldvar = root_grp.createVariable(fieldName, 'float32', ('time', 'lat', 'lon'), fill_value=-999) key = fieldName for ncattr in input_nc.variables[key].ncattrs(): if ncattr != "_FillValue": root_grp.variables[fieldName].setncattr( ncattr, input_nc.variables[key].getncattr(ncattr)) fieldvar[:] = field_array root_grp.close()
dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' file_nc = os.path.join(dir_testinput, 'DFM_3D_z_Grevelingen', 'computations', 'run01', 'DFM_OUTPUT_Grevelingen-FM', 'Grevelingen-FM_0000_map.nc') #file_nc = os.path.join(dir_testinput,'DFM_sigma_curved_bend\\DFM_OUTPUT_cb_3d\\cb_3d_map.nc') #file_nc = 'p:\\1204257-dcsmzuno\\2013-2017\\3D-DCSM-FM\\A17b\\DFM_OUTPUT_DCSM-FM_0_5nm\\DCSM-FM_0_5nm_0000_map.nc' #file_nc = 'p:\\11205258-006-kpp2020_rmm-g6\\C_Work\\08_RMM_FMmodel\\computations\\run_156\\DFM_OUTPUT_RMM_dflowfm\\RMM_dflowfm_0000_map.nc' clim_bl = [-40, 10] vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc) ugrid = get_netdata(file_nc=file_nc) #get bed layer data_frommap_x = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_face_x') data_frommap_y = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_face_y') data_frommap_bl = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_flowelem_bl') for maskland_dist in [None, 100]: #interpolate to regular grid x_grid, y_grid, val_grid = scatter_to_regulargrid( xcoords=data_frommap_x, ycoords=data_frommap_y, ncellx=100, ncelly=80, values=data_frommap_bl, method='linear', maskland_dist=maskland_dist)
from dfm_tools.testutils import try_importmodule try_importmodule( modulename='contextily' ) #check if contextily was installed since it is an optional module, also happens in plot_cartopybasemap() import contextily as ctx from dfm_tools.get_nc import get_netdata, get_ncmodeldata, plot_netmapdata dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' file_nc_map = os.path.join( dir_testinput, 'DFM_3D_z_Grevelingen\\computations\\run01\\DFM_OUTPUT_Grevelingen-FM\\Grevelingen-FM_0000_map.nc' ) ugrid = get_netdata(file_nc=file_nc_map) data_frommap_bl = get_ncmodeldata(file_nc=file_nc_map, varname='mesh2d_flowelem_bl') source_list = [ ctx.providers.Stamen.Terrain, #default source ctx.providers.Esri.WorldImagery, ctx.providers.CartoDB.Voyager, #ctx.providers.NASAGIBS.ViirsEarthAtNight2012, ctx.providers.Stamen.Watercolor ] for source_ctx in source_list: source_name = source_ctx['name'].replace('.', '_') fig, ax = plt.subplots(1, 1, figsize=(10, 6)) pc = plot_netmapdata(ugrid.verts, values=data_frommap_bl, ax=ax,
import numpy as np import matplotlib.pyplot as plt # set filename fname = 'C:\\Users\\backeber\\OneDrive - Stichting Deltares\\Desktop\\Project-D-HYDRO-Phase-4\\dflowfm\\dflowfm_serial\\oceaneddy_expt00_20010101_000000_rst.nc' ds = xr.open_dataset(fname) # plt.scatter(ds.FlowElem_xzw.data,ds.FlowElem_yzw.data,1,ds.s1.data[0,:]) ugrid_all = get_netdata( file_nc= 'C:\\Users\\backeber\\OneDrive - Stichting Deltares\\Desktop\\Project-D-HYDRO-Phase-4\\dflowfm\\dflowfm_serial\\DFM_OUTPUT_oceaneddy_expt00\\oceaneddy_expt00_map.nc' ) #ugrid_all = get_netdata(file_nc=fname) ds = xr.open_dataset(fname) ssh = get_ncmodeldata(file_nc=fname, varname='s1', timestep=0) # find location of max ssh to add to plot maxi = np.argmax(ssh) fig, ax = plt.subplots() #(figsize=(15, 10)) pc = plot_netmapdata(ugrid_all.verts, values=ssh[0, :], ax=ax, linewidth=0.5, cmap="jet") #pc.set_clim([0, 0.05]) x, y = ugrid_all.verts[maxi, :, :].mean(axis=0) ax.plot(x, y, 'wx') ax.set_title('%s (%s)' % (ds.s1.long_name, ds.s1.units))
color="crimson", facecolor="None") ax.set_xlabel('x-direction') ax.set_ylabel('y-direction') ax.set_aspect('equal') fig.tight_layout() fig.savefig( os.path.join(dir_output, '%s_grid' % (os.path.basename(file_nc).replace('.', '')))) #PLOT bedlevel if not 'cb_3d_map' in file_nc: print('plot grid and values from mapdata (constantvalue, 1 dim)') ugrid = get_netdata(file_nc=file_nc) #,multipart=False) #iT = 3 #for iT in range(10): data_frommap = get_ncmodeldata( file_nc=file_nc, varname='mesh2d_flowelem_bl') #, multipart=False) data_frommap_flat = data_frommap.flatten() fig, ax = plt.subplots() pc = plot_netmapdata(ugrid.verts, values=data_frommap_flat, ax=None, linewidth=0.5, cmap="jet") pc.set_clim(clim_bl) cbar = fig.colorbar(pc, ax=ax) cbar.set_label('%s [%s]' % (data_frommap.var_ncattrs['long_name'], data_frommap.var_ncattrs['units'])) coordnames_xy = data_frommap.var_ncattrs['coordinates'].split() varcoords_x = get_ncmodeldata(file_nc=file_nc, varname=coordnames_xy[0]) varcoords_y = get_ncmodeldata(file_nc=file_nc,
#uncomment the line below, copy data locally and change this path to increase performance dir_testinput = os.path.join(r'E:\proj\Pescadero\Model_Runs\Testing') file_nc_map = os.path.join(dir_testinput, 'run_tide_test-v12A', 'DFM_OUTPUT_flowfm', 'flowfm_map.nc') file_nc_his = os.path.join(dir_testinput, 'run_tide_test-v12A', 'DFM_OUTPUT_flowfm', 'flowfm_his.nc') #get lists with vars/dims, times, station/crs/structures vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc_map) times_pd = get_timesfromnc(file_nc=file_nc_map) statlist_pd = get_hisstationlist(file_nc=file_nc_his, varname='station_name') #retrieve his data data_fromhis_wl = get_ncmodeldata(file_nc=file_nc_his, varname='waterlevel', station='all', timestep='all') fig, ax = plt.subplots(1, 1, figsize=(10, 5)) for iP, station in enumerate(data_fromhis_wl.var_stations['station_name']): ax.plot(data_fromhis_wl.var_times, data_fromhis_wl[:, iP], '-', label=station) ax.legend() ax.set_ylabel( '%s (%s)' % (data_fromhis_wl.var_varname, data_fromhis_wl.var_ncvarobject.units)) #plot net/grid ugrid_all = get_netdata(file_nc=file_nc_map) #,multipart=False) fig, ax = plt.subplots()
import matplotlib.pyplot as plt plt.close('all') from dfm_tools.get_nc import get_netdata, get_ncmodeldata, plot_netmapdata dir_testinput = r'c:\DATA\dfm_tools_testdata' file_nc = os.path.join(dir_testinput,'oost_tracer_2_map.nc') ugrid = get_netdata(file_nc=file_nc) print('plot grid and values from mapdata (constantvalue, 1 dim)') if 'oost_tracer_map' in file_nc: var_names = ['FColi1','HIWAI1','mspaf1','Pharma1'] #nieuwe file, te veel dimensies var_clims = [None,[0,100000000000],None,[0,10000]] elif 'oost_tracer_2_map' in file_nc: var_names = ['mesh2d_FColi','mesh2d_HIWAI','mesh2d_Pharma'] #oude file var_clims = [None,[0,100000000000],[0,10000]] else: raise Exception('ERROR: no settings provided for this mapfile') for var_name, var_clim in zip(var_names, var_clims): fig, ax = plt.subplots() data_frommap = get_ncmodeldata(file_nc=file_nc, varname=var_name)#, multipart=False) pc = plot_netmapdata(ugrid.verts, values=data_frommap, ax=None, linewidth=0.5, cmap="jet") if var_clim != None: pc.set_clim(var_clim) fig.colorbar(pc, ax=ax) ax.set_aspect('equal') ax.set_xlabel(var_name) plt.savefig('%s_%s'%(os.path.basename(file_nc).replace('.',''),var_name))
from dfm_tools.get_nc_helpers import get_ncvardimlist dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' file_nc_list = [r'p:\11203869-morwaqeco3d\05-Tidal_inlet\02_FM_201910\FM_MF10_Max_30s\wave\wavm-inlet.nc', r'p:\11200665-c3s-codec\2_Hydro\ECWMF_meteo\meteo\ERA-5\2000\ERA5_metOcean_atm_19991201_19991231.nc', r'p:\1204257-dcsmzuno\2014\data\meteo\HIRLAM72_2018\h72_201803.nc', r'p:\11202255-sfincs\Testbed\Original_tests\01_Implementation\08_restartfile\sfincs_map.nc', #not available anymore ] for file_nc in file_nc_list: #get cell center coordinates from regular grid if 'ERA5_metOcean_atm' in file_nc: data_fromnc_x_1D = get_ncmodeldata(file_nc=file_nc, varname='longitude') data_fromnc_y_1D = get_ncmodeldata(file_nc=file_nc, varname='latitude') data_fromnc_x, data_fromnc_y = np.meshgrid(data_fromnc_x_1D, data_fromnc_y_1D) else: data_fromnc_x = get_ncmodeldata(file_nc=file_nc, varname='x') data_fromnc_y = get_ncmodeldata(file_nc=file_nc, varname='y') vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc) x_cen_withbnd = center2corner(data_fromnc_x) y_cen_withbnd = center2corner(data_fromnc_y) grid_verts = meshgridxy2verts(x_cen_withbnd, y_cen_withbnd) fig, axs = plt.subplots(2,1, figsize=(10,9)) ax = axs[0] ax.set_title('xy center data converted to xy corners') ax.plot(data_fromnc_x,data_fromnc_y, linewidth=0.5, color='blue')
vars_pd, dims_pd = get_ncvardimlist(file_nc=file_nc) vars_pd.to_csv(os.path.join(dir_output, 'vars_pd.csv')) vars_pd_sel = vars_pd[vars_pd['long_name'].str.contains('transport')] #vars_pd_sel = vars_pd[vars_pd['dimensions'].str.contains('mesh2d_nFaces') & vars_pd['long_name'].str.contains('wave')] ugrid = get_netdata(file_nc=file_nc) varname = 'mesh2d_mor_bl' var_clims = [-50, 0] var_longname = vars_pd['long_name'][vars_pd['nc_varkeys'] == varname].iloc[0] fig, axs = plt.subplots(3, 1, figsize=(6, 9)) fig.suptitle('%s (%s)' % (varname, var_longname)) ax = axs[0] data_frommap_0 = get_ncmodeldata(file_nc=file_nc, varname=varname, timestep=0, get_linkedgridinfo=True) pc = plot_netmapdata(ugrid.verts, values=data_frommap_0.flatten(), ax=ax, linewidth=0.5, cmap='jet', clim=var_clims) cbar = fig.colorbar(pc, ax=ax) cbar.set_label( '%s (%s)' % (data_frommap_0.var_varname, data_frommap_0.var_ncattrs['units'])) ax.set_title('t=0 (%s)' % (data_frommap_0.var_times.iloc[0])) ax = axs[1] data_frommap_end = get_ncmodeldata(file_nc=file_nc,
[86606.61594052, 434685.69068637], [88689.74425466, 435355.26764449], [90772.8725688, 434983.28044554], [91926.03288556, 435132.07532512] ]) val_ylim = None clim_bl = [-10, 10] #optimize_dist = 150 else: raise Exception('ERROR: no settings provided for this mapfile') ugrid = get_netdata(file_nc=file_nc, multipart=multipart) #get bedlevel and create plot with ugrid and cross section line data_frommap_bl = get_ncmodeldata(file_nc=file_nc, varname='mesh2d_flowelem_bl', multipart=multipart) fig, ax_input = plt.subplots() pc = plot_netmapdata(ugrid.verts, values=data_frommap_bl, ax=ax_input, linewidth=0.5, edgecolors='face', cmap='jet') #, color='crimson', facecolor="None") pc.set_clim(clim_bl) fig.colorbar(pc, ax=ax_input) ax_input.set_aspect('equal') if 0: #click interactive polygon #pol_frominput = Polygon.frominteractive(ax) #this is old, does not work, use code below line, = ax_input.plot([], [], 'o-') # empty line linebuilder = LineBuilder(
import numpy as np import matplotlib.pyplot as plt plt.close('all') from dfm_tools.get_nc import get_netdata, get_ncmodeldata, plot_netmapdata from dfm_tools.get_nc_helpers import get_ncvardimlist, get_hisstationlist #, get_varname_fromnc from dfm_tools.io.polygon import Polygon dir_testinput = r'c:\DATA\dfm_tools_testdata' dir_output = '.' #print gridinfo of several files to compare file_nc = r'p:\1204257-dcsmzuno\2014\data\meteo\HIRLAM72_2018\h72_201803.nc' print('\nfile = %s' % (file_nc)) data_dummy = get_ncmodeldata(file_nc=file_nc, varname='northward_wind', timestep=0, get_linkedgridinfo=True) file_nc = r'p:\archivedprojects\1220688-lake-kivu\2_data\COSMO\COSMOCLM_2012_out02_merged_4Wouter.nc' print('\nfile = %s' % (file_nc)) data_dummy = get_ncmodeldata(file_nc=file_nc, varname='U_10M', timestep=0, get_linkedgridinfo=True) file_nc = r'p:\11200665-c3s-codec\2_Hydro\ECWMF_meteo\meteo\ERA-5\2000\ERA5_metOcean_atm_19991201_19991231.nc' print('\nfile = %s' % (file_nc)) data_dummy = get_ncmodeldata(file_nc=file_nc, varname='msl', timestep=0, get_linkedgridinfo=True) file_nc = r'p:\11202255-sfincs\Testbed\Original_tests\01_Implementation\08_restartfile\sfincs_map.nc' print('\nfile = %s' % (file_nc))