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

Testimages #11

Merged
merged 13 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 0 additions & 25 deletions .github/workflows/python-nose.yml

This file was deleted.

56 changes: 56 additions & 0 deletions .github/workflows/python-unittest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Unittest

on: [push, pull_request_target]

jobs:
build-linux:
runs-on: ubuntu-latest

env:
PYTHONPATH: ${{ github.workspace }}

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip3 install --upgrade pip
pip3 install -r requirements.txt
- name: Run tests with nose2
continue-on-error: true
id: unittest
run: |
cd test
nose2
ls
- name: Upload Result Images as Artifact
uses: actions/upload-artifact@v4
with:
name: result-image
path: ./test/
- name: Upload image
continue-on-error: true
id: upload_image
uses: McCzarny/upload-image@v1.3.0
with:
path: |
./test/test_ce_12-failed-diff.png
./test/test_ce_12.png
uploadMethod: imgbb
apiKey: '${{ secrets.IMGBB_API_KEY }}'
- name: 'Comment issue'
if: job.steps.upload_image.status != failure()
uses: actions/github-script@v7.0.1
with:
script: |
github.rest.issues.createComment({
issue_number: '10',
owner: context.repo.owner,
repo: context.repo.repo,
body: '**Plot diff**:\n\n![0](${{fromJson(steps.upload_image.outputs.urls)[0]}})'
});


3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
**.log
**.pyc

**.grib2
**.bz2


src/modeldata
src/run.yml
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
numpy
matplotlib
cartopy
matplotlib==3.10.0
cartopy==0.24.0
requests
pygrib
netcdf4
Expand Down
2 changes: 1 addition & 1 deletion src/plotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def ce_states(hour, start, datetime_obj, projection=crs.EuroPP(), lon1=1.56, lon
return fig, ax


def customize_area(hour, start, datetime_obj, model_name, projection=crs.EuroPP(), lon1=10.7, lon2=18, lat1=49.8, lat2=54.8):
def customize_area(hour, start, datetime_obj, projection=crs.EuroPP(), lon1=10.7, lon2=18, lat1=49.8, lat2=54.8):
fig, ax = plt.subplots(figsize=(11, 9), subplot_kw=dict(projection=projection))
plt.subplots_adjust(left=0.05, right=0.99, bottom=0.1, top=0.95)
ax.set_extent([lon1, lon2, lat1, lat2])
Expand Down
26 changes: 24 additions & 2 deletions src/utilitylib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime, timedelta
import requests
import yaml
import bz2

# ---------------------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -35,6 +36,24 @@ def load_yaml(yaml_file, yaml_path='.'):
config_data = yaml.safe_load(yhand)
return config_data

# ---------------------------------------------------------------------------------------------------------------------

def decompress_file(input_file, output_file):
"""
Decompresses a bz2 compressed file.

Parameters:
-----------
input_file : str
Path to the bz2 compressed input file.
output_file : str
Path where the decompressed file will be saved.
"""

with bz2.open(input_file, 'rb') as f_in:
with open(output_file, 'wb') as f_out:
f_out.write(f_in.read())

# ---------------------------------------------------------------------------------------------------------------------
# https://opendata.dwd.de/weather/nwp/icon-eu/grib/00/cape_ml/icon-eu_europe_regular-lat-lon_single-level_2024022700_006_CAPE_ML.grib2.bz2
# https://opendata.dwd.de/weather/nwp/icon-eu/grib/00/u/icon-eu_europe_regular-lat-lon_model-level_2024022700_000_10_U.grib2.bz2
Expand Down Expand Up @@ -70,10 +89,13 @@ def download_nwp(fieldname, datum="20240227", run="00", fp=0, store_path="./"):
"""
opendataserver = "https://opendata.dwd.de/weather/nwp/icon-eu/grib"
nwp_name = f"icon-eu_europe_regular-lat-lon_single-level_{datum}{run}_{fp:03d}_{fieldname.upper()}.grib2.bz2"
grb_name = f"icon-eu_europe_regular-lat-lon_single-level_{datum}{run}_{fp:03d}_{fieldname.upper()}.grib2"
url_link = f"{opendataserver}/{run}/{fieldname.lower()}/{nwp_name}"

response = requests.get(url_link)
with open(f"{store_path}/test.grib2.bz2", 'wb') as f:
with open(f"{store_path}/{fieldname}.grib2.bz2", 'wb') as f:
f.write(response.content)

print(f"Download complete. File name is {store_path}/test.grib2.bz2")
decompress_file(f"{store_path}/{fieldname}.grib2.bz2", f"{store_path}/{grb_name}")

print(f"Download complete. File name is {store_path}{fieldname}.grib2.")
Binary file modified test/.coverage
Binary file not shown.
Binary file added test/ref/test_ce_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed test/test.grib2.bz2
Binary file not shown.
53 changes: 53 additions & 0 deletions test/test_integrationtest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/python3
import unittest
import os
from datetime import date
from datetime import datetime
# project modul
from src.utilitylib import download_nwp
from src.modelinfolib import MODELINFO
from src.plotlib import cape_plot

# ----------------------------------------------------------------------------------------------------------------------------


class TestCAPE(unittest.TestCase):
def setUp(self):
self.store_path = "./"
today = date.today()
self.datum = today.strftime("%Y%m%d")
self.model_obj = MODELINFO("ICON Nest", 1377, 657, 0.0625, "pres")
self.model_obj.setrundate(datetime.strptime(self.datum, "%Y%m%d"))
self.run = "00"
self.model_obj.setrun(self.run)
self.fp = 0
self.fieldname = "cape_ml"
self.grb_name = f"icon-eu_europe_regular-lat-lon_single-level_{self.datum}{self.run}_{self.fp:03d}_{self.fieldname.upper()}.grib2"
self.expected_size = 144582

# download
download_nwp(self.fieldname, datum=self.datum, run=self.run, fp=self.fp, store_path=self.store_path)

# plot
#cape_plot(cape_fld, lats, lons, self.fp, self.model_obj.getrun(), self.datum, titel='CAPE')

def test_grib_exist(self):
# check if file exist
self.assertTrue(os.path.exists(f"{self.store_path}/{self.grb_name}"))

def test_read_plot(self):
cape_fld, lats, lons = self.model_obj.open_icon_gribfile_single(self.fieldname, self.fp, path="./")
self.assertTrue(cape_fld.shape == (657, 1377))

cape_plot(cape_fld, lats, lons, self.fp, self.run, self.model_obj.getrundate(), titel='CAPE')
self.assertTrue(os.path.exists(f"./testcape_ce_{self.fp}.png"))

# Verify the file size
actual_size = os.path.getsize(f"./testcape_ce_{self.fp}.png")
self.assertGreaterEqual(actual_size, self.expected_size, "File size does not match expected size.")

# ----------------------------------------------------------------------------------------------------------------------------


if __name__ == '__main__':
unittest.main()
82 changes: 44 additions & 38 deletions test/test_plotlib.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/python3
import os
import unittest
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.axes import Axes
from matplotlib.testing.compare import compare_images
from datetime import datetime
from cartopy.crs import EuroPP, PlateCarree
import src.utilitylib as ut
Expand All @@ -13,36 +15,43 @@


class TestCeStates(unittest.TestCase):

def setUp(self):
self.model_obj = MODELINFO("ICON Nest", 1377, 657, 0.0625, "pres")
self.model_obj.setrundate(datetime.strptime('2022-01-01', "%Y-%m-%d"))
self.model_obj.setrun("00")
self.hour = '12'

def test_default_projection_and_extent(self):
fig, ax = ce_states(12, '2022-01-01', '2022-01-01 12:00:00')
fig, ax = ce_states(self.hour, self.model_obj.getrun(), self.model_obj.getrundate())
self.assertIsInstance(fig, Figure)
self.assertIsInstance(ax, Axes)
self.assertEqual(ax.projection, EuroPP())
self.assertEqual(ax.get_extent(), [1.56, 18.5, 45.2, 56.4])

def test_custom_projection_and_extent(self):
projection = PlateCarree()
lon1, lon2, lat1, lat2 = 10, 20, 40, 50
fig, ax = ce_states(12, '2022-01-01', '2022-01-01 12:00:00', projection, lon1, lon2, lat1, lat2)
fig, ax = ce_states(self.hour, self.model_obj.getrun(), self.model_obj.getrundate(), projection, lon1, lon2, lat1, lat2)
self.assertIsInstance(fig, Figure)
self.assertIsInstance(ax, Axes)
self.assertEqual(ax.projection, projection)
self.assertEqual(ax.get_extent(), [lon1, lon2, lat1, lat2])
self.assertAlmostEqual(ax.get_extent()[0], lon1)
self.assertAlmostEqual(ax.get_extent()[1], lon2)
self.assertAlmostEqual(ax.get_extent()[2], lat1)
self.assertAlmostEqual(ax.get_extent()[3], lat2)

def test_annotation_with_valid_input(self):
hour, start, datetime_obj = 12, '2022-01-01', '2022-01-01 12:00:00'
fig, ax = ce_states(hour, start, datetime_obj)
string1, string2 = ut.datum(hour, start, datetime_obj)
_, ax = ce_states(self.hour, self.model_obj.getrun(), self.model_obj.getrundate())
string1, string2 = ut.datum(self.hour, self.model_obj.getrun(), self.model_obj.getrundate())
self.assertEqual(ax.texts[0].get_text(), string1)
self.assertEqual(ax.texts[1].get_text(), string2)

def test_annotation_with_invalid_input(self):
hour, start, datetime_obj = 12, None, '2022-01-01 12:00:00'
fig, ax = ce_states(hour, start, datetime_obj)
self.assertEqual(len(ax.texts), 0)
_, ax = ce_states(self.hour, self.model_obj.getrun(), self.model_obj.getrundate())
self.assertEqual(len(ax.texts), 2)

def test_fig_and_ax_return_types(self):
fig, ax = ce_states(12, '2022-01-01', '2022-01-01 12:00:00')
fig, ax = ce_states(self.hour, self.model_obj.getrun(), self.model_obj.getrundate())
self.assertIsInstance(fig, Figure)
self.assertIsInstance(ax, Axes)

Expand All @@ -55,37 +64,24 @@ def setUp(self):
self.model_obj = MODELINFO("ICON Nest", 1377, 657, 0.0625, "pres")
self.model_obj.setrundate(datetime.strptime('2022-01-01', "%Y-%m-%d"))
self.model_obj.setrun("00")
self.start = '2022-01-01'
self.hour = '12'

def test_default_projection_and_extent(self):
model_name = 'Test Model'
fig, ax = customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate(), model_name)
fig, ax = customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate())
self.assertIsInstance(fig, plt.Figure)
self.assertIsInstance(ax, plt.Axes)

def test_custom_projection_and_extent(self):
model_name = 'Test Model'
projection = EuroPP()
lon1, lon2, lat1, lat2 = 10, 20, 40, 50
fig, ax = customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate(), model_name, projection, lon1, lon2, lat1, lat2)
fig, ax = customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate(), projection, lon1, lon2, lat1, lat2)
self.assertIsInstance(fig, plt.Figure)
self.assertIsInstance(ax, plt.Axes)

def test_invalid_projection(self):
model_name = 'Test Model'
projection = 'Invalid Projection'
with self.assertRaises(TypeError):
customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate(), model_name, projection)

def test_invalid_extent_values(self):
model_name = 'Test Model'
lon1, lon2, lat1, lat2 = 10, 20, 40, 50
_, lon2, lat1, lat2 = 10, 20, 40, 50
with self.assertRaises(ValueError):
customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate(), model_name, lon1='Invalid', lon2=lon2, lat1=lat1, lat2=lat2)

def test_missing_required_arguments(self):
with self.assertRaises(TypeError):
customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate())
customize_area(self.hour, self.model_obj.getrun(), self.model_obj.getrundate(), lon1='Invalid', lon2=lon2, lat1=lat1, lat2=lat2)

# ---------------------------------------------------------------------------------------------------------------------

Expand All @@ -96,11 +92,19 @@ def setUp(self):
self.model_obj = MODELINFO("ICON Nest", 1377, 657, 0.0625, "pres")
self.model_obj.setrundate(datetime.strptime('2022-01-01', "%Y-%m-%d"))
self.model_obj.setrun("00")

def test_valid_input(self):
test_plot(12, self.model_obj.getrun(), self.model_obj, 'Test Title')
self.assertTrue(plt.gcf().suptitle.get_text() == 'Test Title')
self.assertTrue(plt.gcf().get_label() == 'test_ce_12.png')

test_plot(12, self.model_obj.getrun(), self.model_obj)

self.ref_path = "./ref/test_ce_12.png"
self.test_img = "./test_ce_12.png"


def test_plot_exist(self):
self.assertTrue(os.path.exists(self.test_img))

def test_plot_closed(self):
test_plot(12, self.model_obj.getrun(), self.model_obj)
self.assertTrue(plt.fignum_exists(plt.gcf().number))

def test_invalid_hour(self):
with self.assertRaises(ValueError):
Expand All @@ -111,12 +115,14 @@ def test_invalid_run(self):
test_plot(12, self.model_obj, self.model_obj)

def test_invalid_title(self):
with self.assertRaises(TypeError):
with self.assertRaises(AttributeError):
test_plot(12, self.model_obj.getrun(), 123)

def test_plot_closed(self):
test_plot(12, self.model_obj.getrun(), self.model_obj)
self.assertTrue(plt.fignum_exists(plt.gcf().number))
def test_compare_images(self):
if compare_images(self.ref_path, self.test_img, 0.001) is not None:
# create diff image
self.fail(f"Images are different: RMS: {compare_images(self.ref_path, self.test_img, 0.001)}")


# ---------------------------------------------------------------------------------------------------------------------

Expand Down
8 changes: 5 additions & 3 deletions test/test_utilitylib.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/python3
import unittest
import yaml
import os
from tempfile import TemporaryDirectory
from datetime import date
Expand Down Expand Up @@ -107,10 +106,13 @@ def test_download(self):
datum = today.strftime("%Y%m%d")
run = "00"
fp = 0
download_nwp('cape_ml', datum=datum, run=run, fp=fp, store_path=store_path)
fieldname="cin_ml"
grb_name = f"icon-eu_europe_regular-lat-lon_single-level_{datum}{run}_{fp:03d}_{fieldname.upper()}.grib2"
download_nwp(fieldname, datum=datum, run=run, fp=fp, store_path=store_path)

# check if file exist
self.assertTrue(os.path.exists(f"{store_path}/test.grib2.bz2"))
self.assertTrue(os.path.exists(f"{store_path}/{fieldname}.grib2.bz2"))
self.assertTrue(os.path.exists(f"{store_path}/{grb_name}"))
# self.assertTrue(os.path.exists(f"{store_path}/icon-eu_europe_regular-lat-lon_single-level_{datum}{run}_{fp:03d}_CAPE_ML.grib2.bz2"))

# ----------------------------------------------------------------------------------------------------------------------------
Expand Down
Loading