Skip to content

Commit

Permalink
Allow groups in netcdf files
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuntz committed Jan 13, 2024
1 parent 70ed90b commit 2bf6676
Show file tree
Hide file tree
Showing 10 changed files with 907 additions and 539 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Changelog
---------

v4.2 (??? 2024)
v4.2 (Jan 2024)
* Use local copy of `tooltip.py` from idle.
* Allow groups in netcdf files.
* Made ``ncvue`` work with newer matplotlib versions, updating
colormaps and using matplotlib.pyplot.style 'seaborn-v0_8-dark'.
* Made ``ncvue`` work with newer Tcl/Tk versions (ttk.Style.theme_use).
Expand Down
48 changes: 26 additions & 22 deletions src/ncvue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,54 @@
* v4.1, final add_cyclic and has_cyclic as committed to cartopy,
Nov 2021, Matthias Cuntz
* v4.1.2, ncvue is gui_script entry_point, Jun 2022, Matthias Cuntz
* v4.2, allow groups in netcdf files, Jan 2024, Matthias Cuntz
"""
# version, author
try:
from ._version import __version__
except ImportError: # pragma: nocover
# package is not installed
__version__ = "0.0.0.dev0"
__author__ = "Matthias Cuntz"

# helper functions
# copy from idle
from .tooltip import TooltipBase, OnHoverTooltipBase, Hovertip
# general helper function
from .ncvutils import DIMMETHODS
from .ncvutils import add_cyclic, has_cyclic, clone_ncvmain
from .ncvutils import format_coord_contour, format_coord_map
from .ncvutils import format_coord_scatter, get_slice
from .ncvutils import list_intersection, set_axis_label, set_miss
from .ncvutils import spinbox_values, vardim2var, zip_dim_name_length

from .ncvutils import DIMMETHODS
from .ncvutils import add_cyclic, has_cyclic, clone_ncvmain
from .ncvutils import format_coord_contour, format_coord_map
from .ncvutils import format_coord_scatter, get_slice
from .ncvutils import list_intersection, set_axis_label, set_miss
from .ncvutils import spinbox_values, vardim2var, zip_dim_name_length
#
# common methods of all panels
from .ncvmethods import analyse_netcdf, get_miss, get_slice_miss
from .ncvmethods import set_dim_lat, set_dim_lon, set_dim_var
from .ncvmethods import set_dim_x, set_dim_y, set_dim_y2, set_dim_z

#
# adding widgets with labels, etc.
from .ncvwidgets import Tooltip
from .ncvwidgets import add_checkbutton, add_combobox, add_entry, add_imagemenu
from .ncvwidgets import add_menu, add_scale, add_spinbox, add_tooltip

#
# scatter/line panel
from .ncvscatter import ncvScatter
# contour panel
from .ncvcontour import ncvContour
# map panel
from .ncvmap import ncvMap
from .ncvmap import ncvMap
# main window with panels
from .ncvmain import ncvMain

from .ncvmain import ncvMain
#
# main calling program
from .ncvue import ncvue
#
# version, author
try:
from ._version import __version__
except ImportError: # pragma: nocover
# package is not installed
__version__ = "0.0.0.dev0"
__author__ = "Matthias Cuntz"


__all__ = ["DIMMETHODS",
"add_cyclic", "clone_ncvmain",
__all__ = ['TooltipBase', 'OnHoverTooltipBase', 'Hovertip',
"DIMMETHODS",
"add_cyclic", "has_cyclic", "clone_ncvmain",
"format_coord_contour", "format_coord_map",
"format_coord_scatter", "get_slice",
"list_intersection", "set_axis_label", "set_miss",
Expand Down
41 changes: 21 additions & 20 deletions src/ncvue/ncvcontour.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* Write coordinates and value on bottom of plotting canvas,
May 2021, Matthias Cuntz
* Address fi.variables[name] directly by fi[name], Jan 2024, Matthias Cuntz
* Allow groups in netcdf files, Jan 2024, Matthias Cuntz
"""
from __future__ import absolute_import, division, print_function
Expand Down Expand Up @@ -370,17 +371,17 @@ def newnetcdf(self):
if self.top.fi:
self.top.fi.close()
# reset empty defaults of top
self.top.dunlim = '' # name of unlimited dimension
self.top.time = None # datetime variable
self.top.tname = '' # datetime variable name
self.top.tvar = '' # datetime variable name in netcdf
self.top.dtime = None # decimal year
self.top.latvar = '' # name of latitude variable
self.top.lonvar = '' # name of longitude variable
self.top.latdim = '' # name of latitude dimension
self.top.londim = '' # name of longitude dimension
self.top.maxdim = 0 # maximum num of dims of all variables
self.top.cols = [] # variable list
self.top.dunlim = [] # name of unlimited dimension
self.top.time = [] # datetime variable
self.top.tname = [] # datetime variable name
self.top.tvar = [] # datetime variable name in netcdf
self.top.dtime = [] # decimal year
self.top.latvar = [] # name of latitude variable
self.top.lonvar = [] # name of longitude variable
self.top.latdim = [] # name of latitude dimension
self.top.londim = [] # name of longitude dimension
self.top.maxdim = 0 # maximum num of dims of all variables
self.top.cols = [] # variable list
# open new netcdf file
self.top.fi = nc.Dataset(ncfile, 'r')
analyse_netcdf(self.top)
Expand Down Expand Up @@ -611,13 +612,13 @@ def redraw(self):
if (z != ''):
# z axis
gz, vz = vardim2var(z, self.fi.groups.keys())
if vz == self.tname:
if vz == self.tname[gz]:
# should throw an error later
if mesh:
zz = self.dtime
zz = self.dtime[gz]
zlab = 'Year'
else:
zz = self.time
zz = self.time[gz]
zlab = 'Date'
else:
zz = self.fi[vz]
Expand All @@ -630,12 +631,12 @@ def redraw(self):
if (y != ''):
# y axis
gy, vy = vardim2var(y, self.fi.groups.keys())
if vy == self.tname:
if vy == self.tname[gy]:
if mesh:
yy = self.dtime
yy = self.dtime[gy]
ylab = 'Year'
else:
yy = self.time
yy = self.time[gy]
ylab = 'Date'
else:
yy = self.fi[vy]
Expand All @@ -644,12 +645,12 @@ def redraw(self):
if (x != ''):
# x axis
gx, vx = vardim2var(x, self.fi.groups.keys())
if vx == self.tname:
if vx == self.tname[gx]:
if mesh:
xx = self.dtime
xx = self.dtime[gx]
xlab = 'Year'
else:
xx = self.time
xx = self.time[gx]
xlab = 'Date'
else:
xx = self.fi[vx]
Expand Down
18 changes: 12 additions & 6 deletions src/ncvue/ncvmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* Written Nov-Dec 2020 by Matthias Cuntz (mc (at) macu (dot) de)
* Added check_new_netcdf method that re-initialises all panels if netcdf
file changed, Jan 2021, Matthias Cuntz
* Address fi.variables[name] directly by fi[name], Jan 2024, Matthias Cuntz
* Allow groups in netcdf files, Jan 2024, Matthias Cuntz
"""
from __future__ import absolute_import, division, print_function
Expand Down Expand Up @@ -80,13 +82,17 @@ def __init__(self, master, **kwargs):
self.tab_map = ncvMap(self)

mapfirst = False
if self.top.latvar:
gl, vl = vardim2var(self.top.latvar, self.top.fi.groups.keys())
if np.prod(self.top.fi.variables[vl].shape) > 1:
if any(self.top.latvar):
idx = [ i for i, l in enumerate(self.top.latvar) if l ]
gl, vl = vardim2var(self.top.latvar[idx[0]],
self.top.fi.groups.keys())
if np.prod(self.top.fi[vl].shape) > 1:
mapfirst = True
if self.top.lonvar:
gl, vl = vardim2var(self.top.lonvar, self.top.fi.groups.keys())
if np.prod(self.top.fi.variables[vl].shape) > 1:
if any(self.top.lonvar):
idx = [ i for i, l in enumerate(self.top.lonvar) if l ]
gl, vl = vardim2var(self.top.lonvar[idx[0]],
self.top.fi.groups.keys())
if np.prod(self.top.fi[vl].shape) > 1:
mapfirst = True

if mapfirst:
Expand Down
Loading

0 comments on commit 2bf6676

Please sign in to comment.