Skip to content

Commit

Permalink
cmod7
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelhx committed May 29, 2024
1 parent 8e34540 commit 6a44880
Show file tree
Hide file tree
Showing 6 changed files with 2,191 additions and 237 deletions.
2,116 changes: 2,083 additions & 33 deletions docs/examples/windspeed_inversion.ipynb

Large diffs are not rendered by default.

195 changes: 0 additions & 195 deletions src/scripts/wind_speed_retrieval.py

This file was deleted.

6 changes: 3 additions & 3 deletions src/xsarsea/windspeed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""
windspeed module, for retrieving wind speed from sigma0 and models.
"""
__all__ = ['invert_from_model', 'available_models', 'get_model', 'register_all_sarwing_luts', 'register_all_nc_luts', 'nesz_flattening']
__all__ = ['invert_from_model', 'available_models', 'get_model', 'register_cmod7',
'register_all_sarwing_luts', 'register_all_nc_luts', 'nesz_flattening']
from .windspeed import invert_from_model
from .models import available_models, get_model, register_all_nc_luts
from .sarwing_luts import register_all_sarwing_luts
from .other_luts import register_cmod7
from .utils import nesz_flattening, get_dsig
from . import gmfs
from . import gmfs_impl


1 change: 0 additions & 1 deletion src/xsarsea/windspeed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def _normalize_lut(self, lut, **kwargs):
resolution = 'high'

lut_resolution = lut.attrs['resolution']

if resolution is not None and resolution != lut_resolution:
if resolution == 'high':
# high resolution steps
Expand Down
105 changes: 105 additions & 0 deletions src/xsarsea/windspeed/other_luts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import os
import xarray as xr
from .utils import logger
import numpy as np
from .models import LutModel


class Cmod7Model(LutModel):

_name_prefix = 'gmf_'
_priority = 1

def __init__(self, name, path, **kwargs):
super().__init__(name, **kwargs)
self.path = path

def _raw_lut(self, **kwargs):
if not os.path.isdir(self.path):
raise FileNotFoundError(self.path)

logger.info('load gmf lut from %s' % self.path)

sigma0_path = os.path.join(self.path, 'gmf_cmod7_vv.dat_little_endian')
try:
sigma0 = np.fromfile(sigma0_path, dtype=np.float32)
except FileNotFoundError:
raise FileNotFoundError(sigma0_path)

# Dimensions of GMF table
m = 250 # wind speed min/max = 0.2-50 (step 0.2) [m/s] --> 250 pts
n = 73 # dir min/max = 0-180 (step 2.5) [deg] --> 73 pts
p = 51 # inc min/max = 16-66 (step 1) [deg] --> 51 pts
# Remove head and tail
sigma0 = sigma0[1:-1]

# To access the table as a three-dimensional Fortran-ordered m x n x p matrix,
# reshape it
sigma0 = sigma0.reshape((m, n, p), order="F")

self.wspd_step_lr = 0.2
self.inc_step_lr = 1
self.phi_step_lr = 2.5

self.inc_range_lr = [16, 66]
self.wspd_range_lr = [0.2, 50.0]
self.phi_range_lr = [0, 180]

wspd = np.arange(
self.wspd_range_lr[0], self.wspd_range_lr[1] + self.wspd_step_lr, self.wspd_step_lr)
inc = np.arange(
self.inc_range_lr[0], self.inc_range_lr[1] + self.inc_step_lr, self.inc_step_lr)
phi = np.arange(
self.phi_range_lr[0], self.phi_range_lr[1] + self.phi_step_lr, self.phi_step_lr)

dims = ['wspd', 'phi', 'incidence']
final_dims = ['incidence', 'wspd', 'phi']
coords = {'incidence': inc, 'phi': phi, 'wspd': wspd}

self.wspd_step = 0.2
self.inc_step = 0.2
self.phi_step = 1
self.wspd_range = self.wspd_range_lr
self.inc_range = self.inc_range_lr
self.phi_range = self.phi_range_lr

da_sigma0_db = xr.DataArray(
10*np.log10(sigma0), dims=dims, coords=coords)

da_sigma0_db.name = 'sigma0_gmf'
da_sigma0_db.attrs['units'] = 'dB'
da_sigma0_db.attrs['model'] = self.name
da_sigma0_db.attrs['resolution'] = 'low'

return da_sigma0_db.transpose(*final_dims)


def register_cmod7(topdir):
"""
Register cmod7.
This function return nothing. See `xsarsea.windspeed.available_models` to see registered models.
Parameters
----------
topdir: str
top dir path to cmod7 luts.
Examples
--------
Notes
_____
See Also
--------
xsarsea.windspeed.available_models
xsarsea.windspeed.gmfs.GmfModel.register
"""

path = topdir
name = Cmod7Model._name_prefix + "cmod7"

cmod7_model = Cmod7Model(name, path, pol="VV")
5 changes: 0 additions & 5 deletions src/xsarsea/windspeed/windspeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ def __invert_from_model_1d(inc_1d, sigma0_co_db_1d, sigma0_cr_db_1d, dsig_cr_1d,
lut_idx = (iJ_co // J_co.shape[-1], iJ_co % J_co.shape[-1])
wspd_co = np_wspd_lut[lut_idx]
wphi_co = np_phi_lut[lut_idx]

if phi_180:
# two phi solution (phi & -phi). choose closest from ancillary wind

Expand All @@ -228,11 +227,7 @@ def __invert_from_model_1d(inc_1d, sigma0_co_db_1d, sigma0_cr_db_1d, dsig_cr_1d,
else:
wind_co = wspd_co * np.exp(1j * np.deg2rad(wphi_co))

# wind_co = one_ancillary_wind

else:
# no copol. use ancillary wind as wspd_co (if available)
# wind_co = one_ancillary_wind
wind_co = np.nan * 1j

if not np.isnan(one_sigma0_cr_db) and not np.isnan(one_dsig_cr):
Expand Down

0 comments on commit 6a44880

Please sign in to comment.