-
Notifications
You must be signed in to change notification settings - Fork 24
/
rainfall.c
1165 lines (1036 loc) · 45.8 KB
/
rainfall.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 "rainfall.h"
//This reads in a set of binary files for the rainfall at each link.
//Assumes the file is full of floats. Assumes no IDs are in the file and that IDs are consecutive starting from 0
//Link** sys: An array of links.
//int N: The number of links in sys.
//int my_N: The number of links assigned to this process.
//UnivVars* GlobalVars: Contains all the information that is shared by every link in the system.
//int* my_sys: Array of links assigned to this process (value is location in sys array).
//int* assignments (set by this method): Will be an array with N entries. assignments[i] will the process link sys[i] is assigned to.
//char strfilename[]: String of the filename for the rain file to read (NOT .str files). Filenames should be indexed.
//unsigned int first: The index of the file to read first.
//unsigned int last: The index of the file to read last.
//double t_0: The time at which the first file starts.
//double increment: The amount of time between consecutively indexed files.
//int** id_to_loc (set by this method): Will be an array with N rows and 2 columns, sorted by first col. First col is a link id and second is
// the location of the id in sys.
//unsigned int max_files: The maximum number of files to be read.
int Create_Rain_Data_Par(Link** sys,unsigned int N,unsigned int my_N,UnivVars* GlobalVars,unsigned int* my_sys,int* assignments,char strfilename[],unsigned int first,unsigned int last,double t_0,double increment,Forcing* forcing,unsigned int** id_to_loc,unsigned int max_files,unsigned int forcing_idx)
{
unsigned int i,j,curr_idx;
unsigned int k;
Link* current;
float forcing_buffer;
unsigned int holder;
char filename[128];
FILE* stormdata = NULL;
unsigned int numfiles = last - first + 1;
//This is a time larger than any time in which the integrator is expected to get
double ceil_time = 1e300;
if(sys[my_sys[0]]->last_t > ceil_time*0.1)
printf("[%i]: Warning: integrator time is extremely large (about %e). Loss of precision may occur.\n",my_rank,sys[my_sys[i]]->last_t);
//Check that space for rain data has been allocated.
if(sys[my_sys[0]]->forcing_buff[forcing_idx] == NULL)
{
for(i=0;i<my_N;i++)
{
sys[my_sys[i]]->forcing_buff[forcing_idx] = (ForcingData*) malloc(sizeof(ForcingData));
sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall = (double**) malloc((max_files + 1)*sizeof(double*));
sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = numfiles + 1;
for(j=0;j<max_files+1;j++) sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall[j] = (double*) malloc(2*sizeof(double));
}
}
//Read through the files.
for(k=0;k<numfiles;k++)
{
//sprintf(filename,"%s%i",strfilename,first+k); //This is the usual one to use
sprintf(filename,"%s%i",strfilename,first+k);
//sprintf(filename,"%srain%i",strfilename,first+k);
//sprintf(filename,"%sfile-%i",strfilename,first+k);
stormdata = fopen(filename,"r");
if(stormdata == NULL) printf("[%i]: Error opening file %s\n",my_rank,filename);
for(i=0;i<N;i++)
{
//Find the location of ID i (should be i by assumption)
curr_idx = id_to_loc[i][1];
//Read the index
if(assignments[curr_idx] == my_rank) //If information about the link is needed on this process
{
//Read in the storm data for this link
fread(&forcing_buffer,sizeof(float),1,stormdata);
//This assumes different endianness
holder = *(unsigned int*) &forcing_buffer; //Pointers are fun
holder = (((holder & 0x0000ffff)<<16) | ((holder & 0xffff0000)>>16));
holder = (((holder & 0x00ff00ff)<<8) | ((holder & 0xff00ff00)>>8));
forcing_buffer = *(float*) &holder;
//Store the data
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = t_0 + k*increment;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = forcing_buffer;
}
else //This link data is not needed on this process.
fread(&forcing_buffer,sizeof(float),1,stormdata);
}
fclose(stormdata);
}
if(my_rank == 0)
printf("Read %i binary files.\n",numfiles);
//Add in terms for no rainfall if max_files > numfiles
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
for(j=numfiles;j<max_files;j++)
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][0] = sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j-1][0] + .0001;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][1] = 0.0;
}
}
//Add a ceiling term
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
//sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][0] = GlobalVars->maxtime + 1.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][0] = ceil_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][1] = -1.0;
}
//Calculate the first rain change time and set rain_value
for(i=0;i<my_N;i++)
{
current = sys[my_sys[i]];
forcing_buffer = current->forcing_buff[forcing_idx]->rainfall[0][1];
current->forcing_values[forcing_idx] = forcing_buffer;
current->forcing_indices[forcing_idx] = 0;
for(j=1;j<current->forcing_buff[forcing_idx]->n_times;j++)
{
if( fabs(forcing_buffer - current->forcing_buff[forcing_idx]->rainfall[j][1]) > 1e-14 )
{
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j][0];
break;
}
}
if(j == current->forcing_buff[forcing_idx]->n_times)
{
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j-1][0];
}
}
return 0;
}
//This reads in a set of gzip compressed binary files for the rainfall at each link.
//Assumes the file is full of floats. Assumes no IDs are in the file and that IDs are consecutive starting from 0
//Link** sys: An array of links.
//int N: The number of links in sys.
//int my_N: The number of links assigned to this process.
//UnivVars* GlobalVars: Contains all the information that is shared by every link in the system.
//int* my_sys: Array of links assigned to this process (value is location in sys array).
//int* assignments (set by this method): Will be an array with N entries. assignments[i] will the process link sys[i] is assigned to.
//char strfilename[]: String of the filename for the rain file to read (NOT .str files). Filenames should be indexed.
//unsigned int first: The index of the file to read first.
//unsigned int last: The index of the file to read last.
//double t_0: The time at which the first file starts.
//double increment: The amount of time between consecutively indexed files.
//int** id_to_loc (set by this method): Will be an array with N rows and 2 columns, sorted by first col. First col is a link id and second is
// the location of the id in sys.
//unsigned int max_files: The maximum number of files to be read.
int Create_Rain_Data_GZ(Link** sys,unsigned int N,unsigned int my_N,UnivVars* GlobalVars,unsigned int* my_sys,int* assignments,char strfilename[],unsigned int first,unsigned int last,double t_0,double increment,Forcing* forcing,unsigned int** id_to_loc,unsigned int max_files,unsigned int forcing_idx)
{
unsigned int i,j,curr_idx;
unsigned int k;
Link* current;
float rainfall_buffer;
unsigned int holder;
char filename[128];
FILE* stormdata = NULL;
unsigned int numfiles = last - first + 1;
FILE* compfile = NULL;
unsigned int buffer_size = sizeof(unsigned int)+sizeof(float);
char transferbuffer[buffer_size];
//This is a time larger than any time in which the integrator is expected to get
double ceil_time = 1e300;
if(sys[my_sys[0]]->last_t > ceil_time*0.1)
printf("[%i]: Warning: integrator time is extremely large (about %e). Loss of precision may occur.\n",my_rank,sys[my_sys[i]]->last_t);
//Check that space for rain data has been allocated.
if(sys[my_sys[0]]->forcing_buff[forcing_idx] == NULL)
{
for(i=0;i<my_N;i++)
{
sys[my_sys[i]]->forcing_buff[forcing_idx] = (ForcingData*) malloc(sizeof(ForcingData));
sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall = (double**) malloc((max_files + 1)*sizeof(double*));
sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = numfiles + 1;
for(j=0;j<max_files+1;j++) sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall[j] = (double*) malloc(2*sizeof(double));
}
}
//Read through the files.
MPI_Barrier(MPI_COMM_WORLD);
for(k=0;k<numfiles;k++)
{
if(my_rank == 0)
{
//For compressed file
sprintf(filename,"%s%i.gz",strfilename,first+k);
compfile = fopen(filename,"r");
if(!compfile)
{
printf("[%i]: Error opening file %s.\n",my_rank,filename);
MPI_Abort(MPI_COMM_WORLD,1);
}
sprintf(filename,"%s%i_%i",strfilename,first+k,my_rank);
stormdata = fopen(filename,"w");
if(!stormdata) printf("[%i]: Error opening file %s.\n",my_rank,filename);
int ret = uncompress_gzfile(compfile,stormdata);
if(ret != Z_OK) zerr(ret);
fclose(compfile);
fclose(stormdata);
sprintf(filename,"%s%i_%i",strfilename,first+k,my_rank);
stormdata = fopen(filename,"r");
if(!stormdata) printf("[%i]: Error opening file %s.\n",my_rank,filename);
for(i=0;i<N;i++)
{
//Find the location of ID i (should be i by assumption)
curr_idx = id_to_loc[i][1];
fread(&rainfall_buffer,sizeof(float),1,stormdata);
if(assignments[curr_idx] == my_rank) //Data needed on proc 0; don't send
{
//This assumes the files have a different endianness from the system
holder = *(unsigned int*) &rainfall_buffer; //Pointers are fun!
holder = (((holder & 0x0000ffff)<<16) | ((holder & 0xffff0000)>>16));
holder = (((holder & 0x00ff00ff)<<8) | ((holder & 0xff00ff00)>>8));
rainfall_buffer = *(float*) &holder;
//Store the data
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = t_0 + k*increment;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = rainfall_buffer;
}
else //Send it to the correct proc
{
int pos = 0;
MPI_Pack(&curr_idx,1,MPI_INT,transferbuffer,buffer_size,&pos,MPI_COMM_WORLD);
MPI_Pack(&rainfall_buffer,1,MPI_FLOAT,transferbuffer,buffer_size,&pos,MPI_COMM_WORLD);
MPI_Send(transferbuffer,buffer_size,MPI_PACKED,assignments[curr_idx],N,MPI_COMM_WORLD); //N = tag, as typical communication should not send this many links.
}
}
fclose(stormdata);
remove(filename);
}
else
{
//MPI_Status status;
for(i=0;i<my_N;i++)
{
int pos = 0;
MPI_Recv(transferbuffer,buffer_size,MPI_PACKED,0,N,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
MPI_Unpack(transferbuffer,buffer_size,&pos,&curr_idx,1,MPI_INT,MPI_COMM_WORLD);
MPI_Unpack(transferbuffer,buffer_size,&pos,&rainfall_buffer,1,MPI_FLOAT,MPI_COMM_WORLD);
//This assumes the files have a different endianness from the system
holder = *(unsigned int*) &rainfall_buffer; //Pointers are fun!
holder = (((holder & 0x0000ffff)<<16) | ((holder & 0xffff0000)>>16));
holder = (((holder & 0x00ff00ff)<<8) | ((holder & 0xff00ff00)>>8));
rainfall_buffer = *(float*) &holder;
//Store the data
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = t_0 + k*increment;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = rainfall_buffer;
}
}
}
if(my_rank == 0)
printf("Read %i binary files.\n",numfiles);
//Add in terms for no rainfall if max_files > numfiles
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
for(j=numfiles;j<max_files;j++)
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][0] = sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j-1][0] + .0001;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][1] = 0.0;
}
}
//Add a ceiling term
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
//sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][0] = GlobalVars->maxtime + 1.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][0] = ceil_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][1] = -1.0;
}
//Calculate the first rain change time and set rain_value
for(i=0;i<my_N;i++)
{
current = sys[my_sys[i]];
rainfall_buffer = current->forcing_buff[forcing_idx]->rainfall[0][1];
current->forcing_values[forcing_idx] = rainfall_buffer;
current->forcing_indices[forcing_idx] = 0;
for(j=1;j<current->forcing_buff[forcing_idx]->n_times;j++)
{
if(rainfall_buffer != current->forcing_buff[forcing_idx]->rainfall[j][1])
{
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j][0];
break;
}
}
if(j == current->forcing_buff[forcing_idx]->n_times)
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j-1][0];
}
return 0;
}
//This reads in a set of binary files for the rainfall at each link.
//The data is given as intensities per grid cell.
//Link** sys: An array of links.
//int N: The number of links in sys.
//int my_N: The number of links assigned to this process.
//UnivVars* GlobalVars: Contains all the information that is shared by every link in the system.
//int* my_sys: Array of links assigned to this process (value is location in sys array).
//int* assignments (set by this method): Will be an array with N entries. assignments[i] will the process link sys[i] is assigned to.
//char strfilename[]: String of the filename for the rain file to read (NOT .str files). Filenames should be indexed.
//unsigned int first: The index of the file to read first.
//unsigned int last: The index of the file to read last.
//double t_0: The time at which the first file starts.
//double increment: The amount of time between consecutively indexed files.
//int** id_to_loc (set by this method): Will be an array with N rows and 2 columns, sorted by first col. First col is a link id and second is
// the location of the id in sys.
//unsigned int max_files: The maximum number of files to be read.
int Create_Rain_Data_Grid(Link** sys,unsigned int N,unsigned int my_N,UnivVars* GlobalVars,unsigned int* my_sys,int* assignments,char strfilename[],unsigned int first,unsigned int last,double t_0,double increment,Forcing* forcing,unsigned int** id_to_loc,unsigned int max_files,unsigned int forcing_idx)
{
unsigned int i,j,curr_idx,k,holder,endianness,cell;
short unsigned int intensity;
Link* current;
char filename[128];
float forcing_buffer;
FILE* stormdata = NULL;
unsigned int numfiles = last - first + 1;
size_t result;
//This is a time larger than any time in which the integrator is expected to get
double ceil_time = 1e300;
if(sys[my_sys[0]]->last_t > ceil_time*0.1)
printf("[%i]: Warning: integrator time is extremely large (about %e). Loss of precision may occur.\n",my_rank,sys[my_sys[i]]->last_t);
//Check that space for rain data has been allocated.
if(sys[my_sys[0]]->forcing_buff[forcing_idx] == NULL)
{
for(i=0;i<my_N;i++)
{
sys[my_sys[i]]->forcing_buff[forcing_idx] = (ForcingData*) malloc(sizeof(ForcingData));
sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall = (double**) malloc((max_files + 1)*sizeof(double*));
sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = numfiles + 1;
for(j=0;j<max_files+1;j++) sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall[j] = (double*) malloc(2*sizeof(double));
}
}
//Read through the files.
for(k=0;k<numfiles;k++)
{
if(my_rank == 0)
{
sprintf(filename,"%s%i",strfilename,first+k);
stormdata = fopen(filename,"r");
if(stormdata)
{
for(i=0;i<forcing->num_cells;i++)
forcing->received[i] = 0;
//Check endianness
fread(&i,sizeof(unsigned int),1,stormdata);
if(i == 0x1) endianness = 0;
else if(i == 0x80000000) endianness = 1;
else
{
printf("Error: Cannot read endianness flag in binary file %s.\n",filename);
MPI_Abort(MPI_COMM_WORLD,1);
}
//Read file
if(endianness)
{
while(!feof(stormdata))
{
//Read intensity
result = fread(&cell,sizeof(unsigned int),1,stormdata);
if(!result) break;
fread(&intensity,sizeof(short unsigned int),1,stormdata);
//Swap byte order
holder = (((cell & 0x0000ffff)<<16) | ((cell & 0xffff0000)>>16));
cell = (((holder & 0x00ff00ff)<<8) | ((holder & 0xff00ff00)>>8));
intensity = (((intensity & 0x00ff00ff)<<8) | ((intensity & 0xff00ff00)>>8));
if(cell < forcing->num_cells)
{
if(forcing->received[cell]) printf("Warning: Received multiple intensities for cell %u in file %s.\n",cell,filename);
forcing->received[cell] = 1;
forcing->intensities[cell] = (short int) intensity * forcing->factor;
}
else
printf("Warning: bad grid cell id in file %s.\n",filename);
}
}
else
{
while(!feof(stormdata))
{
//Read intensity
result = fread(&cell,sizeof(unsigned int),1,stormdata);
if(!result) break;
fread(&intensity,sizeof(short unsigned int),1,stormdata);
if(cell < forcing->num_cells)
{
if(forcing->received[cell]) printf("Warning: Received multiple intensities for cell %u in file %s.\n",cell,filename);
forcing->received[cell] = 1;
forcing->intensities[cell] = (short int) intensity * forcing->factor;
}
else
printf("Warning: bad grid cell id in file %s.\n",filename);
}
}
fclose(stormdata);
//Store 0's for remaining cells
for(i=0;i<forcing->num_cells;i++)
if(!forcing->received[i]) forcing->intensities[i] = 0.0;
}
else //No file, no rain
{
for(i=0;i<forcing->num_cells;i++)
forcing->intensities[i] = 0.0;
}
}
MPI_Bcast(forcing->intensities,forcing->num_cells,MPI_FLOAT,0,MPI_COMM_WORLD);
//Load the data
for(cell=0;cell<forcing->num_cells;cell++)
{
for(i=0;i<forcing->num_links_in_grid[cell];i++) //!!!! Assuming only links on this proc !!!!
{
curr_idx = forcing->grid_to_linkid[cell][i];
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = t_0 + k*increment;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = forcing->intensities[cell];
}
}
}
//Add in terms for no rainfall if max_files > numfiles
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
for(j=numfiles;j<max_files;j++)
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][0] = sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j-1][0] + .0001;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][1] = 0.0;
}
}
//Add a ceiling term
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
//sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][0] = GlobalVars->maxtime + 1.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][0] = ceil_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[max_files][1] = -1.0;
}
//Calculate the first rain change time and set rain_value
for(i=0;i<my_N;i++)
{
current = sys[my_sys[i]];
forcing_buffer = current->forcing_buff[forcing_idx]->rainfall[0][1];
current->forcing_values[forcing_idx] = forcing_buffer;
current->forcing_indices[forcing_idx] = 0;
for(j=1;j<current->forcing_buff[forcing_idx]->n_times;j++)
{
if( fabs(forcing_buffer - current->forcing_buff[forcing_idx]->rainfall[j][1]) > 1e-14 )
{
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j][0];
break;
}
}
if(j == current->forcing_buff[forcing_idx]->n_times)
{
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j-1][0];
}
}
return 0;
}
//This reads in rainfall data at each link from an SQL database.
//Link** sys: An array of links.
//int N: The number of links in sys.
//int my_N: The number of links assigned to this process.
//UnivVars* GlobalVars: Contains all the information that is shared by every link in the system.
//int* my_sys: Array of links assigned to this process (value is location in sys array).
//int* assignments (set by this method): Will be an array with N entries. assignments[i] will the process link sys[i] is assigned to.
//char strfilename[]: String of the filename for the rain file to read (NOT .str files). Filenames should be indexed.
//unsigned int first: The index of the file to read first.
//unsigned int last: The index of the file to read last.
//double t_0: The time at which the first file starts.
//double increment: The amount of time between consecutively indexed files.
//int** id_to_loc (set by this method): Will be an array with N rows and 2 columns, sorted by first col. First col is a link id and second is
// the location of the id in sys.
//unsigned int max_files: The maximum number of files to be read.
int Create_Rain_Database(Link** sys,unsigned int N,unsigned int my_N,UnivVars* GlobalVars,unsigned int* my_sys,int* assignments,ConnData *conninfo,unsigned int first,unsigned int last,Forcing* forcing,unsigned int** id_to_loc,double maxtime,unsigned int forcing_idx)
{
unsigned int i,j,k,curr_idx,tuple_count;
Link* current;
float forcing_buffer;
int received_time;
char* query = conninfo->query;
PGresult *res;
unsigned int *db_unix_time,*db_link_id;
float *db_rain_intens;
unsigned int *total_times = (unsigned int*) calloc(my_N,sizeof(unsigned int));
for(i=0;i<my_N;i++)
{
if(sys[my_sys[i]]->forcing_buff[forcing_idx])
{
total_times[i] = sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times;
sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = 1;
}
}
//This is a time larger than any time in which the integrator is expected to get
double ceil_time = 1e300;
static short int gave_warning = 0;
if(sys[my_sys[0]]->last_t > ceil_time*0.1 && !gave_warning)
{
gave_warning = 1;
printf("[%i]: Warning: integrator time is extremely large (about %e). Loss of precision may occur.\n",my_rank,sys[my_sys[i]]->last_t);
}
/*
//!!!! Fix this (or don't?) !!!!
total_times = sys[my_sys[0]]->forcing_buff[forcing_idx]->n_times;
for(i=0;i<my_N;i++) sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = 1;
*/
//GlobalVars->outletlink = 318213;
//Query the database
if(my_rank == 0)
{
//Connect to the database
ConnectPGDB(conninfo);
if(GlobalVars->outletlink == 0)
sprintf(query,conninfo->queries[0],first,last);
else
sprintf(query,conninfo->queries[1],GlobalVars->outletlink,first,last);
//printf("*************************\n");
//printf("First = %u Last = %u t = %e increment = %u\n",first,last,sys[my_sys[0]]->last_t,forcing->increment);
//printf("*************************\n");
//printf("Gmax = %e maxtime = %e\n",GlobalVars->maxtime,maxtime);
//printf("query: %s\n",query);
//printf("*************************\n");
res = PQexec(conninfo->conn,query);
CheckResError(res,"downloading rainfall data");
tuple_count = PQntuples(res);
printf("Received %u intensities.\n",tuple_count);
//Disconnect
DisconnectPGDB(conninfo);
//Allocate space
MPI_Bcast(&tuple_count,1,MPI_INT,0,MPI_COMM_WORLD);
db_unix_time = malloc(tuple_count*sizeof(unsigned int));
db_rain_intens = malloc(tuple_count*sizeof(float));
db_link_id = malloc(tuple_count*sizeof(unsigned int));
//Load up the buffers
for(i=0;i<tuple_count;i++)
{
db_unix_time[i] = atoi(PQgetvalue(res,i,0));
db_rain_intens[i] = atof(PQgetvalue(res,i,1));
db_link_id[i] = atoi(PQgetvalue(res,i,2));
}
//Broadcast the data
MPI_Bcast(db_unix_time,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(db_rain_intens,tuple_count,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Bcast(db_link_id,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
//Clean up
PQclear(res);
}
else
{
//Allocate space
MPI_Bcast(&tuple_count,1,MPI_INT,0,MPI_COMM_WORLD);
db_unix_time = malloc(tuple_count*sizeof(unsigned int));
db_rain_intens = malloc(tuple_count*sizeof(float));
db_link_id = malloc(tuple_count*sizeof(unsigned int));
//Receive the data
MPI_Bcast(db_unix_time,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(db_rain_intens,tuple_count,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Bcast(db_link_id,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
//double stop = time(NULL);
//if(my_rank == 0) printf("Total time to get rain: %f\n%u %u\n",difftime(stop,start),first,last);
//Setup initial time in rainfall data with 0 rain
for(i=0;i<my_N;i++)
{
sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall[0][0] = (int)(first - forcing->raindb_start_time)/60.0;
sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall[0][1] = 0.0;
}
//printf("Started with first = %u, raindb_start = %u, %f\n",first,forcing->raindb_start_time,(int)(first - forcing->raindb_start_time)/60.0);
//Setup the data received
for(i=0;i<tuple_count;i++)
{
//Find the location of ID i (ids should be numbered from 2 to whatever)
//!!!! This will need to be a search at some point !!!!
/*
if(GlobalVars->outletlink == 0)
{
curr_idx = id_to_loc[db_link_id[i]-2][1];
//curr_idx = id_to_loc[atoi(PQgetvalue(res,i,2))-2][1];
if(sys[curr_idx]->ID != db_link_id[i])
printf("Indices do not match %u %u\n",sys[curr_idx]->ID,db_link_id[i]);
//if(sys[curr_idx]->ID != atoi(PQgetvalue(res,i,2)))
//printf("Indices do not match %u %u\n",sys[curr_idx]->ID,atoi(PQgetvalue(res,i,2)));
}
else
*/
curr_idx = find_link_by_idtoloc(db_link_id[i],id_to_loc,N);
//curr_idx = find_link_by_idtoloc(atoi(PQgetvalue(res,i,2)),id_to_loc,N);
if(curr_idx < N && assignments[curr_idx] == my_rank)
{
k = sys[curr_idx]->forcing_buff[forcing_idx]->n_times;
received_time = db_unix_time[i] - forcing->raindb_start_time; //In seconds
//printf("Got k = %i received_time = %i in secs = %i in mins = %f\n",k,received_time,(int)(sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] * 60.0 + 0.01),sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0]);
if(received_time > (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] * 60.0 + 0.01))
{
if( received_time <= (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0]*60.0) + (unsigned int) (forcing->file_time*60.0) || sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][1] == 0.0)
{
//printf("stored ID = %u i = %i k = %i received = %i unix_time = %i raindb_start = %i\n",sys[curr_idx]->ID,i,k,received_time,db_unix_time[i],forcing->raindb_start_time);
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = db_rain_intens[i];
(sys[curr_idx]->forcing_buff[forcing_idx]->n_times)++;
//printf("k = %i (%f %f) Also: received = %i next = %u\n",k,sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0],sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1],received_time,(int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0]*60.0) + (unsigned int) (forcing->file_time*60.0));
}
else //Add a 0 rainfall data
{
//printf("stored 0 i = %i k = %i received = %i unix_time = %i raindb_start = %i\n",i,k,received_time,db_unix_time[i],forcing->raindb_start_time);
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] + forcing->file_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = 0.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][1] = db_rain_intens[i];
sys[curr_idx]->forcing_buff[forcing_idx]->n_times += 2;
//printf("(%f %f)\n",sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0],sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1]);
//printf("(%f %f)\n",sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][0],sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][1]);
}
}
else if(received_time <= (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] * 60.0 + 0.01)) //If the initial rate needs to be reset
{
//printf("stored init i = %i k = %i received = %i unix_time = %i raindb_start = %i\n",i,k,received_time,db_unix_time[i],forcing->raindb_start_time);
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][1] = db_rain_intens[i];
//printf("k = %i Init (%f %f)\n",k,sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0],sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][1]);
}
else
{
printf("!!!! Uh oh... !!!!\n");
printf("!!!! i = %i k = %i received = %i unix_time = %i raindb_start = %i\n",i,k,received_time,db_unix_time[i],forcing->raindb_start_time);
}
}
}
//printf("Got %u\n",sys[0]->forcing_buff[forcing_idx]->n_times);
//Add ceiling terms
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
k = sys[curr_idx]->forcing_buff[forcing_idx]->n_times;
if(sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][1] == 0.0) //No rain, add just a ceiling
{
//sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = maxtime * (1.1) + 1.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = ceil_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = -1.0;
}
else //Add a 0.0, and a ceiling
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] + forcing->file_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = 0.0;
//sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][0] = maxtime * (1.1) + 1.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][0] = ceil_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][1] = -1.0;
}
}
//Reset n_times !!!! Fix (well, this might be ok to do) !!!!
//for(i=0;i<my_N;i++) sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = total_times;
for(i=0;i<my_N;i++) sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = total_times[i];
//Calculate the first rain change time and set rain_value
for(i=0;i<my_N;i++)
{
current = sys[my_sys[i]];
//Get to the time block that corresponds to the current time, set forcing value
for(j=1;j<current->forcing_buff[forcing_idx]->n_times;j++)
{
if(current->last_t < current->forcing_buff[forcing_idx]->rainfall[j][0] - 1e-12)
break;
}
forcing_buffer = current->forcing_buff[forcing_idx]->rainfall[j-1][1];
current->forcing_values[forcing_idx] = forcing_buffer;
current->forcing_indices[forcing_idx] = j-1;
for(;j<current->forcing_buff[forcing_idx]->n_times;j++)
{
if( fabs(forcing_buffer - current->forcing_buff[forcing_idx]->rainfall[j][1]) > 1e-12 )
{
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j][0];
break;
}
}
if(j == current->forcing_buff[forcing_idx]->n_times)
current->forcing_change_times[forcing_idx] = current->forcing_buff[forcing_idx]->rainfall[j-1][0];
}
//Clean up
free(total_times);
free(db_unix_time);
free(db_link_id);
free(db_rain_intens);
return 0;
}
//This reads in rainfall data at each link from an SQL database. The timestamps are assumed to be irregularly spaced.
//Link** sys: An array of links.
//int N: The number of links in sys.
//int my_N: The number of links assigned to this process.
//UnivVars* GlobalVars: Contains all the information that is shared by every link in the system.
//int* my_sys: Array of links assigned to this process (value is location in sys array).
//int* assignments (set by this method): Will be an array with N entries. assignments[i] will the process link sys[i] is assigned to.
//char strfilename[]: String of the filename for the rain file to read (NOT .str files). Filenames should be indexed.
//unsigned int first: The index of the file to read first.
//unsigned int last: The index of the file to read last.
//double t_0: The time at which the first file starts.
//double increment: The amount of time between consecutively indexed files.
//int** id_to_loc (set by this method): Will be an array with N rows and 2 columns, sorted by first col. First col is a link id and second is
// the location of the id in sys.
//unsigned int max_files: The maximum number of files to be read.
int Create_Rain_Database_Irregular(Link** sys,unsigned int N,unsigned int my_N,UnivVars* GlobalVars,unsigned int* my_sys,int* assignments,ConnData *conninfo,unsigned int first,unsigned int last,Forcing* forcing,unsigned int** id_to_loc,double maxtime,unsigned int forcing_idx)
{
unsigned int i,j,k,curr_idx,tuple_count,current_timestamp;
Link* current;
float forcing_buffer;
int received_time;
char* query = conninfo->query;
PGresult *res;
unsigned int *db_unix_time,*db_link_id;
float *db_rain_intens;
unsigned int *actual_timestamps,num_actual_timestamps,max_timestamps = forcing->increment; //The maximum number of intensities to get for each link
/*
unsigned int *total_times = (unsigned int*) calloc(my_N,sizeof(unsigned int));
for(i=0;i<my_N;i++)
{
if(sys[my_sys[i]]->forcing_buff[forcing_idx])
{
total_times[i] = sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times;
sys[my_sys[i]]->forcing_buff[forcing_idx]->n_times = 1;
}
}
*/
//This is a time larger than any time in which the integrator is expected to get
double ceil_time = 1e300;
static short int gave_warning = 0;
if(sys[my_sys[0]]->last_t > ceil_time*0.1 && !gave_warning)
{
gave_warning = 1;
printf("[%i]: Warning: integrator time is extremely large (about %e). Loss of precision may occur.\n",my_rank,sys[my_sys[i]]->last_t);
}
//Query the database
if(my_rank == 0)
{
//Connect to the database
ConnectPGDB(conninfo);
//Download timestamps
sprintf(query,conninfo->queries[3],first,max_timestamps);
res = PQexec(conninfo->conn,query);
CheckResError(res,"downloading rainfall timestamps");
num_actual_timestamps = PQntuples(res);
//Unpack and broadcast the timestamps
actual_timestamps = (unsigned int*) malloc(num_actual_timestamps*sizeof(unsigned int));
for(i=0;i<num_actual_timestamps;i++)
actual_timestamps[i] = atoi(PQgetvalue(res,i,0));
MPI_Bcast(&num_actual_timestamps,1,MPI_UNSIGNED,0,MPI_COMM_WORLD);
MPI_Bcast(actual_timestamps,num_actual_timestamps,MPI_UNSIGNED,0,MPI_COMM_WORLD);
last = actual_timestamps[num_actual_timestamps-1];
PQclear(res);
//Download intensities
if(GlobalVars->outletlink == 0)
sprintf(query,conninfo->queries[0],first,last);
else
sprintf(query,conninfo->queries[1],GlobalVars->outletlink,first,last);
//printf("*************************\n");
//printf("First = %u Last = %u t = %e increment = %u\n",first,last,sys[my_sys[0]]->last_t,forcing->increment);
//printf("*************************\n");
//printf("Gmax = %e maxtime = %e\n",GlobalVars->maxtime,maxtime);
//printf("query: %s\n",query);
//printf("*************************\n");
res = PQexec(conninfo->conn,query);
CheckResError(res,"downloading rainfall data");
tuple_count = PQntuples(res);
printf("Received %u intensities.\n",tuple_count);
//Disconnect
DisconnectPGDB(conninfo);
//Allocate space
MPI_Bcast(&tuple_count,1,MPI_INT,0,MPI_COMM_WORLD);
db_unix_time = malloc(tuple_count*sizeof(unsigned int));
db_rain_intens = malloc(tuple_count*sizeof(float));
db_link_id = malloc(tuple_count*sizeof(unsigned int));
//Load up the buffers
for(i=0;i<tuple_count;i++)
{
db_unix_time[i] = atoi(PQgetvalue(res,i,0));
db_rain_intens[i] = atof(PQgetvalue(res,i,1));
db_link_id[i] = atoi(PQgetvalue(res,i,2));
}
//Broadcast the data
MPI_Bcast(db_unix_time,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(db_rain_intens,tuple_count,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Bcast(db_link_id,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
//Clean up
PQclear(res);
}
else
{
MPI_Bcast(&num_actual_timestamps,1,MPI_UNSIGNED,0,MPI_COMM_WORLD);
actual_timestamps = (unsigned int*) malloc(num_actual_timestamps*sizeof(unsigned int));
MPI_Bcast(actual_timestamps,num_actual_timestamps,MPI_UNSIGNED,0,MPI_COMM_WORLD);
last = actual_timestamps[num_actual_timestamps-1];
//Allocate space
MPI_Bcast(&tuple_count,1,MPI_INT,0,MPI_COMM_WORLD);
db_unix_time = malloc(tuple_count*sizeof(unsigned int));
db_rain_intens = malloc(tuple_count*sizeof(float));
db_link_id = malloc(tuple_count*sizeof(unsigned int));
//Receive the data
MPI_Bcast(db_unix_time,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(db_rain_intens,tuple_count,MPI_FLOAT,0,MPI_COMM_WORLD);
MPI_Bcast(db_link_id,tuple_count,MPI_INT,0,MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
//double stop = time(NULL);
//if(my_rank == 0) printf("Total time to get rain: %f\n%u %u\n",difftime(stop,start),first,last);
/*
printf("+++++++++\n");
for(i=0;i<num_actual_timestamps;i++)
{
printf("%u\n",actual_timestamps[i]);
}
printf("+++++++++\n");
*/
//Set the times and zero out the intensities (some of these might change later)
for(i=0;i<my_N;i++)
{
for(j=0;j<num_actual_timestamps;j++)
{
sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall[j][0] = (double)(actual_timestamps[j] - forcing->raindb_start_time)/60.0;
sys[my_sys[i]]->forcing_buff[forcing_idx]->rainfall[j][1] = 0.0;
}
}
//Setup the forcing values
i = 0;
for(j=0;j<num_actual_timestamps;j++)
{
current_timestamp = actual_timestamps[j];
for(;i < tuple_count && db_unix_time[i] <= current_timestamp;i++)
{
received_time = db_unix_time[i] - forcing->raindb_start_time; //In seconds
curr_idx = find_link_by_idtoloc(db_link_id[i],id_to_loc,N);
if(curr_idx < N && assignments[curr_idx] == my_rank)
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][1] = db_rain_intens[i];
//if(sys[curr_idx]->ID == 456117)
//printf("j = %u received_time = %u intensity = %f\n",j,received_time,sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][1]);
}
//else
//printf("!!!! Huh, wtf? !!!!\n");
}
}
/*
current_timestamp = actual_timestamps[0];
for(i=0;i < tuple_count && current_timestamp <= db_unix_time[i];i++)
{
received_time = db_unix_time[i] - forcing->raindb_start_time; //In seconds
//if(received_time > (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] * 60.0 + 0.01)) break;
curr_idx = find_link_by_idtoloc(db_link_id[i],id_to_loc,N);
if(curr_idx < N && assignments[curr_idx] == my_rank)
{
//k = sys[curr_idx]->forcing_buff[forcing_idx]->n_times;
//if(received_time <= (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] * 60.0 + 0.01)) //If the initial rate needs to be reset
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[0][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[0][1] = db_rain_intens[i];
}
}
}
//!!!! Uh, any difference between this and j=0? !!!!
for(j=1;j<num_actual_timestamps;j++)
{
current_timestamp = actual_timestamps[j];
for(;i < tuple_count && current_timestamp <= db_unix_time[i];i++)
{
received_time = db_unix_time[i] - forcing->raindb_start_time; //In seconds
curr_idx = find_link_by_idtoloc(db_link_id[i],id_to_loc,N);
if(curr_idx < N && assignments[curr_idx] == my_rank)
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[j][1] = db_rain_intens[i];
}
}
}
*/
/*
//Setup the data received
for(i=0;i<tuple_count;i++)
{
//Find the location of ID i (ids should be numbered from 2 to whatever)
curr_idx = find_link_by_idtoloc(db_link_id[i],id_to_loc,N);
if(curr_idx < N && assignments[curr_idx] == my_rank)
{
k = sys[curr_idx]->forcing_buff[forcing_idx]->n_times;
received_time = db_unix_time[i] - forcing->raindb_start_time; //In seconds
if(received_time > (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] * 60.0 + 0.01))
{
if( received_time <= (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0]*60.0) + (unsigned int) (forcing->file_time*60.0) || sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][1] == 0.0)
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = db_rain_intens[i];
(sys[curr_idx]->forcing_buff[forcing_idx]->n_times)++;
}
else //Add a 0 rainfall data before adding this data
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] + forcing->file_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = 0.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k+1][1] = db_rain_intens[i];
sys[curr_idx]->forcing_buff[forcing_idx]->n_times += 2;
}
}
else //if(received_time <= (int) (sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] * 60.0 + 0.01)) //If the initial rate needs to be reset
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][0] = received_time / 60.0;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][1] = db_rain_intens[i];
}
}
}
*/
//Add ceiling terms
for(i=0;i<my_N;i++)
{
curr_idx = sys[my_sys[i]]->location;
//k = sys[curr_idx]->forcing_buff[forcing_idx]->n_times;
k = num_actual_timestamps;
//if(sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k-1][1] == 0.0) //No rain, add just a ceiling
{
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][0] = ceil_time;
sys[curr_idx]->forcing_buff[forcing_idx]->rainfall[k][1] = -1.0;
}
/*
else //Add a 0.0, and a ceiling