0
votes
`sst_gradient = xr.Dataset({'sst_gradient':(['lat','lon','time'],sst_gradient)},/error in this line
                   coords={'lat':(selected_sst.lat.values),
                           'lon':(selected_sst.lon.values),
                           'time':(selected_sst.time.values)})
`

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/variable.py in as_variable(obj, name) 106 try: --> 107 obj = Variable(*obj) 108 except (TypeError, ValueError) as error:

~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/variable.py in init(self, dims, data, attrs, encoding, fastpath) 308 self._data = as_compatible_data(data, fastpath=fastpath) --> 309 self._dims = self._parse_dimensions(dims) 310 self._attrs = None

~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/variable.py in _parse_dimensions(self, dims) 499 "dimensions %s must have the same length as the " --> 500 "number of data dimensions, ndim=%s" % (dims, self.ndim) 501 )

ValueError: dimensions ('lat', 'lon', 'time') must have the same length as the number of data dimensions, ndim=0

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) in 3 coords={'lats':(selected_sst.lat.values), 4 'lons':(selected_sst.lon.values), ----> 5 'times':(selected_sst.time.values)}) 6 7

~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/dataset.py in init(self, data_vars, coords, attrs, compat) 533 534 variables, coord_names, dims, indexes = merge_data_and_coords( --> 535 data_vars, coords, compat=compat 536 ) 537

~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/merge.py in merge_data_and_coords(data, coords, compat, join) 465 indexes = dict(_extract_indexes_from_coords(coords)) 466 return merge_core( --> 467 objects, compat, join, explicit_coords=explicit_coords, indexes=indexes 468 ) 469

~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/merge.py in merge_core(objects, compat, join, priority_arg, explicit_coords, indexes, fill_value) 550 coerced, join=join, copy=False, indexes=indexes, fill_value=fill_value 551 ) --> 552 collected = collect_variables_and_indexes(aligned) 553 554 prioritized = _get_priority_vars_and_indexes(aligned, priority_arg, compat=compat)

~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/merge.py in collect_variables_and_indexes(list_of_mappings) 275 append_all(coords, indexes) 276 --> 277 variable = as_variable(variable, name=name) 278 if variable.dims == (name,): 279 variable = variable.to_index_variable()

~/anaconda3/envs/myenv/lib/python3.6/site-packages/xarray/core/variable.py in as_variable(obj, name) 111 "Could not convert tuple of form " 112 "(dims, data[, attrs, encoding]): " --> 113 "{} to Variable.".format(obj) 114 ) 115 elif utils.is_scalar(obj):

ValueError: Could not convert tuple of form (dims, data[, attrs, encoding]): (['lat', 'lon', 'time'], Dimensions:
(lat: 600, lon: 4320, sst.lat: 72, sst.lon: 600, sst.time: 4320, time: 72) Coordinates: * lat (lat) float32 -40.041668 -40.12501 ... -89.87501 -89.958336 * lon (lon) float32 -179.95833 -179.875 ... 179.87502 179.95836 * time (time) datetime64[ns] 2005-01-15 2005-02-15 ... 2010-12-15 Dimensions without coordinates: sst.lat, sst.lon, sst.time Data variables: sst_gradient (sst.lat, sst.lon, sst.time) float32 2.7785575e-08 ... nan) to Variable.

1
You would need to add some information about the source data (shape, dimensions etc), How should we know otherwise why you are facing these problems. For xarray.Datasets the output of ds.info() would be nice, So the output of sst_selected.info() would be interesting, as well as information about sst_gradient. - kmuehlbauer
the selected sst is taken as latitude below 40 degree and it is having shape of (72, 600, 4320). the sst_gradient is computed using np.gradient::sst_gradient= np.gradient(selected_sst.values,float(dy)*1e3,edge_order=2, axis=1). it is having a length of (72). - hasna

1 Answers

0
votes

The equivalent function for numpy.gradient is xarray.DataSet.differentiate.

You can find details here: xarray differentiate