Skip to content

Commit

Permalink
Merge pull request #39 from umr-lops/replace-datatree-by-array
Browse files Browse the repository at this point in the history
replace all import/usage of xarray-datatree by xarray.datatree
  • Loading branch information
agrouaze authored Nov 12, 2024
2 parents 57ea648 + 4581c3e commit 4c0184c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ Overview
........

**safe_s1** rely on `xarray.open_rasterio` and `rasterio` to read *digital_number* from SAFE
product to return an xarray-datatree.
product to return an xarray.datatree.

Luts are decoded from xml files following `ESA Sentinel-1 Product Specification`_.


`safe_s1.metadata.Sentinel1reader` is the main class and contains a xarray-datatree with the useful data. In the following example, you will find some additional functions and properties that can be useful.
`safe_s1.metadata.Sentinel1reader` is the main class and contains a xarray.datatree with the useful data. In the following example, you will find some additional functions and properties that can be useful.

Examples
........
Expand Down
23 changes: 23 additions & 0 deletions highleveltests/open_GRD_IW.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pdb
from safe_s1 import Sentinel1Reader, getconfig
import time
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug('start GRD test')
conf = getconfig.get_config()
subswath = conf['product_paths'][0]
print(subswath)
t0 = time.time()
if 'GRD' in subswath:
sub_reader = Sentinel1Reader(subswath)
else:
sub_reader = Sentinel1Reader('SENTINEL1_DS:'+subswath+':IW3')
elapse_t = time.time()-t0

dt = sub_reader.datatree
print('out of the reader')
print(dt)
print('time to read the SAFE through nfs: %1.2f sec'%elapse_t)
DN = sub_reader.load_digital_number(chunks={'pol':'VV','line':6000,'sample':8000})
print('DN',DN)
# pdb.set_trace()
2 changes: 1 addition & 1 deletion highleveltests/open_SLC_IW.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
dt = sub_reader.datatree
print('out of the reader')
print(dt)
print('time to read the SAFE through S3: %1.2f sec'%elapse_t)
print('time to read the SAFE through nfs: %1.2f sec'%elapse_t)
pdb.set_trace()
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ license = {text = "MIT"}
dependencies = [
"geopandas",
"numpy",
"xarray",
"xarray-datatree",
"xarray>=2024.10.0",
"lxml",
"rioxarray",
"jmespath",
Expand Down
20 changes: 13 additions & 7 deletions safe_s1/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
import yaml
from affine import Affine
from rioxarray import rioxarray

from . import sentinel1_xml_mappings
from .xml_parser import XmlParser
import logging
from safe_s1 import sentinel1_xml_mappings
from safe_s1.xml_parser import XmlParser
import xarray as xr
import datatree
import pandas as pd
import warnings


class Sentinel1Reader:

def __init__(self, name, backend_kwargs=None):
logging.debug('input name: %s',name)
if not isinstance(name, (str, os.PathLike)):
raise ValueError(f"cannot deal with object of type {type(name)}: {name}")
# gdal dataset name
Expand All @@ -29,13 +29,19 @@ def __init__(self, name, backend_kwargs=None):
"""Gdal dataset name"""
name_parts = self.name.split(':')
if len(name_parts) > 3:
logging.debug('windows case')
# windows might have semicolon in path ('c:\...')
name_parts[1] = ':'.join(name_parts[1:-1])
del name_parts[2:-1]
name_parts[1] = os.path.basename(name_parts[1])
self.short_name = ':'.join(name_parts)
logging.debug('short_name : %s',self.short_name)
"""Like name, but without path"""
self.path = ':'.join(self.name.split(':')[1:-1])
if len(name_parts) == 2:
self.path = self.name.split(':')[1]
else:
self.path = ':'.join(self.name.split(':')[1:-1])
logging.debug('path: %s',self.path)
# remove trailing slash in the safe path
if self.path[-1]=='/':
self.path = self.path.rstrip('/')
Expand Down Expand Up @@ -108,7 +114,7 @@ def __init__(self, name, backend_kwargs=None):
'antenna_pattern':self.antenna_pattern,
'swath_merging': self.swath_merging
}
self.dt = datatree.DataTree.from_dict(self._dict)
self.dt = xr.DataTree.from_dict(self._dict)
assert self.dt==self.datatree
else:
print('multidataset')
Expand Down Expand Up @@ -341,7 +347,7 @@ def datatree(self):
Returns
-------
datatree.DataTree
xr.DataTree
Contains data from the reader
"""
return self.dt
Expand Down

0 comments on commit 4c0184c

Please sign in to comment.