GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — clib: Add the virtualfile_out method for creating output virtualfile by seisman · Pull Request #3057 · GenericMappingTools/pygmt Conversation
|
|
We probably need to think more about the function name. Currently, I use Maybe |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Renamed
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| 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.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.pyfile? 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.infoorpygmt.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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
So I think we can only keep the
datasetone 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.infoorpygmt.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
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.pyfile? 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
datasetone 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
So I think we can only keep the
datasetone 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.infoorpygmt.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
fnameis 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.