Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wind convention parameters #79

Merged
merged 11 commits into from
Sep 23, 2024
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 360 # 6 hours limit for the job

steps:
# Checkout the code
- name: Checkout code
Expand All @@ -21,25 +21,26 @@ jobs:
- uses: mamba-org/setup-micromamba@v1
with:
micromamba-version: "1.5.9-1" # any version from https://github.com/mamba-org/micromamba-releases
channels: tcevaer, conda-forge, defaults
init-shell: bash
post-cleanup: "all"

- name: Create environment and install tools
run: micromamba create -n grdwind_env pytest conda-build boa python=3.10 -y
run: micromamba create -n grdwind_env pytest conda-build boa python=3.10 -y -c tcevaer -c conda-forge

- name: Build package
run: |
cd recipe
eval "$(micromamba shell hook --shell bash)"
micromamba activate grdwind_env
conda mambabuild .
conda mambabuild . -c tcevaer -c conda-forge

# Install the built package into the environment
- name: Install the built package
run: |
eval "$(micromamba shell hook --shell bash)"
micromamba activate grdwind_env
conda install --use-local grdwindinversion -y
conda install --use-local grdwindinversion -y -c tcevaer -c conda-forge

# Cache the test data if previously downloaded (up to 10 GB limit for the cache)
# WARNING : modify the key if the data is modified !!
Expand Down
1 change: 1 addition & 0 deletions grdwindinversion/config_prod.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
no_subdir: True
winddir_convention: "meteorological"
S1A:
GMF_VV_NAME: "gmf_cmod5n"
GMF_VH_NAME: "gmf_s1_v2"
Expand Down
256 changes: 179 additions & 77 deletions grdwindinversion/inversion.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion grdwindinversion/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from grdwindinversion.inversion import makeL2
from grdwindinversion.utils import get_memory_usage
from grdwindinversion.utils_memory import get_memory_usage
import grdwindinversion
import time
import logging
Expand Down
113 changes: 77 additions & 36 deletions grdwindinversion/utils.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,83 @@
def get_memory_usage(unit='Go', var='ru_maxrss', force_psutil=False):
import logging
import xsarsea


def check_incidence_range(incidence, models, **kwargs):
"""
var str: ru_maxrss or ru_ixrss or ru_idrss or ru_isrss or current
Check if the incidence range of the dataset is within the range of the LUT of the model.
If not, warn the user : inversion will be approximate.

Parameters
----------
incidence : xr.DataArray
incidence angle in degrees
models : list of str
list of model names

Returns
-------
list of bool
for each model,
True if the incidence range is within the range of the LUT of the model (correct)
False otherwise
"""
if isinstance(models, str):
models = [models]
elif not isinstance(models, list):
raise TypeError("models should be a string or a list of strings")

rets = []
for model_name in models:
lut_range = xsarsea.windspeed.get_model(model_name).inc_range
if 'inc_range' in kwargs:
logging.debug(
f"GMF {model_name} inc_range will be changed by kwargs to {kwargs['inc_range']}")
lut_range = kwargs['inc_range']

inc_range = [incidence.values.min(), incidence.values.max()]
if (inc_range[0] >= lut_range[0] and inc_range[1] <= lut_range[1]):
rets.append(True)
else:
logging.warn(
f"incidence range {inc_range} is not within the range of the LUT of the model {model_name} {lut_range} : inversion will be approximate using LUT minmium|maximum incidences")
rets.append(False)

return rets


def get_pol_ratio_name(model_co):
"""
Return polarization ration name of copol model

Parameters
----------
model_co : str
copol model name

Returns
-------
str
if pol = 'HH', return polarization ratio name ; else return '/'
"""
if unit == 'Go':
factor = 1000000.
elif unit == 'Mo':
factor = 1000.
elif unit == 'Ko':
factor = 1.

model = xsarsea.windspeed.get_model(model_co)
if model.pol == 'HH':
try:
import re

def check_format(s):
pattern = r'^([a-zA-Z0-9]+)_R(high|low)_hh_([a-zA-Z0-9_]+)$'
match = re.match(pattern, s)
if match:
vvgmf, res, polrationame = match.groups()
return polrationame
else:
logging.warn(
f"String format is not correct for polarization ratio name = {s}\nReturning '/'")
return "/"
get_pol_ratio_name = check_format(model_co)
return get_pol_ratio_name
except AttributeError:
return "not_written_in_lut"
else:
raise Exception('not handle unit')

try:
if force_psutil:
on_purpose_error
import resource
mems = {}
mems['ru_maxrss'] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / factor
mems['ru_ixrss'] = resource.getrusage(resource.RUSAGE_SELF).ru_ixrss / factor
mems['ru_idrss'] = resource.getrusage(resource.RUSAGE_SELF).ru_idrss / factor
mems['ru_isrss'] = resource.getrusage(resource.RUSAGE_SELF).ru_isrss / factor
mems['current'] = getCurrentMemoryUsage() / factor
# memory_used_go = resource.getrusage(resource.RUSAGE_SELF).get(var) /factor
memory_used_go = mems[var]
except: # on windows resource is not usable
import psutil
memory_used_go = psutil.virtual_memory().used / factor / 1000.
str_mem = 'RAM usage: %1.1f %s' % (memory_used_go, unit)
return str_mem


def getCurrentMemoryUsage():
''' Memory usage in kB '''

with open('/proc/self/status') as f:
memusage = f.read().split('VmRSS:')[1].split('\n')[0][:-3]

return int(memusage.strip())
return '/'
46 changes: 46 additions & 0 deletions grdwindinversion/utils_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
def get_memory_usage(unit='Go', var='ru_maxrss', force_psutil=False):
"""
var str: ru_maxrss or ru_ixrss or ru_idrss or ru_isrss or current
Returns
-------

"""
if unit == 'Go':
factor = 1000000.
elif unit == 'Mo':
factor = 1000.
elif unit == 'Ko':
factor = 1.
else:
raise Exception('not handle unit')

try:
if force_psutil:
on_purpose_error
import resource
mems = {}
mems['ru_maxrss'] = resource.getrusage(
resource.RUSAGE_SELF).ru_maxrss / factor
mems['ru_ixrss'] = resource.getrusage(
resource.RUSAGE_SELF).ru_ixrss / factor
mems['ru_idrss'] = resource.getrusage(
resource.RUSAGE_SELF).ru_idrss / factor
mems['ru_isrss'] = resource.getrusage(
resource.RUSAGE_SELF).ru_isrss / factor
mems['current'] = getCurrentMemoryUsage() / factor
# memory_used_go = resource.getrusage(resource.RUSAGE_SELF).get(var) /factor
memory_used_go = mems[var]
except: # on windows resource is not usable
import psutil
memory_used_go = psutil.virtual_memory().used / factor / 1000.
str_mem = 'RAM usage: %1.1f %s' % (memory_used_go, unit)
return str_mem


def getCurrentMemoryUsage():
''' Memory usage in kB '''

with open('/proc/self/status') as f:
memusage = f.read().split('VmRSS:')[1].split('\n')[0][:-3]

return int(memusage.strip())
1 change: 1 addition & 0 deletions tests/config_test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
no_subdir: True
convention: "meteorological"
S1A:
GMF_VV_NAME: "gmf_cmod5n"
GMF_VH_NAME: "gmf_s1_v2"
Expand Down
Loading