Skip to content

Commit

Permalink
Merge pull request #80 from vincelhx/dev_streaks_vinc
Browse files Browse the repository at this point in the history
update xsarsea
  • Loading branch information
vincelhx authored Nov 25, 2024
2 parents 6c60e06 + ad94f1b commit aec8409
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
39 changes: 30 additions & 9 deletions src/xsarsea/gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,31 @@ def histogram(self):

@staticmethod
def _sigma0_resample(sigma0, factor):

if factor == 1:
return sigma0
__sigma0 = sigma0.isel(line=slice(0, None, factor), sample=slice(0, None, factor)).copy(
True
)
__sigma0.values[::] = cv2.resize(sigma0.values, __sigma0.shape[::-1], cv2.INTER_AREA)
return __sigma0

def compute_coords(coords, factor):
n = (len(coords) // factor) * factor
coords_trimmed = coords[:n]
coords_reshaped = coords_trimmed.reshape(-1, factor)
return coords_reshaped.mean(axis=1)

target_line = int(np.floor(sigma0.sizes['line'] / factor))
target_sample = int(np.floor(sigma0.sizes['sample'] / factor))

resized_values = cv2.resize(
sigma0.values, (target_sample, target_line), interpolation=cv2.INTER_AREA)

line_coords = compute_coords(sigma0.line.values, factor)
sample_coords = compute_coords(sigma0.sample.values, factor)
other_coords = {coord: sigma0.coords[coord]
for coord in sigma0.coords if coord not in ['line', 'sample']}

return xr.DataArray(resized_values, dims=['line', 'sample'],
coords={'line': line_coords,
'sample': sample_coords, **other_coords},
attrs=sigma0.attrs)

class PlotGradients:
"""Plotting class"""
Expand Down Expand Up @@ -637,7 +654,8 @@ def _conv2d(in1, in2=in2, mode="same", boundary=boundary, fillvalue=fillvalue):
def smoothing(image):
# filtre gaussien

B2 = np.asmatrix("[1,2,1; 2,4,2; 1,2,1]", float) * 1 / 16
B2 = np.asmatrix('[1,2,1; 2,4,2; 1,2,1]', float) * 1 / 16

B2 = np.array(B2)

_image = convolve2d(image, B2, boundary="symm")
Expand All @@ -661,7 +679,8 @@ def R2(image):
resampled
"""

B2 = np.asmatrix("[1,2,1; 2,4,2; 1,2,1]", float) * 1 / 16
B2 = np.asmatrix('[1,2,1; 2,4,2; 1,2,1]', float) * 1 / 16

B2 = np.array(B2)
B4 = signal.convolve(B2, B2)

Expand Down Expand Up @@ -694,11 +713,13 @@ def Mean(image):
xarray.DataArray
smoothed
"""
B2 = np.asmatrix("[1,2,1; 2,4,2; 1,2,1]", float) * 1 / 16
B2 = np.asmatrix('[1,2,1; 2,4,2; 1,2,1]', float) * 1 / 16
B2 = np.array(B2)
B4 = signal.convolve(B2, B2)

B22 = np.asmatrix("[1,0,2,0,1;0,0,0,0,0;2,0,4,0,2;0,0,0,0,0;1,0,2,0,1]", float) * 1 / 16
B22 = np.asmatrix(
'[1,0,2,0,1;0,0,0,0,0;2,0,4,0,2;0,0,0,0,0;1,0,2,0,1]', float) * 1/16

B42 = signal.convolve(B22, B22)

_image = convolve2d(image, B4, boundary="symm")
Expand Down
2 changes: 2 additions & 0 deletions src/xsarsea/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import aiohttp
import fsspec
import yaml

try:
from importlib_resources import files
except:
from importlib.resources import files # new syntaxe
logger = logging.getLogger("xsarsea")
logger.addHandler(logging.NullHandler())

mem_monitor = True

try:
Expand Down
2 changes: 1 addition & 1 deletion src/xsarsea/windspeed/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pandas as pd
import xarray as xr

logger = logging.getLogger("xsarsea.windspeed")
logger = logging.getLogger('xsarsea.windspeed.models')


class Model:
Expand Down
32 changes: 5 additions & 27 deletions src/xsarsea/windspeed/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import warnings

import numpy as np

import yaml
try:
from importlib_resources import files
except:
from importlib.resources import files # new syntaxe

logger = logging.getLogger("xsarsea.windspeed")
# logger.addHandler(logging.NullHandler())
# logger must print
logger.setLevel(logging.DEBUG)
import logging
logger = logging.getLogger('xsarsea.windspeed.utils')
logger.setLevel(logging.INFO)



def get_dsig(name, inc, sigma0_cr, nesz_cr):
Expand Down Expand Up @@ -131,26 +132,3 @@ def _noise_flattening_1row(noise_row, inc_row):
# incidence is almost constant along line dim, so we can make it 1D
return np.apply_along_axis(_noise_flattening_1row, 1, noise, np.nanmean(inc, axis=0))


def _load_config_luts(config_path):
"""
load config from default xsarsea/windspeed.config_luts_default_direct_01_01_10.yml file or user ~/.xsarsea/config.yml
Returns
-------
dict
"""

user_config_file = open(config_path)
default_config_file = (
files("xsarsea").joinpath("windspeed").joinpath("config_luts_default_direct_01_01_10.yml")
)

if os.exists(user_config_file.exists):
config_file = user_config_file
else:
# logger.info(f"Using default config file {default_config_file}")
# config_file = default_config_file
raise FileNotFoundError(f"Config file {user_config_file} not found")
config = yaml.load(open(config_file), Loader=yaml.FullLoader)

return config
9 changes: 9 additions & 0 deletions src/xsarsea/windspeed/windspeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

import numpy as np
import xarray as xr

import logging
from numba import complex128, float64, guvectorize, void

from xsarsea.utils import timing
from xsarsea.windspeed.models import get_model
from xsarsea.windspeed.utils import logger

logger = logging.getLogger('xsarsea.windspeed')
logger.setLevel(logging.DEBUG)

@timing(logger.debug)
def invert_from_model(
Expand Down Expand Up @@ -295,6 +299,9 @@ def __invert_from_model_vect(*args):
return out_co.reshape(ori_shape), out_cr.reshape(ori_shape)

else:
logger.debug(
'using guvectorize')

# fastmath can be used, but we will need nan handling
__invert_from_model_vect = timing(logger=logger.debug)(
guvectorize(
Expand Down Expand Up @@ -360,6 +367,8 @@ def _invert_from_model_any(inc, sigma0_co_db, sigma0_cr_db, dsig_cr, ancillary_w
raise TypeError

except (ImportError, TypeError):
logger.debug('Finally invert with numpy')

# use numpy array, but store in xarray
da_ws_co.data, da_ws_cr.data = _invert_from_model_numpy(
np.asarray(inc),
Expand Down

0 comments on commit aec8409

Please sign in to comment.