monocongo/climate_indices

Invalid dimensions

Closed this issue · 3 comments

Hi,
when trying to compute the SPI for my precipitation data, I get an error about invalid dimensions. If I interpret that correctly it seems that ('time', 'latitude', 'longitude') is an incorrect order.

2021-03-03 14:09:31 INFO Computing 6-month SPI/Pearson
2021-03-03 14:09:31 ERROR Invalid dimensions for variable 'tp': ('time', 'latitude', 'longitude')
2021-03-03 14:09:31 ERROR Failed to complete
Traceback (most recent call last):
File "c:\python38\lib\site-packages\climate_indices_main_.py", line 1684, in main
compute_write_index(kwrgs)
File "c:\python38\lib\site-packages\climate_indices_main
.py", line 798, in _compute_write_index
drop_data_into_shared_arrays_grid(dataset,
File "c:\python38\lib\site-packages\climate_indices_main
.py", line 597, in _drop_data_into_shared_arrays_grid
raise ValueError(message)

I have now tried to reorder the dimensions, by parsing the dataset into an xarray and using transpose.

import pandas as pd
import xarray as xr

input_format = "tp_in_mm.nc"
output_format = "tp_in_mm_reordered.nc"

ds = xr.open_dataset("C:\Users\helena\precipitation\%s" % input_format,
decode_times=False)
reference_date = "1981-01-01 00:00:00"
ds['time'] = pd.date_range(start=reference_date, periods=ds.sizes['time'], freq='MS')
ds["tp"].attrs["units"] = "mm"
ds["tp"].transpose("latitude", "longitude", "time")
ds.to_netcdf(path="C:\Users\helena\precipitation\%s" % output_format)

But doing that doesn't work. If I then try to compute the SPI, I get a ton of warnings that the input contains negative values, which get clipped to zero, which shouldn't exist. I assume that somehow coordinates then get interpreted as precipitation values, but I am currently at a loss on how to solve the issue.

Hello Helena
You are probably not assigning changed object to variable...
data = data.transpose("lat", "lon", "time")

So I would say in your case:
ds["tp"] = ds["tp"].transpose("latitude", "longitude", "time")

Can you try this?

Ah yes, that did the trick. Thank you very much, I somehow missed that. I'm still curious how forgetting to assign the object to a variable still made some difference and changed the dataset, so that the calculation didn't cause an error.

Well, that's the basic of Python... Variables are just the names. The assignment does not copy a value. It just attaches a name to an object (data). Think about it that Variable is a sticky note... So you need to assign a new sticky note to your changed object (even if it has the same name)