Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
d-chambers committed Jan 10, 2025
1 parent 23525c0 commit 84f1b2a
Show file tree
Hide file tree
Showing 34 changed files with 446 additions and 340 deletions.
7 changes: 7 additions & 0 deletions dascore/core/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ class PatchAttrs(DascoreBaseModel):
network: str = Field(
default="", max_length=max_lens["network"], description="A network code."
)
path: str = Field(default="", description="The origin of the patch data.")
format_name: str = Field(
default="", description="The original format of the patch data."
)
format_version: str = Field(
default="", description="The version of the patch data."
)
history: StrTupleStrSerialized = Field(
default_factory=tuple,
description="A list of processing performed on the patch.",
Expand Down
3 changes: 1 addition & 2 deletions dascore/core/coordmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,9 +995,8 @@ def to_summary(self) -> Self:
new_map[name] = coord.to_summary(dims=dims, name=name)
return CoordManagerSummary(
dim_map=self.dim_map,
coords=new_map,
coord_map=FrozenDict(new_map),
dims=self.dims,
summary=True,
)

def get_coord(self, coord_name: str) -> BaseCoord:
Expand Down
18 changes: 5 additions & 13 deletions dascore/core/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
)
from dascore.core.coords import BaseCoord
from dascore.utils.display import array_to_text, attrs_to_text, get_dascore_text
from dascore.utils.models import ArrayLike, ArraySummary, DascoreBaseModel
from dascore.utils.models import (
ArrayLike,
ArraySummary,
DascoreBaseModel,
)
from dascore.utils.patch import check_patch_attrs, check_patch_coords, get_patch_names
from dascore.utils.time import to_float
from dascore.viz import VizPatchNameSpace
Expand Down Expand Up @@ -380,21 +384,14 @@ def io(self) -> dc.io.PatchIO:

def to_summary(
self,
path=None,
resource_format=None,
resource_version=None,
) -> PatchSummary:
"""
Summarize the contents of the Patch.
"""
path = path if path is not None else self.get_patch_name()
psum = PatchSummary(
uri=path,
coords=self.coords.to_summary(),
attrs=self.attrs,
data=ArraySummary.from_array(self.data),
resource_format=resource_format,
resource_version=resource_version,
)
return psum

Expand All @@ -404,12 +401,7 @@ class PatchSummary(DascoreBaseModel):
A class for summarizing the metadata of the Patch.
"""

path: str
format: str = ""
version: str = ""

data: Annotated[ArraySummary, PlainValidator(ArraySummary.from_array)]

attrs: PatchAttrs
coords: CoordManagerSummary

Expand Down
11 changes: 6 additions & 5 deletions dascore/io/ap_sensing/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,9 @@ def get_format(self, resource: H5Reader, **kwargs) -> tuple[str, str] | bool:

def scan(self, resource: H5Reader, **kwargs) -> list[dc.PatchSummary]:
"""Scan an AP sensing file, return summary info about the contents."""
attrs = _get_attrs_dict(resource)
attrs = _get_attrs_dict(resource, self.name)
coords = _get_coords(resource)
info = {
"path": resource.filename,
"format": self.name,
"version": _get_version_string(resource),
"attrs": attrs,
"coords": coords,
"data": resource["DAS"],
Expand All @@ -64,6 +61,10 @@ def read(
) -> dc.BaseSpool:
"""Read a single file with APSensing data inside."""
patches = _get_patch(
resource, time=time, distance=distance, attr_cls=APSensingPatchAttrs
resource,
self.name,
time=time,
distance=distance,
attr_cls=APSensingPatchAttrs,
)
return dc.spool(patches)
11 changes: 8 additions & 3 deletions dascore/io/ap_sensing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import dascore as dc
from dascore.core import get_coord, get_coord_manager
from dascore.utils.misc import _maybe_unpack, unbyte
from dascore.utils.misc import _maybe_unpack, get_path, unbyte


def _get_version_string(resource):
Expand Down Expand Up @@ -73,28 +73,33 @@ def _get_coords(resource):
return cm


def _get_attrs_dict(resource):
def _get_attrs_dict(resource, format_name):
"""Get attributes."""
version = _get_version_string(resource)
daq = resource["DAQ"]
pserver = resource["ProcessingServer"]
out = dict(
data_category="DAS",
instrumet_id=unbyte(_maybe_unpack(daq["SerialNumber"])),
gauge_length=_maybe_unpack(pserver["GaugeLength"]),
radians_to_nano_strain=_maybe_unpack(pserver["RadiansToNanoStrain"]),
path=get_path(resource),
format_name=format_name,
format_version=version,
)
return out


def _get_patch(
resource,
format_name,
time=None,
distance=None,
attr_cls=dc.PatchAttrs,
**kwargs,
):
"""Get a patch from ap_sensing file."""
attrs = _get_attrs_dict(resource)
attrs = _get_attrs_dict(resource, format_name)
coords = _get_coords(resource)
data = resource["DAS"]
if time is not None or distance is not None:
Expand Down
9 changes: 9 additions & 0 deletions dascore/io/asn/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Support for Alcatel Submarine Networks (ASN) formats.
ASN makes the OptoDAS interrogator.
More info can be found here: https://web.asn.com/
"""
from __future__ import annotations
from .core import OptoDASV8
27 changes: 15 additions & 12 deletions dascore/io/optodas/core.py → dascore/io/asn/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dascore.utils.hdf5 import H5Reader
from dascore.utils.models import UnitQuantity, UTF8Str

from .utils import _get_opto_das_attrs, _get_opto_das_version_str, _read_opto_das
from .utils import _get_opto_das_coords_attrs, _get_opto_das_version_str, _read_opto_das


class OptoDASPatchAttrs(dc.PatchAttrs):
Expand Down Expand Up @@ -41,17 +41,16 @@ def get_format(self, resource: H5Reader, **kwargs) -> tuple[str, str] | bool:
if version_str:
return self.name, version_str

def scan(self, resource: H5Reader, **kwargs) -> list[dc.PatchAttrs]:
def scan(self, resource: H5Reader, **kwargs) -> list[dc.PatchSummary]:
"""Scan a OptoDAS file, return summary information about the file's contents."""
file_version = _get_opto_das_version_str(resource)
extras = {
"path": resource.filename,
"file_format": self.name,
"file_version": str(file_version),
}
attrs = _get_opto_das_attrs(resource)
attrs.update(extras)
return [OptoDASPatchAttrs(**attrs)]
attrs, coords = _get_opto_das_coords_attrs(resource, self.name)
data_node = resource["data"]
summary = dc.PatchSummary(
data=data_node,
coords=coords,
attrs=OptoDASPatchAttrs(**attrs),
)
return [summary]

def read(
self,
Expand All @@ -62,6 +61,10 @@ def read(
) -> dc.BaseSpool:
"""Read a OptoDAS spool of patches."""
patches = _read_opto_das(
resource, time=time, distance=distance, attr_cls=OptoDASPatchAttrs
resource,
time=time,
distance=distance,
attr_cls=OptoDASPatchAttrs,
format_name=self.name,
)
return dc.spool(patches)
29 changes: 17 additions & 12 deletions dascore/io/optodas/utils.py → dascore/io/asn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dascore.core
from dascore.core.coords import get_coord
from dascore.utils.hdf5 import unpack_scalar_h5_dataset
from dascore.utils.misc import unbyte
from dascore.utils.misc import get_path, unbyte

# --- Getting format/version

Expand Down Expand Up @@ -53,15 +53,20 @@ def _get_coord_manager(fi):
return out


def _get_attr_dict(header):
def _get_attr_dict(header, path, format_name, format_version):
"""Map header info to DAS attrs."""
attr_map = {
"gaugeLength": "gauge_length",
"unit": "data_units",
"instrument": "instrument_id",
"experiment": "acquisition_id",
}
out = {"data_category": "DAS"}
out = {
"data_category": "DAS",
"path": path,
"format_name": format_name,
"format_version": format_version,
}
for head_name, attr_name in attr_map.items():
value = header[head_name]
if hasattr(value, "shape"):
Expand All @@ -70,20 +75,20 @@ def _get_attr_dict(header):
return out


def _get_opto_das_attrs(fi) -> dict:
def _get_opto_das_coords_attrs(fi, format_name) -> tuple[dc.CoordManager, dict]:
"""Scan a OptoDAS file, return metadata."""
cm = _get_coord_manager(fi)
attrs = _get_attr_dict(fi["header"])
attrs["coords"] = cm
return attrs
path = get_path(fi)
version = _get_opto_das_version_str(fi)
attrs = _get_attr_dict(fi["header"], path, format_name, version)
return cm, attrs


def _read_opto_das(fi, distance=None, time=None, attr_cls=dc.PatchAttrs):
def _read_opto_das(
fi, distance=None, time=None, attr_cls=dc.PatchAttrs, format_name=""
):
"""Read the OptoDAS values into a patch."""
attrs = _get_opto_das_attrs(fi)
coords, attrs = _get_opto_das_coords_attrs(fi, format_name)
data_node = fi["data"]
coords = attrs.pop("coords")
cm, data = coords.select(array=data_node, distance=distance, time=time)
attrs["coords"] = cm.to_summary_dict()
attrs["dims"] = cm.dims
return [dc.Patch(data=data, coords=cm, attrs=attr_cls(**attrs))]
51 changes: 18 additions & 33 deletions dascore/io/dasdae/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@

from __future__ import annotations

import contextlib

import pandas as pd

import dascore as dc
from dascore.constants import SpoolType
from dascore.io import FiberIO
from dascore.utils.hdf5 import (
H5Reader,
H5Writer,
HDFPatchIndexManager,
NodeError,
)
from dascore.utils.misc import unbyte
from dascore.utils.misc import get_path, unbyte
from dascore.utils.patch import get_patch_names

from .utils import (
_get_contents_from_patch_groups,
_get_summary_from_patch_groups,
_read_patch,
_save_patch,
_write_meta,
Expand Down Expand Up @@ -67,37 +61,19 @@ def write(self, spool: SpoolType, resource: H5Writer, index=False, **kwargs):
This is recommended for files with many patches and not recommended
for files with few patches.
"""
breakpoint()
# write out patches
_write_meta(resource, self.version)
# get an iterable of patches and save them
patches = [spool] if isinstance(spool, dc.Patch) else spool
# create new node called waveforms, else suppress error if it
# already exists.
with contextlib.suppress(NodeError):
resource.create_group(resource.root, "waveforms")
waveforms = resource.get_node("/waveforms")
if "waveforms" not in resource:
resource.create_group(resource, "waveforms")
waveforms = resource["/waveforms"]
# write new patches to file
patch_names = get_patch_names(patches).values
for patch, name in zip(patches, patch_names):
_save_patch(patch, waveforms, resource, name)
indexer = HDFPatchIndexManager(resource)
if index or indexer.has_index:
df = self._get_patch_summary(patches)
indexer.write_update(df)

def _get_patch_summary(self, patches) -> pd.DataFrame:
"""Get a patch summary to put into index."""
df = (
dc.scan_to_df(patches)
.assign(
path=lambda x: get_patch_names(x),
file_format=self.name,
file_version=self.version,
)
.dropna(subset=["time_min", "time_max", "distance_min", "distance_max"])
)
return df

def get_format(self, resource: H5Reader, **kwargs) -> tuple[str, str] | bool:
"""Return the format from a dasdae file."""
Expand All @@ -112,12 +88,22 @@ def get_format(self, resource: H5Reader, **kwargs) -> tuple[str, str] | bool:
def read(self, resource: H5Reader, **kwargs) -> SpoolType:
"""Read a DASDAE file."""
patches = []
path = get_path(resource)
format_version = unbyte(resource.attrs["__DASDAE_version__"])
format_name = self.name
try:
waveform_group = resource.root["/waveforms"]
waveform_group = resource["/waveforms"]
except (KeyError, IndexError):
return dc.spool([])
for patch_group in waveform_group:
patches.append(_read_patch(patch_group, **kwargs))
pa = _read_patch(
patch_group,
path=path,
format_name=format_name,
format_version=format_version,
**kwargs,
)
patches.append(pa)
return dc.spool(patches)

def scan(self, resource: H5Reader, **kwargs) -> list[dc.PatchSummary]:
Expand All @@ -134,5 +120,4 @@ def scan(self, resource: H5Reader, **kwargs) -> list[dc.PatchSummary]:
A path to the file.
"""
file_format = self.name
version = resource.attrs["__DASDAE_version__"]
return _get_contents_from_patch_groups(resource, version, file_format)
return _get_summary_from_patch_groups(resource, file_format)
Loading

0 comments on commit 84f1b2a

Please sign in to comment.