Skip to content

Commit a7a971f

Browse files
authored
Merge pull request #38 from fire2a/37-update-wind-direction-convention
update wind direction convention
2 parents 33c743f + 23a4e89 commit a7a971f

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

src/fire2a/meteo.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
__revision__ = "$Format:%H$"
1919

2020
import sys
21+
import csv
22+
import os
2123
from datetime import datetime, time, timedelta
2224
from pathlib import Path
2325

@@ -40,6 +42,22 @@
4042
# assert (ruta_data / "Estaciones.csv").is_file()
4143

4244

45+
def transform_weather_file(filename):
46+
"""Flip the direction of the wind in a weather file. Column containing wind direction must be called "WD".\
47+
this changes the contents of the given file."""
48+
temp_name = f"{filename}_tmp"
49+
with open(filename, newline='') as csvfile, open(temp_name, mode='w') as outfile:
50+
reader = csv.DictReader(csvfile)
51+
writer = csv.DictWriter(outfile, reader.fieldnames)
52+
writer.writeheader()
53+
for i in reader:
54+
i['WD'] = flip_wind(float(i["WD"]))
55+
writer.writerow(i)
56+
if os.path.isfile(filename):
57+
os.remove(filename)
58+
os.rename(temp_name, filename)
59+
60+
4361
def is_qgis_running():
4462
qgis = False
4563
try:
@@ -87,16 +105,7 @@ def distance(fila, lat, lon):
87105
return eucl_distance(fila, lat, lon)
88106

89107

90-
def meteo_to_c2f(alfa):
91-
"""@private"""
92-
if alfa >= 0 and alfa < 180:
93-
return round(alfa + 180, 2)
94-
elif alfa >= 180 and alfa <= 360:
95-
return round(alfa - 180, 2)
96-
return np.nan
97-
98-
99-
def barlo_sota(a):
108+
def flip_wind(a):
100109
"""Leeward to Windward wind angle direction flip. Barlovento a sotavento. Downwind to upwind."""
101110
return round((a + 180) % 360, 2)
102111

@@ -215,8 +224,6 @@ def generate(
215224
# drop station
216225
# TODO no drop ?
217226
# chosen_meteo = chosen_meteo.drop(columns=["station"])
218-
# wind direction
219-
chosen_meteo.loc[:, "WD"] = chosen_meteo["WD"].apply(barlo_sota)
220227
# scenario name
221228
chosen_meteo.loc[:, "Scenario"] = "DMC" if numsims == 1 else f"DMC_{i+1}"
222229
# TODO sobra: datetime format

tests/test_meteo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def test_weather_lenght(tmp_path):
3333
def test_invert_wind():
3434
from numpy import linspace
3535

36-
from fire2a.meteo import barlo_sota, meteo_to_c2f
36+
from fire2a.meteo import flip_wind
3737

38-
for a in linspace(1, 360, 12):
39-
assert barlo_sota(a) - meteo_to_c2f(a) < 0.01, "Conversion incorrecta"
38+
for a in linspace(0, 359, 12):
39+
assert round(a,2) - flip_wind(flip_wind(a)) < 0.01, "Conversion incorrecta"
4040
# for a in np.linspace(-360, 720, 12):
4141
# print(a, barlo_sota(a), meteo_to_c2f(a))

0 commit comments

Comments
 (0)