|
| 1 | +#!/usr/bin/env python |
| 2 | +# PYTHON_ARGCOMPLETE_OK |
| 3 | + |
| 4 | +"""VICpy command line interface""" |
| 5 | +from __future__ import print_function |
| 6 | +# import argcomplete |
| 7 | +import argparse |
| 8 | +import subprocess |
| 9 | +from vic import version, netcdf2vic, compare_soil_params, grid_params, \ |
| 10 | + ncparam2ascii, vic2netcdf |
| 11 | + |
| 12 | + |
| 13 | +# -------------------------------------------------------------------- # |
| 14 | +def main(): |
| 15 | + """ |
| 16 | + Get the script and path to the config_file |
| 17 | + """ |
| 18 | + # ---------------------------------------------------------------- # |
| 19 | + # create the top-level parser |
| 20 | + parser = argparse.ArgumentParser(prog='VICpy', |
| 21 | + description='Python toolkit for the ' |
| 22 | + 'VIC model', |
| 23 | + version=version.short_version) |
| 24 | + subparsers = parser.add_subparsers(title='VICpy subcommands') |
| 25 | + subparsers.completer = complete_subparser_action |
| 26 | + # ---------------------------------------------------------------- # |
| 27 | + |
| 28 | + # ---------------------------------------------------------------- # |
| 29 | + # create the parser for the "netcdf2vic" command |
| 30 | + netcdf2vic_parser = subparsers.add_parser('netcdf2vic', |
| 31 | + help=netcdf2vic.help, |
| 32 | + description=netcdf2vic.description) |
| 33 | + netcdf2vic_parser.set_defaults(func=netcdf2vic._run) |
| 34 | + netcdf2vic_parser.add_argument("config", |
| 35 | + type=str, |
| 36 | + help="Input netcdf2vic Configuration File") |
| 37 | + # ---------------------------------------------------------------- # |
| 38 | + |
| 39 | + # ---------------------------------------------------------------- # |
| 40 | + # create the parser for the "grid_params" command |
| 41 | + grid_params_parser = subparsers.add_parser('grid_params', |
| 42 | + help=grid_params.help, |
| 43 | + description=grid_params.description) |
| 44 | + grid_params_parser.set_defaults(func=grid_params._run) |
| 45 | + grid_params_parser.add_argument("-g", "--grid_file", |
| 46 | + type=str, |
| 47 | + help="Input netCDF target grid", |
| 48 | + default=None) |
| 49 | + grid_params_parser.add_argument("-s", "--soil_file", |
| 50 | + type=str, |
| 51 | + help="Input file containing soil parameter" |
| 52 | + " data in standard VIC format", |
| 53 | + required=True) |
| 54 | + grid_params_parser.add_argument("-e", "--snow_file", |
| 55 | + type=str, |
| 56 | + help="Input file containing snowband/" |
| 57 | + "elevation band data in standard VIC " |
| 58 | + "format", |
| 59 | + default=None) |
| 60 | + grid_params_parser.add_argument("-v", "--veg_file", |
| 61 | + type=str, |
| 62 | + help="Input file containing vegitation " |
| 63 | + "parameter data " |
| 64 | + "in standard VIC format", |
| 65 | + default=None) |
| 66 | + grid_params_parser.add_argument("-l", "--vegl_file", |
| 67 | + type=str, |
| 68 | + help="Input file containing vegitation " |
| 69 | + "library data in" |
| 70 | + " standard VIC format", |
| 71 | + default=None) |
| 72 | + grid_params_parser.add_argument("-o", "--out_file", |
| 73 | + type=str, |
| 74 | + help="Output file name, " |
| 75 | + "(default=/params.nc)", |
| 76 | + default='params.nc') |
| 77 | + |
| 78 | + grid_params_parser.add_argument("--version", |
| 79 | + type=str, |
| 80 | + help="VIC version to write parameter file " |
| 81 | + "for", |
| 82 | + choices=['4.1.2', '5.0.dev'], |
| 83 | + default='4.1.2') |
| 84 | + # ---------------------------------------------------------------- # |
| 85 | + |
| 86 | + # ---------------------------------------------------------------- # |
| 87 | + # create the parser for the "ncparam2ascii" command |
| 88 | + ncparam2ascii_parser = subparsers.add_parser('ncparam2ascii', |
| 89 | + help=ncparam2ascii.help, |
| 90 | + description=ncparam2ascii.description) |
| 91 | + ncparam2ascii_parser.set_defaults(func=ncparam2ascii._run) |
| 92 | + ncparam2ascii_parser.add_argument("nc_params", |
| 93 | + type=str, |
| 94 | + help="Input netCDF VIC parameter file") |
| 95 | + ncparam2ascii_parser.add_argument("--soil_prefix", |
| 96 | + type=str, |
| 97 | + help="Output soil param file prefix " |
| 98 | + "(default is same as nc_params)", |
| 99 | + default=False) |
| 100 | + ncparam2ascii_parser.add_argument("--veg_prefix", |
| 101 | + type=int, |
| 102 | + help="Output veg param file prefix", |
| 103 | + default=False) |
| 104 | + ncparam2ascii_parser.add_argument("-UL", "--upper_left_corner", |
| 105 | + type=int, |
| 106 | + help="Upper left corner for subset", |
| 107 | + default=False) |
| 108 | + ncparam2ascii_parser.add_argument("-LR", "--lower_right_corner", |
| 109 | + type=int, |
| 110 | + help="Lower right corner for subset", |
| 111 | + default=False) |
| 112 | + ncparam2ascii_parser.add_argument("--outfiles", |
| 113 | + type=int, |
| 114 | + help="Number of outfiles", |
| 115 | + default=1) |
| 116 | + ncparam2ascii_parser.add_argument("--snow_file", |
| 117 | + type=str, |
| 118 | + help="Name of output snow file", |
| 119 | + default=False) |
| 120 | + ncparam2ascii_parser.add_argument("--veg_file", |
| 121 | + type=str, |
| 122 | + help="Name of output veg_file", |
| 123 | + default=False) |
| 124 | + ncparam2ascii_parser.add_argument("--project", |
| 125 | + type=str, |
| 126 | + help='Use project configuration options', |
| 127 | + choices=['RASM']) |
| 128 | + ncparam2ascii_parser.add_argument("--NIJSSEN2ARNO", |
| 129 | + help='Convert soil parameters from ' |
| 130 | + 'NIJSSEN2001 format to ARNO format', |
| 131 | + action='store_true') |
| 132 | + ncparam2ascii_parser.add_argument("--ARNO2NIJSSEN", |
| 133 | + help='Convert soil parameters from ' |
| 134 | + 'ARNO format to NIJSSEN2001 format', |
| 135 | + action='store_true') |
| 136 | + # ---------------------------------------------------------------- # |
| 137 | + |
| 138 | + # ---------------------------------------------------------------- # |
| 139 | + # create the parser for the "compare_soil_params" command |
| 140 | + compare_soil_params_parser = subparsers.add_parser('compare_soil_params', |
| 141 | + help=compare_soil_params.help, |
| 142 | + description=compare_soil_params.description) |
| 143 | + compare_soil_params_parser.set_defaults(func=compare_soil_params._run) |
| 144 | + |
| 145 | + compare_soil_params_parser.add_argument("-d", "--domain_file", |
| 146 | + type=str, |
| 147 | + help="Input netCDF target grid", |
| 148 | + required=True) |
| 149 | + |
| 150 | + compare_soil_params_parser.add_argument("-s1", "--soil_file1", |
| 151 | + type=str, |
| 152 | + help="Input file containing soil " |
| 153 | + "parameter", |
| 154 | + required=True) |
| 155 | + compare_soil_params_parser.add_argument("-s2", "--soil_file2", |
| 156 | + type=str, |
| 157 | + help="Input file containing soil " |
| 158 | + "parameter", |
| 159 | + required=True) |
| 160 | + compare_soil_params_parser.add_argument("-o", "--out_path", |
| 161 | + type=str, |
| 162 | + help="outpath for files", |
| 163 | + default='./') |
| 164 | + compare_soil_params_parser.add_argument("-t1", "--title1", |
| 165 | + type=str, |
| 166 | + help="Input tile for soil_file1", |
| 167 | + default=None) |
| 168 | + compare_soil_params_parser.add_argument("-t2", "--title2", |
| 169 | + type=str, |
| 170 | + help="Input tile for soil_file2", |
| 171 | + default=None) |
| 172 | + # ---------------------------------------------------------------- # |
| 173 | + |
| 174 | + # ---------------------------------------------------------------- # |
| 175 | + # create the parser for the "vic2netcdf" command |
| 176 | + vic2netcdf_parser = subparsers.add_parser('vic2netcdf', |
| 177 | + help=vic2netcdf.help, |
| 178 | + description=vic2netcdf.description) |
| 179 | + vic2netcdf_parser.set_defaults(func=vic2netcdf._run) |
| 180 | + vic2netcdf_parser.add_argument("config", |
| 181 | + type=str, |
| 182 | + help="Input vic2netcdf Configuration File") |
| 183 | + # ---------------------------------------------------------------- # |
| 184 | + |
| 185 | + # argcomplete.autocomplete(parser) |
| 186 | + args = parser.parse_args() |
| 187 | + return |
| 188 | +# -------------------------------------------------------------------- # |
| 189 | + |
| 190 | + |
| 191 | +# -------------------------------------------------------------------- # |
| 192 | +# completer for subparsers |
| 193 | +def complete_subparser_action(prefix, **kwargs): |
| 194 | + internal_actions = ['vic2netcdf', 'netcdf2vic'] |
| 195 | + if prefix and any(action.startswith(prefix) |
| 196 | + for action in internal_actions): |
| 197 | + return internal_actions |
| 198 | + else: |
| 199 | + external_actions = subprocess.check_output("compgen -c " + prefix, |
| 200 | + shell=True, |
| 201 | + executable='/bin/bash') |
| 202 | + external_actions = [a.decode() for a in external_actions.splitlines()] |
| 203 | + return internal_actions + external_actions |
| 204 | +# -------------------------------------------------------------------- # |
| 205 | + |
| 206 | +# -------------------------------------------------------------------- # |
| 207 | +if __name__ == "__main__": |
| 208 | + main() |
| 209 | +# -------------------------------------------------------------------- # |
0 commit comments