-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.c
414 lines (365 loc) · 11.4 KB
/
driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*******************************************************************************
MODULE: driver.c
CONTAINING SYSTEM: SLEUTH-3r (based on SLEUTH Model 3.0 Beta)
(Slope, Land-cover, Exclusion, Urbanization,
Transportation, and Hillshade)
also known as UGM 3.0 Beta (for Urban Growth Model)
VERSION: SLEUTH-3r [Includes Version D features]
REVISION DATE: August 31, 2006a
[Annotations added August 19, 2009]
PURPOSE:
For each combination of inputs during calibration runs or for
the inputs provided for test or prediction runs, the functions
in this module set up and iterate through Monte Carlo runs
and process statistics.
NOTES:
MODIFICATIONS:
TO DO:
**************************************************************************/
#define DRIVER_MODULE
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "igrid_obj.h"
#include "pgrid_obj.h"
#include "landclass_obj.h"
#include "color_obj.h"
#include "coeff_obj.h"
#include "utilities.h"
#include "memory_obj.h"
#include "scenario_obj.h"
#include "transition_obj.h"
#include "ugm_macros.h"
#include "ugm_defines.h"
#include "proc_obj.h"
#include "gdif_obj.h"
#include "growth.h"
#include "stats_obj.h"
#include "timer_obj.h"
#include "color_obj.h"
/*****************************************************************************\
*******************************************************************************
** **
** TYPEDEFS **
** **
*******************************************************************************
\*****************************************************************************/
typedef double fmatch_t;
/*****************************************************************************\
*******************************************************************************
** **
** STATIC FUNCTION PROTOTYPES **
** **
*******************************************************************************
\*****************************************************************************/
static
void drv_monte_carlo (GRID_P z_cumulate_ptr, GRID_P sim_landuse_ptr);
static
fmatch_t drv_fmatch (GRID_P cum_probability_ptr,
GRID_P landuse1_ptr);
/*****************************************************************************\
*******************************************************************************
** **
** SCCS ID **
** **
*******************************************************************************
\*****************************************************************************/
char driver_c_sccs_id[] = "@(#)driver.c 1.629 12/4/00";
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: drv_driver
** PURPOSE: main function for driving the simulation grw_growth()
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
void
drv_driver ()
{
char func[] = "drv_driver";
char name[] = "_cumcolor_urban_";
GRID_P sim_landuse_ptr;
GRID_P z_cumulate_ptr;
GRID_P cum_probability_ptr;
GRID_P cum_uncertainty_ptr;
GRID_P landuse1_ptr;
int total_pixels;
char filename[256];
fmatch_t fmatch = 0.0;
struct colortable *colortable_ptr;
FUNC_INIT;
timer_Start (DRV_DRIVER);
total_pixels = mem_GetTotalPixels ();
z_cumulate_ptr = pgrid_GetCumulatePtr ();
sim_landuse_ptr = pgrid_GetLand1Ptr ();
assert (total_pixels > 0);
assert (z_cumulate_ptr != NULL);
assert (sim_landuse_ptr != NULL);
/*
*
* CREATE ANNUAL LANDUSE PROBABILITY FILE
*
*/
if (proc_GetProcessingType () == PREDICTING)
{
if (scen_GetDoingLanduseFlag ())
{
landclass_AnnualProbInit ();
}
}
/*
*
* MONTE CARLO SIMULATION
*
*/
drv_monte_carlo (z_cumulate_ptr, sim_landuse_ptr);
if (proc_GetProcessingType () == PREDICTING)
{
/*
*
* OUTPUT URBAN IMAGES
*
*/
sprintf (filename, "%scumulate_urban.gif", scen_GetOutputDir ());
colortable_ptr = color_GetColortable (GRAYSCALE_COLORTABLE);
gdif_WriteGIF (z_cumulate_ptr,
colortable_ptr,
filename,
"",
SEED_COLOR_INDEX);
util_WriteZProbGrid (z_cumulate_ptr, name);
if (scen_GetDoingLanduseFlag ())
{
cum_probability_ptr = mem_GetWGridPtr (__FILE__, func, __LINE__);
cum_uncertainty_ptr = mem_GetWGridPtr (__FILE__, func, __LINE__);
assert (cum_probability_ptr != NULL);
assert (cum_uncertainty_ptr != NULL);
landclass_BuildProbImage (cum_probability_ptr, cum_uncertainty_ptr);
/*
*
* OUTPUT CUMULATIVE PROBABILITY IMAGE
*
*/
sprintf (filename, "%scumcolor_landuse.gif", scen_GetOutputDir ());
colortable_ptr = color_GetColortable (LANDUSE_COLORTABLE);
assert (colortable_ptr != NULL);
gdif_WriteGIF (cum_probability_ptr,
colortable_ptr,
filename,
"",
SEED_COLOR_INDEX);
/*
*
* OUTPUT CUMULATIVE UNCERTAINTY IMAGE
*
*/
sprintf (filename, "%suncertainty.landuse.gif", scen_GetOutputDir ());
colortable_ptr = color_GetColortable (GRAYSCALE_COLORTABLE);
assert (colortable_ptr != NULL);
gdif_WriteGIF (cum_uncertainty_ptr,
colortable_ptr,
filename,
"",
SEED_COLOR_INDEX);
cum_probability_ptr = mem_GetWGridFree (__FILE__, func, __LINE__, cum_probability_ptr);
cum_uncertainty_ptr = mem_GetWGridFree (__FILE__, func, __LINE__, cum_uncertainty_ptr);
}
/* end of: if (scen_GetDoingLanduseFlag()) */
}
/* end of: if(proc_GetProcessingType() == PREDICTING) */
if ((!scen_GetDoingLanduseFlag ()) || (proc_GetProcessingType () == PREDICTING))
{
fmatch = 0.0;
}
else
{
landuse1_ptr = igrid_GetLanduseGridPtr (__FILE__, func, __LINE__, 1);
fmatch = drv_fmatch (sim_landuse_ptr, landuse1_ptr);
landuse1_ptr = igrid_GridRelease (__FILE__, func, __LINE__, landuse1_ptr);
}
stats_Analysis (fmatch);
/* end of: if(proc_GetProcessingType() == PREDICTING) */
timer_Stop (DRV_DRIVER);
/*
*
* kludge to stop the TOTAL_TIME timer in main.c from
* overflowing
*
*/
timer_Stop (TOTAL_TIME);
timer_Start (TOTAL_TIME);
FUNC_END;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: drv_monte_carlo
** PURPOSE: Monte Carlo loop
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static
void
drv_monte_carlo (GRID_P cumulate_ptr, GRID_P land1_ptr)
{
char func[] = "drv_monte_carlo";
int imc;
int i;
double *class_slope;
double *ftransition;
GRID_P z_ptr;
int total_pixels;
int num_monte_carlo;
int *new_indices;
GRID_P road_state_ptr;
FUNC_INIT;
class_slope = trans_GetClassSlope ();
ftransition = trans_GetFTransition ();
z_ptr = pgrid_GetZPtr ();
total_pixels = mem_GetTotalPixels ();
new_indices = landclass_GetNewIndicesPtr ();
num_monte_carlo = scen_GetMonteCarloIterations ();
road_state_ptr = pgrid_GetRoadStatePtr();
assert (total_pixels > 0);
assert (land1_ptr != NULL);
assert (z_ptr != NULL);
assert (ftransition != NULL);
assert (class_slope != NULL);
assert (new_indices != NULL);
assert (num_monte_carlo > 0);
for (imc = 0; imc < scen_GetMonteCarloIterations (); imc++)
{
proc_SetCurrentMonteCarlo (imc);
/*
*
* RESET THE PARAMETERS
*
*/
coeff_SetCurrentDiffusion (coeff_GetSavedDiffusion ());
coeff_SetCurrentSpread (coeff_GetSavedSpread ());
coeff_SetCurrentBreed (coeff_GetSavedBreed ());
coeff_SetCurrentSlopeResist (coeff_GetSavedSlopeResist ());
coeff_SetCurrentRoadGravity (coeff_GetSavedRoadGravity ());
if (scen_GetLogFlag ())
{
if (scen_GetLogCoeffFlag ())
{
scen_Append2Log ();
coeff_LogCurrent (scen_GetLogFP ());
scen_CloseLog ();
}
}
/*
*
* RUN SIMULATION
*
*/
stats_InitUrbanizationAttempts ();
grw_grow (z_ptr, land1_ptr, road_state_ptr);
if (scen_GetLogFlag ())
{
if (scen_GetLogUrbanizationAttemptsFlag ())
{
scen_Append2Log ();
stats_LogUrbanizationAttempts (scen_GetLogFP ());
scen_CloseLog ();
}
}
/*
*
* UPDATE CUMULATE GRID
*
*/
/** D. Donato August 16, 2006 "If" statement added to prevent unnecessary ***
*** processing during calibration. **/
if (proc_GetProcessingType () != CALIBRATING)
{
for (i = 0; i < total_pixels; i++)
{
if (z_ptr[i] > 0)
{
cumulate_ptr[i]++;
}
}
}
/*
*
* UPDATE ANNUAL LAND CLASS PROBABILITIES
*
*/
if (proc_GetProcessingType () == PREDICTING)
{
landclass_AnnualProbUpdate (land1_ptr);
}
}
/*
*
* NORMALIZE CUMULATIVE URBAN IMAGE
*
*/
/** D. Donato August 16, 2006 "If" statement added to prevent unnecessary ***
*** processing during calibration. **/
if (proc_GetProcessingType () != CALIBRATING)
{
for (i = 0; i < total_pixels; i++)
{
cumulate_ptr[i] = (100.0 * cumulate_ptr[i]) / num_monte_carlo;
}
}
FUNC_END;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: drv_fmatch
** PURPOSE: calculate fmatch
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static
fmatch_t
drv_fmatch (GRID_P cum_probability_ptr,
GRID_P landuse1_ptr)
{
char func[] = "drv_fmatch";
int match_count;
int trans_count;
float fmatch;
FUNC_INIT;
assert (cum_probability_ptr != NULL);
if (scen_GetDoingLanduseFlag ())
{
assert (landuse1_ptr != NULL);
}
if (!scen_GetDoingLanduseFlag ())
{
fmatch = 1.0;
}
else
{
match_count = util_img_intersection (mem_GetTotalPixels (),
cum_probability_ptr,
landuse1_ptr);
trans_count = mem_GetTotalPixels () - match_count;
if ((match_count == 0) && (trans_count == 0))
{
fmatch = 0.0;
}
else
{
fmatch = (float) match_count / (match_count + trans_count);
}
}
FUNC_END;
return fmatch;
}