-
Notifications
You must be signed in to change notification settings - Fork 10
Add new net_cdf4 codec #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RichardHitier
wants to merge
10
commits into
SciQLop:main
Choose a base branch
from
co-libri-org:netcdf_codec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e383847
Add new net_cdf4 codec reader
RichardHitier 9bbe2f8
Refactor ISTP codecs into istp/ subpackage
RichardHitier ca6cd20
Add read tests on real file
RichardHitier 62c7ba0
Type-annotate CacheCall.cache_retention
RichardHitier 6de5b46
Add new tests fixture as get_codec()
RichardHitier eb53620
Ignore local dev configuration files
RichardHitier a3a852c
Fix type errors in IstpCdf
RichardHitier 3b8fc9f
Move _PTR_rx to cdf.py where it is actually used
RichardHitier f433a41
Add netcdf writer tests
RichardHitier b95f939
Implement netcdf writer
RichardHitier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from .istp_cdf import IstpCdf | ||
| from .hapi.csv import HapiCsv | ||
| from .hapi.binary import HapiBinary | ||
| from .istp.cdf import IstpCdf # noqa: F401 | ||
| from .istp.netcdf import IstpNetCDF # noqa: F401 | ||
| from .hapi.csv import HapiCsv # noqa: F401 | ||
| from .hapi.binary import HapiBinary # noqa: F401 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| from typing import List, Optional | ||
| import re | ||
| import logging | ||
|
|
||
| import numpy as np | ||
|
|
||
| import pyistp | ||
| from pyistp.support_data_variable import SupportDataVariable | ||
|
|
||
| from speasy.core.any_files import any_loc_open | ||
| from speasy.core.url_utils import urlparse, is_local_file | ||
| from speasy.products import SpeasyVariable, VariableAxis, VariableTimeAxis, DataContainer | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _fix_value_type(value): | ||
| if type(value) in (str, int, float): | ||
| return value | ||
| if type(value) is list: | ||
| return [_fix_value_type(sub_v) for sub_v in value] | ||
| if type(value) is bytes: | ||
| return value.decode('utf-8') | ||
| return str(value) | ||
|
|
||
|
|
||
| def _fix_attributes_types(attributes: dict): | ||
| cleaned = {} | ||
| for key, value in attributes.items(): | ||
| cleaned[key] = _fix_value_type(value) | ||
| return cleaned | ||
|
|
||
|
|
||
| def _is_time_dependent(axis, time_axis_name): | ||
| if axis.attributes.get('DEPEND_TIME', '') == time_axis_name: | ||
| return not axis.is_nrv | ||
| if axis.attributes.get('DEPEND_0', '') == time_axis_name: | ||
| return not axis.is_nrv | ||
| return False | ||
|
|
||
|
|
||
| def _display_type(variable: pyistp.loader.DataVariable) -> str: | ||
| if 'DISPLAY_TYPE' in variable.attributes: | ||
| return variable.attributes['DISPLAY_TYPE'] | ||
| if 'display_type' in variable.attributes: | ||
| return variable.attributes['display_type'] | ||
| return '' | ||
|
|
||
|
|
||
| def _make_axis(axis, time_axis_name): | ||
| return VariableAxis(values=axis.values.copy(), meta=_fix_attributes_types(axis.attributes), name=axis.name, | ||
| is_time_dependent=_is_time_dependent(axis, time_axis_name)) | ||
|
|
||
|
|
||
| def _build_labels(variable: pyistp.loader.DataVariable): | ||
| if len(variable.values.shape) != 2: | ||
| return _fix_value_type(variable.labels) | ||
| if type(variable.labels) is list and len(variable.labels) == variable.values.shape[1]: | ||
| return _fix_value_type(variable.labels) | ||
| if type(variable.labels) is list and len(variable.labels) == 1: | ||
| return [f"{variable.labels[0]}[{i}]" for i in range(variable.values.shape[1])] | ||
| return [f"component_{i}" for i in range(variable.values.shape[1])] | ||
|
|
||
|
|
||
| def _filter_extra_axes(variable: pyistp.loader.DataVariable) -> List[SupportDataVariable]: | ||
| return variable.axes[1:] | ||
|
|
||
|
|
||
| def _valid_variable_or_none(variable: SpeasyVariable) -> Optional[SpeasyVariable]: | ||
| if len(variable) == 1 and variable.time[0] < np.datetime64('1900-01-01'): # handle fill values in epoch | ||
| return None | ||
| return variable | ||
|
|
||
|
|
||
| def _load_variable(istp_loader: pyistp.loader.ISTPLoader, variable) -> SpeasyVariable or None: | ||
| if variable in istp_loader.data_variables(): | ||
| var = istp_loader.data_variable(variable) | ||
| elif variable.replace('-', '_') in istp_loader.data_variables(): # THX CSA/ISTP | ||
| var = istp_loader.data_variable(variable.replace('-', '_')) | ||
| else: # CDA https://cdaweb.gsfc.nasa.gov/WebServices/REST/#Get_Data_GET | ||
| alternative = re.sub(r"[\\/.%!@#^&*()\-+=`~|?<> ]", "$", variable) | ||
| if alternative in istp_loader.data_variables(): | ||
| var = istp_loader.data_variable(alternative) | ||
| else: | ||
| return None | ||
| if (var is not None) and (var.values.shape[0] == var.axes[0].values.shape[0]): | ||
| time_axis_name = var.axes[0].name | ||
| return _valid_variable_or_none(SpeasyVariable( | ||
| axes=[VariableTimeAxis(values=var.axes[0].values.copy(), | ||
| meta=_fix_attributes_types(var.axes[0].attributes))] + [ | ||
| _make_axis(axis, time_axis_name) for axis in _filter_extra_axes(var)], | ||
| values=DataContainer(values=var.values.copy(), meta=_fix_attributes_types(var.attributes), | ||
| name=var.name, | ||
| is_time_dependent=True), | ||
| columns=_build_labels(var))) | ||
| return None | ||
|
|
||
|
|
||
| def _resolve_url_type(url, prefix="", cache_remote_files=True): | ||
| if url is None: | ||
| return prefix + "file", None | ||
| if type(url) is str: | ||
| if is_local_file(url): | ||
| return prefix + "file", urlparse(url=url).path | ||
| return prefix + "buffer", any_loc_open(url, mode='rb', cache_remote_files=cache_remote_files).read() | ||
| if type(url) in (memoryview, bytes): | ||
| return prefix + "buffer", url | ||
| if hasattr(url, 'read'): | ||
| return prefix + "buffer", url.read() | ||
| return prefix + "file", None | ||
|
|
||
|
|
||
| def _simplify_shape(values: np.ndarray) -> np.ndarray: | ||
| if len(values.shape) == 2 and values.shape[1] == 1: | ||
| return np.reshape(values, (-1)) | ||
| return values | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding what I mentioned in SciQLop/PyISTP#1 (review), this would be the right place to delegate to the codec the interpretation and conversion of the time variable data into datetime64.