Skip to content

Add cf-python opening 'mode' and basis of integration testing - #43

Draft
sadielbartholomew wants to merge 47 commits into
cedadev:mainfrom
sadielbartholomew:cf-python-support
Draft

Add cf-python opening 'mode' and basis of integration testing#43
sadielbartholomew wants to merge 47 commits into
cedadev:mainfrom
sadielbartholomew:cf-python-support

Conversation

@sadielbartholomew

Copy link
Copy Markdown
Contributor

Close #33, including adding the basis of integration testing for Datapoint in order to verify that the cf-python support aligns with the xarray support.

Status

Opening as draft since I need to:

  • update the testing somewhat to verify the specific xr.Dataset and cf.FieldList outputs are as expected; and
  • update the method check_local_only in the test to confirm the right local_only behaviour, which I am in the process of understanding.

Otherwise essentially ready for review.

@dwest77a dwest77a left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sadielbartholomew could you review these suggested changes and add any comments where you think we can discuss things? Thanks

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sadielbartholomew could you move these aliases to a separate file in core/ i.e mappings.py or aliases.py? I'd prefer to keep any hardcoded mappings/settings in one place.

if convention is None:
def _find_spatial_dims(ds) -> Union[list, None]:
"""Determine the names of the spatial dimensions."""
lat = next((d for d in ds.dims if d.lower() in LATLON_ALIASES["latitude"]), None)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this section allow mixed conventions in your new syntax? This section tries to apply spatial slicing from the AOI in the pystac search to the data itself. I would prefer it not to work if there are any doubts about the dimension names, which is part of the reason this is only allowed to work if the dimension naming conventions match.

lon_names = LATLON_ALIASES["longitude"]

if key in lat_names:
formatted_selection_args["latitude"] = value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here as the comment above about only allowing matching naming conventions for the spatial dimension selectors.

Determine the names of the spatial dims.
Decode pystac datetime to cf-python subspace.
"""
import cf

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My opinion on this would be to add the cf import to the top of the module, with a try/except ImportError setting cf to None if not imported. Any mentions of 'cf' within functions will not raise an error, but you may need to add a check at the point higher upstream where the cf mode is used to raise an error if 'cf' is None at that point.

f'Bad mode value {mode}: only "xarray" and "cf" are valid modes.'
)

self.mode = mode

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially raise an error here if mode is 'cf' and cf is None?

fl = cf.read(self)
if self._cloud_format == 'kerchunk':
# Parse kwargs
mapper_kwargs = {}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this kerchunk logic to a separate _open_kerchunk_cf method for neatness, and also rename the other _open type methods to either _xr or _cf as needed?

def _prepare_dataset_cf(
self,
fl: cf.FieldList,
) -> cf.FieldList:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integrate the prepare_data parameter here which should just be used to turn off the selection component entirely. If prepare_data is False you can skip the whole rest of the function and just return fl.


if y is not None and x is not None:
if intersects is not None:
if intersects["type"] == "Polygon":

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can replace with intersects.get('type',None) == polygon to reduce the indenting?

to access local files only.
"""
href_local = href.replace("https://dap.ceda.ac.uk", "")
ceda_dap_prefix = 'https://dap.ceda.ac.uk'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this to the aliases.py or mapping.py file mentioned above to then be imported. Thanks for highlighting that this was hardcoded!

if isinstance(asset_id, int):
if isinstance(asset_id,int):
asset_id = self._cloud_assets[asset_id][0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that cf is actually used in DataPoint could you change the cf in this function to something like cloudformat or similar, to avoid any confusion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support cf-python as 'mode' for opening product datasets

2 participants