Everglades Landscape Model (ELM) Home Page
Main Page | Data Structures | Directories | File List | Data Fields | Globals

Serial.c

Go to the documentation of this file.
00001 
00023 /* General notes on revisions to this source file. 
00024        Nov/Dec 2004 v2.3.2: documentation upgrade 
00025                 - Unused functions removed 
00026                 - Re-organized, clarified scopes, Doxygen tags added 
00027                 - Functionality same as v2.1/v2.2 
00028         
00029 */
00030 
00031 #include "serial.h"
00032 
00038 byte readMap( char* filename, void* data)
00039 {
00040   int gsize, nbytes; byte bsize;
00041   FILE* bFile;
00042   char binFileName[300];
00043   
00044   bsize = read_header(filename);
00045 
00046   sprintf(binFileName,"%s/%s/Data/Map_bin/%s",ModelPath,ProjName,filename);
00047 
00048   if((bFile = fopen(binFileName,"rb") ) == NULL ) { 
00049     fprintf(stderr,"Can't Open Mapfile: %s",binFileName); 
00050     exit(0); 
00051   }
00052   gsize = gbl_size[0] * gbl_size[1];
00053   nbytes = fread((char*)data, (int)bsize, gsize, bFile );
00054   if(debug) { 
00055     fprintf(dFile," %d of %d items Read for %s, size = %x bytes ",
00056             nbytes,gsize,filename,bsize); 
00057   }
00058   fclose(bFile);
00059   return bsize;
00060 }
00061 
00062 
00067 int read_header(char* filename)
00068 {
00069   FILE* file;
00070   char headerFileName[300];
00071   
00072   int rows, cols, i, Dsize=1, test, offset;
00073   char ch, dirCh;
00074   long int start;
00075         
00076   sprintf(headerFileName,"%s/%s/Data/Map_head/%s",ModelPath,ProjName,filename);
00077 
00078   file = fopen(headerFileName,"r");
00079   if(file==NULL) { 
00080     fprintf(stderr,"Unable to open header file %s.\n",headerFileName); 
00081     fflush(stderr); 
00082     exit(0);
00083   }
00084   else { 
00085     fprintf(dFile,"Successfully opened header file %s.\n",headerFileName);  
00086     fflush(dFile); 
00087   } 
00088   scan_forward(file,"cols:");
00089   fscanf(file,"%d",&cols);
00090   fprintf(dFile,"cols = %d\n",cols); 
00091   scan_forward(file,"rows:");
00092   fscanf(file,"%d",&rows);
00093   fprintf(dFile,"rows = %d\n",rows);
00094   fflush(dFile);
00095   if( !( rows == gbl_size[0] && cols == gbl_size[1] ) ) { 
00096     fprintf(stderr,"Error, Wrong map size: %s: (%d,%d) : (%d,%d)\n",
00097              headerFileName,rows,gbl_size[0],cols,gbl_size[1]); 
00098   }
00099   start = ftell(file);
00100   test = scan_forward(file,"format:");
00101   if(test < 1) { 
00102     clearerr(file); 
00103     fseek(file, start, 0); 
00104   }
00105   else {        
00106     fscanf(file,"%d",&Dsize);
00107     fprintf(dFile,"size = %d\n",Dsize);
00108     Dsize = Dsize + 1; /* using header generated by GRASS, the byte size is actually the value read from header plus 1 */
00109     if( Dsize < 1 || Dsize > 4 ) {  
00110       fprintf(stderr,"Error, Illegal map size: %s  (%d)\n",headerFileName,Dsize); 
00111       exit(0); 
00112     }
00113   }
00114   fclose(file);
00115   return Dsize;
00116 }
00117 
00125 void writeMap(char* filename, void* data, int bSize, unsigned char type, int index)
00126 {
00127   int gsize, istat;
00128   FILE* bFile; 
00129   
00130   if(type=='M') write_header(filename,bSize);
00131   
00132   if(type == 'H') {
00133 #if HDF
00134     if(index==0) { istat = DFR8putimage(filename,(char*)data,s1,s0,0); }
00135     else { istat = DFR8addimage(filename,(char*)data,s1,s0,0); }
00136     if(istat != 0) printf("\nerror writing HDF file: %s\n",filename);
00137 #endif
00138   } 
00139   else {
00140     if((bFile = fopen(filename,"wb") ) == NULL ) { 
00141       fprintf(stderr,"Can't Open Mapfile: %s",filename); 
00142       exit(0); 
00143     }
00144     gsize = gbl_size[0]*gbl_size[1]*bSize;
00145     fwrite((char*)data, gsize, 1, bFile );
00146     fclose(bFile);
00147   }
00148 }
00149 
00154 void  write_header(char* mapFileName,int size)
00155 {
00156   FILE* header;
00157   fprintf(dFile,"Writing Header: filen = %s\n",mapFileName); 
00158   fflush(dFile);
00159   header = fopen(mapFileName,"w");
00160   if(header==NULL) { 
00161     fprintf(stderr,"Can't open header file: %s\n",mapFileName); 
00162     fflush(stderr); exit(-1); 
00163   }
00164   fprintf(header,"ROWS=%d\nCOLUMNS=%d\nCELLSIZE=1", 
00165                   gbl_size[0], gbl_size[1]);
00166   fprintf(header,"\nUNITS=KM\nFORMAT=BIN\nSIZE=%d\nLOCATION=\"%s.bin\"\n",
00167                   size,mapFileName); /* prior to v2.3, had the file delimited by the fileDelimitChar */
00168   fclose(header);
00169   strcat(mapFileName,".bin");
00170 }
00171 
00172 
00173 
00182 float* readSeriesCol(char *filename, int format, int index, int* Npt, float* TStep, int col )
00183 {
00184     int line = 0, cread=1, itest, j, sLen = 372 ;
00185     FILE *cfile;
00186     char  ctest, cmark[373], ret = '\n';
00187     unsigned char cmove[373], cmv=0;
00188     char tfilename[200], date_read[20],ss[200];
00189     int yyyy,mm, dd;
00190     double Jdate_read; 
00191     float  *tvar, *nullvar, first[10];
00192 
00193     sprintf(tfilename,"%s%d.ts",filename,index);
00194     if(debug>4) fprintf(dFile,"\nReading file %s, col %d, index = %d\n",tfilename,col,index); fflush(dFile);
00195     cfile = fopen(tfilename,"r"); 
00196     if(cfile==NULL) {  
00197         if( index > 0 ) {  fprintf(stdout,"\nWARNING: Unable to open timeseries file %s, using %s0.ts\n",tfilename,filename); return 0; } 
00198         else { fprintf(stdout,"\nERROR: Unable to open timeseries file %s\n",tfilename); exit(-1); } 
00199     }
00200 
00201     if (format != 2) { fprintf(stderr,"ERROR: only using floats in read_timeseries: %d\n",format); exit(0); }
00202     
00203            sLen = PORnumday; /* read/determined in genericDriver */
00204             tvar = (float*) nalloc( (sLen+2)*sizeof(float), "timeseries temp" );
00205             scan_forward(cfile,"{END COMMENT}\n");
00206             fgets(ss,200,cfile); /* skip  the header line */
00207             
00208             do {
00209                 fscanf(cfile, "%d %d %d %f %f %f %f", &yyyy,&mm,&dd,&first[1],&first[2],&first[3],&first[4] ); 
00210         /* julian day, returns a double (args = month, day, year, hours, minutes, seconds) */
00211                 Jdate_read = julday(mm, dd, yyyy, 0, 0, 0); 
00212                 if (Jdate_read > Jdate_init) {printf (" \n***Error\nInit date in %s file > simulation start date.\n",tfilename); exit (-1);}
00213                 if (feof(cfile)) {
00214         printf (" \n***Error\nNon-matching starting date for %s meteorological time series\n",tfilename); 
00215                         exit (-1);
00216                 }
00217             } while (Jdate_read < Jdate_init);
00218             
00219             tvar[0] = first[col]; /* have now read the first data record, assign to array */
00220             if(debug>4) fprintf(dFile,"%d %d %d %f\n",yyyy,mm,dd,tvar[0]);
00221             for (line = 1; line<sLen; line++) {
00222             
00223             /* each case for reading a different column */
00224                 switch (col) {
00225                     case 1:
00226                         itest = fscanf(cfile,"%d %d %d %f %f %f %f",&yyyy,&mm,&dd, &(tvar[ line ]),&nullvar, &nullvar, &nullvar);
00227                         break;
00228                     case 2:
00229                         itest = fscanf(cfile,"%d %d %d %f %f %f %f",&yyyy,&mm,&dd,&nullvar, &(tvar[ line ]), &nullvar, &nullvar);
00230                         break;
00231                     case 3:
00232                         itest = fscanf(cfile,"%d %d %d %f %f %f %f",&yyyy,&mm,&dd,&nullvar, &nullvar, &(tvar[ line ]), &nullvar);
00233                         break;
00234                     case 4:
00235                         itest = fscanf(cfile,"%d %d %d %f %f %f %f",&yyyy,&mm,&dd,&nullvar, &nullvar, &nullvar, &(tvar[ line ]));
00236                         break;
00237                     default:
00238                         printf ("\nError in interpolation time series data.\n"); 
00239                         exit(-1);
00240                         break;
00241                 } /* end switch */
00242 
00243                 if(debug>4) fprintf(dFile,"%d %d %d %f\n",yyyy,mm,dd,tvar[ line ]);
00244                 if (feof(cfile)) {printf (" \n***Error\nExceeded number of records for %s meteorological time series\n",tfilename); exit (-1);}
00245             }
00246             tvar[ sLen ] = *TStep;
00247  
00248     if(debug>4) { fprintf(dFile,"\nDone Reading file %s\n",tfilename); fflush(dFile); }
00249     
00250     fflush(stdout);
00251     fclose(cfile);
00252     *Npt = sLen;
00253     return tvar;
00254 }
00255 
00256 
00257 
00258 
00268 float* get_hab_parm( char* s_parm_name, int s_parm_relval, char* parmName ) 
00269 {
00270         int hIndex, end=0;
00271         FILE* parmFile;
00272         float* fTmp;
00273         char lline[2001], *line;
00274         int pLoc = -1;
00275         int foundParm= False;
00276         char parmNameHead[30];
00277         char modelFileName[300];
00278     char HabParmFile[300];
00279     char *fileAppend;
00280     extern ProgAttr *ProgExec;
00281     
00282         
00283         if ( (fTmp = (float*) malloc( MAX_NHAB * sizeof(float) ) ) == NULL) {
00284         sprintf(msgStr, "Failed to allocate memory for HabParm array.\n ") ;
00285         usrErr(msgStr);
00286         exit( -2 ) ;
00287         };
00288         fTmp[0] = 0.0;
00289 
00290     fileAppend = match_Sparm( s_parm_name, s_parm_relval, parmName);
00291         sprintf(HabParmFile,"HabParms_%s",fileAppend);
00292         sprintf(modelFileName,"%s/%s/Data/%s",ModelPath,ProjName,HabParmFile);
00293         parmFile = fopen(modelFileName,"r");
00294         if(parmFile==NULL) {fprintf(stdout,"\nERROR: Unable to open dBase file %s\n",modelFileName); exit(-1); }
00295         
00296      fgets(lline, 2000, parmFile); /* skip 1st header line  */
00297      fgets(lline, 2000, parmFile); /* read 2nd header line with column names */
00298      line=lline;
00299 
00300      while (!foundParm)  
00301      {
00302         sscanf( line,"%s",&parmNameHead);
00303         if (strcmp(parmName,parmNameHead) ==0) foundParm = True;
00304         pLoc++;
00305         if (*line == EOS || *line == EOL) {
00306            sprintf(msgStr,"ERROR: Could not find header tag of %s in HabParms dbase file.", parmName); 
00307            usrErr(msgStr); 
00308            exit(-1);
00309         }
00310         line = Scip( line, TAB );
00311      }
00312     
00313     sprintf(msgStr,"Parameter group: %s",parmName); 
00314     fflush(dFile); 
00315     WriteMsg(msgStr,1); 
00316         for( hIndex = 1; hIndex < MAX_NHAB; hIndex++) {
00317                 goto_index (parmFile, gHabChar, hIndex); 
00318                 fTmp[hIndex] = get_Nth_parm( parmFile, pLoc, &end, hIndex );
00319                 if(end) break;
00320         }
00321         fclose(parmFile);
00322         habNumTot = hIndex-1;
00323 
00324         fflush(dFile); 
00325         /* TODO: this HARDCODES into the BIR (BIRavg) model output file ONLY, 
00326         the incorrect impression of running sensitivity on a single habitat ID 
00327         (hab 2 = sawgrass plain, ELMv2.4) */
00328     if (strcmp(s_parm_name,parmName) == 0) ProgExec->S_ParmVal = fTmp[2];
00329         return fTmp; 
00330 }
00331 
00339 char* match_Sparm( char* s_parm_name, int s_parm_relval, char* parmName) 
00340 {
00341     char *fileAppend;
00342  
00343     if (strcmp(s_parm_name,parmName) == 0) /* match, it is a parameter being varied in this set of runs */
00344         { 
00345                 switch (s_parm_relval) {
00346                    case 0: fileAppend = "NOM"; break;
00347                    case 1: fileAppend = "LO"; break;
00348                    case 2: fileAppend = "HI"; break;
00349                    default:  {
00350                       sprintf(msgStr,"ERROR: The relative parameter value (%d) is unknown for %s",s_parm_relval, s_parm_name); 
00351                       usrErr(msgStr);
00352                       exit(-1);
00353                    }
00354            }
00355         }
00356     else
00357         { /* no match, it is NOT a parameter being varied in this set of runs */
00358               fileAppend = "NOM"; 
00359             }
00360     return (fileAppend);
00361 }
00362 
00363 
00372 float get_global_parm( char* s_parm_name, int s_parm_relval, char* parmName) 
00373 {
00374   int test;
00375   char modelFileName[300];
00376   FILE *parmFile;
00377   float parmVal;
00378   char parmNameHead[30];
00379   extern ProgAttr *ProgExec;
00380 
00381 
00382   char GlobalParmFile[50];
00383   char *fileAppend;
00384   
00385   fileAppend = match_Sparm( s_parm_name, s_parm_relval, parmName);
00386   sprintf(GlobalParmFile,"GlobalParms_%s",fileAppend);
00387   sprintf(modelFileName,"%s/%s/Data/%s",ModelPath,ProjName,GlobalParmFile);
00388   
00389   parmFile = fopen(modelFileName,"r");
00390   if(parmFile==NULL) { 
00391         sprintf(msgStr,"ERROR, can't open data file %s",modelFileName); 
00392     usrErr(msgStr);
00393         Exit(0); 
00394   }
00395 
00396   sprintf(parmNameHead,"%s=", parmName);
00397   scan_forward(parmFile,parmNameHead);
00398   if ( (test = fscanf(parmFile,"%f",&parmVal) ) <= 0 ) { 
00399     sprintf(msgStr,"ERROR in reading %s from %s; see Driver0.out debug file.", parmName, modelFileName); 
00400     usrErr(msgStr);
00401     Exit(0); 
00402   }
00403   sprintf(msgStr,"%s %f\n",parmNameHead, parmVal); 
00404   WriteMsg(msgStr,1); 
00405 
00406   fclose(parmFile);
00407   if (strcmp(s_parm_name,parmName) == 0) ProgExec->S_ParmVal = parmVal;
00408   return parmVal;
00409 }
00410 
00431 void open_point_lists(SeriesParm *pSeries, int nSeries)
00432 {
00433   FILE *oFile;
00434   int i, j, ix, iy, k= 0 ;
00435 
00436   for (i=0; i<nSeries; i++) {
00437      if( pSeries[i].data == NULL ) {
00438         if ( numPtFiles>0 ) fclose(oFile); /* in (rare?) case where user asks for no point output, no files stream to close */
00439         return;
00440       }
00441       if (strcmp(pSeries[i].name,pSeries[i-1].name) != 0) {
00442               /* this is new variable, thus new file.  numPtFiles== increment counter of number of variables (one file per variable) used for output */
00443           if (i>0) { 
00444             numPtFiles++; 
00445             fclose (oFile); 
00446           }
00447           
00448           sprintf(msgStr,"%s%s/Output/PtSer/%s.pts",OutputPath,ProjName,pSeries[i].name);
00449           if( (oFile = fopen(msgStr,"w") ) == NULL) { 
00450               fprintf(stderr,"\nERROR, unable to open %s point time series output file.",msgStr); 
00451               exit(-1); 
00452           }
00453           fprintf (oFile, "%s %s %s %s scenario: \nDate\t", &modelName, &modelVers, &SimAlt, &SimModif);
00454           
00455       }
00456 /* populate the grid cell locations in the header for this variable/file */
00457       ix = pSeries[i].Loc.x;
00458       iy = pSeries[i].Loc.y;
00459       fprintf(oFile,"%s:(%d,%d)\t",pSeries[i].name,ix,iy);
00460       cell_pts[numPtFiles]++; /* count the number of cell point locs per variable (1 var per file) */
00461       
00462   }
00463 }
00464 
00465 
00474 void send_point_lists2(SeriesParm *pSeries, int nSeries)
00475 {
00476   FILE *oFile;
00477   int i=0, ii, j, ix, iy, k= 0, last_pt;
00478   int yr_pt[2],mo_pt[2],da_pt[2],hr_pt[2],mi_pt[2];    
00479   double se_pt[2];           
00480   double Jdate_pt;
00481   
00482   if (numPtFiles==0) return;
00483   /* k = file/variable counter; i = indiv cellLoc time series counter */
00484   for (k = 0; k <= numPtFiles; k++) { /* loop over the number of point ts files */
00485       sprintf(msgStr,"%s%s/Output/PtSer/%s.pts",OutputPath,ProjName,pSeries[i].name);
00486       if( (oFile = fopen(msgStr,"a") ) == NULL) { 
00487           fprintf(stderr,"\nERROR, unable to open %s point time series output file.",msgStr); 
00488           exit(-1); 
00489       }
00490 
00491       for(j=pSeries[i].laststep; j<pSeries[i].Length; j++ ) { /*temporal loop */
00492           Jdate_pt = Jdate_init+j*pSeries[i].outstep;
00493           calcdate( Jdate_pt, mo_pt, da_pt, yr_pt, hr_pt, mi_pt, se_pt); /* get the calendar date info from the julian date */
00494           fprintf(oFile,"\n%d/%d/%d\t",yr_pt[0],mo_pt[0],da_pt[0] ); /* calendar date in col 1 */
00495 
00496           last_pt = i+cell_pts[k]; /* # of last cellLoc point of the current variable's file */
00497           for (ii=i; ii<last_pt; ii++) {/* loop over number of point locations per file */
00498               fprintf(oFile,"%f\t",pSeries[ii].data[j]);
00499           }
00500       }
00501       fclose (oFile); 
00502       pSeries[i].laststep = j; /* remember the last temporal outstep for this file */
00503       i += cell_pts[k];     /* increment the indiv cellLoc time series counter */ 
00504   }
00505   return;
00506 }
00507 
00508 
00520 void writeSeries(void* fValue, char* label, char* desc, int N0, int N1, byte Mtype, byte format )
00521 {
00522         /* check on these! (who wrote this? Why? (HCF Dec'04) )*/
00523   int ix, iy; 
00524   static unsigned char first_write = 1;
00525   long int  ret, dimsizes[2];
00526   unsigned short int refnum;
00527   if(format == 'H') {
00528   
00529 #if HDF   
00530           dimsizes[0] = N0;
00531           dimsizes[1] = N1;
00532           sprintf(cTemp,"%s%s:Output:Windows.hdf",OutputPath,ProjName);
00533           DFSDclear();
00534           
00535           switch(Mtype) {
00536                  case 'f': 
00537                     DFSDsetNT(DFNT_FLOAT32);  break;
00538                  case 'L': 
00539                     DFSDsetNT(DFNT_FLOAT32);  break;
00540                  case 'E': 
00541                     DFSDsetNT(DFNT_FLOAT32);  break;
00542                  case 'd': case 'i': 
00543                     DFSDsetNT(DFNT_INT32);  break;
00544                  case 'c': 
00545                     DFSDsetNT(DFNT_UINT8);  break;
00546           }
00547           if(first_write) {
00548                 ret = DFSDputdata(cTemp,2,dimsizes,fValue);
00549                 first_write = 0;
00550           } else {
00551                 ret = DFSDadddata(cTemp,2,dimsizes,fValue);
00552           }
00553           HDF_VERIFY("DFSDadddata");
00554           refnum = DFSDlastref();
00555           ret = DFANputlabel(cTemp,DFTAG_SDG,refnum,label);
00556           HDF_VERIFY("DFANputlabel");
00557           ret = DFANputdesc(cTemp,DFTAG_SDG,refnum,desc,strlen(desc));
00558           HDF_VERIFY("DFANputdesc");
00559 #endif
00560   }
00561   else {
00562     fprintf(Driver_outfile,"\n_________%s\n",label);    
00563     fprintf(Driver_outfile,"%s\n",desc);        
00564     for (ix=0; ix<N0; ix++) {
00565       fprintf(Driver_outfile,"\n");
00566       for (iy=0; iy<N1; iy++) {
00567         switch(Mtype) {
00568           case 'f': 
00569                 if(format=='L')                 fprintf(Driver_outfile,"%f ",*(((float*)fValue)+iy+ix*N1)); 
00570                 else if(format=='E')    fprintf(Driver_outfile,"%.3E ",*(((float*)fValue)+iy+ix*N1)); 
00571                         else                            fprintf(Driver_outfile,"%.3f ",*(((float*)fValue)+iy+ix*N1));
00572                         break;
00573         case 'd': case 'i': 
00574                         fprintf(Driver_outfile,"%d ",*(((int*)fValue)+iy+ix*N1)); 
00575                         break;
00576         case 'c': 
00577                         fprintf(Driver_outfile,"%x ",*(((UCHAR*)fValue)+iy+ix*N1)); 
00578                         break;
00579         }
00580       }
00581     }  
00582   }
00583 }
00584 
00598 void Combine(float* fValue, char* label, int nComp, int cType, int step)
00599 {
00600         int print, cIndex, i, cum;
00601         static int type = 11;
00602         char tmpStr[50];
00603         if( ++type > 99) type = 11;
00604 
00605         cum = ((cType == kAVECUM) || (cType == kSUMCUM)) ? 1 : 0;       
00606         if(cum) { cIndex = getCombineIndex(label,step,cType,&print); }
00607         
00608         switch(cType) {
00609                 case kMAXMIN: 
00610                         sprintf(msgStr,"\nMAXMIN(%d) for %s:",step,label); 
00611                         for(i=0; i<nComp; i++) { sprintf(tmpStr," %f",fValue[i]); strcat(msgStr,tmpStr); }
00612                         WriteMsg(msgStr,1);
00613                 break;
00614                 case kMAX:
00615                         sprintf(msgStr,"\nMAX(%d) for %s:",step,label); 
00616                         for(i=0; i<nComp; i++) { sprintf(tmpStr," %f",fValue[i]); strcat(msgStr,tmpStr); }
00617                         WriteMsg(msgStr,1);
00618                 break;
00619                 case kMIN:
00620                         sprintf(msgStr,"\nMIN(%d) for %s:",step,label); 
00621                         for(i=0; i<nComp; i++) { sprintf(tmpStr," %f",fValue[i]); strcat(msgStr,tmpStr); }
00622                         WriteMsg(msgStr,1);
00623                 break;
00624                 case kSUM:
00625                         sprintf(msgStr,"\nSUM(%d) for %s:",step,label); 
00626                         for(i=0; i<nComp; i++) { sprintf(tmpStr," %f",fValue[i]); strcat(msgStr,tmpStr); }
00627                         WriteMsg(msgStr,1);
00628                 break;
00629                 case kAVE:
00630                         sprintf(msgStr,"\nAVE(%d) for %s:",step,label); 
00631                         for(i=0; i<nComp; i+=2) { sprintf(tmpStr," %f",fValue[i]/fValue[i+1]); strcat(msgStr,tmpStr); }
00632                         WriteMsg(msgStr,1);
00633                 break;
00634                 case kSUMCUM:
00635                         if( print == -1) {
00636                                 for(i=0; i<nComp; i++) ctable[cIndex].fvalue[i] = fValue[i]; 
00637                         
00638                         } else {
00639                                 for(i=0; i<nComp; i++) ctable[cIndex].fvalue[i] += fValue[i]; 
00640                         }
00641                         if(print==1) {                          
00642                                 sprintf(msgStr,"\nSUMCUM(%d) for %s:",step,label); 
00643                                 for(i=0; i<nComp; i++) { sprintf(tmpStr," %f",ctable[cIndex].fvalue[i]); strcat(msgStr,tmpStr); }
00644                                 WriteMsg(msgStr,1);
00645                         }
00646                 break;
00647                 case kAVECUM:
00648                         if( print == -1) {
00649                                 for(i=0; i<nComp; i++) ctable[cIndex].fvalue[i] = fValue[i]; 
00650                         
00651                         } else {
00652                                 for(i=0; i<nComp; i++) ctable[cIndex].fvalue[i] += fValue[i]; 
00653                         }
00654                         if(print==1) {                          
00655                                 sprintf(msgStr,"\nAVECUM(%d) for %s:",step,label); 
00656                                 for(i=0; i<nComp; i+=2) { sprintf(tmpStr," %f",ctable[cIndex].fvalue[i]/ctable[cIndex].fvalue[i+1]); strcat(msgStr,tmpStr); }
00657                                 WriteMsg(msgStr,1);
00658                         }
00659                 break;
00660         }
00661 }       
00662 
00675 int getCombineIndex( char* name, int step,int type,int *last)
00676 {
00677         int count, index = -1;
00678         *last = 0;
00679         
00680         for(count = 0; count < max_combos_open; count++) {
00681         
00682                 if( ctable[count].free )        {                       /* Determine if the slot is in use. */
00683                         if(index == -1) index = count;          /* Nope, keep track of first free global stream table slot. */ 
00684                         if(debug) { fprintf(dFile,"cnt = %d, index = %d, free combo stream\n",count,index); fflush(dFile); }
00685                 }
00686                 else if( strncmp(name,ctable[count].name,25) == 0 && ctable[count].type == type ) {
00687                     *last = 1; 
00688                     if(debug) { 
00689                       fprintf(dFile,"\n(%s)combo cnt = %d, step = %d, type = %d\n",
00690                               name,count,step,type); 
00691                         fflush(dFile); 
00692                     }
00693                     return count;
00694                 }
00695         }
00696         if( index == -1 ) { 
00697                 index = max_combos_open; 
00698                 if( ++max_combos_open > MAXCOMBOS) { sprintf(msgStr,"Out of combo slots: %s, step=%d, type=%d\n",name,step,type);  usrErr(msgStr); return -1; }
00699         }
00700         if(debug) { fprintf(dFile,"\n(%s)index = %d, step=%d, set-up combo\n",name,index,step); fflush(dFile); }
00701         strncpy(ctable[index].name,name,25);
00702         ctable[index].step = step;
00703         ctable[index].type = type;
00704         ctable[index].free = 0;
00705         *last = -1;
00706         return index;
00707 }
00720 void open_debug_outFile(int index)
00721 {
00722   char filename[120];
00723   
00724   sprintf(filename,"%s/%s/Output/Debug/Driver%d.out",OutputPath,ProjName,index);
00725 
00726   Driver_outfile = fopen(filename,"w");
00727   if(Driver_outfile == NULL) { 
00728         fprintf(stderr,"Error, unable to open %s file.\n",filename);
00729         fprintf(stderr,"OutputPath: %s\n",OutputPath);  
00730         fprintf(stderr,"Project: %s\n",ProjName);  
00731         exit(0); 
00732   }
00733 
00734 }
00735 
00747 int init_config_file(FILE *vpFile, char term1, char term2, char term3, char term4)
00748 {
00749   char test;
00750   int format,size=-1;
00751 
00752   gTerm[0] = term1;
00753   gTerm[1] = term2;  
00754   gTerm[2] = term3;  
00755   gTerm[3] = term4;  
00756   skip_white(vpFile);
00757   test = fgetc(vpFile);
00758   if(test == gTerm[0])
00759       fscanf(vpFile,"%d",&format);
00760   else
00761       format = -1;
00762   return format;
00763 }
00764 
00771 int skip_cnfg_space(FILE *vpFile, char* tch)
00772 {
00773         char ch; int rv = 1;
00774         ch = *tch;
00775         while( isspace(ch) ) { 
00776                 if( (ch=fgetc(vpFile)) == EOF || ch == gTerm[3] || ch == '\0' ) {
00777                         fclose(vpFile); cnfgFile = NULL; return -3; 
00778                 }
00779         }
00780         *tch = ch;
00781         return rv;
00782 }
00783 
00796 int parse_packet( FILE *vpFile, int* nArgs, char* test) 
00797 {
00798     static      int     gVPindex = -1;
00799     static      char    gCnfg;
00800     
00801         char ch = ' ', eChar = ' '; 
00802         int btype=0, argc=2, go=1, i=0, j=0, new_name = 0;
00803 
00804         if(vpFile == NULL) return -3;
00805         while (1) {
00806                 if ( skip_cnfg_space(vpFile, &ch) < 0) return -3; j=0;
00807                 if( ch == gTerm[1] || ch == gTerm[2] ) {
00808                     gCnfg = ch; ch = ' ';
00809                         if ( skip_cnfg_space(vpFile, &ch) < 0 ) return -3;
00810                         while ( !isspace(ch) ) { cTemp[j++] = ch; ch=fgetc(vpFile); }
00811                         cTemp[j++] = '\0'; new_name = 1;
00812                         gVPindex++;
00813                 }
00814                 else break;
00815         }
00816         strcpy(gCArg[0],cTemp);
00817         *test = gCnfg;
00818         while ( isalnum( ch ) ) { gCArg[1][i++]=ch; ch=fgetc(vpFile); }
00819         gCArg[1][i]='\0'; i=0;
00820         while( 1 ) {
00821                 if( ch == '(' ) { eChar = ')'; break;} 
00822                 else if( ch == '[' ) { eChar = ']'; break;} 
00823                 else if( ch == '{' ) { eChar = '}'; break;} 
00824                 else if( !isspace(ch) ) { return -3;}
00825                 ch=fgetc(vpFile);
00826         }
00827         while (go) {
00828                 while( ch=fgetc(vpFile) ) {
00829                         if( ch == ',' ) { argc++; break; }
00830                         if( ch == ')' ||  ch == '}' ||  ch == ']')  { 
00831                                 if( ch == eChar ) {  argc++; go=0; break;} 
00832                                 else { printf( "\nWarning: Syntax error in parse_config, var: %s\n",gCArg[0]); return -2; } 
00833                         }
00834                         gCArg[argc][i++]=ch;
00835                         if( i==(kCArgWidth-1) ) break;
00836                 }
00837                 if(i==0) { printf( "\nWarning: Syntax error in parse_config, var: %s\n",gCArg[0]); return -2; } 
00838                 else { gCArg[argc-1][i]='\0'; i=0; }
00839                 if(argc == kCArgDepth) { 
00840                         while( ch != eChar ) ch=fgetc(vpFile);  
00841                         go = 0; 
00842                 }
00843         }
00844         *nArgs = argc;
00845         if(argc==0) { printf( "\nWarning: Syntax error in parse_config, var: %s\n",gCArg[0]); return -2; }
00846         else return gVPindex;           
00847 } 
00848 
00849 
00855 int get_number( FILE *infile ) {
00856         char ch;
00857         int rv;
00858         
00859         ch = fgetc(infile);
00860         if( !isdigit( ch ) ) return(-1);
00861         rv = ch - '0';
00862         
00863         while( isdigit( ch = fgetc(infile) ) ) 
00864                 rv = rv*10 + ( ch - '0' );
00865                 
00866         return rv;
00867 }
00868 
00876 int goto_index ( FILE *infile, char tchar, int index) 
00877 {
00878         int rv=1, current_index=-1, itest;
00879 
00880         while(current_index != index) { 
00881                 itest = find_char (infile, tchar);
00882                 if(itest <= 0) return 0;
00883                 current_index = get_number(infile);
00884                 if(current_index < 0) fatal("Bad number format in dBase");
00885         }
00886         return 1;
00887 }
00888 
00900 float get_Nth_parm( FILE *infile, int pIndex, int* end, int hIndex ) 
00901 {
00902         int i=1, itest=1;
00903         float rv; char ch;
00904         
00905         while ( (i++ < pIndex) && itest > 0 ) { itest = find_char ( infile, '\t' ); }
00906         if( itest == 0 ) { *end = 1; return(0.0); }
00907         
00908         itest = fscanf(infile,"%f",&rv);
00909         if(itest==1) { 
00910         if(debug) { 
00911            sprintf(msgStr,"Habitat %d:\t%f",hIndex,rv); 
00912            WriteMsg(msgStr,1); 
00913         }
00914         }
00915         else if ( (ch = fgetc(infile)) == EOF ) { *end = 1; return(0.0); }
00916         else {
00917                 sprintf(msgStr,"Read Error in dBase(%d)\n REad Dump:\n",itest); WriteMsg(msgStr,1); usrErr(msgStr);
00918                 for(i=0; i<12; i++) { 
00919                         ch = fgetc(infile);
00920                         if(ch==EOF) { sprintf(msgStr,"\nAt EOF\n"); WriteMsg(msgStr,1); break; }
00921                         else fputc(ch,dFile);
00922                 }
00923                 exit(0);
00924         }
00925         *end = 0;
00926         return(rv);
00927 }
00928 
00929 
00931 float SMDRAND(float fminVal, float fmaxVal )
00932 { 
00933   float rv, tmp;
00934   tmp = rand();
00935   rv = fminVal + (fmaxVal-fminVal)*(tmp/RAND_MAX); 
00936   return(rv);
00937 } 
00938 
00939 
00950 void local_setup(int argc, char** argv)
00951 {
00952   int i;
00953   char debugfileName[300];
00954   nprocs[0] = 1;
00955   nprocs[1] = 1;
00956   tramType =  0;
00957   procnum = 1;
00958   recpnum[0] = 0;
00959   recpnum[1] = 0;
00960   Lprocnum = 1;
00961   /* the combo_table (ctable) used in Combine operation for spatial array summaries (debug-related) */ 
00962   max_combos_open = 0;  
00963   for(i = 0; i < MAXCOMBOS; i++) {
00964     ctable[i].ocnt = 0; 
00965     ctable[i].free = 1;
00966   }
00967 
00968   /* this debug output file is not used extensively in current code - it is opened before we get
00969      environment vars, thus is written to user's current directory ($ModelPath/$ProjName/Load if
00970      ELM is executed from the "go" script) */ 
00971  dFile = fopen("ELM.debug","w");
00972   if(dFile == NULL) { 
00973     usrErr("Can't open ELM.debug file."); 
00974     exit(0); 
00975   } 
00976   /* there are no random processes in current ELM (v2.4) w/o fire */ 
00977   srand(seed);
00978   fprintf(dFile," RAND_MAX = %d\n",(int)RAND_MAX);
00979 }
00980 
00981 /*****/
00982 /* begin parallel, effectively unused, code (ex== EXpress parallel compiler compatibility) */
00983 /*****/
00984 
00986 void exparam( struct nodenv* envInfo)
00987 {       
00988         envInfo->procnum = 1;
00989         envInfo->nprocs = 1;
00990         envInfo->groupid = 0;
00991         envInfo->taskid = 0;
00992  
00993 }
00994 
00996 int  exgridinit(int dim, int* nprocs)   
00997 { return 0;}
00998 
01000 void exgridsplit(int nprocs, int ndim,int nprocs2[2])
01001 {
01002   nprocs2[0] = 1;
01003   nprocs2[1] = 1;
01004 }
01005 
01007 void exgridcoord(int pnum, int rnum[2])
01008 {
01009     tramNum[0] = rnum[0] = 0;
01010     tramNum[1] = rnum[1] = 0;
01011 }
01012 
01014 void exgridsize( int pnum, int gsize[2],int lsize[2],int lstart[2])
01015 {
01016         int rem[2], i, j;
01017         for( i=0; i<2; i++ ) {
01018                 lsize[i] = gsize[i]/nprocs[i];
01019                 rem[i] = gsize[i] - lsize[i]*nprocs[i];
01020                 if(recpnum[i]<rem[i]) lsize[i]++;
01021                 for(j=0; j<recpnum[i]; j++) if( j<rem[i] && recpnum[i] >= rem[i] ) lstart[i] += (lsize[i]+1); else lstart[i] += lsize[i];
01022         }
01023 }
01024 
01026 void set_async_mode(FILE* file) 
01027 { return;}
01028 
01030 void fmulti(FILE* file) 
01031 { return;}
01032 
01034 void fsingl(FILE* file) 
01035 { return;}
01036 
01038 void fasync(FILE* file) 
01039 { return;}
01040 
01041 
01043 void exchange_borders(UCHAR* map, int size) 
01044 {return;}
01045 
01047 int on_this_proc(int x,int y)
01048 { return 1; }
01049 
01051 void Cplot(VOIDP Map, unsigned char Mtype, float max_value, float min_value) 
01052 {}
01053 
01055 void broadcastMsg( UCHAR* msgPtr)
01056 {}
01057 
01059 void broadcastInt(int* iValPtr )
01060 {}
01061 
01063 void broadcastChar( UCHAR* cPtr)
01064 {}
01065 
01067 void broadcastData( void* dataPtr, int *dataSize)
01068 {}
01069 
01071 void sync_processors() 
01072 {return;}
01073 
01075 void broadcastFloat(void* dataPtr)
01076 {}
01077 
01078 /*****/
01079 /* end parallel, effectively unused, code (ex== EXpress parallel compiler compatibility) */
01080 /*****/
01081 
01082 /****************************************************/

Generated on Thu Jul 6 11:17:48 2006 for ELM source code by  doxygen 1.3.9.1