umr-lops / xsar

Synthetic Aperture Radar (SAR) Level-1 GRD python mapper for efficient xarray/dask based processing
https://cyclobs.ifremer.fr/static/sarwing_datarmor/xsar/
MIT License
29 stars 8 forks source link

`swaths` dataset missing in IW SLC example #206

Closed agrouaze closed 6 months ago

agrouaze commented 6 months ago

issue appers with the compilation of the notebook examples/xsar_tops_slc.ipynb

traceback

KeyError                                  Traceback (most recent call last)
/opt/conda-envs/dev/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, name)
   1447             variable = self._variables[name]
   1448         except KeyError:
-> 1449             _, name, variable = _get_virtual_variable(self._variables, name, self.dims)
   1450 

KeyError: 'swaths'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_1620036/3271660200.py in ?()
----> 1 s1ds.add_high_resolution_variables()
      2 s1ds.dataset

/home1/datahome/agrouaze/sources/git/xsar/src/xsar/sentinel1_dataset.py in ?(self, luts, patch_variable, skip_variables, load_luts, lazy_loading)
    500             self._dataset = self._dataset.merge(
    501                 self._load_from_geoloc(geoloc_vars, lazy_loading=lazy_loading))
    502 
    503 
--> 504             self.add_swath_number()
    505 
    506             if self.apply_recalibration:
    507                 self.add_gains(config["auxiliary_names"][self.sar_meta.short_name.split(":")[-2][0:3]][self.aux_config_name]["AUX_CAL"],

/home1/datahome/agrouaze/sources/git/xsar/src/xsar/sentinel1_dataset.py in ?(self)
    556         flag_tab = xr.DataArray(np.zeros_like(self._dataset.elevation, dtype=int), coords={
    557                                 'line': self._dataset.coords["line"], 'sample': self._dataset.coords["sample"]})
    558 
    559         # Supposons que dataset.swaths ait 45 éléments comme mentionné
--> 560         for i in range(len(self.datatree['swath_merging'].ds['swaths'])):
    561             y_min, y_max = self.datatree['swath_merging']['firstAzimuthLine'][
    562                 i], self.datatree['swath_merging']['lastAzimuthLine'][i]
    563             x_min, x_max = self.datatree['swath_merging']['firstRangeSample'][

/opt/conda-envs/dev/lib/python3.10/site-packages/datatree/datatree.py in ?(self, key)
    167     def __getitem__(self, key) -> DataArray:
    168         # TODO call the `_get_item` method of DataTree to allow path-like access to contents of other nodes
    169         # For now just call Dataset.__getitem__
--> 170         return Dataset.__getitem__(self, key)

/opt/conda-envs/dev/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, key)
   1541         """
   1542         if utils.is_dict_like(key):
   1543             return self.isel(**key)
   1544         if utils.hashable(key):
-> 1545             return self._construct_dataarray(key)
   1546         if utils.iterable_of_hashable(key):
   1547             return self._copy_listed(key)
   1548         raise ValueError(f"Unsupported key-type {type(key)}")

/opt/conda-envs/dev/lib/python3.10/site-packages/xarray/core/dataset.py in ?(self, name)
   1445 
   1446         try:
   1447             variable = self._variables[name]
   1448         except KeyError:
-> 1449             _, name, variable = _get_virtual_variable(self._variables, name, self.dims)
   1450 
   1451         needed_dims = set(variable.dims)
   1452 

/opt/conda-envs/dev/lib/python3.10/site-packages/xarray/core/dataset.py in ?(variables, key, dim_sizes)
    210         raise KeyError(key)
    211 
    212     split_key = key.split(".", 1)
    213     if len(split_key) != 2:
--> 214         raise KeyError(key)
    215 
    216     ref_name, var_name = split_key
    217     ref_var = variables[ref_name]

KeyError: 'swaths'
agrouaze commented 6 months ago

@vincelhx 2 questions:

  1. I can't see where in the xsar source code the swaths dataset is defined in the datatree, is there a piece of code missing?
  2. Are you OK with the fact that such swaths dataset aimaing at giving information on the swath merging, is only relevant for GRD product?
agrouaze commented 6 months ago

I found explanation for point 1.: https://github.com/umr-lops/xarray-safe-s1/blob/2fc774caf1281e4e402b675b273f046fb087720b/safe_s1/reader.py#L106 and https://github.com/umr-lops/xarray-safe-s1/blob/2fc774caf1281e4e402b675b273f046fb087720b/safe_s1/reader.py#L515 For is means that dataset['swath_merging'] is supposed to be present only for GRD. This security was not added in xsar so far.

agrouaze commented 6 months ago

The reason of the bug is now understood,GRD SAFE path given with a trailing slash leads to self.product='XXX' then swath_merging dataset is an empty xarray.Dataset.

agrouaze commented 6 months ago

fixed with https://github.com/umr-lops/xarray-safe-s1/pull/28