Create Hindcast Wave Wind

Source Code

create_hindcast_wave_wind.py
  1"""Read wave data from nora3 and generate metocean hindcast readable from SIMA"""
  2
  3from pathlib import Path
  4import time
  5import numpy as np
  6import xarray as xr
  7from dmt.dmt_writer import DMTWriter
  8from metocean_api import ts
  9import simapy.metocean.hindcast as hc
 10
 11
 12def __create_wave(wave_name, hs, tp, direction):
 13    # NORA3_wave_wind: North East Down, wave_going_to
 14    # SIMA MET: North East Down, wave coming from
 15    dir_sima = (180.0 + direction) % 360.0
 16    return hc.StochasticWave(name=wave_name, hs=hs, tp=tp, direction=dir_sima)
 17
 18
 19def __create_hindcast(hc_name, hc_values, lat_pos, lon_pos):
 20    waves = []
 21
 22    waves.append(
 23        __create_wave("total", hc_values["hs"], hc_values["tp"], hc_values["thq"])
 24    )
 25    waves.append(
 26        __create_wave(
 27            "windSea", hc_values["hs_sea"], hc_values["tp_sea"], hc_values["thq_sea"]
 28        )
 29    )
 30    waves.append(
 31        __create_wave(
 32            "swell",
 33            hc_values["hs_swell"],
 34            hc_values["tp_swell"],
 35            hc_values["thq_swell"],
 36        )
 37    )
 38
 39    # Time is given in Unix epoch
 40    # convert dates to strings
 41    dates = hc_values["time"].astype("datetime64[s]")
 42    sdates = np.datetime_as_string(dates, unit="h", timezone="UTC").astype("|S")
 43
 44    hindcast = hc.Hindcast()
 45    hindcast.description = hindcast.description = (
 46        f"Collected using Norway MET metocean-api with product {product}"
 47    )
 48    hindcast.name = hc_name
 49    hindcast.date = sdates
 50    hindcast.latitude = lat_pos
 51    hindcast.longitude = lon_pos
 52    hindcast.wave = waves
 53
 54    speed = hc_values["wind_speed"]
 55    # Wind direction in fixed heights above surface,wind_from_direction	degrees clockwise from north (meteorological)
 56    # SIMA: North East Down, wind coming from, same as MET
 57    direction = hc_values["wind_direction"]
 58    heights = hc_values["height"]
 59    winds = []
 60    for i, height in enumerate(heights):
 61        wind = hc.StochasticWind()
 62        level = height.values
 63        wind.level = level
 64        wind.name = f"{level}m"
 65        wind.speed = speed[:, i].values
 66        wind.direction = direction[:, i].values
 67        winds.append(wind)
 68
 69    hindcast.wind = winds
 70    return hindcast
 71
 72
 73if __name__ == "__main__":
 74    start_date = "2017-01-19"
 75    end_date = "2017-01-20"
 76
 77    lon_pos = 5.835813
 78    lat_pos = 64.731729
 79
 80    product = "NORA3_wind_wave"
 81
 82    name = f"hindcast-{product}-{start_date}-{end_date}"
 83    requested_values = [
 84        "hs",
 85        "tp",
 86        "thq",
 87        "hs_sea",
 88        "tp_sea",
 89        "thq_sea",
 90        "hs_swell",
 91        "tp_swell",
 92        "thq_swell",
 93        "wind_speed",
 94        "wind_direction",
 95    ]
 96
 97    output_dir = Path("./output/simamet")
 98    output_dir.mkdir(exist_ok=True, parents=True)
 99    nc_file = str(output_dir / f"{name}.nc")
100
101    df_ts = ts.TimeSeries(
102        lon=lon_pos,
103        lat=lat_pos,
104        datafile=nc_file,
105        start_time=start_date,
106        end_time=end_date,
107        product=product,
108    )
109
110    # Start timing
111    start = time.time()
112    df_ts.import_data(save_csv=False, save_nc=True, use_cache=True)
113
114    # End timing and print elapsed time
115    end = time.time()
116    print("Elapsed time: " + str(end - start) + " seconds")
117
118    with xr.open_dataset(nc_file) as values:
119        hindcast_data = __create_hindcast(name, values, df_ts.lat, df_ts.lon)
120        dates = values["time"].values
121        path = Path(f"./output/simamet/{name}.h5")
122        path.parent.mkdir(parents=True, exist_ok=True)
123        DMTWriter().write(hindcast_data, path)
124        print(f"Written to {path.resolve()}")
125