monocongo/climate_indices

'DataArray' object has no attribute 'units'

HEXU-123 opened this issue · 2 comments

Hi:
When I run the the process climate indices it shows me an error about 【'DataArray' object has no attribute 'units'】
Input code
process_climate_indices --index spi --periodicity monthly --netcdf_precip ./rain.nc --var_name_precip pre --output_file_base ./123/cmorph_lowres_daily_conus --scales 3 --calibration_start_year 1961 --calibration_end_year 2021 --multiprocessing all
Traceback (most recent call last):
File "D:\Anaconda3\envs\easy\lib\site-packages\climate_indices_main_.py", line 1617, in main
compute_write_index(kwrgs)
File "D:\Anaconda3\envs\easy\lib\site-packages\climate_indices_main
.py", line 753, in _compute_write_index
precip_unit = dataset[precip_var_name].units.lower()
File "D:\Anaconda3\envs\easy\lib\site-packages\xarray\core\common.py", line 278, in getattr
raise AttributeError(
AttributeError: 'DataArray' object has no attribute 'units'
Traceback (most recent call last):
File "D:\Anaconda3\envs\easy\lib\runpy.py", line 197, in _run_module_as_main
return run_code(code, main_globals, None,
File "D:\Anaconda3\envs\easy\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "D:\Anaconda3\envs\easy\Scripts\process_climate_indices.exe_main
.py", line 7, in
File "D:\Anaconda3\envs\easy\lib\site-packages\climate_indices_main
.py", line 1617, in main
compute_write_index(kwrgs)
File "D:\Anaconda3\envs\easy\lib\site-packages\climate_indices_main
.py", line 753, in _compute_write_index
precip_unit = dataset[precip_var_name].units.lower()
File "D:\Anaconda3\envs\easy\lib\site-packages\xarray\core\common.py", line 278, in getattr
raise AttributeError(
AttributeError: 'DataArray' object has no attribute 'units'

Seems your rain.nc doesn't have units attribute. You can add it using NCO command, see below:

ncatted -a units,pre,o,c,mm rain.nc

pre is your variable name
o specifies that the attribute should be overwritten if it already exists, or created if it does not exist.
c indicates that the type of the attribute is character/string.

Ref: https://linux.die.net/man/1/ncatted

Thanks for bringing this up @HEXU-123, the error message is not very helpful, another for the to-do list

Thank you @bennyistanto for accurately describing the problem of the missing units attribute, I need to amend the exception message to highlight that this is required and how the user can fix it (in case they don't stumble onto this issue comments thread to see the NCO command above)