Skip to content

Commit 4beb12a

Browse files
committed
Review updates
1 parent 7e1c02e commit 4beb12a

12 files changed

+13432
-3233
lines changed

notebooks/3-2-dealiasing-online.ipynb

Lines changed: 1565 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/3-2-dealiasing.ipynb

Lines changed: 1967 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/JAMES_figures-review.ipynb

Lines changed: 3364 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/JAMES_figures.ipynb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@
44
"cell_type": "code",
55
"execution_count": 2,
66
"metadata": {},
7-
"outputs": [
8-
{
9-
"name": "stderr",
10-
"output_type": "stream",
11-
"text": [
12-
"/ext3/miniconda3/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
13-
" from .autonotebook import tqdm as notebook_tqdm\n"
14-
]
15-
}
16-
],
7+
"outputs": [],
178
"source": [
189
"import xarray as xr\n",
1910
"import numpy as np\n",
@@ -2644,7 +2635,7 @@
26442635
],
26452636
"metadata": {
26462637
"kernelspec": {
2647-
"display_name": "env_07_04",
2638+
"display_name": "environment_13_Jul_2023",
26482639
"language": "python",
26492640
"name": "my_env"
26502641
},
@@ -2658,7 +2649,7 @@
26582649
"name": "python",
26592650
"nbconvert_exporter": "python",
26602651
"pygments_lexer": "ipython3",
2661-
"version": "3.9.12"
2652+
"version": "3.11.4"
26622653
},
26632654
"vscode": {
26642655
"interpreter": {

notebooks/Shallow_CNNs.ipynb

Lines changed: 1059 additions & 4 deletions
Large diffs are not rendered by default.

notebooks/generalization.ipynb

Lines changed: 1327 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/grid_convergence.ipynb

Lines changed: 190 additions & 3203 deletions
Large diffs are not rendered by default.

notebooks/hires-subgrid-forcing.ipynb

Lines changed: 1974 additions & 0 deletions
Large diffs are not rendered by default.

notebooks/molecular-viscosity.ipynb

Lines changed: 1881 additions & 0 deletions
Large diffs are not rendered by default.

pyqg_generative/tools/comparison_tools.py

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ def coarsegrain_reference_dataset(ds, resolution, operator):
7575
operator = op.Operator2
7676
elif operator == 'Operator4':
7777
operator = op.Operator4
78+
elif operator == 'Operator5':
79+
operator = op.Operator5
7880
else:
7981
raise ValueError('operator must be Operator1 or Operator2')
8082

8183
dsf = xr.Dataset()
8284
for var in ['q', 'u', 'v', 'psi']:
85+
print(f'var={var}')
8386
dsf[var] = operator(ds[var], resolution)
8487

8588
# Coarsegrain spectral flux
@@ -191,22 +194,98 @@ def compute_Eflux(ds):
191194

192195
return normalized_differences, differences, scales
193196

197+
def dataset_statistics(ds, delta=0.25, **kw_ispec):
198+
'''
199+
If path is given, the dataset is returned as is
200+
If dataset is given, statistics are computed
201+
'''
202+
if isinstance(ds, str):
203+
ds = xr.open_mfdataset(ds, combine='nested', concat_dim='run', decode_times=False, chunks={'time':1, 'run':1})
204+
if 'years' not in ds['time'].attrs:
205+
ds['time'] = ds['time'] / 360
206+
ds['time'].attrs = {'long_name':'time [$years$]'}
207+
return ds
208+
209+
def KE(ds):
210+
return (ds.u**2 + ds.v**2) * 0.5
211+
212+
def KE_time(ds):
213+
if 'run' in ds.dims:
214+
dims = ['run', 'x', 'y']
215+
else:
216+
dims = ['x', 'y']
217+
return op.ave_lev(KE(ds), delta=delta).mean(dims)
218+
219+
stats = xr.Dataset()
220+
221+
m = pyqg.QGModel(nx=len(ds.x), log_level=0)
222+
for key in ['APEflux', 'APEgenspec', 'Dissspec', 'ENSDissspec',
223+
'ENSflux', 'ENSfrictionspec', 'ENSgenspec', 'ENSparamspec',
224+
'Ensspec', 'KEflux', 'KEfrictionspec', 'KEspec', 'entspec',
225+
'paramspec', 'paramspec_APEflux', 'paramspec_KEflux']:
226+
if key not in ds.keys():
227+
continue
228+
var = ds[key]
229+
if 'run' in ds.dims:
230+
var = var.mean(dim='run')
231+
if 'lev' in var.dims:
232+
sps = []
233+
for z in [0,1]:
234+
k, sp = calc_ispec(m, var.isel(lev=z).values, **kw_ispec)
235+
sps.append(sp)
236+
sp = np.stack(sps, axis=0)
237+
stats[key+'r'] = \
238+
xr.DataArray(sp, dims=['lev', 'kr'],
239+
coords=[[1,2], coord(k, 'wavenumber, $m^{-1}$')])
240+
241+
var_mean = op.ave_lev(var, delta)
242+
k, sp = calc_ispec(m, var_mean.values, **kw_ispec)
243+
stats[key+'r_mean'] = \
244+
xr.DataArray(sp, dims=['kr'],
245+
coords=[coord(k, 'wavenumber, $m^{-1}$')])
246+
else:
247+
k, sp = calc_ispec(m, var.values, **kw_ispec)
248+
stats[key+'r'] = \
249+
xr.DataArray(sp, dims=['kr'],
250+
coords=[coord(k, 'wavenumber, $m^{-1}$')])
251+
252+
budget_sum = 0
253+
for key in ['KEfluxr', 'APEfluxr', 'APEgenspecr', 'KEfrictionspecr',
254+
'paramspec_APEfluxr', 'paramspec_KEfluxr']:
255+
if key in stats.keys():
256+
budget_sum += stats[key]
257+
stats['Energysumr'] = budget_sum
258+
259+
Eflux = 0
260+
for key in ['KEfluxr', 'APEfluxr', 'paramspec_KEfluxr', 'paramspec_APEfluxr']:
261+
if key in stats.keys():
262+
Eflux = Eflux + stats[key]
263+
stats['Efluxr'] = Eflux
264+
265+
stats['KE_time'] = KE_time(ds)
266+
267+
if 'years' not in stats['time'].attrs:
268+
stats['time'] = stats['time'] / 360
269+
stats['time'].attrs = {'long_name':'time [$years$]'}
270+
271+
return stats
272+
194273
def cache_path(path):
195274
dir = os.path.dirname(path)
196275
files = os.path.basename(path)
197276
#https://www.delftstack.com/howto/python/str-to-hex-python/
198277
cachename = files.encode('utf-8').hex() + '.cache_netcdf'
199278
return os.path.join(dir,cachename)
200279

201-
def dataset_smart_read(path, delta=0.25, read_cache=True):
280+
def dataset_smart_read(path, delta=0.25, read_cache=True, compute_all=True):
202281
#print(path)
203282
nfiles = len(glob.glob(path))
204283
#if nfiles < 10:
205284
#print('Warning! Computations are unstable. Number of files is less than 10 and equal to', nfiles)
206285
cache = cache_path(path)
207286
if os.path.exists(cache) and read_cache:
208287
#print('Read cache ' + cache)
209-
ds1 = xr.open_mfdataset(path, combine='nested', concat_dim='run', decode_times=False)
288+
ds1 = xr.open_mfdataset(path, combine='nested', concat_dim='run', decode_times=False, chunks={'time':1, 'run':1})
210289
ds2 = xr.open_dataset(cache)
211290
ds1['time'] = ds1['time'] / 360
212291
ds1['time'].attrs = {'long_name':'time [$years$]'}
@@ -216,7 +295,7 @@ def dataset_smart_read(path, delta=0.25, read_cache=True):
216295
#print('Delete cache ' + cache)
217296
os.remove(cache)
218297

219-
ds = xr.open_mfdataset(path, combine='nested', concat_dim='run', decode_times=False)
298+
ds = xr.open_mfdataset(path, combine='nested', concat_dim='run', decode_times=False, chunks={'time':1, 'run':1})
220299
ds['time'] = ds['time'] / 360
221300
ds['time'].attrs = {'long_name':'time [$years$]'}
222301

@@ -238,13 +317,18 @@ def KE_time(ds):
238317
def Vabs(ds):
239318
return np.sqrt(2*KE(ds))
240319

241-
stats['omega'] = relative_vorticity(ds)
242-
stats['KE'] = KE(ds)
243-
stats['Ens'] = Ens(ds)
244-
stats['Vabs'] = Vabs(ds)
320+
if compute_all:
321+
stats['omega'] = relative_vorticity(ds)
322+
stats['KE'] = KE(ds)
323+
stats['Ens'] = Ens(ds)
324+
stats['Vabs'] = Vabs(ds)
245325

246326
def PDF_var(ds, var, lev):
247-
ds_ = ds.isel(time=AVERAGE_SLICE_ANDREW).isel(lev=lev)
327+
if compute_all:
328+
time = AVERAGE_SLICE_ANDREW
329+
else:
330+
time = slice(-1, None)
331+
ds_ = ds.isel(time=time).isel(lev=lev)
248332
if var == 'KE':
249333
values = KE(ds_)
250334
elif var == 'Ens':
@@ -268,7 +352,12 @@ def PDF_var(ds, var, lev):
268352
points, density = PDF_histogram(values, xmin=xmin, xmax=xmax)
269353
return xr.DataArray(density, dims=f'{var}_{lev}', coords=[points])
270354

271-
for var in ['q', 'u', 'v', 'KE', 'Ens']:
355+
if compute_all:
356+
variables = ['q', 'u', 'v', 'KE', 'Ens']
357+
else:
358+
variables = ['q', 'u', 'v', 'KE']
359+
360+
for var in variables:
272361
for lev in [0,1]:
273362
stats[f'PDF_{var}{lev+1}'] = PDF_var(ds, var, lev)
274363

0 commit comments

Comments
 (0)