~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster/r.topmodel/main.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * r.topmodel: simulates TOPMODEL based on TMOD9502.FOR.
3
 
 *
4
 
 * TMOD9502.FOR Author: Keith Beven <k.beven@lancaster.ac.uk>
5
 
 *                      http://www.es.lancs.ac.uk/hfdg/topmodel.html
6
 
 *
7
 
 *      Copyright (C) 2000 by the GRASS Development Team
8
 
 *      Author: Huidae Cho <grass4u@gmail.com>
9
 
 *              Hydro Laboratory, Kyungpook National University
10
 
 *              South Korea
11
 
 *
12
 
 *      This program is free software under the GPL (>=v2)
13
 
 *      Read the file COPYING coming with GRASS for details.
14
 
 *
15
 
 */
16
 
 
17
 
#define MAIN
 
1
 
 
2
/****************************************************************************
 
3
 *
 
4
 * MODULE:       r.topmodel
 
5
 *
 
6
 * AUTHOR(S):    Huidae Cho <grass4u gmail.com>, Hydro Laboratory,
 
7
 *               Kyungpook National University
 
8
 *               Based on TMOD9502.FOR by Keith Beven <k.beven lancaster.ac.uk>
 
9
 *
 
10
 * PURPOSE:      Simulates TOPMODEL.
 
11
 *
 
12
 * COPYRIGHT:    (C) 2000-2014 by the GRASS Development Team
 
13
 *
 
14
 *               This program is free software under the GNU General Public
 
15
 *               License (>=v2). Read the file COPYING that comes with GRASS
 
16
 *               for details.
 
17
 *
 
18
 *****************************************************************************/
 
19
 
 
20
#define _MAIN_C_
18
21
#include <stdio.h>
19
22
#include <stdlib.h>
 
23
#include <grass/gis.h>
20
24
#include <grass/glocale.h>
21
25
#include "global.h"
22
26
 
23
 
 
24
27
int main(int argc, char **argv)
25
28
{
26
 
 
27
29
    struct GModule *module;
28
30
    struct
29
31
    {
30
 
        struct Option *basin;
31
 
        struct Option *elev;
32
 
        struct Option *fill;
33
 
        struct Option *dir;
34
 
        struct Option *belev;
35
 
        struct Option *topidx;
36
 
        struct Option *nidxclass;
37
 
        struct Option *idxstats;
38
32
        struct Option *params;
 
33
        struct Option *topidxstats;
39
34
        struct Option *input;
40
35
        struct Option *output;
41
 
        struct Option *Qobs;
42
36
        struct Option *timestep;
43
 
        struct Option *idxclass;
44
 
    } param;
45
 
 
 
37
        struct Option *topidxclass;
 
38
        struct Option *topidx;
 
39
        struct Option *ntopidxclasses;
 
40
        struct Option *outtopidxstats;
 
41
    } params;
46
42
    struct
47
43
    {
48
 
        struct Flag *input;
49
 
    } flag;
 
44
        struct Flag *preprocess;
 
45
    } flags;
50
46
 
51
47
    /* Initialize GRASS and parse command line */
52
48
    G_gisinit(argv[0]);
53
49
 
54
50
    module = G_define_module();
55
 
    module->keywords = _("raster, hydrology");
 
51
    G_add_keyword(_("raster"));
 
52
    G_add_keyword(_("hydrology"));
56
53
    module->description =
57
54
        _("Simulates TOPMODEL which is a physically based hydrologic model.");
58
55
 
59
56
    /* Parameter definitions */
60
 
    param.basin = G_define_option();
61
 
    param.basin->key = "basin";
62
 
    param.basin->description =
63
 
        _("(i)   Basin map created by r.water.outlet (MASK)");
64
 
    param.basin->type = TYPE_STRING;
65
 
    param.basin->required = NO;
66
 
    param.basin->gisprompt = "old,cell,raster";
67
 
 
68
 
    param.elev = G_define_option();
69
 
    param.elev->key = "elevation";
70
 
    param.elev->description = _("(i)   Elevation map");
71
 
    param.elev->type = TYPE_STRING;
72
 
    param.elev->required = NO;
73
 
    param.elev->gisprompt = "old,cell,raster";
74
 
 
75
 
    param.fill = G_define_option();
76
 
    param.fill->key = "depressionless";
77
 
    param.fill->description = _("(o)   Depressionless elevation map");
78
 
    param.fill->type = TYPE_STRING;
79
 
    param.fill->required = NO;
80
 
    param.fill->gisprompt = "new,cell,raster";
81
 
 
82
 
    param.dir = G_define_option();
83
 
    param.dir->key = "direction";
84
 
    param.dir->description =
85
 
        _("(o)   Direction map for depressionless elevation map");
86
 
    param.dir->type = TYPE_STRING;
87
 
    param.dir->required = NO;
88
 
    param.dir->gisprompt = "new,cell,raster";
89
 
 
90
 
    param.belev = G_define_option();
91
 
    param.belev->key = "belevation";
92
 
    param.belev->description = _("(o/i) Basin elevation map (MASK applied)");
93
 
    param.belev->type = TYPE_STRING;
94
 
    param.belev->required = NO;
95
 
    param.belev->gisprompt = "new,cell,raster";
96
 
 
97
 
    param.topidx = G_define_option();
98
 
    param.topidx->key = "topidx";
99
 
    param.topidx->description =
100
 
        _("(o)   Topographic index ln(a/tanB) map (MASK applied)");
101
 
    param.topidx->type = TYPE_STRING;
102
 
    param.topidx->required = NO;
103
 
    param.topidx->gisprompt = "new,cell,raster";
104
 
 
105
 
    param.nidxclass = G_define_option();
106
 
    param.nidxclass->key = "nidxclass";
107
 
    param.nidxclass->description =
108
 
        _("(i)   Number of topographic index classes");
109
 
    param.nidxclass->type = TYPE_INTEGER;
110
 
    param.nidxclass->required = NO;
111
 
    param.nidxclass->answer = "30";
112
 
 
113
 
    param.idxstats = G_define_option();
114
 
    param.idxstats->key = "idxstats";
115
 
    param.idxstats->description =
116
 
        _("(o/i) Topographic index statistics file");
117
 
    param.idxstats->type = TYPE_STRING;
118
 
    param.idxstats->required = YES;
119
 
 
120
 
    param.params = G_define_option();
121
 
    param.params->key = "parameters";
122
 
    param.params->description = _("(i)   TOPMODEL Parameters file");
123
 
    param.params->type = TYPE_STRING;
124
 
    param.params->required = YES;
125
 
 
126
 
    param.input = G_define_option();
127
 
    param.input->key = "input";
128
 
    param.input->description =
129
 
        _("(i)   Rainfall and potential evapotranspiration data file");
130
 
    param.input->type = TYPE_STRING;
131
 
    param.input->required = YES;
132
 
 
133
 
    param.output = G_define_option();
134
 
    param.output->key = "output";
135
 
    param.output->description = _("(o)   Output file");
136
 
    param.output->type = TYPE_STRING;
137
 
    param.output->required = YES;
138
 
 
139
 
    param.Qobs = G_define_option();
140
 
    param.Qobs->key = "Qobs";
141
 
    param.Qobs->description = _("(i)   OPTIONAL Observed flow file");
142
 
    param.Qobs->type = TYPE_STRING;
143
 
    param.Qobs->required = NO;
144
 
 
145
 
    param.timestep = G_define_option();
146
 
    param.timestep->key = "timestep";
147
 
    param.timestep->description =
148
 
        _("(i)   OPTIONAL Output for given time step");
149
 
    param.timestep->type = TYPE_INTEGER;
150
 
    param.timestep->required = NO;
151
 
 
152
 
    param.idxclass = G_define_option();
153
 
    param.idxclass->key = "idxclass";
154
 
    param.idxclass->description =
155
 
        _("(i)   OPTIONAL Output for given topographic index class");
156
 
    param.idxclass->type = TYPE_INTEGER;
157
 
    param.idxclass->required = NO;
158
 
 
159
 
 
160
 
    /* Flag definitions */
161
 
    flag.input = G_define_flag();
162
 
    flag.input->key = 'i';
163
 
    flag.input->description = _("Input data given for (o/i)");
 
57
    params.params = G_define_standard_option(G_OPT_F_INPUT);
 
58
    params.params->key = "parameters";
 
59
    params.params->description = _("Name of TOPMODEL parameters file");
 
60
 
 
61
    params.topidxstats = G_define_standard_option(G_OPT_F_INPUT);
 
62
    params.topidxstats->key = "topidxstats";
 
63
    params.topidxstats->description =
 
64
        _("Name of topographic index statistics file");
 
65
 
 
66
    params.input = G_define_standard_option(G_OPT_F_INPUT);
 
67
    params.input->description =
 
68
        _("Name of rainfall and potential evapotranspiration data file");
 
69
 
 
70
    params.output = G_define_standard_option(G_OPT_F_OUTPUT);
 
71
    params.output->description = _("Name for output file");
 
72
 
 
73
    params.timestep = G_define_option();
 
74
    params.timestep->key = "timestep";
 
75
    params.timestep->label = _("Time step");
 
76
    params.timestep->description = _("Generate output for this time step");
 
77
    params.timestep->type = TYPE_INTEGER;
 
78
    params.timestep->required = NO;
 
79
 
 
80
    params.topidxclass = G_define_option();
 
81
    params.topidxclass->key = "topidxclass";
 
82
    params.topidxclass->label = _("Topographic index class");
 
83
    params.topidxclass->description =
 
84
        _("Generate output for this topographic index class");
 
85
    params.topidxclass->type = TYPE_INTEGER;
 
86
    params.topidxclass->required = NO;
 
87
 
 
88
    params.topidx = G_define_standard_option(G_OPT_R_INPUT);
 
89
    params.topidx->key = "topidx";
 
90
    params.topidx->label =
 
91
        _("Name of input topographic index raster map");
 
92
    params.topidx->description =
 
93
        _("Must be clipped to the catchment boundary. Used for generating outtopidxstats");
 
94
    params.topidx->required = NO;
 
95
    params.topidx->guisection = _("Preprocess");
 
96
 
 
97
    params.ntopidxclasses = G_define_option();
 
98
    params.ntopidxclasses->key = "ntopidxclasses";
 
99
    params.ntopidxclasses->label = _("Number of topographic index classes");
 
100
    params.ntopidxclasses->description =
 
101
        _("Used for generating outtopidxstats");
 
102
    params.ntopidxclasses->type = TYPE_INTEGER;
 
103
    params.ntopidxclasses->required = NO;
 
104
    params.ntopidxclasses->answer = "30";
 
105
    params.ntopidxclasses->guisection = _("Preprocess");
 
106
 
 
107
    params.outtopidxstats = G_define_standard_option(G_OPT_F_OUTPUT);
 
108
    params.outtopidxstats->key = "outtopidxstats";
 
109
    params.outtopidxstats->label =
 
110
        _("Name for output topographic index statistics file");
 
111
    params.outtopidxstats->description =
 
112
        _("Requires topidx and ntopidxclasses");
 
113
    params.outtopidxstats->required = NO;
 
114
    params.outtopidxstats->guisection = _("Preprocess");
 
115
 
 
116
    flags.preprocess = G_define_flag();
 
117
    flags.preprocess->key = 'p';
 
118
    flags.preprocess->description =
 
119
        _("Preprocess only and stop after generating outtopidxstats");
 
120
    flags.preprocess->suppress_required = YES;
164
121
 
165
122
    if (G_parser(argc, argv))
166
123
        exit(EXIT_FAILURE);
167
124
 
168
 
    /* Store given parameters and flags */
169
 
    map.basin = param.basin->answer;
170
 
    map.elev = param.elev->answer;
171
 
    map.belev = param.belev->answer;
172
 
    map.fill = param.fill->answer;
173
 
    map.dir = param.dir->answer;
174
 
    map.topidx = param.topidx->answer;
175
 
    file.idxstats = param.idxstats->answer;
176
 
    file.params = param.params->answer;
177
 
    file.input = param.input->answer;
178
 
    file.output = param.output->answer;
179
 
    file.Qobs = param.Qobs->answer;
180
 
 
181
 
    misc.nidxclass = atoi(param.nidxclass->answer);
182
 
 
183
 
    if (!param.timestep->answer)
184
 
        param.timestep->answer = "0";
185
 
    if (!param.idxclass->answer)
186
 
        param.idxclass->answer = "0";
187
 
 
188
 
    misc.timestep = atoi(param.timestep->answer);
189
 
    misc.idxclass = atoi(param.idxclass->answer);
190
 
 
191
 
    flg.input = flag.input->answer;
192
 
    flg.overwr = module->overwrite;
193
 
 
194
 
 
195
 
    gisbase = G_gisbase();
196
 
    mapset = G_mapset();
197
 
 
198
 
 
199
 
    /* Check run conditions */
200
 
    if (check_ready())
201
 
        exit(1);
202
 
 
203
 
    /* Adjust cell header */
204
 
    gregion();
205
 
 
206
 
    /* Create required maps */
207
 
    if (!flg.input) {
208
 
        if (map.fill)
209
 
            depressionless();
210
 
        basin_elevation();
 
125
    /* Store given parameters */
 
126
    file.params = params.params->answer;
 
127
    file.topidxstats = params.topidxstats->answer;
 
128
    file.input = params.input->answer;
 
129
    file.output = params.output->answer;
 
130
 
 
131
    if (!params.timestep->answer)
 
132
        params.timestep->answer = "0";
 
133
    misc.timestep = atoi(params.timestep->answer);
 
134
 
 
135
    if (!params.topidxclass->answer)
 
136
        params.topidxclass->answer = "0";
 
137
    misc.topidxclass = atoi(params.topidxclass->answer);
 
138
 
 
139
    if (params.topidx->answer && params.outtopidxstats->answer) {
 
140
        char *topidx;
 
141
        int ntopidxclasses;
 
142
        char *outtopidxstats;
 
143
        
 
144
        topidx = params.topidx->answer;
 
145
        ntopidxclasses = atoi(params.ntopidxclasses->answer);
 
146
        outtopidxstats = params.outtopidxstats->answer;
 
147
 
 
148
        if (ntopidxclasses <= 1)
 
149
            G_fatal_error(_("%s must be greater than 1"), "ntopidxclasses");
 
150
 
 
151
        create_topidxstats(topidx, ntopidxclasses, outtopidxstats);
 
152
    } else if (params.topidx->answer) {
 
153
        G_warning(_("Ignoring %s because %s is not specified"),
 
154
                        params.topidx->key, params.outtopidxstats->key);
 
155
    } else if (params.outtopidxstats->answer) {
 
156
        G_warning(_("Ignoring %s because %s is not specified"),
 
157
                        params.outtopidxstats->key, params.topidx->key);
211
158
    }
212
159
 
213
 
    top_index();
214
 
 
215
 
    /* Read required files */
216
 
    read_inputs();
217
 
 
218
 
    /* Implement TOPMODEL */
219
 
    topmodel();
220
 
 
221
 
    /* Write outputs */
222
 
    write_outputs();
223
 
 
224
 
 
225
 
    exit(0);
 
160
    if (flags.preprocess->answer)
 
161
        exit(EXIT_SUCCESS);
 
162
 
 
163
    /* Read input */
 
164
    read_input();
 
165
 
 
166
    /* Run TOPMODEL */
 
167
    run_topmodel();
 
168
 
 
169
    /* Write output */
 
170
    write_output();
 
171
 
 
172
    exit(EXIT_SUCCESS);
226
173
}