-
Notifications
You must be signed in to change notification settings - Fork 1
/
NG_FWI.c
737 lines (699 loc) · 24.2 KB
/
NG_FWI.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
/* Phase1 NG-FWI.c
inputs full hourly weather sttream only.
new hourly DMC and DC
calcuats solar radiation from lat/long
grass moisture, grass spread index and grass fire weahter index added.\
THis is only set up for ONE station at a time. And ONE year really.
version 0.9 (still testing)
bmw/2021
*/
#include "util.h"
#include <stdlib.h>
static const double HOURLY_K_DMC = 2.10;
static const double HOURLY_K_DC = 0.017;
static const double DC_OFFSET_TEMP = 0.0;
static const double OFFSET_SUNRISE = 2.5;
static const double OFFSET_SUNSET = 0.5;
/* Fuel Load (kg/m^2) */
static const double DEFAULT_GRASS_FUEL_LOAD = 0.35;
static const double MAX_SOLAR_PROPAGATION = 0.85;
/* default startup values */
static const double FFMC_DEFAULT = 85;
static const double DMC_DEFAULT = 6;
static const double DC_DEFAULT = 15;
/* FIX: figure out what this should be */
static const double DEFAULT_LATITUDE = 55.0;
static const double DEFAULT_LONGITUDE = -120.0;
static const double MPCT_TO_MC = 147.2772277;
static const double FFMC_INTERCEPT = 0.5;
static const double DMC_INTERCEPT = 1.5;
static const double DC_INTERCEPT = 2.8;
/*
* Fine Fuel Moisture Code (FFMC) from moisture %
*/
double fine_fuel_moisture_code(double moisture_percent)
{
return (59.5 * (250 - moisture_percent) / (MPCT_TO_MC + moisture_percent));
}
/*
* Fine Fuel Moisture (percent) from FFMC
*/
double fine_fuel_moisture_from_code(double moisture_code)
{
return MPCT_TO_MC * (101 - moisture_code) / (59.5 + moisture_code);
}
/**
* Calculate hourly Fine Fuel Moisture (percent) value
*
* @param temp Temperature (Celcius)
* @param rh Relative Humidity (percent, 0-100)
* @param ws Wind Speed (km/h)
* @param rain Precipitation (mm)
* @param lastmc Previous Fine Fuel Moisture (percent)
* @return Hourly Fine Fuel Moisture (percent)
*/
double hourly_fine_fuel_moisture(const double temp,
const double rh,
const double ws,
const double rain,
const double lastmc)
{
/* printf("%.1f,%.1f,%.1f,%.1f,%.1f\n", temp, rh, ws, rain, lastmc); */
static const double rf = 42.5;
static const double drf = 0.0579;
/* Time since last observation (hours) */
static const double time = 1.0;
/* use moisture directly instead of converting to/from ffmc */
/* expects any rain intercept to already be applied */
double mo = lastmc;
if (rain != 0.0)
{
/* duplicated in both formulas, so calculate once */
/* lastmc == mo, but use lastmc since mo changes after first equation */
mo += rf * rain * exp(-100.0 / (251 - lastmc)) * (1.0 - exp(-6.93 / rain));
if (lastmc > 150)
{
mo += 0.0015 * pow(lastmc - 150, 2) * sqrt(rain);
}
if (mo > 250)
{
mo = 250;
}
}
/* duplicated in both formulas, so calculate once */
const double e1 = 0.18 * (21.1 - temp) * (1.0 - (1.0 / exp(0.115 * rh)));
const double ed = 0.942 * pow(rh, 0.679) + (11.0 * exp((rh - 100) / 10.0)) + e1;
const double ew = 0.618 * pow(rh, 0.753) + (10.0 * exp((rh - 100) / 10.0)) + e1;
/* m = ed if mo >= ed else ew */
double m = mo < ed
? ew
: ed;
/* printf("%.8f,%.8f,%.8f,%.8f,%.8f,%.8f\n", lastmc, mo, e1, ed, ew, m); */
if (mo != ed)
{
/* these are the same formulas with a different value for a1 */
const double a1 = mo > ed
? rh / 100.0
: (100.0 - rh) / 100.0;
const double k0_or_k1 = 0.424 * (1 - pow(a1, 1.7)) + (0.0694 * sqrt(ws) * (1 - pow(a1, 8)));
const double kd_or_kw = drf * k0_or_k1 * exp(0.0365 * temp);
m += (mo - m) * pow(10, -kd_or_kw * time);
/* printf("%.8f,%.8f,%.8f,%.8f\n", a1, k0_or_k1, kd_or_kw, m); */
}
return m;
}
/**
* Calculate Initial Spread Index (ISI)
*
* @param ws Wind Speed (km/h)
* @param ffmc Fine Fuel Moisure Code
* @return Initial Spread Index
*/
double initial_spread_index(double ws, double ffmc)
{
const double fm = fine_fuel_moisture_from_code(ffmc);
const double fw = 40 <= ws
? 12 * (1 - exp(-0.0818 * (ws - 28)))
: exp(0.05039 * ws);
const double ff = 91.9 * exp(-0.1386 * fm) * (1.0 + pow(fm, 5.31) / 4.93e07);
const double isi = 0.208 * fw * ff;
return isi;
}
/**
* Calculate Build-up Index (BUI)
*
* @param dmc Duff Moisture Code
* @param dc Drought Code
* @return Build-up Index
*/
double buildup_index(double dmc, double dc)
{
double bui = 0 == dmc && 0 == dc
? 0.0
: 0.8 * dc * dmc / (dmc + 0.4 * dc);
if (bui < dmc)
{
const double p = (dmc - bui) / dmc;
const double cc = 0.92 + pow(0.0114 * dmc, 1.7);
bui = dmc - cc * p;
if (bui <= 0)
{
bui = 0;
}
}
return bui;
}
/**
* Calculate Fire Weather Index (FWI)
*
* @param isi Initial Spread Index
* @param bui Build-up Index
* @return Fire Weather Index
*/
double fire_weather_index(double isi, double bui)
{
const double bb = 0.1 * isi
* (bui > 80
? 1000.0 / (25.0 + 108.64 / exp(0.023 * bui))
: 0.626 * pow(bui, 0.809) + 2.0);
const double fwi = bb <= 1
? bb
: exp(2.72 * pow(0.434 * log(bb), 0.647));
return fwi;
}
double daily_severity_rating(double fwi)
{
return 0.0272 * pow(fwi, 1.77);
}
/**
* Calculate Hourly Grass Fuel Moisture. Needs to be converted to get GFMC.
*
* @param temp Temperature (Celcius)
* @param rh Relative Humidity (percent, 0-100)
* @param ws Wind Speed (km/h)
* @param rain Precipitation (mm)
* @param lastmc Previous grass fuel moisture (percent)
* @param solrad Solar radiation (kW/m^2)
* @return Grass Fuel Moisture (percent)
*/
double hourly_grass_fuel_moisture(double temp,
double rh,
double ws,
double rain,
double solrad,
double lastmc)
{
/* MARK II of the model (2016) wth new solar rad model specific to grass
Temp is temperature in C
RH is relative humidty in %
ws is average wind speed in km/h
rain is rainfall in mm
solrad is kW/m2 (radiaiton reaching fuel)
mo is the old grass fuel moisture (not as a code value...so elimates the conversion to code)
time - time between obs in HOURS
DRF of 1/16.1 comes from reducting the standard response time curve
at 26.7C, 20%RH, 2 km/h to 0.85hr.
bmw
*/
static const double rf = 0.27;
static const double drf = 0.389633;
/* Time since last observation (hours) */
static const double time = 1.0;
double mo = lastmc;
if (rain != 0)
{
/* mo+=rain*rf*exp(-100.0/(251.0-mo))*(1.0-exp(-6.93/rain));*/ /* old routine*/
/* this new routine assumes layer is 0.3 kg/m2 so 0.3mm of rain adds +100%MC*/
/* *100 to convert to %... *1/.3 because of 0.3mm=100% */
mo += rain / 0.3 * 100.0;
if (mo > 250.0)
{
mo = 250.0;
}
}
/* fuel temp from CEVW*/
const double tf = temp + 17.9 * solrad * exp(-0.034 * ws);
/* fuel humidity */
const double rhf = tf > temp
? (rh * 6.107 * pow(10.0, 7.5 * temp / (temp + 237.0))
/ (6.107 * pow(10.0, 7.5 * tf / (tf + 237.0))))
: rh;
/* printf("%.1f,%.1f,%.1f,%.1f,%.8f,%.8f\n", temp, rh, ws, rain, tf, rhf); */
/* duplicated in both formulas, so calculate once */
const double e1 = rf * (26.7 - tf) * (1.0 - (1.0 / exp(0.115 * rhf)));
/*GRASS EMC*/
const double ed = 1.62 * pow(rhf, 0.532) + (13.7 * exp((rhf - 100) / 13.0)) + e1;
const double ew = 1.42 * pow(rhf, 0.512) + (12.0 * exp((rhf - 100) / 18.0)) + e1;
/* m = ed if mo >= ed else ew */
double m = (mo < ed && mo < ew)
? ew
: ed;
/* printf("%.8f,%.8f,%.8f,%.8f,%.8f,%.8f\n", lastmc, mo, e1, ed, ew, m); */
if (mo > ed || (mo < ed && mo < ew))
{
/* these are the same formulas with a different value for a1 */
const double a1 = mo > ed
? rhf / 100.0
: (100.0 - rhf) / 100.0;
const double k0_or_k1 = 0.424 * (1 - pow(a1, 1.7)) + (0.0694 * sqrt(ws) * (1 - pow(a1, 8)));
const double kd_or_kw = drf * k0_or_k1 * exp(0.0365 * tf);
m += (mo - m) * pow(10, -kd_or_kw * time);
/* printf("%.8f,%.8f,%.8f,%.8f\n", a1, k0_or_k1, kd_or_kw, m); */
}
return m;
}
/**
* Calculate Grass Spread Index (GSI)
*
* @param ws Wind Speed (km/h)
* @param mc Grass moisture content (percent)
* @param cur Degree of curing (percent, 0-100)
* @return Grass Spread Index
*/
double grass_spread_index(double ws, double mc, double cur)
{
const double fw = 16.67 * (ws < 5 ? 0.054 + 0.209 * ws : 1.1 + 0.715 * (ws - 5.0) * 0.844);
/* NOTE: between [12, ~12.01754] the value for ws < 10 is greater than ws >= 10 */
/* using 0.6838 instead would mean this is always less than ws >= 10 */
/* mc < 23.9 because of check at start of function, so last expression is any ws >= 10 */
const double fm = mc < 12
? exp(-0.108 * mc)
: (mc < 20.0 && ws < 10.0
? 0.684 - 0.0342 * mc
: (mc < 23.9 && ws >= 10.0
? 0.547 - 0.0228 * mc
: 0.0));
const double cf = cur > 20
? 1.034 / (1 + 104 * exp(-0.1 * (cur - 20)))
: 0.0;
return 1.11 * fw * fm * cf;
}
/**
* Calculate Grass Fire Weather Index
*
* @param gsi Grass Spread Index
* @param load Fuel Load (kg/m^2)
* @return Grass Fire Weather Index
*/
double grass_fire_weather_index(double gsi, double load)
{
/* this just converts back to ROS in m/min*/
const double ros = gsi / 1.11;
const double Fint = 300.0 * load * ros;
return Fint > 100
? log(Fint / 60.0) / 0.14
: Fint / 25.0;
}
double dmc_wetting(double rain_total, double lastdmc)
{
/* compare floats by using tolerance */
if (rain_total <= DMC_INTERCEPT)
{
return 0.0;
}
const double b = lastdmc <= 33
? 100.0 / (0.5 + 0.3 * lastdmc)
: (lastdmc <= 65
? 14.0 - 1.3 * log(lastdmc)
: 6.2 * log(lastdmc) - 17.2);
const double rw = 0.92 * rain_total - 1.27;
const double wmi = 20 + 280 / exp(0.023 * lastdmc);
const double wmr = wmi + 1000 * rw / (48.77 + b * rw);
double dmc = 43.43 * (5.6348 - log(wmr - 20));
if (dmc <= 0.0)
{
dmc = 0.0;
}
/* total amount of wetting since lastdmc */
const double w = lastdmc - dmc;
return w;
}
double dc_wetting(double rain_total, double lastdc)
{
/* compare floats by using tolerance */
if (rain_total <= DC_INTERCEPT)
{
return 0.0;
}
const double rw = 0.83 * rain_total - 1.27;
const double smi = 800 * exp(-lastdc / 400);
/* TOTAL change for the TOTAL 24 hour rain from FWI1970 model */
return 400.0 * log(1.0 + 3.937 * rw / smi);
}
double dmc_wetting_between(double rain_total_previous, double rain_total, double lastdmc)
{
if (rain_total_previous >= rain_total)
{
return 0.0;
}
/* wetting is calculated based on initial dmc when rain started and rain since */
const double current = dmc_wetting(rain_total, lastdmc);
/* recalculate instead of storing so we don't need to reset this too */
/* NOTE: rain_total_previous != (rain_total - cur.rain) due to floating point math */
const double previous = dmc_wetting(rain_total_previous, lastdmc);
return current - previous;
}
double dc_wetting_between(double rain_total_previous, double rain_total, double lastdc)
{
if (rain_total_previous >= rain_total)
{
return 0.0;
}
/* wetting is calculated based on initial dc when rain started and rain since */
const double current = dc_wetting(rain_total, lastdc);
/* recalculate instead of storing so we don't need to reset this too */
/* NOTE: rain_total_previous != (rain_total - cur.rain) due to floating point math */
const double previous = dc_wetting(rain_total_previous, lastdc);
return current - previous;
}
double dmc_drying_ratio(double temp, double rh)
{
return _max(0.0, (temp + 1.1) * (100.0 - rh) * 0.0001);
}
double duff_moisture_code(double last_dmc,
double temp,
double rh,
double ws,
double rain,
int mon,
int hour,
double solrad,
double sunrise,
double sunset,
double* dmc_before_rain,
double rain_total_prev,
double rain_total)
{
/* printf("%.1f,%.1f,%.1f,%.1f,%d,%d\n", temp, rh, ws, rain, mon, hour); */
if (0 == rain_total)
{
*dmc_before_rain = last_dmc;
}
/* apply wetting since last period */
double dmc_wetting_hourly = dmc_wetting_between(
rain_total_prev,
rain_total,
*dmc_before_rain);
/* at most apply same wetting as current value (don't go below 0) */
double dmc = _max(0.0, last_dmc - dmc_wetting_hourly);
double sunrise_start = _round(sunrise + OFFSET_SUNRISE, 0);
double sunset_start = _round(sunset + OFFSET_SUNSET, 0);
double dmc_hourly = (((hour >= sunrise_start) && (hour < sunset_start))
? (HOURLY_K_DMC * dmc_drying_ratio(temp, rh))
: 0.0);
dmc = dmc + dmc_hourly;
/* printf("%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f\n",
*dmc_before_rain,
last_dmc,
dmc,
dmc_daily,
df,
dmc_hourly,
dmc_wetting_hourly); */
return dmc;
}
double dc_drying_hourly(double temp)
{
return _max(0.0, HOURLY_K_DC * (temp + DC_OFFSET_TEMP));
}
double drought_code(double last_dc,
double temp,
double rh,
double ws,
double rain,
int mon,
int hour,
double solrad,
double sunrise,
double sunset,
double* dc_before_rain,
double rain_total_prev,
double rain_total)
{
if (0 == rain_total)
{
*dc_before_rain = last_dc;
}
/* apply wetting since last period */
double dc_wetting_hourly = dc_wetting_between(rain_total_prev, rain_total, *dc_before_rain);
/* at most apply same wetting as current value (don't go below 0) */
double dc = _max(0.0, last_dc - dc_wetting_hourly);
double dc_hourly = dc_drying_hourly(temp);
/* double drying = HOURLY_K_DC * (temp + DC_OFFSET_TEMP);
printf("temp=%0.2f, HOURLY_K_DC=%0.3f, DC_OFFSET_TEMP=%0.2f, drying=%0.2f, _max=%0.2f, last_dc=%0.2f, dc_wetting_hourly=%0.2f, dc=%0.2f, dc_hourly=%0.2f\n",
temp,
HOURLY_K_DC,
DC_OFFSET_TEMP,
drying,
_max(0.0, drying),
last_dc,
dc_wetting_hourly,
dc,
dc_hourly); */
dc = dc + dc_hourly;
return dc;
}
/*
* Calculate number of drying "units" this hour contributes
*/
double drying_units(double temp, double rh, double wind, double rain, double solrad)
{
/* for now, just add 1 drying "unit" per hour */
return 1.0;
}
struct rain_intercept
{
double rain_total;
double rain_total_prev;
double drying_since_intercept;
};
/* HACK: use struct so it's closer to how R can return multiple values */
void rain_since_intercept_reset(double temp,
double rh,
double ws,
double rain,
int mon,
int hour,
double solrad,
double sunrise,
double sunset,
struct rain_intercept* canopy)
{
/* for now, want 5 "units" of drying (which is 1 per hour to start) */
static const double TARGET_DRYING_SINCE_INTERCEPT = 5;
if (0 < rain)
{
/* no drying if still raining */
canopy->drying_since_intercept = 0.0;
}
else
{
canopy->drying_since_intercept += drying_units(temp, rh, ws, rain, solrad);
if (canopy->drying_since_intercept >= TARGET_DRYING_SINCE_INTERCEPT)
{
/* reset rain if intercept reset criteria met */
canopy->rain_total = 0.0;
canopy->drying_since_intercept = 0.0;
}
}
canopy->rain_total_prev = canopy->rain_total;
canopy->rain_total += rain;
}
int populate_row(FILE* inp, struct row* cur, double TZadjust)
{
int err = read_row(inp, cur);
cur->solrad = sun(cur->lat, cur->lon, cur->mon, cur->day, cur->hour, TZadjust, &(cur->sunrise), &(cur->sunset));
double julian_day = julian(cur->mon, cur->day);
/* assuming we want curing to change based on current day and not remain */
/* the same across entire period based on start date */
cur->percent_cured = seasonal_curing(julian_day);
/* FIX: use a constant grass fuel load for now */
cur->grass_fuel_load = DEFAULT_GRASS_FUEL_LOAD;
return err;
}
void main(int argc, char* argv[])
{
/* CSV headers */
static const char* header = "lat,long,yr,mon,day,hr,temp,rh,ws,prec";
static const char* header_out = "lat,long,yr,mon,day,hr,temp,rh,ws,prec,solrad,ffmc,dmc,dc,isi,bui,fwi,dsr,gfmc,gsi,gfwi,mcffmc,mcgfmc,percent_cured,grass_fuel_load";
if (7 != argc)
{
printf("Command line: %s <local GMToffset> <starting FFMC> <starting DMC> <starting DC> <input file> <output file>\n\n", argv[0]);
printf("<local GMToffset> is the off of Greenich mean time (for Eastern = -5 Central=-6 MT=-7 PT=-8 ) \n");
printf("All times should be local standard time\n");
printf("INPUT FILE format must be HOURLY weather data, comma seperated and take the form\n");
printf("%s\n\n", header);
exit(1);
}
FILE* inp = fopen(argv[5], "r");
printf("Opening input file >>> %s \n", argv[5]);
if (inp == NULL)
{
printf("\n\n ***** FILE %s does not exist\n", argv[5]);
exit(1);
}
int TZadjust = atoi(argv[1]);
if (TZadjust < -9 || TZadjust > -2)
{
printf("/n ***** Local time zone adjustment must be vaguely in Canada so between -9 and -2 \n");
exit(1);
}
double ffmc_old = atof(argv[2]);
if (ffmc_old > 101 || ffmc_old < 0)
{
printf(" /n/n ***** FFMC must be between 0 and 101 \n");
exit(1);
}
const double dmc_old = atof(argv[3]);
if (dmc_old < 0)
{
printf(" /n/n ***** starting DMC must be >=0 \n");
exit(1);
}
double dc_old = atof(argv[4]);
if (dc_old < 0)
{
printf(" /n/n ***** starting DC must be >=0\n");
exit(1);
}
/* printf("TZ=%d start ffmc=%f dmc=%f\n", TZadjust, ffmc_old, dmc_old); */
double mcffmc = fine_fuel_moisture_from_code(ffmc_old);
/* assuming this is fine because swiss sfms uses it now */
double mcgfmc = mcffmc;
/* check that the header matches what is expected */
check_header(inp, header);
struct row cur;
int err = populate_row(inp, &cur, TZadjust);
struct row old = cur;
double dmc = dmc_old;
double dmc_before_rain = dmc_old;
double dc = dc_old;
double dc_before_rain = dc_old;
struct rain_intercept canopy = {0.0, 0.0, 0.0};
FILE* out = fopen(argv[6], "w");
fprintf(out, "%s\n", header_out);
while (err > 0)
{
/*
if (cur.day != old.day || cur.mon != old.mon)
{
printf("here : %f %f %d %d %d SUNrise=%5.2f sunset=%5.2f\n",
cur.lat,
cur.lon,
cur.year,
cur.mon,
cur.day,
cur.sunrise,
cur.sunset);
}
*/
rain_since_intercept_reset(
cur.temp,
cur.rh,
cur.ws,
cur.rain,
cur.mon,
cur.hour,
cur.solrad,
cur.sunrise,
cur.sunset,
&canopy);
/* use lesser of remaining intercept and current hour's rain */
double rain_ffmc = canopy.rain_total <= FFMC_INTERCEPT
? 0.0
: ((canopy.rain_total - FFMC_INTERCEPT) > cur.rain
? cur.rain
: canopy.rain_total - FFMC_INTERCEPT);
mcffmc = hourly_fine_fuel_moisture(cur.temp, cur.rh, cur.ws, rain_ffmc, mcffmc);
/* convert to code for output, but keep using moisture % for precision */
double ffmc = fine_fuel_moisture_code(mcffmc);
/* not ideal, but at least encapsulates the code for each index */
dmc = duff_moisture_code(
dmc,
cur.temp,
cur.rh,
cur.ws,
cur.rain,
cur.mon,
cur.hour,
cur.solrad,
cur.sunrise,
cur.sunset,
&dmc_before_rain,
canopy.rain_total_prev,
canopy.rain_total);
dc = drought_code(
dc,
cur.temp,
cur.rh,
cur.ws,
cur.rain,
cur.mon,
cur.hour,
cur.solrad,
cur.sunrise,
cur.sunset,
&dc_before_rain,
canopy.rain_total_prev,
canopy.rain_total);
double isi = initial_spread_index(cur.ws, ffmc);
double bui = buildup_index(dmc, dc);
double fwi = fire_weather_index(isi, bui);
double dsr = daily_severity_rating(fwi);
mcgfmc = hourly_grass_fuel_moisture(cur.temp, cur.rh, cur.ws, cur.rain, cur.solrad, mcgfmc);
double gfmc = fine_fuel_moisture_code(mcgfmc);
double gsi = grass_spread_index(cur.ws, mcgfmc, cur.percent_cured);
double gfwi = grass_fire_weather_index(gsi, cur.grass_fuel_load);
/* printf("\n"); */
fprintf(out,
"%.4f,%.4f,%4d,%02d,%02d,%02d,%.1f,%.0f,%.1f,%.2f,%.4f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.4f,%.4f,%.1f,%.2f\n",
cur.lat,
cur.lon,
cur.year,
cur.mon,
cur.day,
cur.hour,
_round(cur.temp, 1),
cur.rh,
_round(cur.ws, 1),
_round(cur.rain, 2),
cur.solrad,
ffmc,
dmc,
dc,
isi,
bui,
fwi,
dsr,
gfmc,
gsi,
gfwi,
mcffmc,
mcgfmc,
cur.percent_cured,
cur.grass_fuel_load);
/* printf("%.4f,%.4f,%4d,%02d,%02d,%02d,%.1f,%.0f,%.1f,%.2f,%.4f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.1f,%.4f,%.4f,%.1f,%.2f\n",
cur.lat,
cur.lon,
cur.year,
cur.mon,
cur.day,
cur.hour,
cur.temp,
cur.rh,
cur.ws,
cur.rain,
cur.solrad,
ffmc,
dmc,
dc,
isi,
bui,
fwi,
dsr,
gfmc,
gsi,
gfwi,
mcffmc,
mcgfmc,
cur.percent_cured,
cur.grass_fuel_load); */
old = cur;
err = populate_row(inp, &cur, TZadjust);
if (err > 0 && (old.lon != cur.lon || old.lat != cur.lat))
{
printf("Latitude and Longitude must be constant\n");
exit(1);
}
if (err > 0 && (1 != (cur.hour - old.hour) && !(23 == old.hour && 0 == cur.hour)))
{
printf("Hours must be sequential but went from %d to %d\n",
old.hour,
cur.hour);
exit(1);
}
} /* end the main while(err>0) */
/* printf("output has been written to>>> %s\n",argv[6]); */
fclose(inp);
fclose(out);
}