|
10 | 10 | import warnings
|
11 | 11 | from contextlib import contextmanager, nullcontext
|
12 | 12 |
|
13 |
| -from pygmt.datatypes import GMT_GRID |
14 | 13 | import numpy as np
|
15 | 14 | import pandas as pd
|
16 | 15 | from packaging.version import Version
|
|
22 | 21 | vectors_to_arrays,
|
23 | 22 | )
|
24 | 23 | from pygmt.clib.loading import load_libgmt
|
| 24 | +from pygmt.datatypes import GMT_GRID |
25 | 25 | from pygmt.exceptions import (
|
26 | 26 | GMTCLibError,
|
27 | 27 | GMTCLibNoSessionError,
|
@@ -1627,7 +1627,8 @@ def virtualfile_from_data(
|
1627 | 1627 |
|
1628 | 1628 | def read_virtualfile(self, vfname, kind=None):
|
1629 | 1629 | """
|
1630 |
| - Read data from a virtual file and cast it into a GMT data container if requested. |
| 1630 | + Read data from a virtual file and cast it into a GMT data container if |
| 1631 | + requested. |
1631 | 1632 |
|
1632 | 1633 | Parameters
|
1633 | 1634 | ----------
|
@@ -1665,27 +1666,37 @@ def read_virtualfile(self, vfname, kind=None):
|
1665 | 1666 | return ctp.cast(pointer, ctp.POINTER(type))
|
1666 | 1667 |
|
1667 | 1668 | @contextmanager
|
1668 |
| - def virtualfile_to_data(self, kind): |
| 1669 | + def virtualfile_to_data(self, kind, fname=None): |
1669 | 1670 | """
|
1670 |
| - Create a virtual file for writing a GMT data container. |
| 1671 | + Create a virtual file for writing a GMT data container or yield the |
| 1672 | + output file name. |
1671 | 1673 |
|
1672 | 1674 | Parameters
|
1673 | 1675 | ----------
|
1674 | 1676 | kind : str
|
1675 | 1677 | The kind of data container to create. Choose from "grid" or
|
1676 |
| - "dataset". |
| 1678 | + "dataset". It has no effect if ``fname`` is given. |
| 1679 | +
|
| 1680 | + fname : str or None |
| 1681 | + If given, yield the output file name instead of the virtual file. |
1677 | 1682 |
|
1678 | 1683 | Yields
|
1679 | 1684 | ------
|
1680 | 1685 | vfile : str
|
1681 |
| - Name of the virtual file. |
| 1686 | + Name of the virtual file or the output file name. |
1682 | 1687 | """
|
1683 |
| - family, geometry = { |
1684 |
| - "grid": ("GMT_IS_GRID", "GMT_IS_SURFACE"), |
1685 |
| - "dataset": ("GMT_IS_DATASET", "GMT_IS_PLP"), |
1686 |
| - }[kind] |
1687 |
| - with self.open_virtual_file(family, geometry, "GMT_OUT", None) as vfile: |
1688 |
| - yield vfile |
| 1688 | + # If fname is given, yield the output file name. |
| 1689 | + if fname is not None: |
| 1690 | + yield fname |
| 1691 | + # Otherwise, create a virtual file for writing a GMT data container. |
| 1692 | + else: |
| 1693 | + # Determine the family and geometry of the data container based on 'kind'. |
| 1694 | + family, geometry = { |
| 1695 | + "grid": ("GMT_IS_GRID", "GMT_IS_SURFACE"), |
| 1696 | + "dataset": ("GMT_IS_DATASET", "GMT_IS_PLP"), |
| 1697 | + }[kind] |
| 1698 | + with self.open_virtual_file(family, geometry, "GMT_OUT", None) as vfile: |
| 1699 | + yield vfile |
1689 | 1700 |
|
1690 | 1701 | @contextmanager
|
1691 | 1702 | def virtualfile_from_gmtgrid(self, grid_pointer):
|
|
0 commit comments