convert between dataarray and dataset using xarray

Solutions on MaxInterview for convert between dataarray and dataset using xarray by the best coders in the world

showing results for - "convert between dataarray and dataset using xarray"
Carla
11 Jan 2017
1#import xarray
2import xarray as xr
3
4#open the dataset
5ds = xr.open_dataset(file_name.nc)
6
7#convert between dataarray and dataset
8bands = ['var_1', 'var_2', 'var_3', ... , 'var_n']
9
10    #da is a conversion to dataarray where we say we want a dataarray
11    #containing only the variables inside bands and we want to convert
12    #this subset of data to a dataarray
13    #the second line is to convert back to dataset 
14da = ds[bands].to_array(dim='enter a dimension of the dataset')
15da.to_dataset(dim='enter a dimension of the dataset')