-
Notifications
You must be signed in to change notification settings - Fork 5
/
ST_output.c
277 lines (236 loc) · 7.78 KB
/
ST_output.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
/**
* \file ST_output.c
* \brief Outputs mortality or biomass on a yearly time step.
*
* This file differs from \ref ST_stats.c because it does not accumulate values.
* It simply prints the values to the given year.
*
* \ingroup OUTPUT
*/
/* =================================================== */
/* INCLUDES / DEFINES */
/* --------------------------------------------------- */
#include <string.h>
#include <stdlib.h>
#include "ST_steppe.h"
#include "ST_globals.h"
#include "sw_src/include/filefuncs.h"
#include "sw_src/include/myMemory.h"
/******** Modular External Function Declarations ***********/
/* -- truly global functions are declared in functions.h --*/
/***********************************************************/
/*------------------------------------------------------*/
/* 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 output_Bmass_Yearly( Int year );
void output_Mort_Yearly( void );
/**
* \brief Outputs the current year's values to the file denoted in
* [Globals.bmass.fp_year](\ref Globals)
*
* \param year is the year that these values are being printed. This is 1 indexed.
*
* \ingroup OUTPUT
*/
void output_Bmass_Yearly( Int year ) {
char **fields;
GrpIndex rg;
SppIndex sp;
int i, fc = 0;
char *s;
char filename[FILENAME_MAX];
if (!BmassFlags.yearly) return;
fields = (char **)Mem_Calloc(MAX_OUTFIELDS, sizeof(char *), "output_Bmass_Yearly", &LogInfo);
s = (char *)Mem_Calloc(MAX_FIELDLEN + 1, sizeof(char), "output_Bmass_Yearly", &LogInfo);
for (i = 0; i < MAX_OUTFIELDS; i++) {
fields[i] = (char *)Mem_Calloc(MAX_FIELDLEN + 1, sizeof(char), "output_Bmass_Yearly", &LogInfo);
}
if(Globals->currYear == 1) // At year one we need a header.
{
/* --------- Begin setting up header ------- */
if (BmassFlags.yr)
strcpy(fields[fc++], "Year");
if (BmassFlags.dist)
strcpy(fields[fc++], "Disturbs");
if (BmassFlags.ppt) {
strcpy(fields[fc++], "PPT");
}
if (BmassFlags.pclass)
strcpy(fields[fc++], "PPTClass");
if (BmassFlags.tmp) {
strcpy(fields[fc++], "Temp");
}
if (BmassFlags.grpb) {
if(BmassFlags.wildfire){
strcpy(fields[fc++], "Wildfire");
}
ForEachGroup(rg)
{
strcpy(fields[fc++], RGroup[rg]->name);
if (BmassFlags.size) {
strcpy(fields[fc], RGroup[rg]->name);
strcat(fields[fc++], "_RSize");
}
if (BmassFlags.pr) {
strcpy(fields[fc], RGroup[rg]->name);
strcat(fields[fc++],"_PR");
}
if(BmassFlags.prescribedfire){
strcpy(fields[fc], RGroup[rg]->name);
strcat(fields[fc++],"_PFire");
}
}
}
if (BmassFlags.sppb) {
ForEachSpecies(sp) {
strcpy(fields[fc++], Species[sp]->name);
if (BmassFlags.indv) {
strcpy(fields[fc], Species[sp]->name);
strcat(fields[fc++], "_Indivs");
}
}
}
sprintf(filename, "%s%0*d.csv", Parm_name(F_BMassPre),
Globals->bmass.suffixwidth,
Globals->currIter);
Globals->bmass.fp_year = OpenFile(filename, "a", &LogInfo);
/* Write data line to already opened file */
for (i=0; i< fc-1; i++) {
fprintf(Globals->bmass.fp_year,"%s%c", fields[i], BmassFlags.sep);
}
if (i) fprintf(Globals->bmass.fp_year,"%s\n", fields[i]);
fflush(Globals->bmass.fp_year);
CloseFile(&Globals->bmass.fp_year, &LogInfo);
fc = 0; //reset fc for first line of data.
}
/* ------------- end setting up header -------------- */
if (BmassFlags.yr) {
sprintf(fields[fc++], "%d", SoilWatRun.Model.year);
}
if (BmassFlags.dist) {
switch (Plot->disturbance) {
case NoDisturb: strcpy(s, "None"); break;
case FecalPat: strcpy(s, "Pat"); break;
case AntMound: strcpy(s, "Mound"); break;
case Burrow: strcpy(s, "Burrow"); break;
default: strcpy(s, "Unknown!"); break;
}
sprintf(fields[fc++], "%s", s);
}
if (BmassFlags.ppt)
sprintf(fields[fc++], "%d", Env->ppt);
if (BmassFlags.pclass) {
switch (Env->wet_dry) {
case Ppt_Norm: strcpy(s, "Normal"); break;
case Ppt_Wet: strcpy(s, "Wet"); break;
case Ppt_Dry: strcpy(s, "Dry"); break;
default: strcpy(s, "Unknown!"); break;
}
sprintf(fields[fc++], "%s", s);
}
if (BmassFlags.tmp)
sprintf(fields[fc++], "%0.1f", Env->temp);
if (BmassFlags.grpb) {
if (BmassFlags.wildfire)
sprintf(fields[fc++],"%d", RGroup[0]->wildfire);
ForEachGroup(rg) {
sprintf(fields[fc++], "%f", RGroup_GetBiomass(rg));
if (BmassFlags.size)
sprintf(fields[fc++],"%f", getRGroupRelsize(rg));
if (BmassFlags.pr)
sprintf(fields[fc++],"%f", RGroup[rg]->pr);
if (BmassFlags.prescribedfire)
sprintf(fields[fc++],"%d", RGroup[rg]->prescribedfire);
}
}
if (BmassFlags.sppb) {
ForEachSpecies(sp) {
sprintf(fields[fc++], "%f", Species_GetBiomass(sp));
if (BmassFlags.indv)
sprintf(fields[fc++],"%d", Species[sp]->est_count);
}
}
sprintf(filename, "%s%0*d.csv", Parm_name(F_BMassPre),
Globals->bmass.suffixwidth,
Globals->currIter);
Globals->bmass.fp_year = OpenFile(filename, "a", &LogInfo);
/* Write data line to already opened file */
for (i=0; i< fc-1; i++) {
fprintf(Globals->bmass.fp_year,"%s%c", fields[i], BmassFlags.sep);
}
if (i) fprintf(Globals->bmass.fp_year,"%s\n", fields[i]);
fflush(Globals->bmass.fp_year);
CloseFile(&Globals->bmass.fp_year, &LogInfo);
for (i = 0; i < MAX_OUTFIELDS; i++) {
free(fields[i]);
}
free(s);
free(fields);
}
/**
* \brief Outputs the current year's values to the file specified in
* [Globals->mort.fp_year](\ref Globals)
*
* Prints mortality values. These values are indexed by age at death.
*
* \ingroup OUTPUT
*/
void output_Mort_Yearly( void ) {
IntS age, rg, sp;
char filename[FILENAME_MAX];
sprintf(filename, "%s%0*d.csv", Parm_name(F_MortPre), Globals->mort.suffixwidth, Globals->currIter);
Globals->mort.fp_year = OpenFile(filename, "a", &LogInfo);
FILE *f = Globals->mort.fp_year;
if (!MortFlags.yearly)
return;
/* Print header line */
fprintf(f, "Age");
if (MortFlags.group) {
ForEachGroup(rg) fprintf(f, "%c%s", MortFlags.sep, RGroup[rg]->name);
}
if (MortFlags.species) {
ForEachSpecies(sp) fprintf(f, "%c%s", MortFlags.sep, Species[sp]->name);
}
/* Header line is now complete */
fprintf(f, "\n");
/* Print a line of establishments */
fprintf(f, "(Estabs)");
if (MortFlags.group) {
ForEachGroup(rg)
fprintf(Globals->mort.fp_year, "%c%d", MortFlags.sep, RGroup[rg]->estabs);
}
if (MortFlags.species) {
ForEachSpecies(sp)
fprintf(f, "%c%d", MortFlags.sep, Species[sp]->estabs);
}
fprintf(f, "\n");
/* now print the kill data */
for (age = 0; age < Globals->Max_Age; age++) {
fprintf(f, "%d", age + 1);
if (MortFlags.group) {
ForEachGroup(rg)
{
if (age < GrpMaxAge(rg) && RGroup[rg]->use_me)
fprintf(f, "%c%d", MortFlags.sep, RGroup[rg]->kills[age]);
else
fprintf(f, "%c", MortFlags.sep);
}
}
if (MortFlags.species) {
ForEachSpecies(sp)
{
/*adding fix for bus error while on/off some species:
* Reason proper species use-me boolean values were not use in check so added the same
* Modify By: Ashish
*/
if (age < SppMaxAge(sp) && Species[sp]->use_me && RGroup[Species[sp]->res_grp]->use_me)
fprintf(f, "%c%d", MortFlags.sep, Species[sp]->kills[age]);
else
fprintf(f, "%c", MortFlags.sep);
}
}
fprintf(f, "\n");
}
CloseFile(&Globals->mort.fp_year, &LogInfo);
}