00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "gridmap.h"
00029
00030 int NOT_ELM_NOT_GRIO = -1;
00031 int NOT_ELM_IN_GRIO = -2;
00032 int IN_ELM_NOT_GRIO = -3;
00033
00034 #define A_NEG_VAL -90
00035
00040 void formatDate(char* sd) {
00041
00042 char month[12],yr[5],mo[3],da[3], dig1[2], dig2[2], day_str[5] = "";
00043 char sdate[GRID_TAG_LENGTH];
00044
00045 sscanf(initDateRead, "%4s/%2s/%2s",yr,mo,da);
00046 if (atoi(mo) == 1) strcpy(month,"January ");
00047 else if (atoi(mo) == 2) strcpy(month,"February ");
00048 else if (atoi(mo) == 3) strcpy(month,"March ");
00049 else if (atoi(mo) == 4) strcpy(month,"April ");
00050 else if (atoi(mo) == 5) strcpy(month,"May ");
00051 else if (atoi(mo) == 6) strcpy(month,"June ");
00052 else if (atoi(mo) == 7) strcpy(month,"July ");
00053 else if (atoi(mo) == 8) strcpy(month,"August ");
00054 else if (atoi(mo) == 9) strcpy(month,"September ");
00055 else if (atoi(mo) == 10) strcpy(month,"October ");
00056 else if (atoi(mo) == 11) strcpy(month,"November ");
00057 else if (atoi(mo) == 12) strcpy(month,"December ");
00058
00059 strcpy(sdate,month);
00060
00061 sscanf(da, "%1s%1s\n",dig1,dig2);
00062 if(strcmp(dig1,"0") != 0 )
00063 strcpy(day_str,dig1);
00064 else strcat(day_str," ");
00065 strcat(day_str,dig2);
00066 strcat(day_str,", ");
00067 strcat(sdate,day_str);
00068 strcat(sdate,yr);
00069 strcpy(sd,sdate);
00070 }
00071
00072
00079 int mapGrids(char *gridmapfilename) {
00080
00081 int i;
00082 int success = 1, fail = -1;
00083 char line[80];
00085 FILE *gridmapfile_ptr;
00086
00088 int ogid=0;
00090 int ogr=0, ogc=0;
00092 int eid=0;
00094 int ii=0;
00096 int jj=0;
00097
00099 int eINog=0;
00100 for(i=0; i<80; i++)
00101 line[i]=' ';
00102
00103
00104 if((gridmapfile_ptr = fopen(gridmapfilename,"r")) == NULL) {
00105 printf( "Can't open the grid-map definition file %s!\n ",gridmapfilename ) ;
00106 exit (fail);
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 fgets(line,80,gridmapfile_ptr);
00120 fgets(line,80,gridmapfile_ptr);
00121 fgets(line,80,gridmapfile_ptr);
00122 fgets(line,80,gridmapfile_ptr);
00123 fgets(line,80,gridmapfile_ptr);
00124
00125
00126 fgets(line,80,gridmapfile_ptr);
00127 sscanf(line,"%d %d %d %d",&eINog, &OGrow_tot, &OGcol_tot, &gridio_batch_len);
00128
00129
00130 if(elm_OG_map==NULL) elm_OG_map = (struct eOGMap*) malloc(sizeof(struct eOGMap ) * OGrow_tot * OGcol_tot);
00131 if(elm_OG_map==NULL) {
00132 printf("\nFailed to allocate memory for elm_OG_map\n");
00133 exit (fail);
00134 }
00135
00136
00137 for(ii=0; ii<OGrow_tot*OGcol_tot; ii++) {
00138
00139 elm_OG_map[ii].elmID = (int*)malloc(sizeof(int)*eINog);
00140 if(elm_OG_map[ii].elmID==NULL) {
00141 printf("\nFailed to allocate memory for grid-map elmID\n");
00142 exit (fail);
00143 }
00144 }
00145 for(ii=0; ii<OGrow_tot*OGcol_tot; ii++) {
00146 elm_OG_map[ii].eidCnt=0;
00147 elm_OG_map[ii].OGID=0;
00148 elm_OG_map[ii].OGROW=0;
00149 elm_OG_map[ii].OGCOL=0;
00150 for(jj=0; jj<eINog; jj++) {
00151 elm_OG_map[ii].elmID[jj]= A_NEG_VAL;
00152 }
00153 }
00154 ii=0, jj=0, OGidCnt = 0;
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 fgets(line,80,gridmapfile_ptr);
00165 sscanf(line,"%d %d %d %d",&elm_OG_map[ii].OGID,&elm_OG_map[ii].OGROW,
00166 &elm_OG_map[ii].OGCOL,&elm_OG_map[ii].elmID[jj]);
00167
00168 elm_OG_map[ii].index = ii;
00169 elm_OG_map[ii].eidCnt++;
00170 OGidCnt++;
00171 ii++;
00172
00173 while(fgets(line,80,gridmapfile_ptr) != NULL) {
00174 sscanf(line,"%d %d %d %d",&ogid,&ogr,&ogc,&eid);
00175 if(ogid == elm_OG_map[ii-1].OGID) {
00176
00177
00178
00179
00180
00181 jj++;
00182 elm_OG_map[ii-1].elmID[jj] = eid;
00183 elm_OG_map[ii-1].OGROW = ogr;
00184 elm_OG_map[ii-1].OGCOL = ogc;
00185 elm_OG_map[ii-1].eidCnt++;
00186 elm_OG_map[ii-1].index = ii-1;
00187 }
00188 else {
00189 jj=0;
00190 elm_OG_map[ii].OGID = ogid;
00191 elm_OG_map[ii].elmID[jj] = eid;
00192 elm_OG_map[ii].OGROW = ogr;
00193 elm_OG_map[ii].OGCOL = ogc;
00194 elm_OG_map[ii].index = ii;
00195 elm_OG_map[ii].eidCnt++;
00196
00197 OGidCnt++;
00198 ii++;
00199 }
00200
00201 }
00202 fclose(gridmapfile_ptr);
00203 return success;
00204 }
00205
00217 int initDataStruct(char* binfilename, applicationStruct *this_struct) {
00218
00219 char sdate[GRID_TAG_LENGTH];
00220 int comp;
00221
00222 int i, j;
00223 int fail = -1, success = 1;
00224 int pos;
00225
00226
00227 this_struct->day = this_struct->recRead = this_struct->step = 0;
00228
00229 if(this_struct->dataELM==NULL) this_struct->dataELM = (float*)malloc(sizeof(float)*s0*s1);
00230 if(this_struct->dataELM==NULL) {
00231 printf("\nFailed to allocate memory for grid-map dataELM\n");
00232 exit (fail);
00233 }
00234 for(i=0;i<s0*s1;i++) {
00235 this_struct->dataELM[i]=0.0;
00236 }
00237
00238
00239
00240
00241 if(this_struct->dataAnyGrid==NULL) this_struct->dataAnyGrid = (float**)malloc( gridio_batch_len * sizeof(float)*OGrow_tot*OGcol_tot );
00242 if(this_struct->dataAnyGrid==NULL) {
00243 printf("\nFailed to allocate memory for grid-map dataAnyGrid\n");
00244 exit (fail);
00245 }
00246
00247 for(i=0; i<gridio_batch_len; i++) {
00248 if(this_struct->dataAnyGrid[i]==NULL) this_struct->dataAnyGrid[i] = (float*) malloc(sizeof(float) * (OGrow_tot * OGcol_tot) ) ;
00249 if(this_struct->dataAnyGrid[i]==NULL) {
00250 printf("\nFailed to allocate memory for grid-map gridio_batch_len[%d]\n",i);
00251 exit (fail);
00252 }
00253 for (j=0; j<OGrow_tot*OGcol_tot; j++) {
00254 this_struct->dataAnyGrid[i][j]=0.0;
00255 }
00256 }
00257
00258
00259
00260 if((this_struct->binfile_ptr = fopen(binfilename,"rb")) == NULL) {
00261 printf( "Can't open the grid_io binary data %s file!\n ", binfilename) ;
00262 exit (fail);
00263 }
00264
00265
00266
00267 if(grid_read_header(this_struct->binfile_ptr, &this_struct->grid) != 0) {
00268 fprintf(stdout, "Unable to read grid header from grid_io input file %s\n", binfilename);
00269 exit (fail);
00270 }
00271
00272
00273
00274 if(this_struct->values==NULL) this_struct->values = (float*)nalloc(this_struct->grid.header.number_of_nodes*sizeof(float),"values");
00275 if(this_struct->values==NULL) {
00276 printf("\nFailed to allocate memory for grid_io values array\n");
00277 exit (fail);
00278 }
00279 for(i=0; i< this_struct->grid.header.number_of_nodes; ++i) {
00280 this_struct->values[i] = 0.0;
00281 }
00282
00283
00284 formatDate(sdate);
00285
00286
00287
00288
00289 comp = 1;
00290 while(comp != 0) {
00291 if(grid_read(this_struct->binfile_ptr, &this_struct->grid, this_struct->tag, this_struct->values ) == -1 ) {
00292 fprintf(stdout, "Couldn't match starting date for date = %s in %s\n",sdate, binfilename);
00293 exit (fail);
00294 }
00295 comp = strncmp(this_struct->tag, sdate, strlen(sdate));
00296
00297 if(comp == 0) {
00298 pos = grid_skip(this_struct->binfile_ptr, &this_struct->grid, -1);
00299 }
00300
00301 this_struct->skip++;
00302
00303 }
00304
00305 return success;
00306 }
00307
00308
00318 int processData(char* binfilename, applicationStruct *this_struct) {
00319
00320 int kk=0, ii=0, jj=0;
00321 int jstart = 0;
00322 int jend = 0;
00323 int ogid = 0;
00324 int cntr = 0;
00325 int this_sz = 0;
00326 int row_bound = 0;
00327
00328 float val = 0.0;
00329
00330 int fail = -1, success = 1;
00331
00332
00333
00334
00335
00336
00337
00338 if (this_struct->step >= gridio_batch_len) {
00339
00340
00341 if((this_struct->binfile_ptr = fopen(binfilename, "rb")) == NULL) {
00342 printf("Can't open the rainfall binary %s file!\n",binfilename ) ;
00343 exit (fail);
00344 }
00345
00346 if(grid_read_header(this_struct->binfile_ptr, &this_struct->grid) != 0) {
00347 fprintf(stdout, "Unable to read grid header from input file %s\n",binfilename);
00348 exit (fail);
00349 }
00350
00351 this_sz = grid_skip(this_struct->binfile_ptr, &this_struct->grid, (this_struct->skip+this_struct->recRead-1));
00352
00353 }
00354
00355 this_struct->step = 0;
00356
00357
00358
00359
00360
00361
00362
00363
00364 while(this_struct->recRead < PORnumday && this_struct->step < gridio_batch_len ) {
00365 if(grid_read(this_struct->binfile_ptr, &this_struct->grid, this_struct->tag, this_struct->values ) != 0 ) {
00366 fprintf(stdout, "Unable to read grid value from input file %s\n",binfilename);
00367 exit (fail);
00368 }
00369
00370 ogid = 0;
00371 cntr = 0;
00372 val = 0.0;
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 row_bound = OGidCnt / OGcol_tot;
00384 for(ii=0; ii< row_bound; ++ii) {
00385 jstart = *(this_struct->grid.config.xstart+ii);
00386 jend = *(this_struct->grid.config.xend+ii);
00387 val = this_struct->values[cntr];
00388
00389
00390
00391
00392
00393 for(kk=1; kk<jstart; kk++, ogid++)
00394 this_struct->dataAnyGrid[this_struct->step][ogid] = val;
00395
00396
00397
00398
00399
00400 for(jj=jstart; jj<jend; jj++, ogid++, cntr++) {
00401 this_struct->dataAnyGrid[this_struct->step][ogid] = this_struct->values[cntr];
00402 }
00403
00404
00405 for(kk=jend; kk<OGcol_tot+1; kk++, ogid++) {
00406 this_struct->dataAnyGrid[this_struct->step][ogid] = this_struct->values[cntr];
00407 }
00408
00409 cntr++;
00410 }
00411 this_struct->recRead = this_struct->recRead + 1;
00412 this_struct->step = this_struct->step +1;
00413 }
00414
00415 if(this_struct->step > 0 )
00416 fclose (this_struct->binfile_ptr);
00417
00418 this_struct->step = 0;
00419 }
00420
00421
00422
00433 void returnData(float* dataSME, applicationStruct *this_struct) {
00434
00435 int i=0, j=0;
00436 int ii=0;
00437 int index=0;
00438
00439
00440 this_struct->dayCntr += 1;
00441 this_struct->recCntr = this_struct->recRead - this_struct->dayCntr;
00442
00443
00444
00445
00446
00447 for(ii=0; ii<OGidCnt; ++ii) {
00448
00449 if(debug > 4) {
00450 index = elm_OG_map[ii].index;
00451 printf("%d %d %d %d %d %3.3f\n",index, elm_OG_map[ii].OGID, elm_OG_map[ii].OGROW,
00452 elm_OG_map[ii].OGCOL ,elm_OG_map[ii].elmID[0],
00453 this_struct->dataAnyGrid[this_struct->step][ii]);
00454 }
00455
00456 if(elm_OG_map[ii].elmID[0] == NOT_ELM_NOT_GRIO)
00457 continue;
00458
00459 for(i=0; i<elm_OG_map[ii].eidCnt; i++) {
00460 j = elm_OG_map[ii].elmID[i];
00461
00462 this_struct->dataELM[j] = this_struct->dataAnyGrid[this_struct->step][ii];
00463 }
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475 this_struct->step = this_struct->step + 1;
00476 this_struct->day = this_struct->day + 1;
00477 }
00478
00479
00480
00481
00482
00483
00484
00485
00524 void mapELM2Grid_io(applicationStruct *this_struct) {
00525
00526 int ii=0, tmp_id=0, jstart=0, jend=0, diff=0;
00527 int this_index=0, kk=0,j=0, jj=0;
00528
00529 for(ii=0; ii<OGrow_tot ;ii++) {
00530 jstart = *(this_struct->grid.config.xstart+ii);
00531 jend = *(this_struct->grid.config.xend+ii);
00532 tmp_id = ii * OGcol_tot + jstart;
00533 diff = jend - jstart + 1;
00534
00535 this_index = tmp_id - 1;
00536
00537 for(kk=0;kk<jstart-1;kk++){
00538 j=ii*OGcol_tot+kk;
00539
00540 if(elm_OG_map[j].elmID[0] == NOT_ELM_NOT_GRIO)
00541 elm_OG_map[j].index = NOT_ELM_NOT_GRIO;
00542 else
00543 elm_OG_map[j].index = IN_ELM_NOT_GRIO;
00544 }
00545
00546 for(kk=0;kk<diff;++kk) {
00547 if(elm_OG_map[this_index+kk].OGID == tmp_id+kk) {
00548 if(elm_OG_map[this_index+kk].elmID[0]==NOT_ELM_NOT_GRIO) {
00549 elm_OG_map[this_index+kk].index = NOT_ELM_IN_GRIO;
00550 }
00551
00552 }
00553
00554 }
00555
00556 for(kk=jend;kk<OGcol_tot;kk++) {
00557 if(elm_OG_map[ii*OGcol_tot+kk].elmID[0] == NOT_ELM_NOT_GRIO)
00558 elm_OG_map[ii*OGcol_tot+kk].index = NOT_ELM_NOT_GRIO;
00559 else
00560 elm_OG_map[ii*OGcol_tot+kk].index = IN_ELM_NOT_GRIO;
00561 }
00562 }
00563 }
00564
00565
00566
00567
00568
00569
00570 void printGridMap() {
00571
00572 int i,j;
00573
00574 printf("\ngridmap ==> Printing GridMap .......\n");
00575 printf("GRID_ID GRID_ROW GRID_COL INDEX ELM_ID ELM_CNT\n");
00576 printf("-----------------------------------------------\n");
00577 for(i=0;i<OGrow_tot*OGcol_tot;i++) {
00578 printf("%d\t%d\t%d\t%d\t%d\t%d\n",elm_OG_map[i].OGID,elm_OG_map[i].OGROW,
00579 elm_OG_map[i].OGCOL,elm_OG_map[i].index,elm_OG_map[i].elmID[0],elm_OG_map[i].eidCnt);
00580 for(j=1;j<elm_OG_map[i].eidCnt;j++)
00581 printf("\t\t\t\t%d\n",elm_OG_map[i].elmID[j]);
00582 }
00583 }
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643 void cleanUp (float** dataAnyGrid, float* dataELM) {
00644 int i;
00645
00646 printf("\ngridmap ==> Cleaning up !\n");
00647 for(i=0;i<OGrow_tot*OGcol_tot;i++) {
00648 if(elm_OG_map[i].elmID != NULL)
00649 free(elm_OG_map[i].elmID);
00650 }
00651 if(elm_OG_map != NULL)
00652 free(elm_OG_map);
00653
00654 if(dataELM != NULL)
00655 free(dataELM);
00656
00657 for(i=0;i<gridio_batch_len;i++) {
00658 if(dataAnyGrid[i] != NULL)
00659 free(dataAnyGrid[i]);
00660 }
00661 if(dataAnyGrid != NULL)
00662 free(dataAnyGrid);
00663
00664
00665
00666 }
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715