-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevents.c
244 lines (226 loc) · 8.96 KB
/
events.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
//
// events.c
/**
* @file events.c
* @brief Handles reading, parsing, and storing agronomic events for SIPNET simulations.
*
* The `events.in` file specifies agronomic events with the following columns:
* | col | parameter | description | units |
* |-----|-------------|--------------------------------------------|------------------|
* | 1 | loc | spatial location index | |
* | 2 | year | year of start of this timestep | |
* | 3 | day | day of start of this timestep | Day of year |
* | 4 | event_type | type of event | (e.g., "irrig") |
* | 5...n| event_param| parameters specific to the event type | (varies) |
*
*
* Event types include:
* - "irrig": Parameters = [amount added (cm/d), type (0=canopy, 1=soil, 2=flood)]
* - "fert": Parameters = [Org-N (g/m²), Org-C (g/ha), Min-N (g/m²), Min-N2 (g/m²)]
* - "till": Parameters = [SOM decomposition modifier, litter decomposition modifier]
* - "plant": Parameters = [emergence lag (days), C (g/m²), N (g/m²)]]
* - "harv": Parameters = [fraction aboveground removed, fraction belowground removed, ...]
* See test examples in `tests/sipnet/test_events/`.
*/
//
#include "events.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
void printEvent(EventNode* event);
EventNode* createEventNode(
int loc, int year, int day, int eventType, char* eventParamsStr
) {
EventNode *event = (EventNode*)malloc(sizeof(EventNode));
event->loc = loc;
event->year = year;
event->day = day;
event->type = eventType;
switch (eventType) {
case HARVEST:
{
double fracRA, fracRB, fracTA, fracTB;
HarvestParams *params = (HarvestParams*)malloc(sizeof(HarvestParams));
sscanf(eventParamsStr, "%lf %lf %lf %lf", &fracRA, &fracRB, &fracTA, &fracTB);
params->fractionRemovedAbove = fracRA;
params->fractionRemovedBelow = fracRB;
params->fractionTransferredAbove = fracTA;
params->fractionTransferredBelow = fracTB;
event->eventParams = params;
}
break;
case IRRIGATION:
{
double amountAdded;
int location;
IrrigationParams *params = (IrrigationParams*)malloc(sizeof(IrrigationParams));
sscanf(eventParamsStr, "%lf %d", &amountAdded, &location);
params->amountAdded = amountAdded;
params->location = location;
event->eventParams = params;
}
break;
case FERTILIZATION:
{
// If/when we try two N pools, enable the additional nh4_no3_frac param (likely via
// compiler switch)
double orgN, orgC, minN;
// double nh4_no3_frac;
FertilizationParams *params = (FertilizationParams*)malloc(sizeof(FertilizationParams));
sscanf(eventParamsStr, "%lf %lf %lf", &orgN, &orgC, &minN);
// scanf(eventParamsStr, "%lf %lf %lf %lf", &org_N, &org_C, &min_N, &nh4_no3_frac);
params->orgN = orgN;
params->orgC = orgC;
params->minN = minN;
// params->nh4_no3_frac = nh4_nos_frac;
event->eventParams = params;
}
break;
case PLANTING:
{
int emergenceLag;
double addedC, addedN;
PlantingParams *params = (PlantingParams*)malloc(sizeof(PlantingParams));
sscanf(eventParamsStr, "%d %lf %lf", &emergenceLag, &addedC, &addedN);
params->emergenceLag = emergenceLag;
params->addedC = addedC;
params->addedN = addedN;
event->eventParams = params;
}
break;
case TILLAGE:
{
double fracLT, somDM, litterDM;
TillageParams *params = (TillageParams*)malloc(sizeof(TillageParams));
sscanf(eventParamsStr, "%lf %lf %lf", &fracLT, &somDM, &litterDM);
params->fractionLitterTransferred = fracLT;
params->somDecompModifier = somDM;
params->litterDecompModifier = litterDM;
event->eventParams = params;
}
break;
default:
// Unknown type, error and exit
printf("Error reading from event file: unknown event type %d\n", eventType);
exit(1);
}
event->nextEvent = NULL;
return event;
}
enum EventType getEventType(char *eventTypeStr) {
if (strcmp(eventTypeStr, "irrig") == 0) {
return IRRIGATION;
} else if (strcmp(eventTypeStr, "fert") == 0) {
return FERTILIZATION;
} else if (strcmp(eventTypeStr, "plant") == 0) {
return PLANTING;
} else if (strcmp(eventTypeStr, "till") == 0) {
return TILLAGE;
} else if (strcmp(eventTypeStr, "harv") == 0) {
return HARVEST;
}
return UNKNOWN_EVENT;
}
EventNode** readEventData(char *eventFile, int numLocs) {
int loc, year, day, eventType;
int currLoc, currYear, currDay;
int EVENT_LINE_SIZE = 1024;
int numBytes;
char *eventParamsStr;
char eventTypeStr[20];
char line[EVENT_LINE_SIZE];
EventNode *curr, *next;
printf("Begin reading event data from file %s\n", eventFile);
EventNode** events = (EventNode**)calloc(sizeof(EventNode*), numLocs * sizeof(EventNode*));
// status of the read
FILE *in = openFile(eventFile, "r");
if (fgets(line, EVENT_LINE_SIZE, in) == NULL) {
printf("Error: no event data in %s\n", eventFile);
exit(1);
}
sscanf(line, "%d %d %d %s %n", &loc, &year, &day, eventTypeStr, &numBytes);
eventParamsStr = line + numBytes;
eventType = getEventType(eventTypeStr);
if (eventType == UNKNOWN_EVENT) {
printf("Error: unknown event type %s\n", eventTypeStr);
exit(1);
}
next = createEventNode(loc, year, day, eventType, eventParamsStr);
events[loc] = next;
currLoc = loc;
currYear = year;
currDay = day;
while (fgets(line, EVENT_LINE_SIZE, in) != NULL) {
// We have another event
curr = next;
sscanf(line, "%d %d %d %s %n", &loc, &year, &day, eventTypeStr, &numBytes);
eventParamsStr = line + numBytes;
eventType = getEventType(eventTypeStr);
if (eventType == UNKNOWN_EVENT) {
printf("Error: unknown event type %s\n", eventTypeStr);
exit(1);
}
// make sure location and time are non-decreasing
if (loc < currLoc) {
printf("Error reading event file: was reading location %d, trying to read location %d\n", currLoc, loc);
printf("Event records for a given location should be contiguous, and locations should be in ascending order\n");
exit(1);
}
if ((loc == currLoc) && ((year < currYear) || (day < currDay))) {
printf("Error reading event file: for location %d, last event was at (%d, %d) ", currLoc, currYear, currDay);
printf("next event is at (%d, %d)\n", year, day);
printf("Event records for a given location should be in time-ascending order\n");
exit(1);
}
next = createEventNode(loc, year, day, eventType, eventParamsStr);
if (currLoc == loc) {
// Same location, add the new event to this location's list
curr->nextEvent = next;
}
else {
// New location, update location and start a new list
currLoc = loc;
events[currLoc] = next;
}
}
fclose(in);
return events;
}
void printEvent(EventNode *event) {
if (event == NULL) {
return;
}
int loc = event->loc;
int year = event->year;
int day = event->day;
switch (event->type) {
case IRRIGATION:
printf("IRRIGATION at loc %d on %d %d, ", loc, year, day);
IrrigationParams *const iParams = (IrrigationParams*)event->eventParams;
printf("with params: amount added %4.2f\n", iParams->amountAdded);
break;
case FERTILIZATION:
printf("FERTILIZATION at loc %d on %d %d, ", loc, year, day);
FertilizationParams *const fParams = (FertilizationParams*)event->eventParams;
printf("with params: org N %4.2f, org C %4.2f, min N %4.2f\n", fParams->orgN, fParams->orgC, fParams->minN);
break;
case PLANTING:
printf("PLANTING at loc %d on %d %d, ", loc, year, day);
PlantingParams *const pParams = (PlantingParams*)event->eventParams;
printf("with params: emergence lag %d, added C %4.2f, added N %4.2f\n", pParams->emergenceLag, pParams->addedC, pParams->addedN);
break;
case TILLAGE:
printf("TILLAGE at loc %d on %d %d, ", loc, year, day);
TillageParams *const tParams = (TillageParams*)event->eventParams;
printf("with params: frac litter transferred %4.2f, som decomp modifier %4.2f, litter decomp modifier %4.2f\n", tParams->fractionLitterTransferred, tParams->somDecompModifier, tParams->litterDecompModifier);
break;
case HARVEST:
printf("HARVEST at loc %d on %d %d, ", loc, year, day);
HarvestParams *const hParams = (HarvestParams*)event->eventParams;
printf("with params: frac removed above %4.2f, frac removed below %4.2f, frac transferred above %4.2f, frac transferred below %4.2f\n", hParams->fractionRemovedAbove, hParams->fractionRemovedBelow, hParams->fractionTransferredAbove, hParams->fractionTransferredBelow);
break;
default:
printf("Error printing event: unknown type %d\n", event->type);
}
}