-
Notifications
You must be signed in to change notification settings - Fork 5
/
ST_params.c
1255 lines (1075 loc) · 41.7 KB
/
ST_params.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* \file ST_params.c
* \brief Reads and initializes the model parameters.
*
* Most of the parameters come from the input files and some are computed.
*
* History
* (6/15/2000) -- INITIAL CODING - cwb
* (15-Apr-02) -- added code to interface with SOILWAT (cwb)
* only modified parm_Files_Init().
*
* \author CWB (initial coding)
* \author Chandler Haukap (author of this documentation)
* \date 15 April 2002
*/
/* =================================================== */
/* INCLUDES / DEFINES */
/* --------------------------------------------------- */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "ST_steppe.h"
#include "sw_src/include/filefuncs.h"
#include "sw_src/include/myMemory.h"
#include "sw_src/include/rands.h"
#include "sxw_funcs.h"
#include "ST_globals.h"
#include "sxw_vars.h"
#include "ST_mortality.h" // externs `UseCheatgrassWildfire`
/******** Modular External Function Declarations ***********/
/* -- truly global functions are declared in functions.h --*/
/***********************************************************/
SppIndex species_New(void);
/*------------------------------------------------------*/
/* Modular functions only used on one or two specific */
/* places; that is, they are not generally useful */
/* (like C++ friend functions) but have to be declared. */
void parm_Initialize(void);
void parm_SetFirstName( char *s);
void parm_SetName( char *s, int which);
void parm_free_memory( void );
void maxrgroupspecies_init(void);
void files_init(void);
/*********** Locally Used Function Declarations ************/
/***********************************************************/
static void _env_init( void);
static void _plot_init( void);
static void _setNameLen(char *dest, char *src, Int len);
static void _rgroup_init( void);
static void _species_init( void);
static void _check_species( void);
static void _bmassflags_init( void);
static void _mortflags_init( void);
static void _model_init( void);
static void _rgroup_add1( char name[], RealF space, RealF density,
Int estab, RealF slow, Int stretch,
Int xres, Int estann, Int turnon,
Int styr, RealF xgro, Int veg_prod_type, Int mort,
RealF biomass, RealF transpiration, RealF live_biomass);
static void _rgroup_add2( char name[],
RealF nslope, RealF nint,
RealF wslope, RealF wint,
RealF dslope, RealF dint);
static void _rgroup_add_disturbance( char name[], Int killyr, Int killfreq_startyr,RealF killfreq,
Int extirp, RealF prop_killed, RealF prop_recovered,RealF grazing_frq,RealF prop_grazing,Int grazingfreq_startyr);
static void _rgroup_addsucculent( char name[],
RealF wslope, RealF wint,
RealF dslope, RealF dint);
//static void _recover_names(void);
/************ Module Variable Declarations ******************/
/***********************************************************/
#define NFILES 15
static char *_files[NFILES];
char *MyFileName;
/**************************************************************/
/* fdpierson: This function should only be called once, otherwise
* memory leaks involving resource groups, species, and potentially
* other variables will occur. */
/**************************************************************/
void parm_Initialize() {
/*======================================================*/
_model_init();
_env_init();
_plot_init();
_bmassflags_init();
_mortflags_init();
_rgroup_init();
_species_init();
_check_species();
}
/**************************************************************/
char *Parm_name( ST_FileIndex i) {
/*======================================================*/
return _files[i];
}
/**************************************************************/
void parm_SetFirstName( char *s) {
/*======================================================*/
if (_files[F_First]) Mem_Free( _files[F_First]);
_files[F_First] = Str_Dup(s, &LogInfo);
}
/**************************************************************/
void parm_SetName( char *s, int which) {
/*======================================================*/
// (DLM) - 5/28/2013 - added this function because I needed a way to change the name of some of the files (particularly the output files) easily from other modules
if(which > (NFILES - 1) || which < 0)
return;
if (_files[which]) Mem_Free( _files[which]);
_files[which] = Str_Dup(s, &LogInfo);
}
/**************************************************************/
void files_init( void ) {
/*======================================================*/
/* 7-May-02 (cwb) added code to interface with SOILWAT */
FILE *f;
ST_FileIndex i;
ST_FileIndex last = F_MaxRGroupSpecies;
char inbuf[MAX_FILENAMESIZE];
MyFileName = Parm_name(F_First);
f = OpenFile(MyFileName, "r", &LogInfo);
for(i=F_Log; i <= last; i++) {
if ( ! GetALine(f, inbuf)) break;
_files[i] = Str_Dup(Str_TrimLeftQ(inbuf), &LogInfo);
//printf("FILES: %d : %s\n", i, _files[i]);
}
if ( i < last) {
LogError(&LogInfo, LOGERROR, "%s: Too few input files specified",
MyFileName);
}
CloseFile(&f, &LogInfo);
if(!UseGrid) { //if the gridded option has been specified, then the logfile has already been opened
if ( !strcmp("stdout", _files[F_Log]) )
LogInfo.logfp = stdout;
else
LogInfo.logfp = OpenFile(_files[F_Log], "w", &LogInfo);
}
}
/**************************************************************/
static void _model_init( void) {
/*======================================================*/
FILE *f;
int seed, years;
char tmp[80], inbuf[MAX_FILENAMESIZE];
MyFileName = Parm_name(F_Model);
f = OpenFile(MyFileName, "r", &LogInfo);
/* ----------------------------------------------------*/
/* scan for the first line*/
if (!GetALine(f, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: No data found!\n", MyFileName);
} else {
sscanf( inbuf, "%s %d %d",
tmp,
&years,
&seed);
SuperGlobals.runModelYears = years;
Globals->Max_Age = SuperGlobals.runModelYears;
SuperGlobals.runModelIterations = atoi(tmp);
if (SuperGlobals.runModelIterations < 1 ||
SuperGlobals.runModelYears < 1 ) {
LogError(&LogInfo, LOGERROR,"Invalid parameters for RunModelIterations "
"or RunModelYears (%s)",
MyFileName);
}
Globals->bmass.suffixwidth = Globals->mort.suffixwidth = strlen(tmp);
SuperGlobals.randseed = (IntL) ((seed) ? -abs(seed) : 0);
}
CloseFile(&f, &LogInfo);
}
/**************************************************************/
static void _env_init( void) {
/*======================================================*/
/* Read environmental parameters that drive
the abiotic conditions. These values go
into the Globals structure
*/
FILE *f;
int x,
index=0,
nitems,
use[3];
char inbuf[MAX_FILENAMESIZE];
MyFileName = Parm_name(F_Env);
f = OpenFile(MyFileName, "r", &LogInfo);
while( GetALine(f, inbuf)) {
switch(++index) {
case 1:
x=sscanf( inbuf, "%f %f %hu %hu %hu %hu %f %hu",
&Globals->ppt.avg, &Globals->ppt.std,
&Globals->ppt.min, &Globals->ppt.max,
&Globals->ppt.dry, &Globals->ppt.wet,
&Globals->gsppt_prop, &Globals->transp_window);
nitems = 8;
break;
case 2:
x=sscanf( inbuf, "%f %f %f %f %f",
&Globals->temp.avg, &Globals->temp.std,
&Globals->temp.min, &Globals->temp.max,
&Globals->temp.gstemp);
nitems = 5;
break;
case 3:
x=sscanf( inbuf, "%d %f %f %f %f ", &use[0],
&Globals->pat.occur, &Globals->pat.removal,
&Globals->pat.recol[Slope],
&Globals->pat.recol[Intcpt]);
nitems = 4;
break;
case 4:
x=sscanf( inbuf, "%d %f %hu %hu", &use[1],
&Globals->mound.occur,
&Globals->mound.minyr,
&Globals->mound.maxyr);
nitems = 3;
break;
case 5:
x=sscanf( inbuf, "%d %f %hu", &use[2],
&Globals->burrow.occur,
&Globals->burrow.minyr);
nitems = 2;
break;
case 6:
x=sscanf( inbuf, "%f %f %f %f %f %f",
&Globals->tempparm[CoolSeason][0],
&Globals->tempparm[CoolSeason][1],
&Globals->tempparm[CoolSeason][2],
&Globals->tempparm[WarmSeason][0],
&Globals->tempparm[WarmSeason][1],
&Globals->tempparm[WarmSeason][2]);
nitems = 6;
break;
}
if (x<nitems) {
LogError(&LogInfo, LOGERROR, "%s: Invalid record %d",
MyFileName, index);
}
} /* end while*/
Globals->pat.use = itob(use[0]);
Globals->mound.use = itob(use[1]);
Globals->burrow.use = itob(use[2]);
CloseFile(&f, &LogInfo);
}
/**************************************************************/
static void _plot_init( void) {
/*======================================================*/
FILE *f;
int x, nitems=1;
char inbuf[MAX_FILENAMESIZE];
MyFileName = Parm_name(F_Plot);
f = OpenFile(MyFileName, "r", &LogInfo);
/* ----------------------------------------------------*/
/* scan for the first line*/
if (!GetALine(f, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: No data found!\n", MyFileName);
}
x = sscanf( inbuf, " %f", &Globals->plotsize);
if (x < nitems) {
LogError(&LogInfo, LOGERROR, "%s: Incorrect number of fields",
MyFileName);
}
CloseFile(&f, &LogInfo);
}
/**************************************************************/
static void _check_species( void) {
/*======================================================*/
/* Make sure max_spp_estab <= max_spp */
/* Also this block creates then links */
/* species' numbers to their respective groups. */
/* - and sets up mortality arrays for groups and spp (5/18/01)*/
/* 10-Apr-03 - cwb - compute max_bmass for the group as the
* sum of the mature biomass of each species in the group.
* plot biomass = sum(g->relsize * g->max_density * g->max_bmass)
* 7-Nov-03 (cwb) make sure no annuals are assigned to
* perennials groups.
*/
IntS i, cnt,
maxage, minage, /* placeholders */
runyrs = SuperGlobals.runModelYears; /* shorthand */
SppIndex sp;
GrpIndex rg;
Bool tripped = FALSE;
GroupType *g;
SpeciesType *s;
/* -------------------------------------------*/
/* count and link species to their groups.
* print a message if more specified than available */
ForEachGroup(rg) { g = RGroup[rg];
cnt = 0;
ForEachSpecies(sp) {
if (Species[sp]->res_grp == rg)
g->species[cnt++] = sp;
}
g->max_spp = cnt;
if (cnt < g->max_spp_estab) {
tripped = TRUE;
g->max_spp_estab = cnt;
LogError(&LogInfo, LOGWARN, "Max_Spp_Estab > Number of Spp for %s",
g->name);
}
}
if (tripped) LogError(&LogInfo, LOGWARN,"Continuing.");
/* -------------------------------------------*/
/* determine max age for the species and
* keep track for group's max age */
/* and compute max g/m^2 for this group */
/* 10-Apr-03 - also, compute max_bmass for the group. */
ForEachGroup(rg) { g = RGroup[rg];
maxage = 0; minage = 30000;
g->max_bmass = 0.0;
ForEachGroupSpp(sp, rg, i) {
s = Species[sp];
if (s->max_age == 0) s->max_age = runyrs+1;
/* maxage shouldn't be set to extirp due to age-independent mortality.
extirp happens due to the flag, not age;
but I hesitate to remove the code, so I'll comment it out for now.
if ( g->extirp && g->extirp < s->max_age)
s->max_age = g->extirp;
*/
maxage = max(s->max_age, maxage);
minage = min(s->max_age, minage);
g->max_bmass += s->mature_biomass;
}
if (minage == 1 && maxage != 1) {
LogError(&LogInfo, LOGERROR, "%s: Can't mix annuals and perennials within a group\n"
"Refer to the groups.in and species.in files\n",
RGroup[rg]->name);
}
g->max_age = maxage;
}
/* -------------------------------------------*/
/* check out the definitions for SppMaxAge and GrpMaxAge */
/* they're used here for some hoped-for sense of readability */
if (MortFlags.species) {
/* size each species kills list, age ==0 ok */
ForEachSpecies(sp) {
if ( Species[sp]->use_me) {
Species[sp]->kills = (IntUS *) Mem_Calloc(SppMaxAge(sp),
sizeof(IntUS),
"_check_species(Species.kills)", &LogInfo);
} else {
Species[sp]->kills = NULL;
}
}
} else {
ForEachSpecies(sp)
Species[sp]->kills = NULL;
}
if (MortFlags.group) {
ForEachGroup(rg) {
if (RGroup[rg]->use_me) {
/* find max age of the group and size the kills array */
ForEachGroupSpp( sp, rg, i) {
RGroup[rg]->max_age = (Species[sp]->max_age)
? max(Species[sp]->max_age,
RGroup[rg]->max_age)
: Globals->Max_Age;
}
RGroup[rg]->kills = (IntUS *) Mem_Calloc(GrpMaxAge(rg),
sizeof(IntUS),
"_check_species(RGroup.kills)", &LogInfo);
} else {
RGroup[rg]->kills = NULL;
}
}
} else {
ForEachGroup(rg)
RGroup[rg]->kills = NULL;
}
}
/**************************************************************/
static void _bmassflags_init( void) {
/*======================================================*/
/* read in the flags to control the output quantities etc.
It helps to have the file visible while reading this code.
* 9-Dec-03 (cwb) added code to create output directory.
*/
FILE *fin;
Int x, i,
nitems=17; /* number of items expected in first input line */
/* code controls: */
char u[5], /* summary? if 'n' don't init and don't print */
a[5], /* print yearly files */
h[5], /* header line containing var names */
f[5], /* field separator */
y[5], /* year number */
d[5], /* disturbance type */
p[5], /* precip */
c[5], /* ppt class (wet/dry/norm) */
t[5], /* temperature */
g[5], /* biomass for the groups */
q[5], /* groups PR quotient (see C&L1990, p240) */
r[5], /* relative size for each group */
w[5], /* wildfire count */
m[5], /* prescribed fire count */
s[5], /* biomass for each species */
n[5], /* number of individuals for each species */
b[5]; /* if 'y', output grazed biomass */
char z;
char inbuf[MAX_FILENAMESIZE], bMassAvgFile[FILENAME_MAX],
bMassPreFile[FILENAME_MAX];
MyFileName = Parm_name(F_BMassFlag);
fin = OpenFile(MyFileName, "r", &LogInfo);
if (!GetALine(fin, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: No data found!\n", MyFileName);
}
x = sscanf( inbuf, "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
u, a, h, f, y, d, p, c, t, g, q, r, w, m, b, s, n );
/* don't bother initializing the rest if first flag is 'n' */
BmassFlags.summary = (Bool)(*u=='y'||*u=='Y');
BmassFlags.yearly = (Bool)(*a=='y'||*a=='Y');
if (!(BmassFlags.summary || BmassFlags.yearly)) {
return;
}
if (x < nitems) {
LogError(&LogInfo, LOGERROR, "%s: Invalid number of parameters",
MyFileName);
}
for( i=1; i<= nitems; i++) {
/* to stick in a new item, just add it to the bottom
and adjust nitems in definitions. */
switch (i) {
case 1:
BmassFlags.header = (Bool)(*h=='y'||*h=='Y');
break;
case 2:
z = (char) tolower((Int)*f);
switch( z ) {
case 't': BmassFlags.sep = '\t'; break;
case 's': BmassFlags.sep = ' '; break;
default : BmassFlags.sep = z; break;
}
break;
case 3:
BmassFlags.yr = (Bool)(*y=='y'||*y=='Y');
break;
case 4:
BmassFlags.dist = (Bool)(*d=='y'||*d=='Y');
break;
case 5:
BmassFlags.ppt = (Bool)(*p=='y'||*p=='Y');
break;
case 6:
BmassFlags.pclass = (Bool)(*c=='y'||*c=='Y');
break;
case 7:
BmassFlags.tmp = (Bool)(*t=='y'||*t=='Y');
break;
case 8:
BmassFlags.grpb = (Bool)(*g=='y'||*g=='Y');
break;
case 9:
BmassFlags.pr = (Bool)(*q=='y'||*q=='Y');
break;
case 10:
BmassFlags.size = (Bool)(*r=='y'||*r=='Y');
break;
case 11:
BmassFlags.wildfire = (Bool)(*w=='y'||*w=='Y');
break;
case 12:
BmassFlags.prescribedfire = (Bool)(*m=='y'||*m=='Y');
break;
case 13:
BmassFlags.graz = (Bool)(*b == 'y' || *b == 'Y');
break;
case 14:
BmassFlags.sppb = (Bool)(*s == 'y' || *s == 'Y');
break;
case 15:
BmassFlags.indv = (Bool)(*n == 'y' || *n == 'Y');
break;
}
}
CloseFile(&fin, &LogInfo);
/* remove old output and/or create the output directories if needed */
/* borrow inbuf for filenames */
/* -- do avg file first, otherwise it may get deleted by match
* with BMassPre and then not be there for specific delete.
*/
DirName(Parm_name(F_BMassAvg), bMassAvgFile);
if (DirExists(bMassAvgFile)) {
strcpy(inbuf, Parm_name(F_BMassAvg));
if (!RemoveFiles(inbuf, &LogInfo) )
LogError(&LogInfo, LOGWARN, "Can't remove old average biomass output file %s\n%s",
inbuf, strerror(errno) );
} else if (!MkDir(bMassAvgFile, &LogInfo) ) {
LogError(&LogInfo, LOGERROR,
"Can't make output path for average biomass file: %s\n%s",
bMassAvgFile, strerror(errno));
}
DirName(Parm_name(F_BMassPre), bMassPreFile);
if (DirExists(bMassPreFile)) {
strcpy(inbuf, Parm_name(F_BMassPre));
strcat(inbuf, "*.csv");
if (!RemoveFiles(inbuf, &LogInfo) )
LogError(&LogInfo, LOGWARN, "Can't remove old biomass output files %s\n%s",
inbuf, strerror(errno) );
} else if (!MkDir(bMassPreFile, &LogInfo)) {
LogError(&LogInfo, LOGERROR,
"Can't make output path for yearly biomass files: %s\n%s",
bMassPreFile, strerror(errno) );
}
}
/**************************************************************/
static void _mortflags_init( void) {
/*======================================================*/
/* read in the flags to control the output quantities etc.
It helps to have the file visible while reading this code.
*/
FILE *fin;
Int x, i,
nitems=6; /* number of items expected in first input line */
/* code controls: */
char s[5], /* summary stats? if 'n' don't init and don't print */
y[5], /* print yearly files */
h[5], /* header line containing var names */
f[5], /* field separator */
g[5], /* group data */
k[5]; /* species data */
char z;
char inbuf[MAX_FILENAMESIZE], mortAvgFile[FILENAME_MAX],
mortPreFile[FILENAME_MAX];
MyFileName = Parm_name(F_MortFlag);
fin = OpenFile(MyFileName, "r", &LogInfo);
if (!GetALine(fin, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s No data found!\n", MyFileName);
}
x = sscanf( inbuf, "%s %s %s %s %s %s",
s, y, h, f, g, k );
/* don't bother initializing the rest if first flag is 'n' */
MortFlags.summary = (Bool)(*s=='y'||*s=='Y');
MortFlags.yearly = (Bool)(*y=='y'||*y=='Y');
if (!(MortFlags.summary || MortFlags.yearly)) {
MortFlags.header = MortFlags.group = MortFlags.species = FALSE;
return;
}
if (x < nitems -2) {
LogError(&LogInfo, LOGERROR,"%s: Invalid number of parameters",
MyFileName);
}
for( i=3; i<= nitems ; i++) { /* 3 accounts for first two flags */
/* to stick in a new item, just add it to the bottom
and adjust nitems in definitions. */
switch (i) {
case 3:
MortFlags.header = (Bool)(*h=='y'||*h=='Y');
break;
case 4:
MortFlags.group = (Bool)(*g=='y'||*g=='Y');
break;
case 5:
MortFlags.species = (Bool)(*k=='y'||*k=='Y');
break;
case 6:
z = (char) tolower((Int)*f);
switch( z ) {
case 't': MortFlags.sep = '\t'; break;
case 's': MortFlags.sep = ' '; break;
default : MortFlags.sep = z; break;
}
break;
default:
break;
}
}
CloseFile(&fin, &LogInfo);
/* remove old output and/or create the output directories if needed */
/* borrow inbuf for filenames */
/* -- do avg file first, otherwise it may get deleted by match
* with MortPre and then not be there for specific delete.
*/
DirName(Parm_name(F_MortAvg), mortAvgFile);
if (DirExists(mortAvgFile)) {
strcpy(inbuf, Parm_name(F_MortAvg));
if (!RemoveFiles(inbuf, &LogInfo) )
LogError(&LogInfo, LOGWARN, "Can't remove old average biomass output file %s\n%s",
inbuf, strerror(errno) );
} else if (!MkDir(mortAvgFile, &LogInfo)) {
LogError(&LogInfo, LOGERROR,
"Can't make output path for average biomass file: %s\n%s",
mortAvgFile, strerror(errno));
}
DirName(Parm_name(F_MortAvg), mortPreFile);
if (DirExists(mortPreFile)) {
strcpy(inbuf, Parm_name(F_MortPre));
strcat(inbuf, "*.csv");
if (!RemoveFiles(inbuf, &LogInfo) )
LogError(&LogInfo, LOGWARN, "Can't remove old biomass output files %s\n%s",
inbuf, strerror(errno) );
} else if (!MkDir(mortPreFile, &LogInfo) ) {
LogError(&LogInfo, LOGERROR,
"Can't make output path for yearly biomass files: %s\n%s",
mortPreFile, strerror(errno) );
}
}
/**************************************************************/
static void _setNameLen(char *dest, char *src, Int len) {
/*======================================================*/
strncpy(dest, src, len);
dest[len] = '\0';
}
/**************************************************************/
void maxrgroupspecies_init( void) {
/*======================================================*/
FILE *f;
char inbuf[MAX_FILENAMESIZE];
MyFileName = Parm_name(F_MaxRGroupSpecies);
f = OpenFile(MyFileName, "r", &LogInfo);
/* These values determine the memory allocated for resource groups, species, and their names. */
/* If these limits are exceeded, memory leaks will result. */
/* Resource group limits */
if (!GetALine(f, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum resource groups allowed.", MyFileName);
}
if (sscanf(inbuf, "%zu", &SuperGlobals.max_rgroups) != 1) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum resource groups allowed.", MyFileName);
}
if (!GetALine(f, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum resource group name length.", MyFileName);
}
if (sscanf(inbuf, "%zu", &SuperGlobals.max_groupnamelen) != 1) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum resource group name length.", MyFileName);
}
/* Species limits */
if (!GetALine(f, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum species allowed per resource group.", MyFileName);
}
if (sscanf(inbuf, "%zu", &SuperGlobals.max_spp_per_grp) != 1) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum species allowed per resource group.", MyFileName);
}
if (!GetALine(f, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum individuals allowed per species.", MyFileName);
}
if (sscanf(inbuf, "%zu", &SuperGlobals.max_indivs_per_spp) != 1) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum individuals allowed per species.", MyFileName);
}
if (!GetALine(f, inbuf)) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum species name length.", MyFileName);
}
if (sscanf(inbuf, "%zu", &SuperGlobals.max_speciesnamelen) != 1) {
LogError(&LogInfo, LOGERROR, "%s: Could not read maximum species name length.", MyFileName);
}
CloseFile(&f, &LogInfo);
}
/**************************************************************/
/**************************************************************/
/*****************************************************
* The *_RGroup_* functions read the input from the user
* file for resource-group-level information.
*
*****************************************************/
static void _rgroup_init( void) {
/*======================================================*/
/* Read parameters for resource group
*/
FILE *f;
IntS x;
Bool groupsok;
/* temp vars to hold the group info*/
char *name;
/* input variables*/
Int estab, stretch, xres, turnon, estann,
styr, veg_prod_type, mort;
RealF space, density, slow,
nslope, nint, wslope, wint, dslope, dint, xgro;
/* input variables related to disturbances */
Int extirp, killyr, killfreq_startyr,
grazingfreq_startyr;
RealF killfreq, prop_killed, prop_recovered,grazing_frq, prop_grazing, biomass,
transpiration, live_biomass;
char inbuf[MAX_FILENAMESIZE];
MyFileName = Parm_name(F_RGroup);
f = OpenFile(MyFileName, "r", &LogInfo);
name = (char *)Mem_Calloc(SuperGlobals.max_groupnamelen + 1,
sizeof(char), "_rgroup_init", &LogInfo);
RGroup = (GroupType **)Mem_Calloc(SuperGlobals.max_rgroups,
sizeof(GroupType *), "_rgroup_init", &LogInfo);
/* ------------------------------------------------------------*/
/* Install all the defined groups, except for dry/wet/norm parms */
groupsok = FALSE;
while( GetALine(f,inbuf)) {
if (!isnull(strstr(inbuf,"[end]"))) {
groupsok = TRUE;
break;
}
x=sscanf( inbuf, "%s %f %f %d %f %d %d %d %d %d %f %d %d %d %f %d %d %f %f"
" %f %f %d %f %f %f", name, &space, &density, &estab, &slow,
&stretch, &xres, &estann, &turnon, &styr, &xgro, &veg_prod_type,
&killyr, &killfreq_startyr, &killfreq, &extirp, &mort,
&prop_killed, &prop_recovered,&grazing_frq, &prop_grazing,
&grazingfreq_startyr, &biomass, &transpiration, &live_biomass);
if (x < 25) {
LogError(&LogInfo, LOGERROR, "%s: Too few columns in groups",
MyFileName);
}
// Convert to SOILWAT2 vegetation index
veg_prod_type = get_SW2_veg_index(veg_prod_type);
_rgroup_add1( name, space, density, estab, slow, stretch, xres, estann,
turnon, styr, xgro, veg_prod_type, mort, biomass,
transpiration, live_biomass);
_rgroup_add_disturbance(name, killyr, killfreq_startyr, killfreq,
extirp, prop_killed, prop_recovered,grazing_frq, prop_grazing,
grazingfreq_startyr);
}/* end while*/
if (!groupsok) {
LogError(&LogInfo, LOGERROR, "%s: Incomplete input in group definitions",
MyFileName);
}
/* ------------------------------------------------------------*/
/* Install dry/wet/norm parms for defined groups */
groupsok = FALSE;
while( GetALine(f,inbuf)) {
if (!isnull(strstr(inbuf,"[end]"))) {
groupsok = TRUE;
break;
}
x=sscanf( inbuf, "%s %f %f %f %f %f %f",
name,
&nslope, &nint, &wslope, &wint, &dslope, &dint);
if (x != 7) {
LogError(&LogInfo, LOGERROR, "%s: Wrong number of columns in groups' wet/dry parms",
MyFileName);
}
_rgroup_add2( name, nslope, nint, wslope, wint, dslope, dint);
}/* end while*/
if (!groupsok) {
LogError(&LogInfo, LOGERROR, "%s: Incomplete input in group definitions",
MyFileName);
}
/* ------------------------------------------------------------*/
/* Get succulent growth modifiers*/
GetALine(f, inbuf);
x=sscanf( inbuf, "%s %f %f %f %f",
name, &wslope, &wint, &dslope, &dint);
if (x < 5) {
LogError(&LogInfo, LOGERROR,
"%s: Too few values in succulent growth parameters",
MyFileName);
}
_rgroup_addsucculent( name, wslope, wint, dslope, dint);
/* ------------------------------------------------------------ */
/* Get wildfire parameters */
GetALine(f,inbuf);
groupsok = FALSE;
while(GetALine(f,inbuf)) {
if (!isnull(strstr(inbuf,"[end]"))) {
groupsok = TRUE;
break;
}
x=sscanf( inbuf, "%u", &UseCheatgrassWildfire);
if (x != 1) {
LogError(&LogInfo, LOGERROR, "%s: Cheatgrass-Wildfire flag not read.",
MyFileName);
}
}/* end while*/
Mem_Free(name);
CloseFile(&f, &LogInfo);
}
/**************************************************************/
static void _rgroup_add1( char name[], RealF space, RealF density,
Int estab, RealF slow, Int stretch,
Int xres, Int estann, Int turnon,
Int styr, RealF xgro, Int veg_prod_type, Int mort,
RealF biomass, RealF transpiration, RealF live_biomass) {
GrpIndex rg;
size_t len;
rg = RGroup_New();
len = strlen(name);
_setNameLen(RGroup[rg]->name, name, len);
RGroup[rg]->grp_num = rg;
RGroup[rg]->max_stretch = (IntS) stretch;
RGroup[rg]->max_spp_estab = (IntS) estab;
// input of `density` is in units of [# / m2]; convert to units of [# / plot]
RGroup[rg]->max_density = density * Globals->plotsize; // density per plot
RGroup[rg]->max_per_sqm = density; // density per square-meter
RGroup[rg]->use_mort = itob(mort);
RGroup[rg]->slowrate = slow;
RGroup[rg]->space = space;
RGroup[rg]->min_res_req = space;
RGroup[rg]->est_annually = itob(estann);
RGroup[rg]->startyr = styr;
RGroup[rg]->xgrow = xgro;
RGroup[rg]->use_me = itob(turnon);
RGroup[rg]->veg_prod_type = veg_prod_type;
RGroup[rg]->use_extra_res = itob(xres);
RGroup[rg]->_bvt = biomass / transpiration;
RGroup[rg]->live_biomass = live_biomass;
}
/**************************************************************/
static void _rgroup_add2( char name[], RealF nslope, RealF nint, RealF wslope,
RealF wint, RealF dslope, RealF dint) {
GrpIndex rg;
rg = RGroup_Name2Index(name);
if (rg < 0) {
LogError(&LogInfo, LOGERROR, "%s: Mismatched name (%s) for succulents",
MyFileName, name);
}
RGroup[rg]->ppt_slope[Ppt_Norm] = nslope;
RGroup[rg]->ppt_intcpt[Ppt_Norm]= nint;
RGroup[rg]->ppt_slope[Ppt_Wet] = wslope;
RGroup[rg]->ppt_intcpt[Ppt_Wet] = wint;
RGroup[rg]->ppt_slope[Ppt_Dry] = dslope;
RGroup[rg]->ppt_intcpt[Ppt_Dry] = dint;
}
static void _rgroup_add_disturbance( char name[], Int killyr, Int killfreq_startyr,
RealF killfreq, Int extirp, RealF prop_killed,
RealF prop_recovered, RealF grazing_frq, RealF prop_grazing,
Int grazingfreq_startyr) {
/*======================================================*/
GrpIndex rg;
rg = RGroup_Name2Index(name);
if (rg < 0) {
LogError(&LogInfo, LOGERROR, "%s: Mismatched name (%s) for disturbance",
MyFileName, name);
}
RGroup[rg]->killyr = killyr;
RGroup[rg]->killfreq_startyr = killfreq_startyr;
RGroup[rg]->killfreq = killfreq;
RGroup[rg]->extirp = (IntS) extirp;
RGroup[rg]->proportion_killed = prop_killed;
RGroup[rg]->proportion_recovered = prop_recovered;
RGroup[rg]->grazingfrq = grazing_frq;
RGroup[rg]->proportion_grazing = prop_grazing;
RGroup[rg]->grazingfreq_startyr = grazingfreq_startyr;
RGroup[rg]->extirpated = FALSE;
}
/**************************************************************/
static void _rgroup_addsucculent( char name[], RealF wslope, RealF wint,
RealF dslope, RealF dint) {
/* This should only get called one time, ie, there should be*/
/* only one set of succulent parameters, even if there is more*/
/* than one succulent species to which they pertain.*/
GrpIndex rg;
rg = RGroup_Name2Index(name);
if (rg < 0) {
LogError(&LogInfo, LOGERROR, "%s: Mismatched name (%s) for succulents",
MyFileName, name);
}
RGroup[rg]->succulent = TRUE;
Succulent->growth[Slope] = wslope;
Succulent->growth[Intcpt] = wint;
Succulent->mort[Slope] = dslope;
Succulent->mort[Intcpt] = dint;
}
/**
* \brief Read the species-specific inputs and initialize the \ref Species
* array.
*
* This function will read the species.in files and allocate enough memory
* for the number of species requested. It then adds the species to the
* \ref Species array and initializes all variables in the corresponding
* \ref SpeciesType struct.
*
* \sideeffect Memory will be allocated for multiple \ref SpeciesType structs
* and the memory will be populated according to the input
* parameters.
*