Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
danhamill opened this issue May 13, 2022 · 2 comments
Open

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

danhamill opened this issue May 13, 2022 · 2 comments

Comments

@danhamill
Copy link

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
Copy link
Author

danhamill commented May 13, 2022

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
Copy link
Author

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

def veg_class(vegl_file, veglib_photo=False,
c=Cols(veglib_fcan=False, veglib_photo=False)):
"""
Load the entire vegetation library file into a dictionary of lists.
"""
print('reading {0}'.format(vegl_file))
data = []
sep = ' '
lib_bare_idx = None
row = 0
with open(vegl_file, 'r') as f:
for line in f:
words = line.split()
if row == 0:
col_desc = len(words) - 1
elif line.startswith('#'):
continue # skip additional lines with comments
else:
data.append([])
for col in np.arange(0, col_desc):
data[row - 1][:] = words[:col_desc]
data[row - 1].append(sep.join(words[col_desc:]))
if veglib_photo:
if data[row - 1][c.veglib['lib_Ctype']] == 'C3':
data[row - 1][c.veglib['lib_Ctype']] = 0
elif data[row - 1][c.veglib['lib_Ctype']] == 'C4':
data[row - 1][c.veglib['lib_Ctype']] = 1
if re.match('(bare|barren|unvegetated)',
sep.join(words[col_desc:]), re.I):
lib_bare_idx = row - 1
row += 1
veglib_dict = OrderedDict()
data = np.array(data)
for var in c.veglib:
veglib_dict[var] = np.squeeze(data[..., c.veglib[var]])
return veglib_dict, lib_bare_idx

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant