Skip to content

Commit

Permalink
Fix open_orbit_dataset and ope_attitude_dataset (#10)
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
aurghs authored Apr 14, 2021
1 parent af4e440 commit 796f762
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 34 deletions.
26 changes: 26 additions & 0 deletions tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,32 @@ def test_open_dataset_root() -> None:
assert isinstance(res, xr.Dataset)


def test_open_dataset_orbit() -> None:
annotation_path = (
DATA_FOLDER
/ "S1B_IW_SLC__1SDV_20210401T052622_20210401T052650_026269_032297_EFA4.SAFE"
/ "annotation"
/ "s1b-iw1-slc-vv-20210401t052624-20210401t052649-026269-032297-004.xml"
)
res = xr.open_dataset(annotation_path, engine="sentinel-1", group="orbit") # type: ignore

assert isinstance(res, xr.Dataset)
assert set(res.dims) == {"time"}


def test_open_dataset_attitude() -> None:
annotation_path = (
DATA_FOLDER
/ "S1B_IW_SLC__1SDV_20210401T052622_20210401T052650_026269_032297_EFA4.SAFE"
/ "annotation"
/ "s1b-iw1-slc-vv-20210401t052624-20210401t052649-026269-032297-004.xml"
)
res = xr.open_dataset(annotation_path, engine="sentinel-1", group="attitude") # type: ignore

assert isinstance(res, xr.Dataset)
assert set(res.dims) == {"time"}


def test_open_dataset_gcp() -> None:
annotation_path = (
DATA_FOLDER
Expand Down
12 changes: 6 additions & 6 deletions xarray_sentinel/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"roll": {"units": "degrees"},
"yaw": {"units": "degrees"},
"time": {},
"x": {"units": "m"},
"y": {"units": "m"},
"z": {"units": "m"},
"vx": {"units": "m s-1"},
"vy": {"units": "m s-1"},
"vz": {"units": "m s-1"},
"x": {"units": "m", "long_name": "position_x"},
"y": {"units": "m", "long_name": "position_y"},
"z": {"units": "m", "long_name": "position_z"},
"vx": {"units": "m s-1", "long_name": "velocity_x"},
"vy": {"units": "m s-1", "long_name": "velocity_y"},
"vz": {"units": "m s-1", "long_name": "velocity_z"},
}


Expand Down
10 changes: 5 additions & 5 deletions xarray_sentinel/esa_safe.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def parse_swath_timing(annotation_path: PathType,) -> T.List[T.Dict[str, T.Any]]


def open_manifest(
product_folder: T.Union[str, "os.PathLike[str]"]
product_path: T.Union[str, "os.PathLike[str]"]
) -> ElementTree.ElementTree:
product_folder = pathlib.Path(product_folder)
if product_folder.is_dir():
product_folder = product_folder / "manifest.safe"
return ElementTree.parse(product_folder)
product_path = pathlib.Path(product_path)
if product_path.is_dir():
product_path = product_path / "manifest.safe"
return ElementTree.parse(product_path)


def parse_manifest_sentinel1(
Expand Down
57 changes: 34 additions & 23 deletions xarray_sentinel/sentinel1.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,25 @@ def open_gcp_dataset(product_path: str) -> xr.Dataset:
def open_attitude_dataset(product_path: str) -> xr.Dataset:
attitude = esa_safe.parse_attitude(product_path)
shape = len(attitude)
variables = ["q0", "q1", "q2", "wx", "wy", "wz", "pitch", "roll", "yaw", "time"]
data_vars: T.Dict[str, T.List[T.Any]] = {var: [] for var in variables}

variables = ["q0", "q1", "q2", "wx", "wy", "wz", "pitch", "roll", "yaw"]
time: T.List[T.Any] = []
data_vars: T.Dict[str, T.List[T.Any]] = {var: ("time", []) for var in variables} # type: ignore
for k in range(shape):
time.append(attitude[k]["time"])
for var in variables:
data_vars[var].append((attitude[k][var]))
data_vars[var][1].append(attitude[k][var])

coords = {
"time": ("time", time, {"standard_name": "time", "long_name": "time"},),
}
ds = xr.Dataset(
data_vars=data_vars, # type: ignore
attrs={"Conventions": "CF-1.7"},
coords=coords, # type: ignore
)

ds = conventions.update_attributes(ds)
ds = ds.update({"time": ds.time.astype(np.datetime64)})
return ds


Expand All @@ -100,45 +106,46 @@ def open_orbit_dataset(product_path: str) -> xr.Dataset:
shape = len(orbit)

reference_system = orbit[0]["frame"]
data_vars: T.Dict[str, T.List[T.Any]] = {
"time": [],
"x": [],
"y": [],
"z": [],
"vx": [],
"vy": [],
"vz": [],
}
variables = ["x", "y", "z", "vx", "vy", "vz"]
data_vars: T.Dict[str, T.List[T.Any]] = {var: ("time", []) for var in variables} # type: ignore
time: T.List[T.Any] = []
for k in range(shape):
data_vars["time"].append(orbit[k]["time"])
data_vars["x"].append(orbit[k]["position"]["x"])
data_vars["y"].append(orbit[k]["position"]["y"])
data_vars["z"].append(orbit[k]["position"]["z"])
data_vars["x"].append(orbit[k]["velocity"]["x"])
data_vars["y"].append(orbit[k]["velocity"]["y"])
data_vars["z"].append(orbit[k]["velocity"]["z"])
time.append(orbit[k]["time"])
data_vars["x"][1].append(orbit[k]["position"]["x"])
data_vars["y"][1].append(orbit[k]["position"]["y"])
data_vars["z"][1].append(orbit[k]["position"]["z"])
data_vars["vx"][1].append(orbit[k]["velocity"]["x"])
data_vars["vy"][1].append(orbit[k]["velocity"]["y"])
data_vars["vz"][1].append(orbit[k]["velocity"]["z"])
if orbit[k]["frame"] != reference_system:
warnings.warn(
f"reference_system is not consistent in all the state vectors. "
f"xpath: .//orbit//frame \n File: ${product_path}"
)
reference_system = None

coords = {
"time": ("time", time, {"standard_name": "time", "long_name": "time"},),
}
attrs = {"Conventions": "CF-1.7"}
if reference_system is not None:
attrs.update({"reference_system": reference_system})
ds = xr.Dataset(
data_vars=data_vars, # type: ignore
attrs=attrs, # type: ignore
coords=coords, # type: ignore
)
ds = conventions.update_attributes(ds)
ds = ds.update({"time": ds.time.astype(np.datetime64)})
return ds


def open_root_dataset(filename: str) -> xr.Dataset:
manifest = esa_safe.open_manifest(filename)
def open_root_dataset(product_path: str) -> xr.Dataset:
manifest = esa_safe.open_manifest(product_path)
product_attrs, product_files = esa_safe.parse_manifest_sentinel1(manifest)
product_attrs["groups"] = ["orbit"] + product_attrs["xs:instrument_mode_swaths"]
product_attrs["groups"] = ["orbit", "attitude", "gcp"] + product_attrs[
"xs:instrument_mode_swaths"
]
return xr.Dataset(attrs=product_attrs) # type: ignore


Expand All @@ -153,6 +160,10 @@ def open_dataset( # type: ignore
ds = open_root_dataset(filename_or_obj)
elif group == "gcp":
ds = open_gcp_dataset(filename_or_obj)
elif group == "orbit":
ds = open_orbit_dataset(filename_or_obj)
elif group == "attitude":
ds = open_attitude_dataset(filename_or_obj)
return ds

def guess_can_open(self, filename_or_obj: T.Any) -> bool:
Expand Down

0 comments on commit 796f762

Please sign in to comment.