Skip to content

Commit 70ed90b

Browse files
committed
Forst preparation for allowing groups in netcdf files
1 parent c7aa88c commit 70ed90b

File tree

6 files changed

+160
-528
lines changed

6 files changed

+160
-528
lines changed

src/ncvue/ncvcontour.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Jan 2021, Matthias Cuntz
2525
* Write coordinates and value on bottom of plotting canvas,
2626
May 2021, Matthias Cuntz
27+
* Address fi.variables[name] directly by fi[name], Jan 2024, Matthias Cuntz
2728
2829
"""
2930
from __future__ import absolute_import, division, print_function
@@ -234,7 +235,7 @@ def __init__(self, master, **kwargs):
234235
self.xd.append(xd)
235236
self.xdtip.append(xdtip)
236237
# y-axis selection
237-
spacex = ttk.Label(self.rowxy, text=" "*3)
238+
spacex = ttk.Label(self.rowxy, text=" " * 3)
238239
spacex.pack(side=tk.LEFT)
239240
self.blocky = ttk.Frame(self.rowxy)
240241
self.blocky.pack(side=tk.LEFT)
@@ -609,7 +610,7 @@ def redraw(self):
609610
vz = 'None'
610611
if (z != ''):
611612
# z axis
612-
vz = vardim2var(z)
613+
gz, vz = vardim2var(z, self.fi.groups.keys())
613614
if vz == self.tname:
614615
# should throw an error later
615616
if mesh:
@@ -619,7 +620,7 @@ def redraw(self):
619620
zz = self.time
620621
zlab = 'Date'
621622
else:
622-
zz = self.fi.variables[vz]
623+
zz = self.fi[vz]
623624
zlab = set_axis_label(zz)
624625
zz = get_slice_miss(self, self.zd, zz)
625626
# both contourf and pcolormesh assume (row,col),
@@ -628,7 +629,7 @@ def redraw(self):
628629
zz = zz.T
629630
if (y != ''):
630631
# y axis
631-
vy = vardim2var(y)
632+
gy, vy = vardim2var(y, self.fi.groups.keys())
632633
if vy == self.tname:
633634
if mesh:
634635
yy = self.dtime
@@ -637,12 +638,12 @@ def redraw(self):
637638
yy = self.time
638639
ylab = 'Date'
639640
else:
640-
yy = self.fi.variables[vy]
641+
yy = self.fi[vy]
641642
ylab = set_axis_label(yy)
642643
yy = get_slice_miss(self, self.yd, yy)
643644
if (x != ''):
644645
# x axis
645-
vx = vardim2var(x)
646+
gx, vx = vardim2var(x, self.fi.groups.keys())
646647
if vx == self.tname:
647648
if mesh:
648649
xx = self.dtime
@@ -651,7 +652,7 @@ def redraw(self):
651652
xx = self.time
652653
xlab = 'Date'
653654
else:
654-
xx = self.fi.variables[vx]
655+
xx = self.fi[vx]
655656
xlab = set_axis_label(xx)
656657
xx = get_slice_miss(self, self.xd, xx)
657658
# set z to nan if not selected

src/ncvue/ncvmain.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ def __init__(self, master, **kwargs):
8181

8282
mapfirst = False
8383
if self.top.latvar:
84-
vl = vardim2var(self.top.latvar)
84+
gl, vl = vardim2var(self.top.latvar, self.top.fi.groups.keys())
8585
if np.prod(self.top.fi.variables[vl].shape) > 1:
8686
mapfirst = True
8787
if self.top.lonvar:
88-
vl = vardim2var(self.top.lonvar)
88+
gl, vl = vardim2var(self.top.lonvar, self.top.fi.groups.keys())
8989
if np.prod(self.top.fi.variables[vl].shape) > 1:
9090
mapfirst = True
9191

src/ncvue/ncvmap.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Larger pad for colorbar, Jun 2021, Matthias Cuntz
2929
* Work with files without an unlimited (time) dimension (set_tstep),
3030
Oct 2021, Matthias Cuntz
31+
* Address fi.variables[name] directly by fi[name], Jan 2024, Matthias Cuntz
3132
3233
"""
3334
from __future__ import absolute_import, division, print_function
@@ -42,8 +43,8 @@
4243
import os
4344
import numpy as np
4445
import netCDF4 as nc
45-
from .ncvutils import add_cyclic, clone_ncvmain, format_coord_map
46-
from .ncvutils import set_axis_label, set_miss, vardim2var
46+
from .ncvutils import add_cyclic, clone_ncvmain, format_coord_map
47+
from .ncvutils import set_axis_label, set_miss, vardim2var
4748
from .ncvmethods import analyse_netcdf, get_slice_miss, get_miss
4849
from .ncvmethods import set_dim_lon, set_dim_lat, set_dim_var
4950
from .ncvwidgets import add_checkbutton, add_combobox, add_entry, add_imagemenu
@@ -118,7 +119,7 @@ def __init__(self, master, **kwargs):
118119
command=self.newnetcdf)
119120
self.newfile.pack(side=tk.LEFT)
120121
self.newfiletip = add_tooltip(self.newfile, 'Open a new netcdf file')
121-
spacew = ttk.Label(self.rowwin, text=" "*3)
122+
spacew = ttk.Label(self.rowwin, text=" " * 3)
122123
spacew.pack(side=tk.LEFT)
123124
time_label1 = ttk.Label(self.rowwin, text='Time: ')
124125
time_label1.pack(side=tk.LEFT)
@@ -197,7 +198,7 @@ def __init__(self, master, **kwargs):
197198
self.rowt, label="step", ini=0, from_=0, to=ntime,
198199
length=100, orient=tk.HORIZONTAL, command=self.tstep_t,
199200
tooltip="Slide to go to time step")
200-
spacet = ttk.Label(self.rowt, text=" "*1)
201+
spacet = ttk.Label(self.rowt, text=" " * 1)
201202
spacet.pack(side=tk.LEFT)
202203
# first t
203204
self.first_t = ttk.Button(self.rowt, text="|<<", width=3,
@@ -235,7 +236,7 @@ def __init__(self, master, **kwargs):
235236
self.last_t.pack(side=tk.LEFT)
236237
self.last_ttip = add_tooltip(self.last_t, 'Last time step')
237238
# repeat
238-
spacer = ttk.Label(self.rowt, text=" "*1)
239+
spacer = ttk.Label(self.rowt, text=" " * 1)
239240
spacer.pack(side=tk.LEFT)
240241
reps = ['once', 'repeat', 'reflect']
241242
tstr = "Run time steps once, repeat from start when at end,"
@@ -246,7 +247,7 @@ def __init__(self, master, **kwargs):
246247
self.repeat.set('repeat')
247248
self.last_t.pack(side=tk.LEFT)
248249
# delay
249-
spaced = ttk.Label(self.rowt, text=" "*1)
250+
spaced = ttk.Label(self.rowt, text=" " * 1)
250251
spaced.pack(side=tk.LEFT)
251252
tstr = "Delay run between time steps from 1 to 1000 ms"
252253
self.delaylbl, self.delayval, self.delay, self.delaytip = add_scale(
@@ -282,7 +283,7 @@ def __init__(self, master, **kwargs):
282283
self.rowv, label="transpose var", value=False,
283284
command=self.checked,
284285
tooltip="Transpose array, i.e. exchanging lat and lon")
285-
spacev = ttk.Label(self.rowv, text=" "*1)
286+
spacev = ttk.Label(self.rowv, text=" " * 1)
286287
spacev.pack(side=tk.LEFT)
287288
self.vminlbl, self.vmin, self.vmintip = add_entry(
288289
self.rowv, label="vmin", text=0, width=11, command=self.entered_v,
@@ -350,7 +351,7 @@ def __init__(self, master, **kwargs):
350351
self.lond.append(lond)
351352
self.londtip.append(londtip)
352353
# lat-axis selection
353-
spacex = ttk.Label(self.rowll, text=" "*3)
354+
spacex = ttk.Label(self.rowll, text=" " * 3)
354355
spacex.pack(side=tk.LEFT)
355356
self.blocklat = ttk.Frame(self.rowll)
356357
self.blocklat.pack(side=tk.LEFT)
@@ -440,8 +441,8 @@ def __init__(self, master, **kwargs):
440441
# set global
441442
x = self.lon.get()
442443
if (x != ''):
443-
vx = vardim2var(x)
444-
xx = self.fi.variables[vx]
444+
gx, vx = vardim2var(x, self.fi.groups.keys())
445+
xx = self.fi[vx]
445446
xx = get_slice_miss(self, self.lond, xx)
446447
if np.any(np.isfinite(xx)):
447448
xx = (xx + 360.) % 360.
@@ -580,7 +581,7 @@ def next_t(self):
580581
Command called if next frame button was pressed.
581582
"""
582583
it = int(self.vdval[self.iunlim].get())
583-
if it < self.nunlim-1:
584+
if it < self.nunlim - 1:
584585
it += 1
585586
self.set_tstep(it)
586587
self.update(it, isframe=True)
@@ -806,10 +807,10 @@ def get_vminmax(self):
806807
from numpy.random import default_rng
807808
v = self.v.get()
808809
if (v != ''):
809-
vz = vardim2var(v)
810+
gz, vz = vardim2var(v, self.fi.groups.keys())
810811
if vz == self.tname:
811812
return (0, 1)
812-
vv = self.fi.variables[vz]
813+
vv = self.fi[vz]
813814
imiss = get_miss(self, vv)
814815
iall = self.vall.get()
815816
if iall or (np.sum(vv.shape[:-2]) < 50):
@@ -823,9 +824,9 @@ def get_vminmax(self):
823824
for nn in range(50):
824825
ss = []
825826
for i in range(vv.ndim):
826-
if i < vv.ndim-2:
827+
if i < vv.ndim - 2:
827828
idim = rng.integers(0, vv.shape[i])
828-
s = slice(idim, idim+1)
829+
s = slice(idim, idim + 1)
829830
else:
830831
s = slice(0, vv.shape[i])
831832
ss.append(s)
@@ -940,8 +941,8 @@ def reinit(self):
940941
set_dim_lon(self)
941942
x = self.lon.get()
942943
if (x != ''):
943-
vx = vardim2var(x)
944-
xx = self.fi.variables[vx]
944+
gx, vx = vardim2var(x, self.fi.groups.keys())
945+
xx = self.fi[vx]
945946
xx = get_slice_miss(self, self.lond, xx)
946947
xx = xx + 360.
947948
if (xx.max() - xx.min()) > 150.:
@@ -971,7 +972,7 @@ def set_unlim(self, v):
971972
Set index and length of unlimited dimension of variable `v`.
972973
973974
`v` (str) is the variable name as in the selection comboboxes, i.e.
974-
`var = vardim2var(self.fi.variables[v)]`.
975+
`gvar, var = vardim2var(self.fi[v)]`.
975976
976977
Sets `self.nunlim` to the length of the unlimited dimension and
977978
`self.iunlim` to the index in variable.dimensions if
@@ -980,21 +981,21 @@ def set_unlim(self, v):
980981
Takes `self.iunlim=0` and `self.nunlim=variable.shape[0]` if
981982
self.dunlim == ''` or `self.dunlim` not in var.dimensions.
982983
"""
983-
vz = vardim2var(v)
984+
gz, vz = vardim2var(v, self.fi.groups.keys())
984985
if vz == self.tname:
985986
self.iunlim = 0
986987
self.nunlim = self.time.size
987988
else:
988989
if self.dunlim:
989-
if self.dunlim in self.fi.variables[vz].dimensions:
990+
if self.dunlim in self.fi[vz].dimensions:
990991
self.iunlim = (
991-
self.fi.variables[vz].dimensions.index(self.dunlim))
992+
self.fi[vz].dimensions.index(self.dunlim))
992993
else:
993994
self.iunlim = 0
994995
else:
995996
self.iunlim = 0
996-
if self.fi.variables[vz].ndim > 0:
997-
self.nunlim = self.fi.variables[vz].shape[self.iunlim]
997+
if self.fi[vz].ndim > 0:
998+
self.nunlim = self.fi[vz].shape[self.iunlim]
998999
else:
9991000
self.nunlim = 0
10001001

@@ -1055,7 +1056,7 @@ def redraw(self):
10551056
vz = 'None'
10561057
if (v != ''):
10571058
# variable
1058-
vz = vardim2var(v)
1059+
gz, vz = vardim2var(v, self.fi.groups.keys())
10591060
if vz == self.tname:
10601061
# should throw an error later
10611062
if mesh:
@@ -1065,7 +1066,7 @@ def redraw(self):
10651066
vv = self.time
10661067
vlab = 'Date'
10671068
else:
1068-
vv = self.fi.variables[vz]
1069+
vv = self.fi[vz]
10691070
vlab = set_axis_label(vv)
10701071
vv = get_slice_miss(self, self.vd, vv)
10711072
if trans_v:
@@ -1076,7 +1077,7 @@ def redraw(self):
10761077
vlab = ''
10771078
if (y != ''):
10781079
# y axis
1079-
vy = vardim2var(y)
1080+
gy, vy = vardim2var(y, self.fi.groups.keys())
10801081
if vy == self.tname:
10811082
if mesh:
10821083
yy = self.dtime
@@ -1085,14 +1086,14 @@ def redraw(self):
10851086
yy = self.time
10861087
ylab = 'Date'
10871088
else:
1088-
yy = self.fi.variables[vy]
1089+
yy = self.fi[vy]
10891090
ylab = set_axis_label(yy)
10901091
yy = get_slice_miss(self, self.latd, yy)
10911092
else:
10921093
ylab = ''
10931094
if (x != ''):
10941095
# x axis
1095-
vx = vardim2var(x)
1096+
gx, vx = vardim2var(x, self.fi.groups.keys())
10961097
if vx == self.tname:
10971098
if mesh:
10981099
xx = self.dtime
@@ -1101,7 +1102,7 @@ def redraw(self):
11011102
xx = self.time
11021103
xlab = 'Date'
11031104
else:
1104-
xx = self.fi.variables[vx]
1105+
xx = self.fi[vx]
11051106
xlab = set_axis_label(xx)
11061107
xx = get_slice_miss(self, self.lond, xx)
11071108
# set central longitude of projection
@@ -1314,15 +1315,15 @@ def update(self, frame, isframe=False):
13141315
inv_lon = self.inv_lon.get()
13151316
inv_lat = self.inv_lat.get()
13161317
shift_lon = self.shift_lon.get()
1317-
vz = vardim2var(v)
1318+
gz, vz = vardim2var(v, self.fi.groups.keys())
13181319
if vz == self.tname:
13191320
vz = self.tvar
1320-
vv = self.fi.variables[vz]
1321+
vv = self.fi[vz]
13211322
# slice
13221323
try:
13231324
it = int(self.vdval[self.iunlim].get())
13241325
if not isframe:
1325-
if (self.anim_inc == 1) and (it == self.nunlim-1):
1326+
if (self.anim_inc == 1) and (it == self.nunlim - 1):
13261327
if rep == 'repeat':
13271328
it = 0
13281329
elif rep == 'reflect':
@@ -1353,7 +1354,7 @@ def update(self, frame, isframe=False):
13531354
if trans_v:
13541355
vv = vv.T
13551356
if shift_lon:
1356-
vv = np.roll(vv, vv.shape[1]//2, axis=1)
1357+
vv = np.roll(vv, vv.shape[1] // 2, axis=1)
13571358
self.ivv = vv
13581359
# set data
13591360
if mesh:

0 commit comments

Comments
 (0)