UW-Hydro / tonic

A pre/post processing toolbox for hydrologic models
MIT License
20 stars 34 forks source link

`grid_params` tuple indices must be integers or slices, not str #76

Open danhamill opened 2 years ago

danhamill commented 2 years ago

I am trying to convert the livenh VIC parameter files to netcdf format.

I have installed tonic to a python 3.9 environment with these packages:

conda create -n tonic python=3.9
conda activate tonic
conda install -c conda-forge pandas numpy scipy xarray jupyter netCDF4 configobj

I am getting the following trace back from grid_params

TUPLE INDICIES MUST BE INTEGERS OR SLICES NOT STR

Which points to Line 1013 of grid_params.py and read as:

if extra_class: # <--- Line 1012
    new[:-1, yi, xi] = veglib_dict[lib_var][:, np.newaxis] # <--- Line 1013

Here is my script:

import pandas as pd
from tonic.models.vic.grid_params import soil, snow, veg, veg_class, Cols, Desc, write_netcdf, grid_params, calc_grid
# Read the soil parameters
soil_dict = soil(r'livenh\vic.nldas.mexico.soil.txt', c=Cols(nlayers=3))

# Read the snow parameters
snow_dict = snow(r'livenh\vic.nldas.mexico.snow.txt.L13',
                 soil_dict, c=Cols(snow_bands=5))

# Read the veg parameter file
veg_dict = veg(r'livenh\vic.nldas.mexico.veg.txt',
               soil_dict,
               vegparam_lai=True,
               veg_classes=11)

# Read the veg library file
veg_lib = veg_class(r'livenh\LDAS_veg_lib')

# Determine the grid shape
target_grid, target_attrs = calc_grid(soil_dict['lats'], soil_dict['lons'])

# Grid all the parameters
grid_dict = grid_params(soil_dict, target_grid, version_in = '4.1.2',
                        veg_dict=veg_dict, veglib_dict=veg_lib, snow_dict=snow_dict,
                        )

# Write a netCDF file with all the parameters
write_netcdf(r'livenh\livenh.params.vic5.nc', target_attrs,
             target_grid=target_grid,
             soil_grid=grid_dict['soil_dict'],
             snow_grid=grid_dict['snow_dict'],
             veglib_dict=veg_lib,
             veg_grid=grid_dict['veg_dict'],
             version_in='4.1.2')

Could I be missing something in veg?

danhamill commented 2 years ago

Additionally, the VIC user manual page for tonic seems to be assuming people are using the develop branch (which appears to be more than 5-years old).

To get the tonic example from the user manual to work I had to add a few imports and change the argument 'version' in develop (which I assume means which version you want to write to) to 'version_in' in master (which seems to mean what version of the VIC the ascii files were originally from)

I am working under the assumption I should be using the master branch. Is that correct?

danhamill commented 2 years ago

Apparently the issue was related to veg_class returing two items. That made veg_lib a tuple and failed in grid_param.

https://github.com/UW-Hydro/tonic/blob/67cc1a1efa481a65e304917bc8af36c2a30af055/tonic/models/vic/grid_params.py#L1418-L1456

Full working script:

from tonic.models.vic.grid_params import soil, snow, veg, veg_class, Cols, Desc, write_netcdf, grid_params, calc_grid
# Read the soil parameters
soil_dict = soil(r'livenh\vic.nldas.mexico.soil.txt', c=Cols(nlayers=3))

# Read the snow parameters
snow_dict = snow(r'livenh\vic.nldas.mexico.snow.txt.L13',
                 soil_dict, c=Cols(snow_bands=5))

# Read the veg parameter file
veg_dict = veg(r'livenh\vic.nldas.mexico.veg.txt',
               soil_dict,
               vegparam_lai=True,
               veg_classes=11)

# Read the veg library file
veg_lib, lib_bare_idx= veg_class(r'livenh\LDAS_veg_lib')

# Determine the grid shape
target_grid, target_attrs = calc_grid(soil_dict['lats'], soil_dict['lons'])

# Grid all the parameters
grid_dict = grid_params(soil_dict, target_grid, version_in = '4.1.2',
                        veg_dict=veg_dict, veglib_dict=veg_lib, snow_dict=snow_dict,
                        )

# Write a netCDF file with all the parameters
write_netcdf(r'livenh\livenh.params.vic5.nc', target_attrs,
             target_grid=target_grid,
             soil_grid=grid_dict['soil_dict'],
             snow_grid=grid_dict['snow_dict'],
             veg_grid=grid_dict['veg_dict'],
             version_in='4.1.2')

Posing here in case somebody else runs into this issue.