CLIMADA-project / climada_petals

See https://github.com/CLIMADA-project/climada_python first
GNU General Public License v3.0
22 stars 13 forks source link

Errors in the crop production risk tutorial #126

Open luseverin opened 5 months ago

luseverin commented 5 months ago

The notebook for the crop production risk tutorial is showing some errors:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[5], line 14
     11 path_hist_mean = output_dir / "Hist_mean" # set output directory for hist_mean
     12 path_hist_mean.mkdir(parents=True, exist_ok=True)
---> 14 filelist_haz, hazards_list = set_multiple_rc_from_isimip(input_dir=input_haz_dir, output_dir=output_dir,
     15                                                       isimip_run='ISIMIP2b', return_data=True)
     17 print("\nComputed and saved the following files: \n")
     18 print(filelist_haz)

File c:\users\me\git\climada_petals\climada_petals\hazard\relative_cropyield.py:551, in set_multiple_rc_from_isimip(input_dir, output_dir, bbox, isimip_run, yearrange_his, yearrange_mean, return_data, save, combine_subcrops)
    547 filename_list = list()
    548 output_list = list()
    550 (his_file_list, file_props, hist_mean_per_crop,
--> 551  scenario_list, _, combi_crop_list) = init_hazard_sets_isimip(filenames,
    552                                                               input_dir=input_dir,
    553                                                               bbox=bbox, isimip_run=isimip_run,
    554                                                               yearrange_his=yearrange_his,
    555                                                               combine_subcrops=combine_subcrops)
    557 if (yearrange_mean is None) and (isimip_run == 'ISIMIP2b'):
    558     yearrange_mean = YEARCHUNKS[file_props[his_file_list[0]]['scenario']]['yearrange_mean']

File c:\users\me\git\climada_petals\climada_petals\hazard\relative_cropyield.py:791, in init_hazard_sets_isimip(filenames, input_dir, bbox, isimip_run, yearrange_his, combine_subcrops)
    785 # generate hazard using the first file to determine the size of the historic mean
    786 # file structure: ag_model _ cl_model _ scenario _ soc _ co2 _
    787 #   yield-crop-irr _ fn_str_var _ startyear _ endyear . nc
    788 #e.g. gepic_gfdl-esm2m_ewembi_historical_2005soc_co2_yield-whe-noirr_
    789 #   global_annual_1861_2005.nc
    790 haz_dummy = RelativeCropyield()
--> 791 haz_dummy.set_from_isimip_netcdf(input_dir=input_dir, filename=his_file_list[0], bbox=bbox,
    792                                  scenario=file_props[his_file_list[0]]['scenario'],
    793                                  yearrange=(file_props[his_file_list[0]]['startyear'],
    794                                             file_props[his_file_list[0]]['endyear']))
    796 # initiate the historic mean for each combination of crop and irrigation type
    797 # the idx keeps track of the row in which the hist_mean values are written per crop-irr to
    798 # ensure that all files are assigned to the corresponding crop-irr combination
    799 hist_mean_per_crop = dict()

IndexError: list index out of range

in the Initiate a relative yield hazard set from multiple input files: section and:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[2], line 24
     16 path_hist_mean = output_dir / 'Hist_mean' # set output directory for hist_mean
     18 # # only required if hazard set has not yet been initiated above:
     19 #---------------------------------------------------------------
     20 # set_multiple_rc_from_isimip(input_dir=input_haz_dir, output_dir=output_dir)
     21 # """compute historical mean yield for all runs available in input_haz directory"""
     22 #---------------------------------------------------------------
---> 24 filelist_exp, exposures = init_full_exp_set_isimip(input_dir=input_exp_dir, hist_mean_dir=path_hist_mean, \
     25                                                  output_dir=output_dir, return_data=True)
     26 """create exposures for all hist_mean files available in path_hist_mean directory"""
     27 print("\nExposure files created:\n")

File c:\users\me\git\climada_petals\climada_petals\entity\exposures\crop_production.py:896, in init_full_exp_set_isimip(input_dir, filename, hist_mean_dir, output_dir, bbox, yearrange, unit, isimip_version, return_data)
    893 if not unit:
    894     unit = 't/y'
--> 896 filenames = [f.name for f in hist_mean_dir.iterdir()
    897              if f.is_file() and not f.name.startswith('.')]
    899 # generate output directory if it does not exist yet
    900 target_dir = output_dir / 'Exposure'

File c:\users\me\git\climada_petals\climada_petals\entity\exposures\crop_production.py:896, in <listcomp>(.0)
    893 if not unit:
    894     unit = 't/y'
--> 896 filenames = [f.name for f in hist_mean_dir.iterdir()
    897              if f.is_file() and not f.name.startswith('.')]
    899 # generate output directory if it does not exist yet
    900 target_dir = output_dir / 'Exposure'

File c:\Users\me\miniconda3\envs\env_climada\lib\pathlib.py:1160, in Path.iterdir(self)
   1156 def iterdir(self):
   1157     """Iterate over the files in this directory.  Does not yield any
   1158     result for the special paths '.' and '..'.
   1159     """
-> 1160     for name in self._accessor.listdir(self):
   1161         if name in {'.', '..'}:
   1162             # Yielding a path object for these makes little sense
   1163             continue

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'c:\\Users\\me\\git\\climada_petals\\doc\\tutorial\\results\\ISIMIP_crop\\Output_tutorial\\Hist_mean'

in the Initiating an exposure set from several model runs: section.

I personally did not manage to run the notebook, as running the first cell already fails with a:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], [line 10](vscode-notebook-cell:?execution_count=1&line=10)
      [8](vscode-notebook-cell:?execution_count=1&line=8) yearrange_hist_mean = (2001, 2005) # yearrange for reference historical mean (demo data only available from 2001 to 2005)
      [9](vscode-notebook-cell:?execution_count=1&line=9) haz = RelativeCropyield()
---> [10](vscode-notebook-cell:?execution_count=1&line=10) haz.set_from_isimip_netcdf(input_dir=INPUT_DIR, yearrange=yearrange_haz, ag_model='lpjml',
     [11](vscode-notebook-cell:?execution_count=1&line=11)                         cl_model='ipsl-cm5a-lr', scenario='historical', soc='2005soc',
     [12](vscode-notebook-cell:?execution_count=1&line=12)                         co2='co2', crop='whe', irr='noirr', fn_str_var=FN_STR_DEMO)
     [14](vscode-notebook-cell:?execution_count=1&line=14) print("\nBefore calling set_rel_yield_to_int(), intensity is '%s' with unit '%s'.\n" %(haz.intensity_def, haz.units))
     [16](vscode-notebook-cell:?execution_count=1&line=16) hist_mean = haz.calc_mean(yearrange_hist_mean) # requires reference year range as input

File ~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:123, in RelativeCropyield.set_from_isimip_netcdf(self, *args, **kwargs)
    [120](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:120) """This function is deprecated, use RelativeCropyield.from_isimip_netcdf instead."""
    [121](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:121) LOGGER.warning("The use of RelativeCropyield.set_from_isimip_netcdf is deprecated."
    [122](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:122)                "Use RelativeCropyield.from_isimip_netcdf instead.")
--> [123](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:123) self.__dict__ = RelativeCropyield.from_isimip_netcdf(*args, **kwargs).__dict__

File ~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:250, in RelativeCropyield.from_isimip_netcdf(cls, input_dir, filename, bbox, yearrange, ag_model, cl_model, bias_corr, scenario, soc, co2, crop, irr, fn_str_var)
    [247](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:247) haz.date = np.array(dt.str_to_date(
    [248](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:248)     [event_ + '-01-01' for event_ in haz.event_name]))
    [249](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:249) haz.centroids.set_meta_to_lat_lon()
--> [250](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:250) haz.centroids.region_id = (
    [251](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:251)     coord.coord_on_land(haz.centroids.lat, haz.centroids.lon)).astype(dtype=int)
    [252](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:252) haz.check()
    [253](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/climada_petals/doc/tutorial/~/Documents/PhD/workspace/climada_petals/climada_petals/hazard/relative_cropyield.py:253) return haz

AttributeError: can't set attribute

Maybe someone who knows this module better could have a look to fix those issues..

chahank commented 5 months ago

@carmensteinmann : if I remember correctly, you were involved in writing the Crop module. Do you maybe know what the issue here might be?

carmensteinmann commented 4 months ago

yes, I wrote part of the code, but I it's not apparent to me what the issue might be..