-
Notifications
You must be signed in to change notification settings - Fork 219
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
clib: Add the virtualfile_out method for creating output virtualfile #3057
Conversation
… files for output
5d278fc
to
a878635
Compare
We probably need to think more about the function name. Currently, I use Maybe |
Renamed
|
dfdd3ed
to
a633abf
Compare
Ping @GenericMappingTools/pygmt-maintainers for a final review. Otherwise, I'll merge this PR in 24 hours so we can move forward. |
pygmt/clib/session.py
Outdated
Examples | ||
-------- | ||
>>> from pathlib import Path | ||
>>> from pygmt.clib import Session | ||
>>> from pygmt.datatypes import _GMT_DATASET, _GMT_GRID | ||
>>> from pygmt.helpers import GMTTempFile | ||
>>> | ||
>>> # Create a virtual file for storing the output table. | ||
>>> with GMTTempFile(suffix=".txt") as tmpfile: | ||
... with open(tmpfile.name, mode="w") as fp: | ||
... print("1.0 2.0 3.0 TEXT", file=fp) | ||
... with Session() as lib: | ||
... with lib.virtualfile_out(kind="dataset") as vouttbl: | ||
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td") | ||
... ds = lib.read_virtualfile(vouttbl, kind="dataset") | ||
>>> isinstance(ds.contents, _GMT_DATASET) | ||
True | ||
>>> | ||
>>> # Create a virtual file for storing the output grid. | ||
>>> with Session() as lib: | ||
... with lib.virtualfile_out(kind="grid") as voutgrd: | ||
... lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg") | ||
... outgrd = lib.read_virtualfile(voutgrd, kind="grid") | ||
>>> isinstance(outgrd.contents, _GMT_GRID) | ||
True | ||
>>> | ||
>>> # Write data to file without creating a virtual file | ||
>>> with GMTTempFile(suffix=".nc") as tmpfile: | ||
... with Session() as lib: | ||
... with lib.virtualfile_out( | ||
... kind="grid", fname=tmpfile.name | ||
... ) as voutgrd: | ||
... lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg") | ||
... assert voutgrd == tmpfile.name | ||
... assert Path(voutgrd).stat().st_size > 0 |
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.
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.
Should we move this into a new
test_clib_virtualfile_out.py
file? Two reasons:
The main purpose of these doctests are to show how to use this method and the outpu virtual file in other modules. So I think we can only keep the dataset
one only and remove all others.
- Might want to test the output of the virtualfiles more properly using
pygmt.info
orpygmt.grdinfo
.
Actually we can't. Because the virtual file is an output (i.e., GMT_OUT
), it can't be passed as an input to info
/grdinfo
. I.e., lib.call_module("info", f"{vouttbl}")
won't work.
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.
So I think we can only keep the
dataset
one only and remove all others.
You can move the grid one to test_clib_virtualfile_out.py
3. Might want to test the output of the virtualfiles more properly using
pygmt.info
orpygmt.grdinfo
.Actually we can't. Because the virtual file is an output (i.e.,
GMT_OUT
), it can't be passed as an input toinfo
/grdinfo
. I.e.,lib.call_module("info", f"{vouttbl}")
won't work.
Ah ok, maybe not for the virtualfile then. But we should test that saving to a (non-virtual) file works (when fname
is used), and try loading from there (i.e. roundtrip). Again, can put this in test_clib_virtualfile_out.py
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.
Should we move this into a new
test_clib_virtualfile_out.py
file? Two reasons:The main purpose of these doctests are to show how to use this method and the outpu virtual file in other modules. So I think we can only keep the
dataset
one only and remove all others.
In commit 9108917, I've removed the doctest for grid
and kept the doctests for dataset
and fname
. I also realized that these doctests are almost the same as the ones in read_virtualfile
. This makes sense, because read_virtualfile
was implemented to read the data from an output virtualfile. So, I also simplify the doctests in read_virtualfile
using the newly added virtualfile_out
method.
In other words, we probably need to move the doctests of read_virtualfile
into a separate test file in the future.
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.
So I think we can only keep the
dataset
one only and remove all others.You can move the grid one to
test_clib_virtualfile_out.py
As mentioned in the above comment, the grid one is already doctested in the read_virtualfile
method.
- Might want to test the output of the virtualfiles more properly using
pygmt.info
orpygmt.grdinfo
.Actually we can't. Because the virtual file is an output (i.e.,
GMT_OUT
), it can't be passed as an input toinfo
/grdinfo
. I.e.,lib.call_module("info", f"{vouttbl}")
won't work.Ah ok, maybe not for the virtualfile then. But we should test that saving to a (non-virtual) file works (when
fname
is used), and try loading from there (i.e. roundtrip). Again, can put this intest_clib_virtualfile_out.py
Done in 888f1c9 but not in a separate test file.
Currently, PyGMT has provided several methods for manipulating virtual files. These methods are:
open_virtualfile
: Open a virtual file and close it when done.virtualfile_from_vectors
: Create a virtual file for a table file from 1-D arraysvirtualfile_from_matrix
: Create a virtual file for a table file from a 2-D arrayvirtualfile_from_grid
: Create a virtual file for a grid file from a xarray.DataArray objectvirtualfile_from_data
: Convenience function which wrapsvirtualfile_from_vectors
/virtualfile_from_matrix
/virtualfile_from_grid
and moreread_virtualfile
: Read data from a virtual fileAll the
virtualfile_from_*
methods are for input data. This PR provides a new methodvirtualfile_out
which can create a virtual file for output data. This method is necessary to get rid of temporary files for modules that write tables/grids (#2730).The same codes are already used in PR #2729 and #2398. This PR is created to make those two PRs smaller and easier to review.
Preview: https://pygmt-dev--3057.org.readthedocs.build/en/3057/api/generated/pygmt.clib.Session.virtualfile_out.html
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Slash Commands
You can write slash commands (
/command
) in the first line of a comment to performspecific operations. Supported slash command is:
/format
: automatically format and lint the code