4

I have a DataArray that I want to convert to a numpy array. For reference it's a three dimensional gridded dataset containing (time, latitude, longitude). I have tried using xarray.DataArray.values(), but receive the error: TypeError: 'property' object is not callable.

I want to transform it into a numpy array as I'm using a function that utilizes the reshape capabilities and won't work properly with the DataArray. I've tried simply converting it to a numpy array using np.array(), but it converts every value into a NaN.

datatlt=xr.open_dataset("/nfs/home11/staff/lzhou/Public/Satellite_data/RSS_Tb_Anom_Maps_ch_TLT_V4_0.nc", decode_times=False)

tlt=datatlt['brightness_temperature'].sel(months=slice(121,492))
tlt2=np.ma.masked_invalid(tlt)

tlt2=xr.DataArray.values(tlt2)
Eli Turasky
  • 981
  • 2
  • 11
  • 28
  • Please edit your question to mention what kind of data is present in your data array -- are they integers or floats? What is minimum possible value and maximum possible value? If floats, can they be represented in 32-bit floats? Before creating a numpy array, you need to decide upon a suitable numpy dtype for each element, and that would depend upon details such as what I just mentioned – fountainhead Mar 13 '19 at 00:24
  • 1
    Did you try `xarray.DataArray.values`? The error means you can't use `()` after values because it isn't callable (it isn't a function). What is the `type` of that `values` object? Is it an array? If so, what shape and dtype? – hpaulj Mar 13 '19 at 00:42

1 Answers1

5

DataArray.values is a property. Properties are not callable. Instead, you just access the numpy array as if it were an attribute of the DataArray.

tlt2 = tlt.values

If you want a masked array, you'll want to call the to_masked_array method:

tlt2 = tlt.to_masked_array()
jhamman
  • 5,867
  • 19
  • 39