1
votes

Is there a simple way of getting an array of xyz values (i.e. an array of 3 cols and nrows = number of pixels) from an xarray dataset? Something like what we get from the rasterToPoints function in R.

I'm opening a netcdf file with values for a certain variable (chl). I'm not able to add images here directly, but here is a screenshot of the output:

Xarray dataset structure

I need to end with an array that have this structure:

[[lon1, lat1, val],
 [lon1, lat2, val]]

And so on, getting the combination of lon/lat for each point. I'm sorry if I'm missing something really obvious, but I'm new to Python.

2

2 Answers

0
votes

The natural format you are probably looking for here is a pandas dataframe, where lon, lat and chl are columns. This can be easily created using xarray's to_dataframe method, as follows.

import xarray as xr
ds = xr.open_dataset("infile.nc")
df = (
     ds
     .to_dataframe()
     .reset_index()
     )
0
votes

I can suggest you a small pseudo-code:

import numpy as np
lons = ds.variables['lon'].load()
lats = ds.variables['lat'].load()
chl = ds.variables['chl'].load()
xm,ym = np.meshgrid(lons,lats)
dataout = np.concatenate((xm.flatten()[np.newaxis,:],ym.flatten()[np.newaxis,:],chla.flatten()[np.newaxis,:]),axis=0) 

Might be it does not work out-of-the box, but at least one solution could be similar with this.