William Jacondino
William Jacondino

Reputation: 63

How to keep the original dimensions after creating a new variable using xarray?

After I calculate the wind speed in a new variable, this variable loses its association with the dimension of the main array. My code:

# Lista o caminho de todos os arquivos contidos no diretório e subdiretório com a extensão .nc 

CCMP_DATA = glob.glob(r'E:/Dados - INPE/Dados/CCMP/' + "/**/*.nc", recursive = True)

CCMP_DATA

# Atribuindo a variável nobs aos arquivos netcdfs da versão 2.0

nan = float('nan')

def preprocessing(ds): 
    return ds.assign(nobs=nan)

# Lendo os arquivos como um só 

ds = xr.open_mfdataset(CCMP_DATA, concat_dim='time', decode_times=True, combine='nested', preprocess = preprocessing)

# Convertendo a longitude para variar de -180 a 180

ds.coords['longitude'] = ((ds.coords['longitude']+180) % 360) - 180

# Rearranjando o dataset de forma que a longitude fique crescente (de -180 a 180)

ds = ds.sortby(ds.longitude)

ds dimension

ds dimension

uten  = ds.variables['uwnd']

vten  = ds.variables['vwnd']

# Calculando a velocidade do vento

ws = (uten**2 + vten**2)**(0.5)

# Extrapolando o vento para 100 m

ws100 = windpowerlib.wind_speed.hellman(ws,10,100)

#wdir = (180 + (np.degrees(np.arctan2(uten, vten)))) % 360

wdir = np.mod(180 + np.rad2deg(np.arctan2(uten, vten)),360)

ws100 without dimension

ws100 without dimension

My question is, how do I keep the dimensions in the new calculated variable? Or how can I insert this new variable into the old array while keeping the dimensions?

Thanks in advance!

Upvotes: 0

Views: 160

Answers (1)

Michael Delgado
Michael Delgado

Reputation: 15452

This seems like an issue with windpowerlib not xarray. But yes you should be able to add the coordinates onto your array:

ws100.coord['time'] = ds.time
ws100.coord['latitude'] = ds.latitude
ws100.coord['longitude'] = ds.longitude

Or you could add the array to the dataset with ds['ws100'] = ws100

Upvotes: 0

Related Questions