|
| 1 | +""" |
| 2 | +PyGMT input/output (I/O) utilities. |
| 3 | +""" |
| 4 | +import xarray as xr |
| 5 | + |
| 6 | + |
| 7 | +def load_dataarray(filename_or_obj, **kwargs): |
| 8 | + """ |
| 9 | + Open, load into memory, and close a DataArray from a file or file-like |
| 10 | + object containing a single data variable. |
| 11 | +
|
| 12 | + This is a thin wrapper around :py:func:`xarray.open_dataarray`. It differs |
| 13 | + from :py:func:`xarray.open_dataarray` in that it loads the DataArray into |
| 14 | + memory, gets GMT specific metadata about the grid via |
| 15 | + :py:meth:`GMTDataArrayAccessor`, closes the file, and returns the |
| 16 | + DataArray. In contrast, :py:func:`xarray.open_dataarray` keeps the file |
| 17 | + handle open and lazy loads its contents. All parameters are passed directly |
| 18 | + to :py:func:`xarray.open_dataarray`. See that documentation for further |
| 19 | + details. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + filename_or_obj : str or pathlib.Path or file-like or DataStore |
| 24 | + Strings and Path objects are interpreted as a path to a netCDF file |
| 25 | + or an OpenDAP URL and opened with python-netCDF4, unless the filename |
| 26 | + ends with .gz, in which case the file is gunzipped and opened with |
| 27 | + scipy.io.netcdf (only netCDF3 supported). Byte-strings or file-like |
| 28 | + objects are opened by scipy.io.netcdf (netCDF3) or h5py (netCDF4/HDF). |
| 29 | +
|
| 30 | + Returns |
| 31 | + ------- |
| 32 | + datarray : xarray.DataArray |
| 33 | + The newly created DataArray. |
| 34 | +
|
| 35 | + See Also |
| 36 | + -------- |
| 37 | + xarray.open_dataarray |
| 38 | + """ |
| 39 | + if "cache" in kwargs: |
| 40 | + raise TypeError("cache has no effect in this context") |
| 41 | + |
| 42 | + with xr.open_dataarray(filename_or_obj, **kwargs) as dataarray: |
| 43 | + result = dataarray.load() |
| 44 | + _ = result.gmt # load GMTDataArray accessor information |
| 45 | + |
| 46 | + return result |
0 commit comments