Skip to content

Commit

Permalink
fix: fix all issues reported by pylint
Browse files Browse the repository at this point in the history
Signed-off-by: Davide Madrisan <[email protected]>
  • Loading branch information
madrisan committed Dec 22, 2024
1 parent 943ffe6 commit 57308bb
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 130 deletions.
3 changes: 0 additions & 3 deletions .pylintrc

This file was deleted.

60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ HadCRUT5 data are downloaded from: https://www.metoffice.gov.uk/hadobs/hadcrut5/

## Plot of the temperature anomalies

The following plots have been generated by the Python scripts `hadcrut5-plot.py` and `hadcrut5-bars.py`.
The following plots have been generated by the Python scripts `hadcrut5_plot.py` and `hadcrut5_bars.py`.
They require the Python libraries: Matplotlib, netCDF4, NumPy, and Requests.

If Python and the required libraries are not installed on your system, you can simply
[install](https://docs.astral.sh/uv/getting-started/installation/) `uv` and run the commands listed below prefixed
with `uv run`. For example `uv run ./hadcrut5-plot.py`.
with `uv run`. For example `uv run ./hadcrut5_plot.py`.

## hadcrut5-plot.py &mdash; Script usage
## hadcrut5_plot.py &mdash; Script usage

```
$ ./hadcrut5-plot.py --help
usage: hadcrut5-plot.py [-h] [-f OUTFILE] [-p PERIOD] [-m SMOOTHER] [-g] [-n] [-s] [-a ANNOTATE] [-v]
$ ./hadcrut5_plot.py --help
usage: hadcrut5_plot.py [-h] [-f OUTFILE] [-p PERIOD] [-m SMOOTHER] [-g] [-n] [-s] [-a ANNOTATE] [-v]
Parse and plot the HadCRUT5 temperature datasets v2023.1 (stable)
Copyright (C) 2020-2023 Davide Madrisan <[email protected]>
Expand All @@ -55,28 +55,28 @@ options:
-v, --verbose make the operation more talkative
examples:
hadcrut5-plot.py
hadcrut5-plot.py --global --annotate=2
hadcrut5-plot.py --period "1850-1900"
hadcrut5-plot.py --period "1850-1900" --smoother 5
hadcrut5-plot.py --period "1880-1920" --outfile HadCRUT5-1880-1920.png
hadcrut5-plot.py --period "1880-1920" --time-series monthly --global
hadcrut5_plot.py
hadcrut5_plot.py --global --annotate=2
hadcrut5_plot.py --period "1850-1900"
hadcrut5_plot.py --period "1850-1900" --smoother 5
hadcrut5_plot.py --period "1880-1920" --outfile HadCRUT5-1880-1920.png
hadcrut5_plot.py --period "1880-1920" --time-series monthly --global
```

`hadcrut5-plot.py` select the period `1961-90` by default but supports (see the command-line switch`--period`) two other base periods found in the literature: `1850-1900`, and `1880-1920`.
`hadcrut5_plot.py` select the period `1961-90` by default but supports (see the command-line switch`--period`) two other base periods found in the literature: `1850-1900`, and `1880-1920`.

```
$ ./hadcrut5-plot.py --annotate=2 --outfile plots/HadCRUT5-1961-1990.png
$ ./hadcrut5_plot.py --annotate=2 --outfile plots/HadCRUT5-1961-1990.png
```
![HadCRUT5 anomalies related to 1961-1990](plots/HadCRUT5-1961-1990.png)

```
$ ./hadcrut5-plot.py --annotate=2 --period "1850-1900" --outfile plots/HadCRUT5-1850-1900.png
$ ./hadcrut5_plot.py --annotate=2 --period "1850-1900" --outfile plots/HadCRUT5-1850-1900.png
```
![HadCRUT5 anomalies related to 1850-1900](plots/HadCRUT5-1850-1900.png)

```
$ ./hadcrut5-plot.py --annotate=2 --period "1880-1920" --outfile plots/HadCRUT5-1880-1920.png
$ ./hadcrut5_plot.py --annotate=2 --period "1880-1920" --outfile plots/HadCRUT5-1880-1920.png
```
![HadCRUT5 anomalies related to 1880-1920](plots/HadCRUT5-1880-1920.png)

Expand All @@ -87,7 +87,7 @@ For instance `--smoother 5` will get you a better idea of the trend lines.

Image generated for the anomalies related to the period `1880-1920`.
```
$ ./hadcrut5-plot.py --period "1880-1920" --smoother 5 --outfile plots/HadCRUT5-1880-1920-smoother.png
$ ./hadcrut5_plot.py --period "1880-1920" --smoother 5 --outfile plots/HadCRUT5-1880-1920-smoother.png
```
![HadCRUT5 anomalies related to 1880-1920 with 5-year means](plots/HadCRUT5-1880-1920-smoother.png)

Expand All @@ -97,14 +97,14 @@ The command-line option `--time-series monthly` selects the monthly HadCRUT5 dat

Image displying the monthly anomalies related to the period `1880-1920`, for the global temperatures only.
```
$ ./hadcrut5-plot.py --global --period "1880-1920" --time-series monthly
$ ./hadcrut5_plot.py --global --period "1880-1920" --time-series monthly
```
![HadCRUT5 monthly global anomalies related to 1880-1920 means](plots/HadCRUT5-monthly-global-1880-1920.png)

## hadcrut5-bars.py &mdash; Script usage
## hadcrut5_bars.py &mdash; Script usage

```
usage: hadcrut5-bars.py [-h] [-f OUTFILE] [-p PERIOD] [-v]
usage: hadcrut5_bars.py [-h] [-f OUTFILE] [-p PERIOD] [-v]
Parse and plot the HadCRUT5 temperature datasets v2023.1 (stable)
Copyright (C) 2020-2023 Davide Madrisan <[email protected]>
Expand All @@ -119,21 +119,21 @@ options:
-v, --verbose make the operation more talkative
examples:
hadcrut5-bars.py
hadcrut5-bars.py --period "1850-1900"
hadcrut5-bars.py --period "1880-1920"
hadcrut5-bars.py --outfile HadCRUT5-global.png
hadcrut5_bars.py
hadcrut5_bars.py --period "1850-1900"
hadcrut5_bars.py --period "1880-1920"
hadcrut5_bars.py --outfile HadCRUT5-global.png
```
The image for to the anomalies related to the period `1880-1920` follows.
```
$ ./hadcrut5-bars.py --period "1880-1920" --outfile plots/HadCRUT5-global-1880-1920.png
$ ./hadcrut5_bars.py --period "1880-1920" --outfile plots/HadCRUT5-global-1880-1920.png
```
![HadCRUT5 bar plotting related to 1880-1920](plots/HadCRUT5-global-1880-1920.png)

## hadcrut5-stripe.py &mdash; Script usage
## hadcrut5_stripe.py &mdash; Script usage

```
usage: hadcrut5-stripe.py [-h] [-f OUTFILE] [-r {global,northern,southern}] [-v] [-l]
usage: hadcrut5_stripe.py [-h] [-f OUTFILE] [-r {global,northern,southern}] [-v] [-l]
Parse and plot a stripe image of the HadCRUT5 temperature datasets v2023.1 (stable)
Copyright (C) 2020-2023 Davide Madrisan <[email protected]>
Expand All @@ -149,14 +149,14 @@ options:
-l, --no-labels do not disply the header and footer labels
examples:
hadcrut5-stripe.py
hadcrut5-stripe.py --no-labels --region northern
hadcrut5-stripe.py --region global --outfile HadCRUT5-stripe-global.png
hadcrut5_stripe.py
hadcrut5_stripe.py --no-labels --region northern
hadcrut5_stripe.py --region global --outfile HadCRUT5-stripe-global.png
```

Below is a generated striped image for global anomalies.
```
$ ./hadcrut5-stripe.py --region global
$ ./hadcrut5_stripe.py --region global
```
![HadCRUT5 global warming stripe](plots/HadCRUT5-global-stripe.png)

Expand Down
17 changes: 8 additions & 9 deletions hadcrut5-bars.py → hadcrut5_bars.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def major_formatter(x, pos):
plt.style.use("dark_background")
_, ax = plt.subplots()

cmap = plt.cm.jet # or plt.cm.bwr
cmap = plt.cm.jet # or plt.cm.bwr # pylint: disable=no-member
norm = matplotlib.colors.Normalize(vmin=-1, vmax=max(mean))
colors = cmap(norm(mean))

Expand All @@ -94,16 +94,15 @@ def major_formatter(x, pos):

upper, left = 0.95, 0.025
last_year = years[-1]

text_props = dict(
horizontalalignment="left", verticalalignment="top", transform=ax.transAxes
)
text_props = {
"horizontalalignment": "left",
"verticalalignment": "top",
"transform": ax.transAxes,
}
plt.text(
left,
upper,
"\n".join(
(r"Global average temperature difference *", r"1850-{}".format(last_year))
),
"\n".join((r"Global average temperature difference *", f"1850-{last_year}")),
fontdict=fontxl,
linespacing=1.2,
**text_props,
Expand All @@ -113,7 +112,7 @@ def major_formatter(x, pos):
upper - 0.125,
"\n".join(
(
r"(*) Compared to {} pre-industrial levels".format(period),
f"(*) Compared to {period} pre-industrial levels",
r"Data source - HadCRUT5",
)
),
Expand Down
38 changes: 16 additions & 22 deletions hadcrut5-plot.py → hadcrut5_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
Display a plot of the HadCRUT5 temperature dataset.
"""

from math import trunc

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

from math import trunc
from hadcrut5lib import argparser, HadCRUT5


Expand Down Expand Up @@ -119,7 +120,7 @@ def dataset_smoother(years, temperatures, chunksize):
return subset_years, subset_temperatures


def plotline(hc5, chunksize, annotate, outfile, verbose):
def plotline(hc5, chunksize, annotate, outfile):
"""
Create a plot for the specified period and arguments and diplay it or save
it to file if outfile is set
Expand All @@ -139,21 +140,21 @@ def plotline(hc5, chunksize, annotate, outfile, verbose):

if chunksize > 1:
years, mean = dataset_smoother(dataset_years, mean, chunksize)
hc5.logging_debug("years: \\\n{}".format(np.array(years)))
hc5.logging_debug("temperatures ({}): \\\n{}".format(region, mean))
hc5.logging_debug("delta ({}): \\\n{}".format(years[-1], mean[-1]))
hc5.logging_debug(f"years: \\\n{np.array(years)}")
hc5.logging_debug(f"temperatures ({region}): \\\n{mean}")
hc5.logging_debug(f"delta ({years[-1]}): \\\n{mean[-1]}")
else:
years = dataset_years
plt.fill_between(years, lower, upper, color="lightgray")

anomaly_current[region] = dataset_current_anomaly(mean)
anomaly_max[region] = dataset_max_anomaly(mean)
hc5.logging_debug("Current anomalies: {}".format(anomaly_current[region]))
hc5.logging_debug("Max anomalies: {}".format(anomaly_max[region]))
hc5.logging_debug(f"Current anomalies: {anomaly_current[region]}")
hc5.logging_debug(f"Max anomalies: {anomaly_max[region]}")

if annotate > 1:
plt.annotate(
"{0:.2f}°C".format(anomaly_current[region]),
f"{anomaly_current[region]:.2f}°C",
xy=(years[-1] - 2, anomaly_current[region] - 0.15),
fontsize=6,
horizontalalignment="left",
Expand All @@ -171,38 +172,32 @@ def plotline(hc5, chunksize, annotate, outfile, verbose):
linestyles="dotted",
)

plt.title(
(
"HadCRUT5: land and sea temperature anomalies relative to {}".format(
hc5.dataset_period
)
)
)
plt.title(f"HadCRUT5: land and sea temperature anomalies relative to {hc5.dataset_period}")
plt.xlabel("year", fontsize=10)

ylabel = "{} Temperature Anomalies in °C".format(hc5.dataset_datatype.capitalize())
ylabel = f"{hc5.dataset_datatype.capitalize()} Temperature Anomalies in °C"

if chunksize > 1:
ylabel += " ({}-year averages)".format(chunksize)
ylabel += f" ({chunksize}-year averages)"
else:
current = anomaly_current.get(hc5.GLOBAL_REGION)
maximum = anomaly_max.get(hc5.GLOBAL_REGION)

if annotate > 0 and current and maximum:
current_year = trunc(hc5.dataset_years()[-1])
facecolor = "blue" if current <= 0 else "red"
plt.annotate(
(
"current global anomaly ({0}): {1:+.2f}°C, max: {2:+.2f}°C".format(
current_year, current, maximum
)
f"current global anomaly ({current_year}): "
f"{current:+.2f}°C, max: {maximum:+.2f}°C"
),
xy=(0.98, 0.03),
xycoords="axes fraction",
fontsize=8,
horizontalalignment="right",
verticalalignment="bottom",
bbox={
"facecolor": "{}".format("blue" if current <= 0 else "red"),
"facecolor": facecolor,
"alpha": 0.3,
"pad": 5,
},
Expand Down Expand Up @@ -253,7 +248,6 @@ def main():
smoother,
int(args.annotate) if args.annotate else 1,
args.outfile,
args.verbose,
)


Expand Down
6 changes: 1 addition & 5 deletions hadcrut5-stripe.py → hadcrut5_stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ def plotstripe(region, outfile, labels, verbose):
ax.tick_params(axis="both", which="both", length=0)
# pylint: disable=consider-using-f-string
plt.title(
(
"{} Temperature Change ({}-{})".format(
regions_switch[region], yfirst, ylast
)
),
("{} Temperature Change ({}-{})".format(regions_switch[region], yfirst, ylast)),
fontsize=16,
loc="left",
fontweight="bold",
Expand Down
Loading

0 comments on commit 57308bb

Please sign in to comment.