forked from kettner/hydrotrend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hydrotrend_irf.c
1377 lines (1257 loc) · 51.1 KB
/
hydrotrend_irf.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
#include "hydrotrend_irf.h"
#include "hydroalloc_mem.h"
#include "hydroparams.h"
#include "hydrotimeser.h"
#include "hydrotrend.h"
#include "hydroclimate.h"
#include "hydroinout.h"
#include "hydrofree_mem.h"
#include <stdlib.h>
#include <string.h>
extern int hydrorandomsediment(void);
HydrotrendData*
new_data()
{
HydrotrendData *s;
s = malloc(sizeof(HydrotrendData));
s->day = 0;
s->n_days = 0;
s->q = NULL;
s->velocity = NULL;
s->width = NULL;
s->depth = NULL;
s->qs = NULL;
s->cs = NULL;
s->qb = NULL;
s->prec = NULL;
s->temp = NULL;
return s;
}
void
initialize_data(HydrotrendData* self, int n_days)
{
self->day = 0;
self->n_days = n_days;
self->q = malloc1d (self->n_days, double);
self->velocity = malloc1d (self->n_days, double);
self->width = malloc1d (self->n_days, double);
self->depth = malloc1d (self->n_days, double);
self->qs = malloc1d (self->n_days, double);
self->cs = malloc1d (self->n_days, double);
self->qb = malloc1d (self->n_days, double);
self->prec = malloc1d (self->n_days, double);
self->temp = malloc1d (self->n_days, double);
}
static HydrotrendData *
_free_data (HydrotrendData * s)
{
free (s->q);
free (s->velocity);
free (s->width);
free (s->depth);
free (s->qs);
free (s->cs);
free (s->qb);
free (s->prec);
free (s->temp);
free (s);
return NULL;
}
static int
_ht_save_data (HydrotrendData * s)
{
int err = 0;
long day;
// long start = (ep * nyears[ep] * daysiy) + (yr - syear[ep]) * daysiy;
long start = (yr - syear[0]) * daysiy;
if (start + daysiy > s->n_days)
{
fprintf( stderr, "ERROR: ht_save_data: %ld > %ld\n", start+daysiy, s->n_days );
fprintf (stderr, "%d\n", ep);
fprintf (stderr, "%d\n", nyears[ep]);
fprintf (stderr, "%d\n", yr);
fprintf (stderr, "%d\n", daysiy);
fprintf (stderr, "%d\n", syear[ep]);
fflush (stderr);
exit (-1);
}
for (day = 0; day < daysiy; day++)
{
s->q[start + day] = Qsumtot[day];
s->velocity[start + day] = (velcof[ep] * pow (Qsumtot[day], velpow[ep]));
s->width[start + day] = (widcof[ep] * pow (Qsumtot[day], widpow[ep]));
s->depth[start + day] = (depcof[ep] * pow (Qsumtot[day], deppow[ep]));
s->qs[start + day] = Qs[day];
s->cs[start + day] = Qs[day] / (s->velocity[start + day] * s->width[start + day] * s->depth[start + day]);
s->qb[start + day] = Qb[day];
s->prec[start + day] = Pdaily[day];
s->temp[start + day] = Tdaily[day];
}
return (err);
}
void
hydro_initialize(HydrotrendData *self, char* in_dir, char* in_file_prefix, char* out_dir)
{
char* in_file;
/*---------------------
* Start the program
*---------------------*/
{
/*-------------------
* Local Variables
*-------------------*/
int err, ii, lyear, maxnran, p, k, x;
long totaldays;
double logarea, baseflowpercentage;
gw_rainfall_etc *gw_rain;
/*------------------------
* Initialize Variables
*------------------------*/
err = 0;
verbose = 0;
maxnran = 0;
maxerr = 0.0;
totalmass = 0.0;
globalparflag = 0;
Qgrandtotaltot = 0.0;
Qsgrandtotaltot = 0.0;
TEtot = 0.0;
gw_rain = (gw_rainfall_etc *) malloc (sizeof (gw_rainfall_etc));
/*-----------------------------------------------------------------
* Check the command line input; get file name or directory name
*-----------------------------------------------------------------*/
/*
err = hydrocommandline (&argc, argv);
if (err)
{
fprintf (stderr, " ERROR in HydroCommandLine: HydroTrend Aborted \n\n");
exit (1);
}
*/
/*--------------------------------
* Read the main input file.
* This reads all of the epochs,
* at the very begining
*--------------------------------*/
if (verbose)
//printf ("Reading input data from: %s.IN... \n", commandlinearg[1]);
printf ("Reading input data from: %s.IN... \n", in_file_prefix );
{ /* build the input file name */
in_file = malloc (strlen (in_dir) + strlen ("/") +
strlen (in_file_prefix) + strlen (".IN"));
in_file[0] = '\0';
strcat (in_file, in_dir);
strcat (in_file, "/");
strcat (in_file, in_file_prefix);
strcat (in_file, ".IN");
}
err = hydroreadinput (in_file);
if (err)
{
fprintf (stderr, " ERROR in HydroReadInput: HydroTrend Aborted \n\n");
exit (1);
}
if (out_dir!=NULL)
{ /* Override output directory if it was specified on the command line. */
if (strlen (out_dir)>98)
fprintf (stderr, "Error: output directory is too long: %s", out_dir);
strncpy (directory, out_dir, 98);
strcat (directory, "/");
}
/*--------------------
* Allowcate memory
*--------------------*/
totpercentageQ = malloc1d (ep, double);
/*---------------------------------------
* Set the output path name + filename
*---------------------------------------*/
strcpy (startname, directory);
strcat (startname, in_file_prefix );
/*---------------------
* Open the log file
*---------------------*/
if (verbose)
printf ("Opening the log file (to append)... \n");
strcpy (ffnamelog, startname);
strcat (ffnamelog, fnamelog);
if ((fidlog = fopen (ffnamelog, "w+")) == NULL)
{
printf (" HydroTrend WARNING: Unable to open the log file %s \n",
ffnamelog);
printf (" non-fatal error, continuing. \n\n");
}
/*---------------------------------
* Read the hypsometric curve
* and set maxalt and totalarea.
*---------------------------------*/
if (verbose)
printf ("Reading hypsometric data from: %s/%s.HYPS... \n",
in_dir, in_file_prefix);
err = hydroreadhypsom (in_dir, in_file_prefix);
if (err)
{
fprintf (stderr, " ERROR in HydroReadHypsom: HydroTrend Aborted \n\n");
fprintf (fidlog, " ERROR in HydroReadHypsom: HydroTrend Aborted \n\n");
exit (1);
}
/*---------------------------------------------
* Read climate file if its their.
*---------------------------------------------*/
if (verbose)
printf ("Reading live climate data files... \n");
err = hydroreadclimate (gw_rain, in_dir, in_file_prefix);
if (err)
{
fprintf (stderr, " ERROR in HydroReadClimate: HydroTrend Aborted \n\n");
fprintf (fidlog, " ERROR in HydroReadClimate: HydroTrend Aborted \n\n");
exit (1);
}
/*---------------------------------------------
* Read earthquake file if its their.
*---------------------------------------------*/
if (verbose)
printf ("Reading live earthquake data files... \n");
err = hydroreadearthquake (in_dir, in_file_prefix);
if (err)
{
fprintf (stderr, " ERROR in HydroReadEarthquake: HydroTrend Aborted \n\n");
fprintf (fidlog, " ERROR in HydroReadEarthquake: HydroTrend Aborted \n\n");
exit (1);
}
/*--------------------------------
* Set the hardwired parameters
*--------------------------------*/
if (verbose)
printf ("Setting hardwired parameters... \n");
hydrosetparams ();
if (err)
{
fprintf (stderr, " ERROR in HydroSetParams: HydroTrend Aborted \n\n");
exit (1);
}
/*------------------------------------------
* Set global values for those parameters
* which doesn't have any input
*------------------------------------------*/
if (globalparflag > 0)
{
if (verbose)
printf ("Set global values for not filed out input parameters... \n");
err = hydrosetglobalpar ();
if (err)
{
fprintf (stderr,
" ERROR in HydroSetGlobalPar: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroSetGlobalPar: HydroTrend Aborted \n\n");
exit (1);
}
}
/*---------------------------------------------
* Check all of the input values.
* This also checks to make sure the climate
* variables match at the epoch breaks.
*---------------------------------------------*/
if (verbose)
printf ("Checking all input parameters... \n");
err = hydrocheckinput ();
if (err)
{
fprintf (stderr, " ERROR in HydroCheckInput: HydroTrend Aborted \n\n");
fprintf (fidlog, " ERROR in HydroCheckInput: HydroTrend Aborted \n\n");
exit (1);
}
/*-----------------------
* Open the data files
*-----------------------*/
if (verbose)
printf ("Opening output data files... \n");
err = hydroopenfiles ();
if (err)
{
fprintf (stderr, " ERROR in HydroOpenFiles: HydroTrend Aborted \n\n");
fprintf (fidlog, " ERROR in HydroOpenFiles: HydroTrend Aborted \n\n");
exit (1);
}
/* allocate memory for new Hydrotrend data */
{
long total_days = 0;
for (ep = 0; ep < nepochs; ep++)
total_days += nyears[ep] * daysiy;
initialize_data(self, total_days);
}
/*--------------------------
* Run each epoch of data
*--------------------------*/
if (verbose)
printf (" \nStarting epoch loop... \n");
for (ep = 0; ep < nepochs; ep++)
{
total_yr += nyears[ep];
ranarray = malloc1d (2 * maxran, double);
ranarraysediment = malloc1d (2 * nyears[ep], double);
/*-----------------------------------------------------------------
* Read Qs constant parameters set by geolocation of river mouth
*-----------------------------------------------------------------*/
if (verbose)
printf ("Calling HydroSetGeoParams... \n");
err = hydrosetgeoparams (gw_rain);
if (err)
{
fprintf (stderr,
" ERROR in HydroSetGeoParams: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroSetGeoParams: HydroTrend Aborted \n\n");
exit (1);
}
/*----------------------------------------------------------------------------
* Run each epoch 5 times. This to set a couple of parameters or averages of
* parameters with exactly the same random numbers.
* If setstartmeanQandQs ==:
* 0) calculate the long term mean discharge (Qbartotal[ep]); daily discharge
* is calculated without any baseflow.
* 1) calculate daily discharge (taking baseflow into account) + calculate
* discharge multiple outlets.
* 2) calculate the long term mean suspended sediment load (Qsbartot[ep]).
* 3) calculate the constant of proportionality so that the mean of the sum
* of daily suspended sediment is equal to the long term mean suspended
* sediment (Qsbarnew[ep]). Glacier created sediment is added to the long
* term sediment during this step.
* 4) calculate the daily suspended sediment (Qs[i]); write to output files.
*
*----------------------------------------------------------------------------*/
for (setstartmeanQandQs = 0; setstartmeanQandQs < 5;
setstartmeanQandQs++)
{
yr = syear[ep];
if (setstartmeanQandQs == 0)
{
fprintf (stderr, " Calculate mean discharge, epoch: %d\n",
(ep + 1));
fprintf (fidlog, " Calculate mean discharge, epoch: %d\n",
(ep + 1));
}
if (setstartmeanQandQs == 1)
{
fprintf (stderr, " Calculate daily discharge, epoch: %d\n",
(ep + 1));
fprintf (fidlog, " Calculate daily discharge, epoch: %d\n",
(ep + 1));
}
if (setstartmeanQandQs == 2)
{
fprintf (stderr,
" Calculate mean suspended sediment load, epoch: %d\n",
(ep + 1));
fprintf (fidlog,
" Calculate mean suspended sediment load, epoch: %d\n",
(ep + 1));
}
if (setstartmeanQandQs == 4)
{
fprintf (stderr,
" Calculate daily suspended sediment load, epoch: %d\n",
(ep + 1));
fprintf (fidlog,
" Calculate daily suspended sediment load, epoch: %d\n",
(ep + 1));
}
/*---------------------------------------------------
* Free memory for possible multiple outlet module
*---------------------------------------------------*/
if ((ep > 0 && setstartmeanQandQs == 0))
hydrofreememoutlet (nyears[ep - 1]);
if ((ep > 0 && setstartmeanQandQs == 0))
hydrofreememoutlet1 (ep);
/*-------------------------------------------------------
* Allocate memory for possible multiple outlet module
*-------------------------------------------------------*/
if (setstartmeanQandQs == 0)
hydroallocmemoutlet (ep, nepochs);
if (setstartmeanQandQs == 1)
hydroallocmemoutlet1 (ep, nepochs);
/*----------------------------------
* Initialize Variables per epoch
*----------------------------------*/
if ((nooutletpctflag == 0) && (setstartmeanQandQs > 0))
for (p = 0; p < maxnoutlet; p++)
{
Qdummy[ep][p] = 0.0;
for (k = 0; k < eventsnr[ep]; k++)
{
Qbar[ep][p][k] = 1.0; /* Just a start value */
outletpct[p][ep][k] = outletpctdummy[p][ep];
Qbar[ep][p][k] = outletpct[p][ep][k] * Qbartotal[ep];
}
}
if (setstartmeanQandQs == 0)
{
for (p = 0; p < nyears[ep]; p++)
for (k = 0; k < daysiy; k++)
Qpeakfloodtemp[p][k] = 0.0;
Qicetotal[ep] = 0.0;
Qsglaciertotal[ep] = 0.0;
GlacierMstorage[ep] = 0.0;
fractionglaciersediment[ep] = 0.0;
GlacierMinput[ep] = 0.0;
}
/*-----------------------------------------------------------------
* Start new random number sequence.
* Get 'maxran' worth of random numbers and pluck them as needed
* nran counts through the numbers stored in ranarray
*-----------------------------------------------------------------*/
if (verbose)
printf ("Calling HydroRandom... \n");
err = hydrorandom ();
if (err)
{
fprintf (stderr,
" ERROR in HydroRandom: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroRandom: HydroTrend Aborted \n\n");
exit (1);
}
nran = 0;
/*-----------------------------------------------------------------
* Start new random number sequence for sediment.
* Get 'maxran' worth of random numbers and pluck them as needed
* nransediment counts through the numbers stored in ranarraysediment
*-----------------------------------------------------------------*/
if (verbose)
printf ("Calling HydroRandomsed... \n");
err = hydrorandomsediment ();
if (err)
{
fprintf (stderr,
" ERROR in HydroRandomsed: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroRandomsed: HydroTrend Aborted \n\n");
exit (1);
}
nransediment = 0;
/*-------------------------------------------------
* Initialize Variables per loop through program
*-------------------------------------------------*/
Qpeakall[ep] = 0.0;
eventcounter = 0;
for (p = 0; p < maxnoutlet; p++)
Qpeakperoutletall[ep][p] = 0.0;
/*-------------------------------------------
* Set the number of outlets for 10 events
* if number of outlets is not specified,
* or given in a range.
*-------------------------------------------*/
if (noutletflag == 1 && setstartmeanQandQs == 1)
{
for (x = 0; x < eventsnr[ep]; x++)
{
nroutlets[x] = hydrosetnumberoutlet (x);
noutlet = nroutlets[x];
if (verbose)
printf ("Calling hydrooutletfraction... \n");
err = hydrooutletfraction (x);
if (err)
{
fprintf (stderr,
" ERROR in HydroOutletFraction (HydroOutlet): HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroOutletFraction (HydroOutlet): HydroTrend Aborted \n\n");
exit (1);
}
}
noutlet = nroutlets[eventcounter];
}
if (noutletflag == 0 && outletmodelflag == 1
&& steadyoutletpctflag == 1 && setstartmeanQandQs == 1)
for (x = 0; x < eventsnr[ep]; x++)
{
nroutlets[x] = noutlet;
if (verbose)
printf ("Calling hydrooutletfraction... \n");
err = hydrooutletfraction (x);
if (err)
{
fprintf (stderr,
" ERROR in HydroOutletFraction (HydroOutlet): HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroOutletFraction (HydroOutlet): HydroTrend Aborted \n\n");
exit (1);
}
}
/*-----------------------------------------------------------------------
* Set the discharge fraction per outlet if there are multiple outlets
* and if the discharge fraction is not set yet in the outlet file
*-----------------------------------------------------------------------*/
if (outletmodelflag == 1 && nooutletpctflag == 1
&& steadyoutletpctflag == 0 && setstartmeanQandQs == 1)
{
x = 0;
err = hydrooutletfraction (x);
if (verbose)
printf ("Calling hydrooutletfraction... \n");
if (err)
{
fprintf (stderr,
" ERROR in HydroOutletFraction (HydroOutlet): HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroOutletFraction (HydroOutlet): HydroTrend Aborted \n\n");
exit (1);
}
}
/*---------------------------------------------------------
* Calculate the maximum predicted flood size.
* Since the basin area does not change within an epoch,
* only need to do this once for each epoch.
*
* Maximum Flood Size for a Basin:
* Mulder T. and Syvitski J.P.M., 1995.
* Turbidity currents generated at river mouths
* during exceptional discharge to the world oceans.
* Journal of Geology, 103: 285-298.
*
* The equation wants area in km^2
*
*---------------------------------------------------------*/
if (verbose)
printf (" Epoch = %d \n", ep + 1);
logarea = log10 (totalarea[ep] / 1e6);
maxflood =
pow (10.0, (2.084 + 0.865 * logarea - 0.07 * sq (logarea)));
/*---------------------------------------------------
* Create flood wave attenuation "shoulder" params
*---------------------------------------------------*/
if (verbose)
printf ("Calling HydroShoulder... \n");
err = hydroshoulder ();
if (err)
{
fprintf (stderr,
" ERROR in HydroShoulder: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroShoulder: HydroTrend Aborted \n\n");
exit (1);
}
/*---------------------------------------
* Loop through each year of the epoch
*---------------------------------------*/
lyear = syear[ep] + nyears[ep];
for (yr = syear[ep]; yr < lyear; yr++)
{
/*-------------------------------------------------------------
* Reset annual arrays tracking carryover from previous year
*-------------------------------------------------------------*/
if (ep > 0 || yr != syear[ep])
for (ii = 0; ii < maxday - daysiy; ii++)
{
Qrainwrap[ii] = Qrain[ii + daysiy];
Qicewrap[ii] = Qice[ii + daysiy];
Qnivalwrap[ii] = Qnival[ii + daysiy];
Qsswrap[ii] = Qnival[ii + daysiy];
}
else
for (ii = 0; ii < maxday - daysiy; ii++)
{
Qrainwrap[ii] = 0.0;
Qicewrap[ii] = 0.0;
Qnivalwrap[ii] = 0.0;
Qsswrap[ii] = 0.0;
}
/*---------------------------------------
* Keep track of groundwater pool size
*---------------------------------------*/
gwlast = gwstore[daysiy - 1];
/*--------------------------------------------------
* In case the model exceeds the maximum flood,
* loop through a number of times. This normally
* gets the maximum modeled flood to be below
* the maximum predicted flood.
*--------------------------------------------------*/
exceedflood = 1;
floodtry = 0;
while (exceedflood > 0 && floodtry < 10)
{
/*------------------------------
* Reset annual arrays/values
*------------------------------*/
for (ii = 0; ii < maxday; ii++)
{
Qrain[ii] = 0.0;
Qice[ii] = 0.0;
Qnival[ii] = 0.0;
Qss[ii] = 0.0;
Qsumtot[ii] = 0.0;
for (p = 0; p < maxnoutlet; p++)
Qsum[ii][p] = 0.0;
}
for (ii = 0; ii < daysiy; ii++)
{
if (outletmodelflag == 1)
for (p = 0; p < maxnoutlet; p++)
{
Csoutlet[ii][p] = 0.0;
Qboutlet[ii][p] = 0.0;
Qsoutlet[ii][p] = 0.0;
}
Cs[ii] = 0.0;
Qb[ii] = 0.0;
Qs[ii] = 0.0;
Qicetogw[ii] = 0.0;
Qnivaltogw[ii] = 0.0;
Pdaily[ii] = 0.0;
Tdaily[ii] = 0.0;
gwstore[ii] = 0.0;
}
Enivalannual = 0.0;
Eiceannual = 0.0;
/*---------------------------
* Set the initial GW pool
*---------------------------*/
if (yr == syear[0])
{
gwstore[0] = gwinitial;
gwlast = gwinitial;
}
if (yr != syear[0])
gwstore[0] = gwlast;
/*-----------------------------------------------------------------
* Start new random number sequence.
* Get 'maxran' worth of random numbers and pluck them as needed
* nran counts through the numbers stored in ranarray
*-----------------------------------------------------------------*/
rmin = -6.0;
while (rmin < -5.0 || rmax > 5.0)
{
if (verbose)
printf ("Calling HydroRandom... \n");
err = hydrorandom ();
if (err)
{
fprintf (stderr,
" ERROR in HydroRandom: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroRandom: HydroTrend Aborted \n\n");
exit (1);
}
}
/*---------------------------------
* Set the climate for this year
*---------------------------------*/
if (verbose)
printf ("Calling HydroClimate... \n");
err = hydroclimate (gw_rain);
if (err)
{
fprintf (stderr,
" ERROR in HydroClimate: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroClimate: HydroTrend Aborted \n\n");
exit (1);
}
/*-------------------------------------
* Calculate weather for each day of
* the year, for each hypsometric bin
*-------------------------------------*/
if (verbose)
printf ("Calling HydroWeather... \n");
err = hydroweather (gw_rain);
if (err)
{
fprintf (stderr,
" ERROR in HydroWeather: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroWeather: HydroTrend Aborted \n\n");
exit (1);
}
/*-------------------------------------------------
* Calculate elev grid and T, for each elevation
*-------------------------------------------------*/
if (verbose)
printf ("Calling HydroHypsom... \n");
err = hydrohypsom ();
if (err)
{
fprintf (stderr,
" ERROR in HydroHypsom: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroHypsom: HydroTrend Aborted \n\n");
exit (1);
}
/*-------------------------------------------------
* Calculate ice accumulation/melt for each day.
* This is done before HydroRain or HydroSnow to
* find the glaciated area
*-------------------------------------------------*/
if (verbose)
printf ("Calling HydroGlacial... \n");
err = hydroglacial ();
if (err)
{
fprintf (stderr,
" ERROR in HydroGlacial: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroGlacial: HydroTrend Aborted \n\n");
exit (1);
}
/*------------------------------------------
* Calculate snow fall/melt for each day.
* This is done before HydroRain to find
* the "snow" area for each day
*------------------------------------------*/
if (verbose)
printf ("Calling HydroSnow... \n");
err = hydrosnow ();
if (err)
{
fprintf (stderr,
" ERROR in HydroSnow: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroSnow: HydroTrend Aborted \n\n");
exit (1);
}
/*---------------------------------
* Calculate precip for each day
*---------------------------------*/
if (verbose)
printf ("Calling HydroRain... \n");
err = hydrorain ();
if (err)
{
fprintf (stderr,
" ERROR in HydroRain: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroRain: HydroTrend Aborted \n\n");
exit (1);
}
/*------------------------------------------------------------
* Add the component flows and find peakflow for the year.
* Store the lagged overflow and groundwater pool size for
* the following year.
*------------------------------------------------------------*/
if (verbose)
printf ("Calling HydroSumFlow... \n");
err = hydrosumflow ();
if (err)
{
fprintf (stderr,
" ERROR in HydroSumFlow: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroSumFlow: HydroTrend Aborted \n\n");
exit (1);
}
/*-----------------------------------------
* Is flood peak less than max allowed ?
*-----------------------------------------*/
if (Qpeak < maxflood)
exceedflood = 0;
else
{
if (setstartmeanQandQs == 4)
{
fprintf (stderr,
"\n FLOOD WARNING: epoch %d, year %d \n",
ep + 1, yr);
fprintf (stderr,
" \t Max.Allowed %.1f, Qpeak %.1f, retry # %d \n",
maxflood, Qpeak, floodtry);
}
floodtry++;
if (floodtry < 10)
{
Qgrandtotal[ep] -= Qtotal;
}
}
} /* end flood exceedence while loop */
/*-------------------------------------------------------
* Track the max flow and if we still exceed the max
* predicted flood, send warning flag, but keep going
*-------------------------------------------------------*/
Qpeakall[ep] = mx (Qpeak, Qpeakall[ep]);
if (outletmodelflag == 1)
for (p = 0; p < maxnoutlet; p++)
Qpeakperoutletall[ep][p] =
mx (Qpeakperoutlet[p], Qpeakperoutletall[ep][p]);
if (exceedflood > 0 && setstartmeanQandQs == 4)
{
fprintf (stderr,
" FLOOD WARNING: the maximum predicted flood size");
fprintf (stderr, " has been exceeded. \n");
fprintf (stderr, " Epoch %d, year %d \n", ep + 1, yr);
fprintf (stderr,
" Maximum predicted flood %g (m^3/s) \n",
maxflood);
fprintf (stderr, " Modeled flood peak %g (m^3/s) \n",
Qpeak);
fprintf (fidlog,
" FLOOD WARNING: the maximum predicted flood size");
fprintf (fidlog, " has been exceeded. \n");
fprintf (fidlog, " Epoch %d, year %d \n", ep + 1, yr);
fprintf (fidlog,
" Maximum predicted flood %g (m^3/s) \n",
maxflood);
fprintf (fidlog, " Modeled flood peak %g (m^3/s) \n",
Qpeak);
}
/*-------------------------------------------------------------
* Calculate the maximal events = (biggest Qpeaks per epoch)
* for channel switching at delta if option is turned on
*------------------------------------------------------------- */
if (setstartmeanQandQs == 1 && eventnrflag == 0)
{
if (verbose)
printf ("Calling HydroMaxEvents... \n");
err = hydromaxevents ();
if (err)
{
fprintf (stderr,
" ERROR in HydroMaxEvents: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroMaxEvents: HydroTrend Aborted \n\n");
exit (1);
}
}
/*---------------------------
* Calculate sediment load
*---------------------------*/
if (setstartmeanQandQs > 1)
{
if (verbose)
printf ("Calling HydroSedLoad... \n");
err = hydrosedload (gw_rain);
if (err)
{
fprintf (stderr,
" ERROR in HydroSedLoad: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroSedLoad: HydroTrend Aborted \n\n");
exit (1);
}
}
/*------------------------------------------
* Output the binary daily discharge file
*------------------------------------------*/
if (setstartmeanQandQs == 4)
{
if (verbose)
printf ("Calling HydroOutput... \n");
err = hydrooutput ();
if (err)
{
fprintf (stderr,
" ERROR in HydroOutput: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in HydroOutput: HydroTrend Aborted \n\n");
exit (1);
}
/*------------------------------------------
* Output the binary daily discharge file
*------------------------------------------*/
if (verbose)
printf ("Calling ht_save_data... \n");
err = _ht_save_data (self);
if (err)
{
fprintf (stderr,
" ERROR in ht_save_data: HydroTrend Aborted \n\n");
fprintf (fidlog,
" ERROR in ht_save_data: HydroTrend Aborted \n\n");
exit (1);
}
/*------------------------------------------------
* Print out annual values to trend (trn) files
*------------------------------------------------*/
if (verbose)
printf ("Calling HydroPrintAnnual... \n");
err = hydroprintannual ();
if (err)
{
fprintf (stderr,
" WARNING in HydroPrintTrend: Continuing \n\n");
fprintf (fidlog,
" WARNING in HydroPrintTrend: Continuing \n\n");
}
} /* end setstartmeanQandQs == 4 */
/*-------------------------------------------------------
* Did random number generator create enough values ?
*-------------------------------------------------------*/
if (nran >= maxran)
{
fprintf (stderr,
"\n\n HydroTrend ERROR: nran exceeded maxran.\n");
fprintf (stderr, "\t increase maxran in HydroParams.h. \n");
fprintf (stderr, "\t nran = %d, maxran = %d \n\n", nran,
maxran);
exit (1);
}
} /* end year loop */
/*------------------------------------------
* Calculate Qbartotal[ep] (before the
* river might split in multiple outlets)
*------------------------------------------*/
if (setstartmeanQandQs == 0)
{
Qbartotal[ep] = Qgrandtotal[ep] / (daysiy * nyears[ep] * dTOs);
baseflowpercentage =
((Qbartotal[ep] - baseflowtot[ep]) / Qbartotal[ep]);
Qicebartotal[ep] =
(Qicetotal[ep] / (daysiy * nyears[ep] * dTOs)) *
baseflowpercentage;
if (eventnrflag == 1)
{
if (floodcounter == 0)
eventsnr[ep] = 1;
if (floodcounter > 0)
{
floodcounter = 0;
for (p = 0; p < nyears[ep]; p++)
for (k = 0; k < daysiy; k++)
if (Qpeakfloodtemp[p][k] > 0.0)
{
Qpeakfloodtemp[p][k] =
(Qpeakfloodtemp[p][k] *
((Qbartotal[ep] -
baseflowtot[ep]) / Qbartotal[ep])) +
baseflowtot[ep];
if (Qpeakfloodtemp[p][k] > floodvalue[ep])
floodcounter++;
}