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

Writer api #596

Open
wants to merge 56 commits into
base: develop
Choose a base branch
from
Open

Writer api #596

wants to merge 56 commits into from

Conversation

sandorkertesz
Copy link
Collaborator

@sandorkertesz sandorkertesz commented Jan 23, 2025

This PR

Adds a new writer API:

  • Target
  • Encoder
  • to_target()
  • create_target()
  • create_encoder()

https://earthkit-data--596.org.readthedocs.build/en/596/guide/targets.html
https://earthkit-data--596.org.readthedocs.build/en/596/guide/encoders.html

Deprecates:

  • write() and save() method
  • new_grib_output(), new_grib_coder()
  • readers.grib.output.GribOutput, readers.grib.output.GribCoder

Removes:

  • writers.GribWriter

Some counter-intuitive features

When we call to_target() it does the following:

  • creates a target
  • writes the data into the target
  • calls close() on the target

This has implications in the "file" target. Currently, when we call FileTarget.close() it behaves differently whether the target was created with a file path or a file pointer:

  • if the FileTarget was created with a path close() closes the associated file pointer
  • if the FileTarget was created with a file pointer close() does not close the associated file pointer

Examples (assume ds, ds1 and ds2 are fieldlists)

# this opens the target file and closes it 
ds.to_target("file", "my.grib") 

fp = open("my_file", "wb")
ds1.to_target("file", fp)  # does not close fp
ds2.to_target("file", fp) # does not close fp
# fp is still open at this point, we need to close it explicitly
close(fp)

fp = open("my_file", "wb")
with create_target("file", fp) as t:
    for d in ds:
        t.write(data=d)

   # this will not close fp
   t.close()

# when t goes out of scope the context manager calls 
# close() which does not close fp
# fp is still open at this point, we need to close it explicitly
close(fp)

If we closed the user provided file pointer when close() is called this code would not work:

fp = open("my_file", "wb")
ds1.to_target("file", fp)  # close fp
ds2.to_target("file", fp) # ERROR: fp closed, cannot write to it

@codecov-commenter
Copy link

codecov-commenter commented Feb 5, 2025

Codecov Report

Attention: Patch coverage is 87.93970% with 72 lines in your changes missing coverage. Please review.

Project coverage is 90.02%. Comparing base (a8f4437) to head (633b88d).

Files with missing lines Patch % Lines
tests/targets/test_target_fdb.py 40.00% 30 Missing ⚠️
tests/plugins/test_encoders_plugin.py 44.82% 15 Missing and 1 partial ⚠️
tests/targets/test_target_file.py 91.44% 13 Missing ⚠️
tests/grib/test_grib_output.py 92.51% 2 Missing and 9 partials ⚠️
tests/sources/test_cds.py 80.00% 1 Missing ⚠️
tests/sources/test_mars.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #596      +/-   ##
===========================================
- Coverage    90.28%   90.02%   -0.27%     
===========================================
  Files          153      156       +3     
  Lines        11565    11957     +392     
  Branches       546      583      +37     
===========================================
+ Hits         10442    10764     +322     
- Misses         953     1013      +60     
- Partials       170      180      +10     

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

@sandorkertesz sandorkertesz changed the title WIP: writer api Writer api Feb 7, 2025
@sandorkertesz sandorkertesz marked this pull request as ready for review February 7, 2025 19:30
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