Skip to content
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
31 changes: 19 additions & 12 deletions src/fire2a/meteo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
__revision__ = "$Format:%H$"

import sys
import csv
import os
from datetime import datetime, time, timedelta
from pathlib import Path

Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/test_meteo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))