monocongo/climate_indices

ValueError: could not broadcast input array from shape (0,) into shape (366,)

Opened this issue · 2 comments

I am using Daymet data (netcdf) to run the climate-indices for the Caribbean.
When trying to run the daily SPI for scales 30 days, I am using a netcdf with 2 years of daily precipiration data (lat=231, lon=364, time=730).
However, I am getting the following error regarding the shape of the array:

"process_climate_indices --index spi --periodicity daily --netcdf_precip input/PR_precip_V4.nc --var_name_precip prcp --output_file_base output/PR_spi --scales 30 --calibration_start_year 1980 --calibration_end_year 1981 --multiprocessing all
2021-03-09 21:39:56 INFO Start time: 2021-03-09 21:39:56.448634
2021-03-09 21:39:56 INFO Computing 30-day SPI/Pearson
2021-03-09 21:39:58 ERROR Failed to complete
Traceback (most recent call last):
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 1684, in main
_compute_write_index(kwrgs)
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 798, in _compute_write_index
_drop_data_into_shared_arrays_grid(dataset,
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 614, in _drop_data_into_shared_arrays_grid
var_values = np.apply_along_axis(utils.transform_to_366day,
File "<array_function internals>", line 5, in apply_along_axis
File "/home/fdsm/.local/lib/python3.8/site-packages/numpy/lib/shape_base.py", line 379, in apply_along_axis
res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
ValueError: could not broadcast input array from shape (0,) into shape (366,)
Traceback (most recent call last):
File "/home/fdsm/.local/bin/process_climate_indices", line 8, in
sys.exit(main())
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 1684, in main
_compute_write_index(kwrgs)
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 798, in _compute_write_index
_drop_data_into_shared_arrays_grid(dataset,
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 614, in _drop_data_into_shared_arrays_grid
var_values = np.apply_along_axis(utils.transform_to_366day,
File "<array_function internals>", line 5, in apply_along_axis
File "/home/fdsm/.local/lib/python3.8/site-packages/numpy/lib/shape_base.py", line 379, in apply_along_axis
res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
ValueError: could not broadcast input array from shape (0,) into shape (366,)"

If anyone has an idea about what could be causing this error, it would be helpful!

Thank you!

This may have to do with the requirement to use 366-day years. It looks like you are using 365-day years.

To fix this you need to add a time step for Feb 29th in each year, and have the precipitation value be NaN for all years except the leap year. Or you may be able to use this function in utils.py which converts normal daily data (Feb 29th only represented in leap years) to all 366-day years.

Thank you, James! I tried what you suggested and converted my years into 366-days years, but when trying to run the "spi_pearson_30' I got this error complaining about my time dimension being 366 instead of length 365 (below)

(clim_ind) fdsm@fdsm-VirtualBox:~/NOAA_Clim_In$ process_climate_indices --index spi --periodicity daily --netcdf_precip /home/fdsm/NOAA_Clim_In/input/daymet_v3_prcp_1981_366day_puertorico_TEST.nc4 --var_name_precip prcp --output_file_base /home/fdsm/NOAA_Clim_In/output/TEST --scales 30 --calibration_start_year 1981 --calibration_end_year 1981 --multiprocessing all
2021-04-12 21:36:43 INFO Start time: 2021-04-12 21:36:43.277189
2021-04-12 21:36:43 INFO Computing 30-day SPI/Pearson
2021-04-12 21:37:28 ERROR Failed to complete
Traceback (most recent call last):
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 1684, in main
_compute_write_index(kwrgs)
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 1084, in _compute_write_index
dataset[output_var_name] = variable
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/dataset.py", line 1429, in setitem
self.update({key: value})
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/dataset.py", line 3899, in update
merge_result = dataset_update_method(self, other)
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/merge.py", line 956, in dataset_update_method
return merge_core(
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/merge.py", line 619, in merge_core
dims = calculate_dimensions(variables)
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/dataset.py", line 206, in calculate_dimensions
raise ValueError(
ValueError: conflicting sizes for dimension 'time': length 365 on 'spi_pearson_30' and length 366 on 'time'
Traceback (most recent call last):
File "/home/fdsm/.local/bin/process_climate_indices", line 8, in
sys.exit(main())
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 1684, in main
_compute_write_index(kwrgs)
File "/home/fdsm/.local/lib/python3.8/site-packages/climate_indices/main.py", line 1084, in _compute_write_index
dataset[output_var_name] = variable
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/dataset.py", line 1429, in setitem
self.update({key: value})
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/dataset.py", line 3899, in update
merge_result = dataset_update_method(self, other)
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/merge.py", line 956, in dataset_update_method
return merge_core(
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/merge.py", line 619, in merge_core
dims = calculate_dimensions(variables)
File "/home/fdsm/.local/lib/python3.8/site-packages/xarray/core/dataset.py", line 206, in calculate_dimensions
raise ValueError(
ValueError: conflicting sizes for dimension 'time': length 365 on 'spi_pearson_30' and length 366 on 'time'

What should I do?
Thank you for any help!