Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing logging in mswms #2594

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions mslib/mswms/dataaccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@
from abc import ABCMeta, abstractmethod
import itertools
import os
import logging
import netCDF4
import numpy as np
import pint

from mslib.utils import netCDF4tools
from mslib.utils import netCDF4tools, LOGGER
from mslib.utils.units import units


Expand Down Expand Up @@ -230,7 +229,7 @@ def _determine_filename(self, variable, vartype, init_time, valid_time, reload=T
self.setup()
return self._determine_filename(variable, vartype, init_time, valid_time, reload=False)
else:
logging.error("Could not identify filename. %s %s %s %s %s %s",
LOGGER.error("Could not identify filename. %s %s %s %s %s %s",
variable, vartype, init_time, valid_time, type(ex), ex)
raise ValueError(f"Variable '{variable}' not available for type '{vartype}', "
f"init_time '{init_time}', and valid_time '{valid_time}'")
Expand Down Expand Up @@ -283,18 +282,18 @@ def _parse_file(self, filename):
if (ncvar.dimensions[0] != time_name or
ncvar.dimensions[-2] != lat_name or
ncvar.dimensions[-1] != lon_name):
logging.error("Skipping variable '%s' in file '%s': Incorrect order of dimensions",
LOGGER.error("Skipping variable '%s' in file '%s': Incorrect order of dimensions",
ncvarname, filename)
continue
if not hasattr(ncvar, "units"):
logging.error("Skipping variable '%s' in file '%s': No units attribute",
LOGGER.error("Skipping variable '%s' in file '%s': No units attribute",
ncvarname, filename)
continue
if ncvar.standard_name != "time":
try:
units(ncvar.units)
except (AttributeError, ValueError, pint.UndefinedUnitError, pint.DefinitionSyntaxError):
logging.error("Skipping variable '%s' in file '%s': unparsable units attribute '%s'",
LOGGER.error("Skipping variable '%s' in file '%s': unparsable units attribute '%s'",
ncvarname, filename, ncvar.units)
continue
if len(ncvar.shape) == 4 and vert_name in ncvar.dimensions:
Expand All @@ -310,22 +309,22 @@ def _parse_file(self, filename):
}

def _add_to_filetree(self, filename, content):
logging.info("File '%s' identified as '%s' type", filename, content["vert_type"])
logging.info("Found init time '%s', %s valid_times and %s standard_names",
LOGGER.info("File '%s' identified as '%s' type", filename, content["vert_type"])
LOGGER.info("Found init time '%s', %s valid_times and %s standard_names",
content["init_time"], len(content["valid_times"]), len(content["standard_names"]))
if len(content["valid_times"]) == 0 or len(content["standard_names"]) == 0:
logging.error(
LOGGER.error(
"Something is wrong with this file... valid_times='%s' standard_names='%s'",
content["valid_times"], content["standard_names"])
else:
logging.debug("valid_times='%s' standard_names='%s'",
LOGGER.debug("valid_times='%s' standard_names='%s'",
content["valid_times"], content["standard_names"])
leaf = self._filetree.setdefault(content["vert_type"], {}).setdefault(content["init_time"], {})
for standard_name in content["standard_names"]:
var_leaf = leaf.setdefault(standard_name, {})
for valid_time in content["valid_times"]:
if valid_time in var_leaf:
logging.warning(
LOGGER.warning(
"some data was found twice! vartype='%s' init_time='%s' standard_name='%s' "
"valid_time='%s' first_file='%s' second_file='%s'",
content["vert_type"], content["init_time"], standard_name,
Expand All @@ -337,7 +336,7 @@ def setup(self):
# Get a list of the available data files.
self._available_files = [
_filename for _filename in sorted(os.listdir(self._root_path)) if self._domain_id in _filename]
logging.info("Files identified for domain '%s': %s",
LOGGER.info("Files identified for domain '%s': %s",
self._domain_id, self._available_files)

for filename in list(self._file_cache):
Expand All @@ -351,7 +350,7 @@ def setup(self):
for filename in self._available_files:
mtime = os.path.getmtime(os.path.join(self._root_path, filename))
if (filename in self._file_cache) and (mtime == self._file_cache[filename][0]):
logging.info("Using cached candidate '%s'", filename)
LOGGER.info("Using cached candidate '%s'", filename)
content = self._file_cache[filename][1]
if content["vert_type"] != "sfc":
if content["vert_type"] not in self._elevations:
Expand All @@ -361,16 +360,16 @@ def setup(self):
(not np.allclose(
self._elevations[content["vert_type"]]["levels"],
content["elevations"]["levels"]))):
logging.error("Skipping file '%s' due to elevation mismatch", filename)
LOGGER.error("Skipping file '%s' due to elevation mismatch", filename)
continue
else:
if filename in self._file_cache:
del self._file_cache[filename]
logging.info("Opening candidate '%s'", filename)
LOGGER.info("Opening candidate '%s'", filename)
try:
content = self._parse_file(filename)
except IOError as ex:
logging.error("Skipping file '%s' (%s: %s)", filename, type(ex), ex)
LOGGER.error("Skipping file '%s' (%s: %s)", filename, type(ex), ex)
continue
self._file_cache[filename] = (mtime, content)
if content["vert_type"] not in self._elevations:
Expand All @@ -393,7 +392,7 @@ def get_valid_times(self, variable, vartype, init_time):
try:
return sorted(self._filetree[vartype][init_time][variable])
except KeyError as ex:
logging.error("Could not find times! %s %s", type(ex), ex)
LOGGER.error("Could not find times! %s %s", type(ex), ex)
return []

def get_elevations(self, vert_type):
Expand Down Expand Up @@ -457,7 +456,7 @@ def _determine_filename(self, variable, vartype, init_time, valid_time, reload=T
self.setup()
self._determine_filename(self, variable, vartype, init_time, valid_time, reload=False)
else:
logging.error("Could not identify filename. %s %s %s %s %s %s",
LOGGER.error("Could not identify filename. %s %s %s %s %s %s",
variable, vartype, init_time, valid_time, type(ex), ex)
raise ValueError(f"variable type {vartype} not available for variable {variable}")

Expand Down
7 changes: 4 additions & 3 deletions mslib/mswms/gallery_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@
import os
from PIL import Image
import io
import logging
from matplotlib import pyplot as plt
import defusedxml.ElementTree as etree
import inspect
from mslib.mswms.mpl_vsec import AbstractVerticalSectionStyle
from mslib.mswms.mpl_lsec import AbstractLinearSectionStyle
from mslib.utils import LOGGER

STATIC_LOCATION = ""

try:
import mswms_settings
if hasattr(mswms_settings, "_gallerypath"):
STATIC_LOCATION = mswms_settings._gallerypath
else:
STATIC_LOCATION = os.path.join(os.path.dirname(os.path.abspath(mswms_settings.__file__)), "gallery")
except ImportError as e:
logging.warning("%s. Can't generate gallery.", e)
LOGGER.warning("%s. Can't generate gallery.", e)

DOCS_LOCATION = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "..", "docs", "gallery")

Expand Down Expand Up @@ -457,7 +458,7 @@ def write_html(path, sphinx=False, plot_types=None):

with open(os.path.join(path, "plots.html"), "w+") as file:
file.write(html + end)
logging.info("plots.html created at %s", os.path.join(path, 'plots.html'))
LOGGER.info("plots.html created at %s", os.path.join(path, 'plots.html'))


def import_instructions(plot_object, l_type, layer, native_import=None, dataset=""):
Expand Down
8 changes: 4 additions & 4 deletions mslib/mswms/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
limitations under the License.
"""

import logging
import numpy as np
import matplotlib

from mslib.utils.units import convert_to
from mslib.utils.loggerdef import configure_mpl_logger
from mslib.utils import LOGGER

"""
Number of levels in discrete colourmaps
Expand Down Expand Up @@ -608,14 +608,14 @@ def get_style_parameters(dataname, style, cmin, cmax, data):
cmin, cmax, clev, cmap, norm, ticks = \
STYLES[style](dataname, style, cmin, cmax, cmap, data)
except KeyError:
logging.error("Unknown plotting style '%s' for dataname '%s'", style, dataname)
LOGGER.error("Unknown plotting style '%s' for dataname '%s'", style, dataname)
raise
except (ValueError, TypeError):
logging.error("Illegal number of arguments/return values for plotting style '%s' "
LOGGER.error("Illegal number of arguments/return values for plotting style '%s' "
"and dataname '%s'", style, dataname)
raise
except BaseException:
logging.error("Unknown error in style call: plotting style '%s' "
LOGGER.error("Unknown error in style call: plotting style '%s' "
"and dataname '%s'", style, dataname)
raise
if clev[0] == clev[-1]:
Expand Down
31 changes: 15 additions & 16 deletions mslib/mswms/mpl_hsec.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


import io
import logging
from abc import abstractmethod
import mswms_settings

Expand All @@ -46,7 +45,7 @@
from mslib.utils.units import convert_to
from mslib.mswms.utils import make_cbar_labels_readable
from mslib.utils.loggerdef import configure_mpl_logger

from mslib.utils import LOGGER

BASEMAP_CACHE = {}
BASEMAP_REQUESTS = []
Expand Down Expand Up @@ -239,7 +238,7 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
if crs is not None:
proj_params, bbox_units = [get_projection_params(crs)[_x] for _x in ("basemap", "bbox")]

logging.debug("plotting data..")
LOGGER.debug("plotting data..")

# Check if required data is available.
self.data_units = self.driver.data_units.copy()
Expand All @@ -251,7 +250,7 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
data[dataitem] = convert_to(data[dataitem], origunit, dataunit)
self.data_units[dataitem] = dataunit
else:
logging.debug("Please add units to plot variables")
LOGGER.debug("Please add units to plot variables")

# Copy parameters to properties.
self.data = data
Expand All @@ -266,15 +265,15 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
self.crs = crs

# Derive additional data fields and make the plot.
logging.debug("preparing additional data fields..")
LOGGER.debug("preparing additional data fields..")
self._prepare_datafields()

logging.debug("creating figure..")
LOGGER.debug("creating figure..")
dpi = 80
figsize = (figsize[0] / dpi), (figsize[1] / dpi)
facecolor = "white"
fig = mpl.figure.Figure(figsize=figsize, dpi=dpi, facecolor=facecolor)
logging.debug("\twith frame and legends" if not noframe else
LOGGER.debug("\twith frame and legends" if not noframe else
"\twithout frame")
if noframe:
ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
Expand Down Expand Up @@ -329,7 +328,7 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
bm = basemap.Basemap(resolution=None, **bm_params)
(bm.resolution, bm.coastsegs, bm.coastpolygontypes, bm.coastpolygons,
bm.coastsegs, bm.landpolygons, bm.lakepolygons, bm.cntrysegs) = BASEMAP_CACHE[key]
logging.debug("Loaded '%s' from basemap cache", key)
LOGGER.debug("Loaded '%s' from basemap cache", key)
else:
bm = basemap.Basemap(resolution='l', **bm_params)
# read in countries manually, as those are loaded only on demand
Expand All @@ -355,7 +354,7 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
try:
bm.drawcoastlines(color='0.25')
except ValueError as ex:
logging.error("Error in basemap/matplotlib call of drawcoastlines: %s", ex)
LOGGER.error("Error in basemap/matplotlib call of drawcoastlines: %s", ex)
bm.drawcountries(color='0.5')
bm.drawmapboundary(fill_color='white')

Expand Down Expand Up @@ -386,7 +385,7 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
canvas.print_png(output)

if show:
logging.debug("saving figure to mpl_hsec.png ..")
LOGGER.debug("saving figure to mpl_hsec.png ..")
canvas.print_png("mpl_hsec.png")

# Convert the image to an 8bit palette image with a significantly
Expand All @@ -397,14 +396,14 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
# requested, the figure face colour is stored as the "transparent"
# colour in the image. This works in most cases, but might lead to
# visible artefacts in some cases.
logging.debug("converting image to indexed palette.")
LOGGER.debug("converting image to indexed palette.")
# Read the above stored png into a PIL image and create an adaptive
# colour palette.
output.seek(0) # necessary for PIL.Image.open()
palette_img = PIL.Image.open(output).convert(mode="RGB").convert("P", palette=PIL.Image.Palette.ADAPTIVE)
output = io.BytesIO()
if not transparent:
logging.debug("saving figure as non-transparent PNG.")
LOGGER.debug("saving figure as non-transparent PNG.")
palette_img.save(output, format="PNG") # using optimize=True doesn't change much
else:
# If the image has a transparent background, we need to find the
Expand All @@ -424,13 +423,13 @@ def plot_hsection(self, data, lats, lons, bbox=(-180, -90, 180, 90),
facecolor_rgb[i] = int(facecolor_rgb[i] * 255)
try:
facecolor_index = lut.index(tuple(facecolor_rgb))
logging.debug("saving figure as transparent PNG with transparency index %s.", facecolor_index)
LOGGER.debug("saving figure as transparent PNG with transparency index %s.", facecolor_index)
palette_img.save(output, format="PNG", transparency=facecolor_index)
except ValueError:
logging.debug("transparency requested but not possible, saving non-transparent instead")
LOGGER.debug("transparency requested but not possible, saving non-transparent instead")
palette_img.save(output, format="PNG")

logging.debug("returning figure..")
LOGGER.debug("returning figure..")
return output.getvalue()

def shift_data(self):
Expand All @@ -451,7 +450,7 @@ def shift_data(self):
axis = self.bm.ax.axis()
ulcrnrlon, ulcrnrlat = self.bm(axis[0], axis[3], inverse=True)
left_longitude = min(self.bm.llcrnrlon, ulcrnrlon)
logging.debug("shifting data grid to leftmost longitude in map %2.f..", left_longitude)
LOGGER.debug("shifting data grid to leftmost longitude in map %2.f..", left_longitude)

# Add a column of nans for non-global data to keep it from being
# interpolated over long gaps in cylindrical
Expand Down
5 changes: 2 additions & 3 deletions mslib/mswms/mpl_hsec_styles.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
limitations under the License.
"""

import logging
import warnings

import numpy as np
Expand All @@ -76,7 +75,7 @@
from mslib.mswms.mpl_hsec import MPLBasemapHorizontalSectionStyle
from mslib.mswms.utils import make_cbar_labels_readable
import mslib.mswms.generics as generics
from mslib.utils import thermolib
from mslib.utils import thermolib, LOGGER
from mslib.utils.units import convert_to


Expand Down Expand Up @@ -1530,7 +1529,7 @@ def _plot_style(self):

tempC = data["msg_brightness_temperature_108"]

logging.debug("Min: %.2f K, Max: %.2f K", tempC.min(), tempC.max())
LOGGER.debug("Min: %.2f K, Max: %.2f K", tempC.min(), tempC.max())

tc = bm.contourf(self.lonmesh, self.latmesh, tempC,
np.arange(cmin, cmax, 2), cmap=plt.cm.gray_r, extend="both")
Expand Down
5 changes: 3 additions & 2 deletions mslib/mswms/mpl_lsec.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
"""
# style definitions should be put in mpl_lsec_styles.py

import logging

from xml.dom.minidom import getDOMImplementation
import matplotlib as mpl
from pint import Quantity

from mslib.mswms import mss_2D_sections
from mslib.utils.units import convert_to
from mslib.utils.loggerdef import configure_mpl_logger
from mslib.utils import LOGGER

mpl.rcParams['xtick.direction'] = 'out'
mpl.rcParams['ytick.direction'] = 'out'
Expand Down Expand Up @@ -80,7 +81,7 @@ def plot_lsection(self, data, lats, lons, valid_time, init_time):
data[dataitem] = convert_to(data[dataitem], origunit, dataunit)
self.data_units[dataitem] = dataunit
else:
logging.debug("Please add units to plot variables")
LOGGER.debug("Please add units to plot variables")

# Copy parameters to properties.
self.data = data
Expand Down
Loading
Loading