0

one little question. Is there an easy way to convert netcdf file data to a grid which can be used in pygmt? Pygmt has as default example the following dataset:

<xarray.DataArray 'elevation' (lat: 120, lon: 120)>
array([[-3539.5, -3510.5, -3557.5, ..., -3013. , -2982. , -2981.5],
       [-3583.5, -3467.5, -3575.5, ..., -2941. , -2967. , -2982.5],
       [-3663. , -3497. , -3452.5, ..., -2900.5, -2976. , -2941. ],
       ...,
       [-3600.5, -3568. , -3572.5, ..., -2558. , -2985. , -3190. ],
       [-3535.5, -3499. , -3542. , ..., -2604. , -3016. , -3091.5],
       [-3480. , -3455. , -3488.5, ..., -2758. , -3155. , -3248.5]],
      dtype=float32)
Coordinates:
  * lon      (lon) float64 -92.46 -92.38 -92.29 -92.21 ... -82.71 -82.62 -82.54
  * lat      (lat) float64 -2.958 -2.875 -2.792 -2.708 ... 6.792 6.875 6.958
Attributes:
    long_name:         elevation relative to the geoid
    units:             meters
    vertical_datum:    EMG96
    horizontal_datum:  WGS84

After using to common nc-read syntax I get an masked array.

Greetings

S.Kociok
  • 168
  • 1
  • 14

2 Answers2

0

I have a easy solution:

nc = nc4.Dataset('filename.nc', mode='r')  # Or from siphon.ncss
dataset = xr.open_dataset(xr.backends.NetCDF4DataStore(nc))
S.Kociok
  • 168
  • 1
  • 14
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 14 '21 at 16:11
  • PyGMT supports the usage of xarray to work with netcdf files. – mgrund Dec 15 '21 at 08:51
0

PyGMT has a function pygmt.load_dataarray() which can take an .nc file name as input and should return a xr.dataarray

example_grid = pygmt.load_dataarray('example.nc')