diff --git a/src/fire2a/meteo.py b/src/fire2a/meteo.py index 3720640..ac76990 100644 --- a/src/fire2a/meteo.py +++ b/src/fire2a/meteo.py @@ -18,6 +18,8 @@ __revision__ = "$Format:%H$" import sys +import csv +import os from datetime import datetime, time, timedelta from pathlib import Path @@ -40,6 +42,22 @@ # assert (ruta_data / "Estaciones.csv").is_file() +def transform_weather_file(filename): + """Flip the direction of the wind in a weather file. Column containing wind direction must be called "WD".\ + this changes the contents of the given file.""" + temp_name = f"{filename}_tmp" + with open(filename, newline='') as csvfile, open(temp_name, mode='w') as outfile: + reader = csv.DictReader(csvfile) + writer = csv.DictWriter(outfile, reader.fieldnames) + writer.writeheader() + for i in reader: + i['WD'] = flip_wind(float(i["WD"])) + writer.writerow(i) + if os.path.isfile(filename): + os.remove(filename) + os.rename(temp_name, filename) + + def is_qgis_running(): qgis = False try: @@ -87,16 +105,7 @@ def distance(fila, lat, lon): return eucl_distance(fila, lat, lon) -def meteo_to_c2f(alfa): - """@private""" - if alfa >= 0 and alfa < 180: - return round(alfa + 180, 2) - elif alfa >= 180 and alfa <= 360: - return round(alfa - 180, 2) - return np.nan - - -def barlo_sota(a): +def flip_wind(a): """Leeward to Windward wind angle direction flip. Barlovento a sotavento. Downwind to upwind.""" return round((a + 180) % 360, 2) @@ -215,8 +224,6 @@ def generate( # drop station # TODO no drop ? # chosen_meteo = chosen_meteo.drop(columns=["station"]) - # wind direction - chosen_meteo.loc[:, "WD"] = chosen_meteo["WD"].apply(barlo_sota) # scenario name chosen_meteo.loc[:, "Scenario"] = "DMC" if numsims == 1 else f"DMC_{i+1}" # TODO sobra: datetime format diff --git a/tests/test_meteo.py b/tests/test_meteo.py index cf7ea6f..93645dd 100644 --- a/tests/test_meteo.py +++ b/tests/test_meteo.py @@ -33,9 +33,9 @@ def test_weather_lenght(tmp_path): def test_invert_wind(): from numpy import linspace - from fire2a.meteo import barlo_sota, meteo_to_c2f + from fire2a.meteo import flip_wind - for a in linspace(1, 360, 12): - assert barlo_sota(a) - meteo_to_c2f(a) < 0.01, "Conversion incorrecta" + for a in linspace(0, 359, 12): + assert round(a,2) - flip_wind(flip_wind(a)) < 0.01, "Conversion incorrecta" # for a in np.linspace(-360, 720, 12): # print(a, barlo_sota(a), meteo_to_c2f(a))