Skip to content
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

Adds netCDF3 vs netCDF4 distinction to _automatically_determine_filetype. #43

Merged
merged 4 commits into from
Mar 22, 2024

Conversation

norlandrhagen
Copy link
Collaborator

Curious on what you think of this @TomNicholas.

  • Allows _automatically_determine_filetype to guess between netCDF3 vs netcdf4.
  • Adds a netCDF4 dependency to tests. 🤷

Copy link

codecov bot commented Mar 20, 2024

Codecov Report

Attention: Patch coverage is 89.47368% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 88.14%. Comparing base (5eb15bb) to head (a2e2cd5).

Files Patch % Lines
virtualizarr/kerchunk.py 80.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #43      +/-   ##
==========================================
+ Coverage   87.66%   88.14%   +0.48%     
==========================================
  Files          13       13              
  Lines         851      869      +18     
==========================================
+ Hits          746      766      +20     
+ Misses        105      103       -2     
Flag Coverage Δ
unittests 88.14% <89.47%> (+0.48%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +75 to +81
with netCDF4.Dataset(filepath, "r") as dataset:
if dataset.data_model == "NETCDF4":
filetype = "netCDF4"
elif dataset.data_model == "NETCDF3_CLASSIC":
filetype = "netCDF3"
else:
raise NotImplementedError(
Copy link
Member

Choose a reason for hiding this comment

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

This is cool! Presumably this has to open the file to read this data_model information? Is that likely to incur any significant cost over the opening that kerchunk already needs to do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the read done by the NetCDF4 library is only reading attributes and not loading the entire file. Not 100% sure on this though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can do this without opening the file with netCDF at all. Both file types have a "magic" at the beginning of the file.

def guess_file_type(fp) -> FileType:
    magic = fp.read(4)
    fp.seek(0)
    if magic[:3] == b"CDF":
        return FileType.netcdf3
    elif magic == b"\x89HDF":
        return FileType.hdf5
    else:
        raise ValueError(f"Unknown file type - magic {magic}")

@TomNicholas TomNicholas merged commit b80059b into main Mar 22, 2024
7 checks passed
@TomNicholas TomNicholas deleted the netcdf3_netcdf4 branch March 22, 2024 15:53
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.

3 participants