pp_smtnatl_dissipation#
[1]:
%matplotlib inline
%load_ext autoreload
%autoreload 2
[2]:
import pyicon as pyic
import xarray as xr
import glob
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import seawater as sw
import maps_icon_smt_temp as smt
import datetime
from matplotlib.patches import Rectangle
from icon_smt_levels import dzw, dzt, depthc, depthi
import dask
import dask.array as da
from dask.distributed import Client, LocalCluster
import multiprocessing
from dask.diagnostics import ProgressBar
-----calc
sys glob os
numpy
netcdf
Done modules calc.
-----calc_xr
sys glob os
numpy
netcdf
xarray
Done modules calc.
-----tb
sys
json
numpy
scipy
netcdf datetime
matplotlib
mybreak
pnadas
xarray
done xarray
-----IconData
-----plotting
-----accessor
-----view
-----calc
-----calc_xr
-----tb
-----IconData
-----plotting
-----accessor
-----view
-----quickplots
-----quickplots
[3]:
run = 'ngSMT_tke'
savefig = False
path_fig = '../pics/'
nnf=0
gname = 'smt'
lev = 'L128'
# path_data = f'/work/mh0287/users/leonidas/icon/ngSMT/results/????-??/'
path_data = '/work/mh0287/from_Mistral/mh0287/users/leonidas/icon/ngSMT/results/????-??/'
#fpath_tgrid = '/mnt/lustre01/work/mh0287/users/leonidas/icon/submeso/grid/cell_grid-OceanOnly_SubmesoNA_2500m_srtm30-icon.nc'
fpath_tgrid = '/home/m/m300602/work/icon/grids/smt/smt_tgrid.nc'
fpath_Tri = '/work/mh0033/m300602/tmp/Tri.pkl'
path_grid = f'/work/mh0033/m300602/icon/grids/{gname}/'
path_ckdtree = f'{path_grid}ckdtree/'
# fpath_ckdtree = f'{path_grid}ckdtree/rectgrids/{gname}_res0.30_180W-180E_90S-90N.npz'
fpath_ckdtree = '/work/mh0033/m300602/icon/grids/smt/ckdtree/rectgrids/smt_res0.02_180W-180E_90S-90N.npz'
[4]:
# time0 = np.datetime64('2010-02-23T19:00:00')
time0 = np.datetime64('2010-03-09T01:00:00')
lon_reg_1 = [-88, 0]
lat_reg_1 = [ 5, 68]
lon_reg_2 = [-75, -55]
# lat_reg_2 = [30, 40]
lat_reg_2 = [33, 43]
# lon_reg_3 = [-72, -68]
# lat_reg_3 = [34, 36]
# lon_reg_3 = [-69.5, -65.5]
# lat_reg_3 = [36, 38]
lon_reg_3 = [-68.5, -66.5]
lat_reg_3 = [36.5, 37.5]
lon_reg_4 = [-66., -64.]
lat_reg_4 = [41, 42]
depth0 = 50.
iz = (depthc<depth0).sum()
iz, depthc[iz]
[4]:
(16, 51.5)
Load the grid#
[5]:
ds_tg = xr.open_dataset(fpath_tgrid)
Crop grid#
[35]:
clon = ds_tg.clon.compute().data * 180./np.pi
clat = ds_tg.clat.compute().data * 180./np.pi
[36]:
lon_reg = [-75, -55]
lat_reg = [33, 43]
[38]:
%%time
ireg_c = np.where(
(clon>lon_reg[0]) & (clon<=lon_reg[1]) & (clat>lat_reg[0]) & (clat<=lat_reg[1])
)[0]
CPU times: user 135 ms, sys: 84.6 ms, total: 220 ms
Wall time: 219 ms
Do it step by step#
[42]:
%%time
vertex_of_cell = ds_tg.vertex_of_cell.compute().data[:,ireg_c].transpose()-1
edge_of_cell = ds_tg.edge_of_cell.compute().data[:,ireg_c].transpose()-1
ireg_e, inde = np.unique(edge_of_cell, return_index=True)
ireg_v, indv = np.unique(vertex_of_cell, return_index=True)
CPU times: user 1.26 s, sys: 553 ms, total: 1.81 s
Wall time: 1.92 s
[43]:
%%time
ds_tg_cut = xr.Dataset(coords=dict(
clon=ds_tg['clon'].data[ireg_c],
clat=ds_tg['clat'].data[ireg_c],
elon=ds_tg['elon'].data[ireg_e],
elat=ds_tg['elat'].data[ireg_e],
vlon=ds_tg['vlon'].data[ireg_v],
vlat=ds_tg['vlat'].data[ireg_v],
))
ds_tg_cut['ireg_e'] = xr.DataArray(ireg_e, dims=['edge'])
ds_tg_cut['ireg_v'] = xr.DataArray(ireg_v, dims=['vertex'])
CPU times: user 85.3 ms, sys: 40.9 ms, total: 126 ms
Wall time: 125 ms
[44]:
%%time
reindex_c = np.zeros_like(ds_tg.clon, dtype='int32')-1
reindex_c[ireg_c] = np.arange(ireg_c.size, dtype='int32')
reindex_e = np.zeros_like(ds_tg.elon, dtype='int32')-1
reindex_e[ireg_e] = np.arange(ireg_e.size, dtype='int32')
reindex_v = np.zeros_like(ds_tg.vlon, dtype='int32')-1
reindex_v[ireg_v] = np.arange(ireg_v.size, dtype='int32')
CPU times: user 131 ms, sys: 142 ms, total: 273 ms
Wall time: 273 ms
[51]:
%%time
var = 'vertex_of_cell'
da = ds_tg[var].data[:,ireg_c]-1
data = reindex_v[da.flatten()].reshape(da.shape)
ds_tg_cut[var] = xr.DataArray(data+1, dims=ds_tg[var].dims)
var = 'vertices_of_vertex'
da = ds_tg[var].data[:,ireg_v]-1
data = reindex_v[da.flatten()].reshape(da.shape)
ds_tg_cut[var] = xr.DataArray(data+1, dims=ds_tg[var].dims)
var = 'edge_of_cell'
da = ds_tg[var].data[:,ireg_c]-1
data = reindex_e[da.flatten()].reshape(da.shape)
ds_tg_cut[var] = xr.DataArray(data+1, dims=ds_tg[var].dims)
var = 'edges_of_vertex'
da = ds_tg[var].data[:,ireg_v]-1
data = reindex_e[da.flatten()].reshape(da.shape)
ds_tg_cut[var] = xr.DataArray(data+1, dims=ds_tg[var].dims)
var = 'adjacent_cell_of_edge'
da = ds_tg[var].data[:,ireg_e]-1
data = reindex_c[da.flatten()].reshape(da.shape)
ds_tg_cut[var] = xr.DataArray(data+1, dims=ds_tg[var].dims)
var = 'cells_of_vertex'
da = ds_tg[var].data[:,ireg_v]-1
data = reindex_c[da.flatten()].reshape(da.shape)
ds_tg_cut[var] = xr.DataArray(data+1, dims=ds_tg[var].dims)
CPU times: user 847 ms, sys: 1.53 s, total: 2.37 s
Wall time: 2.73 s
[60]:
%%time
cvars = ['dual_area', 'edge_orientation',
'cartesian_x_vertices', 'cartesian_y_vertices', 'cartesian_z_vertices']
# cvars = ['dual_area']
for var in cvars:
ds_tg_cut[var] = ds_tg[var].compute().isel(vertex=ireg_v)
CPU times: user 139 ms, sys: 1.14 s, total: 1.28 s
Wall time: 1.69 s
Use pyicon to do it all at once#
[61]:
%%time
ds_tg_cut = pyic.xr_crop_tgrid(ds_tg, ireg_c)
sys glob os
numpy
netcdf
xarray
Done modules calc.
find edges
cut coordinates
reindex
cut vertex variables
cut edge variables
cut cell variables
CPU times: user 3.65 s, sys: 13.4 s, total: 17.1 s
Wall time: 19.8 s
[62]:
ds_tg_cut
[62]:
<xarray.Dataset> Dimensions: (clon: 5615241, clat: 5615241, elon: 8428132, elat: 8428132, vlon: 2812772, vlat: 2812772, edge: 8428132, vertex: 2812772, nv: 3, cell: 5615241, ne: 6, nc: 2) Coordinates: * clon (clon) float64 -0.9602 -0.9602 ... -1.309 * clat (clat) float64 0.6375 0.6376 ... 0.6028 * elon (elon) float64 -0.9602 -0.9602 ... -1.309 * elat (elat) float64 0.6376 0.6375 ... 0.6028 * vlon (vlon) float64 -0.9601 -0.9603 ... -1.309 * vlat (vlat) float64 0.6376 0.6375 ... 0.6029 Dimensions without coordinates: edge, vertex, nv, cell, ne, nc Data variables: (12/34) ireg_e (edge) int32 50208 50209 ... 46040008 ireg_v (vertex) int32 16930 16931 ... 15384155 vertex_of_cell (nv, cell) int32 1 1 2 ... 2812762 2812772 vertices_of_vertex (ne, vertex) int32 2 1 2 ... 2812751 2812762 edge_of_cell (nv, cell) int32 1 4 6 ... 8428131 8428132 edges_of_vertex (ne, vertex) int32 1 1 2 ... 8428129 8428132 ... ... cell_area_p (cell) float64 3.17e+05 ... 4.242e+05 cell_sea_land_mask (cell) int32 -2 -2 -2 -2 -2 ... -2 -2 -2 -2 orientation_of_normal (nv, cell) int32 1 1 1 1 1 ... -1 1 -1 -1 -1 cell_circumcenter_cartesian_x (cell) float64 0.4607 0.4607 ... 0.2133 cell_circumcenter_cartesian_y (cell) float64 -0.6584 -0.6584 ... -0.7956 cell_circumcenter_cartesian_z (cell) float64 0.5952 0.5953 ... 0.567
Calculate coefficients#
[63]:
%%time
with ProgressBar():
ds_IcD = pyic.convert_tgrid_data(ds_tg_cut).compute()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File ~/miniconda3/envs/pyicon_py39_cartopy19/lib/python3.9/site-packages/xarray/core/dataset.py:1317, in Dataset._construct_dataarray(self, name)
1316 try:
-> 1317 variable = self._variables[name]
1318 except KeyError:
KeyError: 'edge_vertices'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
File <timed exec>:2, in <module>
File ~/pyicon/pyicon/pyicon_calc_xr.py:58, in convert_tgrid_data(ds_tg)
56 ds_IcD['vertices_of_vertex'] = ds_tg['vertices_of_vertex'].transpose()-1
57 ds_IcD['edges_of_vertex'] = ds_tg['edges_of_vertex'].transpose()-1
---> 58 ds_IcD['edge_vertices'] = ds_tg['edge_vertices'].transpose()-1
59 ds_IcD['adjacent_cell_of_edge'] = ds_tg['adjacent_cell_of_edge'].transpose()-1
60 ds_IcD['cells_of_vertex'] = ds_tg['cells_of_vertex'].transpose()-1
File ~/miniconda3/envs/pyicon_py39_cartopy19/lib/python3.9/site-packages/xarray/core/dataset.py:1410, in Dataset.__getitem__(self, key)
1408 return self.isel(**key)
1409 if utils.hashable(key):
-> 1410 return self._construct_dataarray(key)
1411 if utils.iterable_of_hashable(key):
1412 return self._copy_listed(key)
File ~/miniconda3/envs/pyicon_py39_cartopy19/lib/python3.9/site-packages/xarray/core/dataset.py:1319, in Dataset._construct_dataarray(self, name)
1317 variable = self._variables[name]
1318 except KeyError:
-> 1319 _, name, variable = _get_virtual_variable(self._variables, name, self.dims)
1321 needed_dims = set(variable.dims)
1323 coords: dict[Hashable, Variable] = {}
File ~/miniconda3/envs/pyicon_py39_cartopy19/lib/python3.9/site-packages/xarray/core/dataset.py:175, in _get_virtual_variable(variables, key, dim_sizes)
173 split_key = key.split(".", 1)
174 if len(split_key) != 2:
--> 175 raise KeyError(key)
177 ref_name, var_name = split_key
178 ref_var = variables[ref_name]
KeyError: 'edge_vertices'
[7]:
%%time
edge2cell_coeff_cc = pyic.xr_calc_edge2cell_coeff_cc(ds_IcD)
div_coeff = pyic.xr_calc_div_coeff(ds_IcD)
grad_coeff = pyic.xr_calc_grad_coeff(ds_IcD)
rot_coeff = ( ds_IcD.dual_edge_length[ds_IcD.edges_of_vertex]*ds_IcD.edge_orientation ) / ds_IcD.dual_area
CPU times: user 15 s, sys: 19 s, total: 34 s
Wall time: 34.8 s
Load the data#
[17]:
mfdset_kwargs = dict(combine='nested', concat_dim='time',
data_vars='minimal', coords='minimal', compat='override', join='override',
parallel=True,
)
[56]:
%%time
search_str = f'{run}_vn_sp_001-016_*.nc'
flist = np.array(glob.glob(path_data+search_str))
flist.sort()
ds = xr.open_mfdataset(flist, **mfdset_kwargs, chunks=dict(time=1))
CPU times: user 1.75 s, sys: 1.06 s, total: 2.82 s
Wall time: 2.07 s
[57]:
ds
[57]:
<xarray.Dataset> Dimensions: (time: 984, ncells: 89813639) Coordinates: * time (time) float64 2.01e+07 2.01e+07 2.01e+07 ... 2.01e+07 2.01e+07 Dimensions without coordinates: ncells Data variables: (12/16) vn001_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn002_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn003_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn004_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn005_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn006_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> ... ... vn011_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn012_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn013_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn014_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn015_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn016_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> Attributes: CDI: Climate Data Interface version 1.8.2 (http://mpimet... Conventions: CF-1.6 number_of_grid_used: 42 uuidOfHGrid: 0d39853e-c26b-11e9-8454-0b16a6d45f73 institution: Max Planck Institute for Meteorology/Deutscher Wett... title: ICON simulation source: git@gitlab.dkrz.de:icon/icon-oes.git@e7e7a45736e586... history: /work/mh0287/users/leonidas/icon/icon-oes_ngSMT/int... references: see MPIM/DWD publications comment: Leonidas Linardakis (m300056) on m20180 (Linux 2.6....
[59]:
depthc_xr = xr.DataArray(depthc[:15], dims=['depth'])
depthc_xr
[59]:
<xarray.DataArray (depth: 15)> array([ 2.5, 6.5, 9.5, 12.5, 15.5, 18.5, 21.5, 24.5, 27.5, 30.5, 33.5, 36.5, 39.5, 42.5, 45.5]) Dimensions without coordinates: depth
[60]:
ds
[60]:
<xarray.Dataset> Dimensions: (time: 984, ncells: 89813639) Coordinates: * time (time) float64 2.01e+07 2.01e+07 2.01e+07 ... 2.01e+07 2.01e+07 Dimensions without coordinates: ncells Data variables: (12/16) vn001_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn002_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn003_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn004_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn005_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn006_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> ... ... vn011_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn012_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn013_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn014_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn015_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> vn016_sp (time, ncells) float32 dask.array<chunksize=(1, 89813639), meta=np.ndarray> Attributes: CDI: Climate Data Interface version 1.8.2 (http://mpimet... Conventions: CF-1.6 number_of_grid_used: 42 uuidOfHGrid: 0d39853e-c26b-11e9-8454-0b16a6d45f73 institution: Max Planck Institute for Meteorology/Deutscher Wett... title: ICON simulation source: git@gitlab.dkrz.de:icon/icon-oes.git@e7e7a45736e586... history: /work/mh0287/users/leonidas/icon/icon-oes_ngSMT/int... references: see MPIM/DWD publications comment: Leonidas Linardakis (m300056) on m20180 (Linux 2.6....
[70]:
%%time
ve = xr.concat([
ds['vn001_sp'], ds['vn002_sp'], ds['vn003_sp'], ds['vn004_sp'],
ds['vn005_sp'], ds['vn006_sp'], ds['vn007_sp'], ds['vn008_sp'],
ds['vn009_sp'], ds['vn010_sp'], ds['vn011_sp'], ds['vn012_sp'],
ds['vn013_sp'], ds['vn014_sp'], ds['vn015_sp'],# ds['vn016_sp'],
], dim=depthc_xr[:15])
CPU times: user 119 ms, sys: 168 ms, total: 286 ms
Wall time: 286 ms
[73]:
ve.dims
[73]:
('depth', 'time', 'ncells')
[75]:
ds_IcD.edges_of_vertex.dims
[75]:
('vertex', 'ne')
[80]:
ve0 = ve.isel(depth=0, time=0)
[82]:
%%time
curl_v = (ve0.isel(ncells=ds_IcD.edges_of_vertex))# * rot_coeff)
KeyboardInterrupt
[ ]: