How to use the tonic.pycompat.pyzip function in tonic

To help you get started, we’ve selected a few tonic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github UW-Hydro / tonic / tonic / models / vic / ncparam2ascii.py View on Github external
def veg(data, xinds, yinds, veg_file, rootzones=3, global_lai=True):
    """Write VIC formatted veg parameter file"""

    print('writing veg parameter file: {0}'.format(veg_file))

    # counter for bad grid cells
    count = 0

    f = open(veg_file, 'w')

    for y, x in pyzip(yinds, xinds):
        gridcell = int(data['gridcell'][y, x])
        n_veg = int(data['Nveg'][y, x])
        cv = data['Cv'][:, y, x]
        veg_class = np.nonzero(cv)[0]

        if not len(veg_class) == n_veg:
            count += 1

        line1 = str(gridcell) + ' ' + str(n_veg) + '\n'
        f.write(line1)
        if n_veg > 0:
            for j in veg_class:
                line2 = [str(j + 1)]
                line2.append(str(cv[j]))
                for k in range(rootzones):
                    line2.append(str(data['root_depth'][j, k, y, x]))
github UW-Hydro / tonic / tonic / models / vic / vic2netcdf.py View on Github external
print('setting point attributes (fileformat, names, usecols, and dtypes)')
    # pandas.read_table does not 'honor' the order of the columns in usecols
    # it simply uses them in ascending order. So the names need to be sorted
    # the same way. For example, if the columns in the VIC file are:
    # 3: prcp; 4: evap; 5: runoff; 6; baseflow; 7: sm1; 8: sm2; 9: sm3; 10: swe
    # and this is parsed from the configuration file as
    # usecols = [3, 4, 5, 6, 10, 7, 8, 9]
    # names=['prcp', 'evap', 'runoff', 'baseflow', 'swe', 'sm1', 'sm2', 'sm3']
    # then without sorting, the netcdf file will have the wrong variables:
    # nc_swe will contain sm1, nc_sm1 will contain sm2, nc_sm2: sm3 and
    # nc_swe: sm3
    # the following will ensure that the names are sorted in increasing column
    # order. Note that sorted(usecols) is not strictly necessary, since
    # apparently that is done in read_table, but it keeps the names and columns
    # in the same order
    names = [x for (y, x) in sorted(pyzip(usecols, names))]
    usecols = sorted(usecols)
    points.set_names(names)
    points.set_usecols(usecols)
    points.set_dtypes(dtypes)
    # set binary attributes
    if options['input_file_format'].lower() == 'binary':
        points.set_bin_dtypes(bin_dtypes)
        points.set_bin_mults(bin_mults)
    points.set_fileformat(options['input_file_format'])
    print('done')
    # ---------------------------------------------------------------- #

    # ---------------------------------------------------------------- #
    if memory_mode == 'big_memory':
        # ------------------------------------------------------------ #
        # run in big memory mode
github UW-Hydro / tonic / tonic / models / vic / grid_params.py View on Github external
varnames = ['albedo'] + varnames
        if veglib_fcan and fcan_src == 'FROM_VEGLIB':
            varnames = ['fcanopy'] + varnames
        if lai_src == 'FROM_VEGLIB':
            varnames = ['LAI'] + varnames
        for var in varnames:
            lib_var = 'lib_{0}'.format(var)
            shape = (nveg_classes, veglib_dict[lib_var].shape[1],
                     ysize, xsize)
            new = np.full(shape, FILLVALUE_F)
            if extra_class:
                new[:-1, :, yi, xi] = veglib_dict[lib_var][:, :, np.newaxis]
                new[-1, :, yi, xi] = bare_vegparam[var]
            else:
                new[:, :, yi, xi] = veglib_dict[lib_var][:, :, np.newaxis]
            for y, x in pyzip(ymask, xmask):
                new[:, :, y, x] = fill_val
            out_dicts['veg_dict'][var] = np.ma.masked_values(new, FILLVALUE_F)

        # Finally, transfer veglib class descriptions (don't distribute)
        # This deviates from dimensions of other grid vars
        var = 'comment'
        lib_var = 'lib_{0}'.format(var)
        new = np.empty(nveg_classes, 'O')
        if extra_class:
            new[:-1] = veglib_dict['lib_comment']
            new[-1] = 'Bare Soil'
        else:
            new[:] = veglib_dict['lib_comment']
        out_dicts['veg_dict']['comment'] = new

    return out_dicts
github UW-Hydro / tonic / tonic / models / vic / vic2netcdf.py View on Github external
if fileformat == 'ascii':
            delimeter = r'\t'  # VIC ascii files are tab seperated
        else:
            delimeter = r','  # true csv

        for p in self:
            p.fileformat = fileformat
            if fileformat in ['ascii', 'csv']:
                p.open = p._open_ascii
                p.delimeter = delimeter
                p.read = p._read_ascii
            elif fileformat == 'binary':
                p.open = p._open_binary
                p.read = p._read_binary
                p.dt = np.dtype(list(pyzip(p.names, p.bin_dtypes)))
            elif fileformat == 'netcdf':
                p.open = p._open_netcdf
                p.read = p._read_netcdf
            else:
                raise ValueError('Unknown file format: {0}'.format(fileformat))
        return
github UW-Hydro / tonic / tonic / models / vic / grid_params.py View on Github external
steps = mydict[var].shape[1]
                out_dict[var] = np.ma.zeros((steps, ysize, xsize),
                                            dtype=dtype)
                for i in pyrange(steps):
                    out_dict[var][i, yi, xi] = mydict[var][:, i]
                out_dict[var][:, ymask, xmask] = fill_val

            elif mydict[var].ndim == 3:
                j = mydict[var].shape[1]
                k = mydict[var].shape[2]
                out_dict[var] = np.ma.zeros((j, k, ysize, xsize),
                                            dtype=dtype)
                for jj in pyrange(j):
                    for kk in pyrange(k):
                        out_dict[var][jj, kk, yi, xi] = mydict[var][:, jj, kk]
                for y, x in pyzip(ymask, xmask):
                    out_dict[var][:, :, y, x] = fill_val

            out_dict[var] = np.ma.masked_values(out_dict[var], fill_val)

        out_dicts[name] = out_dict

    # Merge information from veglib and veg and transfer to veg_dict
    if veglib_dict and veg_dict:

        # Check for any missing Cv areas that will become bare soil
        var = 'Cv'
        bare = 1 - out_dicts['veg_dict'][var].sum(axis=0)
        bare[bare < 0.0] = 0.0

        # Determine the final number of veg classes, accounting for
        # potential new bare soil class, and determine that class's idx