Skip to content

Commit

Permalink
reformat tests so pytest can discover them
Browse files Browse the repository at this point in the history
  • Loading branch information
LuukBlom committed Jan 22, 2025
1 parent 8be29de commit 0d42f29
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 144 deletions.
60 changes: 0 additions & 60 deletions best_track_idai.cyc

This file was deleted.

76 changes: 40 additions & 36 deletions tests/test_forecasting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,43 @@
from cht_cyclones.cyclone_track_database import CycloneTrackDatabase
from cht_cyclones.tropical_cyclone import TropicalCyclone, TropicalCycloneEnsemble

# Create track file
database_file = Path(__file__).parent / "IBTrACS.ALL.v04r00.nc"
db = CycloneTrackDatabase("ibtracs", file_name=database_file)
ind = db.list_names().index("IDAI")
tc = db.get_track(index=ind)
tc.write_track(Path(__file__).parent / "best_track_idai.cyc", "ddb_cyc")

# Define folder and track
tc = TropicalCyclone(name="Idai")
current_directory = os.path.dirname(os.path.abspath(__file__))

# Read a track
file_path = os.path.join(current_directory, "best_track_idai.cyc")
tc.from_ddb_cyc(file_path)
tc.account_for_forward_speed()
tc.estimate_missing_values()
tc.include_rainfall = True

# Forecasting
# Uses DeMaria et al. 2009 formulations
tc2 = TropicalCycloneEnsemble(name="Idai_forecastng", TropicalCyclone=tc)
tc2.dt = 3 # 3 hourly
tc2.include_best_track = 0 # not included best-track
tc2.tstart = datetime(2019, 3, 9, 0, 0, 0) # March 5, 2019 at midnight UTC
tc2.tend = datetime(2019, 3, 16, 6, 0, 0) # last timestamp in cyc
nm_to_m = float(1.852) * 1000 # nm to m
tc2.mean_abs_cte24 = (
19.0397 * nm_to_m
) # mean absolute error in cross-track error (CTE) in meter
tc2.sc_cte = 1.3253 # auto-regression CTE; typically >1
tc2.compute_ensemble(10)

# Write out
tc2.to_shapefile(current_directory)
tc2.to_spiderweb(current_directory)
tc2.make_figures(current_directory)

def test_forecasting():
# Create track file
database_file = Path(__file__).parent / "IBTrACS.ALL.v04r00.nc"
db = CycloneTrackDatabase("ibtracs", file_name=database_file)
ind = db.list_names().index("IDAI")
tc = db.get_track(index=ind)
tc.write_track(Path(__file__).parent / "best_track_idai.cyc", "ddb_cyc")

# Define folder and track
tc = TropicalCyclone(name="Idai")
current_directory = os.path.dirname(os.path.abspath(__file__))

# Read a track
file_path = os.path.join(current_directory, "best_track_idai.cyc")
tc.from_ddb_cyc(file_path)
tc.account_for_forward_speed()
tc.estimate_missing_values()
tc.include_rainfall = True

# Forecasting
# Uses DeMaria et al. 2009 formulations
tc2 = TropicalCycloneEnsemble(name="Idai_forecastng", TropicalCyclone=tc)
tc2.dt = 3 # 3 hourly
tc2.include_best_track = 0 # not included best-track
tc2.tstart = datetime(2019, 3, 9, 0, 0, 0) # March 5, 2019 at midnight UTC
tc2.tend = datetime(2019, 3, 16, 6, 0, 0) # last timestamp in cyc
nm_to_m = float(1.852) * 1000 # nm to m
tc2.mean_abs_cte24 = (
19.0397 * nm_to_m
) # mean absolute error in cross-track error (CTE) in meter
tc2.sc_cte = 1.3253 # auto-regression CTE; typically >1
tc2.compute_ensemble(10)

# Write out
tc2.to_shapefile(current_directory)
tc2.to_spiderweb(current_directory)
tc2.make_figures(current_directory)

# asserts ?
95 changes: 52 additions & 43 deletions tests/test_reading_TRK_forecasted.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,61 @@
# Load modules
import os
from pathlib import Path

from cht_cyclones.tropical_cyclone import (
TropicalCyclone,
TropicalCycloneEnsemble,
)

# Define a track
tc = TropicalCyclone(name="Maria_forecast")

# Read a track
current_directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_directory, "TRK_COAMPS_CTCX_3_2017092006_15L")
tc.read_track(file_path, "trk")

# Define settings
# Uses typical Wind Enhance Scheme (WES) formulations
tc.include_rainfall = (
True # using empirical formulation to compute rainfall (IPET default)
)
tc.rainfall_factor = (
2.0 # factor to calibrate rainfall 2.0 means 2x as much as relationship
)

# Write as cyc
tc.convert_units_metric_imperial()
file_path = os.path.join(current_directory, "forecasted_track_Maria.cyc")
tc.write_track(file_path, "ddb_cyc")

# create (regular) ASCII spiderweb
file_path = os.path.join(current_directory, "forecasted_track_Maria.spw")
tc.to_spiderweb(file_path)

# create (netcdf) spiderweb
file_path = os.path.join(current_directory, "forecasted_track_Maria.nc")
tc.to_spiderweb(file_path, format_type="netcdf")

# Write out as shapefile
file_path = os.path.join(current_directory, "forecasted_track_Maria.shp")
tc.track.to_file(file_path)

# Write out as geojson
file_path = os.path.join(current_directory, "forecasted_track_Maria.json")
tc.track.to_file(file_path, driver="GeoJSON")

# TC-FF ensemble members
tc2 = TropicalCycloneEnsemble(name="Maria_forecast_ensembles", TropicalCyclone=tc)
tc2.dt = 3 # 3 hourly
tc2.include_best_track = 0 # not included best-track
tc2.compute_ensemble(100)
tc2.to_shapefile(current_directory)
def test_read_TRK_forecasted():
# Define a track
tc = TropicalCyclone(name="Maria_forecast")

# Read a track
current_directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_directory, "TRK_COAMPS_CTCX_3_2017092006_15L")
tc.read_track(file_path, "trk")

# Define settings
# Uses typical Wind Enhance Scheme (WES) formulations
tc.include_rainfall = (
True # using empirical formulation to compute rainfall (IPET default)
)
tc.rainfall_factor = (
2.0 # factor to calibrate rainfall 2.0 means 2x as much as relationship
)

# Write as cyc
tc.convert_units_metric_imperial()
file_path = os.path.join(current_directory, "forecasted_track_Maria.cyc")
tc.write_track(file_path, "ddb_cyc")

# create (regular) ASCII spiderweb
file_path = os.path.join(current_directory, "forecasted_track_Maria.spw")
tc.to_spiderweb(file_path)

# create (netcdf) spiderweb
file_path = os.path.join(current_directory, "forecasted_track_Maria.nc")
tc.to_spiderweb(file_path, format_type="netcdf")

# Write out as shapefile
file_path = os.path.join(current_directory, "forecasted_track_Maria.shp")
tc.track.to_file(file_path)

# Write out as geojson
file_path = os.path.join(current_directory, "forecasted_track_Maria.json")
tc.track.to_file(file_path, driver="GeoJSON")

# TC-FF ensemble members
tc2 = TropicalCycloneEnsemble(name="Maria_forecast_ensembles", TropicalCyclone=tc)
tc2.dt = 3 # 3 hourly
tc2.include_best_track = 0 # not included best-track
tc2.compute_ensemble(100)
tc2.to_shapefile(current_directory)

suffixes = ["cpg", "cyc", "dbf", "json", "shp", "shx", "prj", "spw"]
assert all(
(Path(current_directory) / f"forecasted_track_Maria.{suffix}").exists()
for suffix in suffixes
)
7 changes: 2 additions & 5 deletions tests/test_track_database.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import os
from pathlib import Path

from cht_cyclones.cyclone_track_database import CycloneTrackDatabase


def test_load_track_database():
database_file = Path(os.getcwd()).joinpath(
"cyclone_track_database", "IBTrACS.NA.v04r00.nc"
)
database_file = Path(__file__).parent / "IBTrACS.ALL.v04r00.nc"
tc = CycloneTrackDatabase("ibtracs", file_name=database_file)

assert tc.nstorms == 2323
assert tc.nstorms == 13330

0 comments on commit 0d42f29

Please sign in to comment.