Main Page   Compound List   File List   Compound Members   File Members  

FireConfig.c

Go to the documentation of this file.
00001 
00025 #include "FireConfig.h"
00026             
00027 int InitPropsFromFireConfig(ChHashTable ** proptbl, char * cfg_fname)   {
00028     FILE * fstream  = NULL;
00029     fpos_t begin_file_pos;
00030     int i;
00031     char * val      = NULL; 
00032 
00033     if ( cfg_fname == NULL )    {
00034         ERR_ERROR("Must supply the name of configuration file to retrieve config values. \n", ERR_EINVAL);
00035         }
00036     if ( (fstream = fopen(cfg_fname, "r")) == NULL )    {
00037         ERR_ERROR("Unable to open configuration file. \n", ERR_EIOFAIL);
00038         }
00039     if ( (*proptbl = InitChHashTable(FIRE_CONFIG_TBL_INI_CAPCTY, UniversalStringHashFunc,
00040                                         MatchStringKeyVal, FreeKeyVal)) == NULL )   {
00041         ERR_ERROR("Unable to initialize Hash Table of simulation properties. \n", ERR_EFAILED);
00042         }
00043 
00044     /* retrieve all properties from cfg file and place in hash table */
00045     for(i = 0; i < PROP_UP_BOUND; i++)  {
00046         fgetpos(fstream, &begin_file_pos);
00047         if ( (val = GetValFromKeyStringFStreamIO(fstream, GetFireProp((EnumFireProp)i), 
00048                         FIRE_CONFIG_SEPARATOR_CHARS, FIRE_CONFIG_COMMENT_CHAR)) == NULL )   {
00049             /* insert a key with val string of NULL */
00050             ChHashTableInsert(*proptbl, GetFireProp((EnumFireProp)i), 
00051                             InitKeyVal(GetFireProp((EnumFireProp)i), GetFireVal(VAL_NULL)));
00052             }
00053         else    {
00054             /*insert the key and val */
00055             ChHashTableInsert(*proptbl, GetFireProp((EnumFireProp)i), InitKeyVal(GetFireProp((EnumFireProp)i), val));
00056             }
00057         fsetpos(fstream, &begin_file_pos);
00058         }
00059         
00060     fclose(fstream);
00061         
00062     return ERR_SUCCESS;
00063     }
00064 
00065 int InitGridsFromPropsFireConfig(ChHashTable * proptbl, GridData ** elev, GridData ** slope, GridData ** aspect)    {
00066     if ( proptbl == NULL )  {
00067         ERR_ERROR("Unable to initialize GridData with unitialized property Hash Table. \n", ERR_EINVAL);
00068         }   
00069 
00070     /* retrieve elev data */
00071     *elev = GetGridDataFromPropsFireGridData(proptbl, FIRE_GRIDDATA_ELEV_DATA);
00072 
00073     /* retrieve slope data */
00074     *slope = GetGridDataFromPropsFireGridData(proptbl, FIRE_GRIDDATA_SLOPE_DATA);
00075     
00076     /* retrieve aspect data */
00077     *aspect = GetGridDataFromPropsFireGridData(proptbl, FIRE_GRIDDATA_ASPECT_DATA);
00078     
00079     if ( *elev == NULL || *slope == NULL || *aspect == NULL )   {
00080         ERR_ERROR("Unable to initialize one of the GridData structures specified in config file. \n", ERR_EFAILED);
00081         }   
00082     
00083     if ( !IsGridDataMatchingFireGridData(*elev, *slope) || !IsGridDataMatchingFireGridData(*slope, *aspect) )   {
00084         ERR_ERROR("GridData structures specified in config file have mismatched extent or cell dimensions. \n", ERR_EFAILED);
00085         }
00086     
00087     return ERR_SUCCESS;                                 
00088     }
00089 
00090 int InitFireTimerFromPropsFireConfig(ChHashTable * proptbl, FireTimer ** ft)    {
00091     /* store values used to construct FireTimer: styr, stmo, stdy, sthr, edyr, edmo, eddy, edhr */
00092     int styr, stmo, stdy, sthr, edyr, edmo, eddy, edhr;
00093     KeyVal * entry  = NULL; 
00094         
00095     styr = stmo = stdy = sthr = edyr = edmo = eddy = edhr = 0;
00096     
00097     if ( proptbl == NULL )  {
00098         ERR_ERROR("Unable to initialize FireTimer with uninitialized property Hash Table. \n", ERR_EINVAL);
00099         }
00100 
00101     /* simulation start properties */
00102     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTYR), (void *)&entry) )  {
00103         ERR_ERROR("Unable to retrieve simulation start year from property HashTable. \n", ERR_EFAILED);
00104         }
00105     styr = atoi(entry->val);
00106     
00107     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTMO), (void *)&entry) )  {
00108         ERR_ERROR("Unable to retrieve simulation start month from property HashTable. \n", ERR_EFAILED);
00109         }
00110     stmo = atoi(entry->val);    
00111 
00112     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTDY), (void *)&entry) )  {
00113         ERR_ERROR("Unable to retrieve simulation start day from property HashTable. \n", ERR_EFAILED);
00114         }
00115     stdy = atoi(entry->val);    
00116 
00117     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTHR), (void *)&entry) )  {
00118         ERR_ERROR("Unable to retrieve simulation start hour from property HashTable. \n", ERR_EFAILED);
00119         }
00120     sthr = atoi(entry->val);    
00121 
00122     /* simulation end properties */
00123     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDYR), (void *)&entry) )  {
00124         ERR_ERROR("Unable to retrieve simulation end year from property HashTable. \n", ERR_EFAILED);
00125         }
00126     edyr = atoi(entry->val);
00127     
00128     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDMO), (void *)&entry) )  {
00129         ERR_ERROR("Unable to retrieve simulation end month from property HashTable. \n", ERR_EFAILED);
00130         }
00131     edmo = atoi(entry->val);    
00132 
00133     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDDY), (void *)&entry) )  {
00134         ERR_ERROR("Unable to retrieve simulation end day from property HashTable. \n", ERR_EFAILED);
00135         }
00136     eddy = atoi(entry->val);    
00137 
00138     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDHR), (void *)&entry) )  {
00139         ERR_ERROR("Unable to retrieve simulation end hour from property HashTable. \n", ERR_EFAILED);
00140         }
00141     edhr = atoi(entry->val);
00142     
00143     /* initialize the FireTimer */
00144     if ( (*ft = InitFireTimer(styr, stmo, stdy, sthr, edyr, edmo, eddy, edhr)) == NULL )    {
00145         ERR_ERROR("Unable to initialize FireTimer from config file. \n", ERR_EFAILED);
00146         }
00147 
00148     return ERR_SUCCESS; 
00149     }
00150     
00151 int InitFuelModelHashTableFromFuelModelListFireConfig(List * fmlist, ChHashTable ** fmtble) {
00152     ListElmt * lel          = NULL;
00153     FuelModel * fm          = NULL;
00154     
00155     if ( fmlist == NULL )   {
00156         ERR_ERROR("Unable to initialize fuel model Hash Table with empty FuelModel list. \n", ERR_EINVAL);
00157         }
00158         
00159     /* construct an instance of a ChHashTable backed by FuelModel list */
00160     if ( (*fmtble = InitChHashTable(LIST_SIZE(fmlist), SimpleDivisionIntHashFunc, 
00161                                 CmpNumToFuelModelNum, NULL)) == NULL )  {
00162         ERR_ERROR("Unable to initialize fuel model Hash Table. \n", ERR_EFAILED);
00163         }
00164     
00165     /* insert FuelModels one-by-one in Hash Table */    
00166     lel = LIST_HEAD(fmlist);
00167     while( lel != NULL )    {
00168         fm = LIST_GET_DATA(lel);
00169         if ( fm != NULL )   {
00170             ChHashTableInsert(*fmtble, &(fm->model_num), fm);
00171             }       
00172         lel = LIST_GET_NEXT_ELMT(lel);
00173         } 
00174                 
00175     return ERR_SUCCESS;
00176     }
00177 
00178 int InitStandAgeFromPropsFireConfig(ChHashTable * proptbl, GridData * elev, GridData ** std_age)    {
00179     IntTwoDArray * agearr   = NULL;             /* temporary stand age array used to initialize GridData */
00180     KeyVal * entry          = NULL;             /* key/val instances from table */
00181     int domain_rows         = NULL;             /* size of domain rows */
00182     int domain_cols         = NULL;             /* size of domain columns */
00183     double edata;
00184     int i, j;   
00185     int fixed_age;
00186         
00187     if ( proptbl == NULL || elev == NULL )  {
00188         ERR_ERROR("Arguments supplied to initialize stand age invalid. \n", ERR_EINVAL);
00189         }
00190         
00191     /* retrieve stand age type */
00192     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SAGETYP), (void *)&entry) )  {
00193         ERR_ERROR("Unable to retrieve properties for Stand Age. \n", ERR_EFAILED);
00194         }
00195 
00196     if  ( strcmp(entry->val, GetFireVal(VAL_SPATIAL)) == 0) {
00197         /* initialize stand age from grid */
00198         *std_age = GetGridDataFromPropsFireGridData(proptbl, FIRE_GRIDDATA_STD_AGE_DATA);
00199         }       
00200     else if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0)   {
00201         /* initialize stand age grid to a fixed value */
00202         if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SAGEFIX), (void *)&entry) )  {
00203             ERR_ERROR("Unable to retrieve properties for Stand Age. \n", ERR_EFAILED);
00204             }
00205         fixed_age = atoi((char *) entry->val);
00206 
00207         /* get dimensions for stand age from elev */    
00208         domain_rows = elev->ghdr->nrows; 
00209         domain_cols = elev->ghdr->ncols;
00210                     
00211         /* create temp array to initialize stand age GridData */
00212         if ( (agearr = InitIntTwoDArraySizeEmpty(domain_rows, domain_cols)) == NULL )   {
00213             ERR_ERROR("Unable to allocate memory for stand age TwoDArray. \n", ERR_ENOMEM);
00214             }
00215             
00216         /* populate temp array with fixed_age, set no data same as elev data */
00217         for(i = 0; i < domain_rows; i++)    {
00218             for(j = 0; j < domain_cols; j++)    {
00219                 GRID_DATA_GET_DATA(elev, i, j, edata);
00220                 if ( ((int)edata) == elev->ghdr->NODATA_value ) {
00221                     INTTWODARRAY_SET_DATA(agearr, i, j, elev->ghdr->NODATA_value);
00222                     }
00223                 else    {
00224                     INTTWODARRAY_SET_DATA(agearr, i, j, fixed_age);
00225                     }
00226                 }
00227             }
00228             
00229         /* initialize the stand age GridData from array */
00230         if ( (*std_age = InitGridDataFromIntTwoDArray(agearr, elev->ghdr->xllcorner, elev->ghdr->yllcorner, 
00231                                 elev->ghdr->cellsize, elev->ghdr->NODATA_value)) == NULL )  {
00232             FreeIntTwoDArray(agearr);
00233             ERR_ERROR("Unable to allocate memory for stand age GridData. \n", ERR_ENOMEM);
00234             }
00235         
00236         /* free the temp array */
00237         FreeIntTwoDArray(agearr);           
00238         }
00239     else    {
00240         /* invalid entry */
00241         ERR_ERROR("Invalid keyword specified for STAND_AGE_TYPE. \n", ERR_EFAILED);
00242         } 
00243 
00244     if ( *std_age == NULL ) {
00245         ERR_ERROR("Unable to initialize Stand Age grid. \n", ERR_EFAILED);
00246         }
00247     
00248     return ERR_SUCCESS;
00249     }
00250 
00251 int InitFireEnvFromPropsFireConfig(ChHashTable * proptbl, FireEnv ** fe)        {
00252     KeyVal * entry  = NULL;
00253 
00254     if ( proptbl == NULL || (*fe = InitFireEnv()) == NULL ) {
00255         ERR_ERROR("Unable to initialize FireEnv from properties HashTable. \n", ERR_EINVAL);
00256         }
00257         
00258     /* determine fuels regrowth type and set the appropriate function pointer in FireEnv */
00259     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_RGRTYP), (void *)&entry) )   {
00260         ERR_ERROR("Unable to retrieve FUELS_REGROWTH_TYPE property for FireEnv. \n", ERR_EFAILED);
00261         }
00262     if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0)    {
00263         (*fe)->GetFuelsRegrowthFromProps = GetFuelsRegrowthFIXEDFromProps; 
00264         }
00265     else if ( strcmp(entry->val, GetFireVal(VAL_STATIC)) == 0)  {
00266         (*fe)->GetFuelsRegrowthFromProps = GetFuelsRegrowthSTATICFromProps;
00267         }
00268     else if ( strcmp(entry->val, GetFireVal(VAL_PNV)) == 0) {
00269         (*fe)->GetFuelsRegrowthFromProps = GetFuelsRegrowthPNVFromProps;
00270         }
00271     else    {
00272         FreeFireEnv(*fe);
00273         ERR_ERROR("Unable to retrieve FUELS_REGROWTH_TYPE property for FireEnv. \n", ERR_EFAILED);  
00274         }
00275         
00276     /* determine ignition type and set the appropriate function pointer in FireEnv */
00277     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_IGTYP), (void *)&entry) )    {
00278         ERR_ERROR("Unable to retrieve IGNITION_TYPE property for FireEnv. \n", ERR_EFAILED);
00279         }
00280     if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0)    {
00281         (*fe)->IsIgnitionNowFromProps  = IsIgnitionNowFIXEDFromProps;
00282         (*fe)->GetIgnitionLocFromProps     = GetIgnitionLocFIXEDFromProps; 
00283         }
00284     else if ( strcmp(entry->val, GetFireVal(VAL_RANDU)) == 0)   {
00285         (*fe)->IsIgnitionNowFromProps  = IsIgnitionNowRANDFromProps;
00286         (*fe)->GetIgnitionLocFromProps     = GetIgnitionLocRANDUFromProps; 
00287         }
00288     else if ( strcmp(entry->val, GetFireVal(VAL_RANDS)) == 0)   {
00289         (*fe)->IsIgnitionNowFromProps  = IsIgnitionNowRANDFromProps;   
00290         (*fe)->GetIgnitionLocFromProps     = GetIgnitionLocRANDSFromProps; 
00291         }
00292     else    {
00293         FreeFireEnv(*fe);
00294         ERR_ERROR("Unable to retrieve IGNITION_TYPE property for FireEnv. \n", ERR_EFAILED);    
00295         }
00296     
00297     /* determine wind azimuth type and set the appropriate function pointer in FireEnv */
00298     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_WAZTYP), (void *)&entry) )   {
00299         ERR_ERROR("Unable to retrieve WIND_AZIMUTH_TYPE property for FireEnv. \n", ERR_EFAILED);
00300         }
00301     if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0)    {
00302         (*fe)->GetWindAzimuthFromProps     = GetWindAzimuthFIXEDFromProps;
00303         }
00304     else if ( strcmp(entry->val, GetFireVal(VAL_RANDU)) == 0)   {
00305         (*fe)->GetWindAzimuthFromProps     = GetWindAzimuthRANDUFromProps;
00306         }
00307     else if ( strcmp(entry->val, GetFireVal(VAL_RANDH)) == 0)   {
00308         (*fe)->GetWindAzimuthFromProps     = GetWindAzimuthRANDHFromProps; 
00309         }       
00310     else if ( strcmp(entry->val, GetFireVal(VAL_SPATIAL)) == 0) {
00311         (*fe)->GetWindAzimuthFromProps     = GetWindAzimuthSPATIALFromProps;
00312         }
00313     else    {
00314         FreeFireEnv(*fe);
00315         ERR_ERROR("Unable to retrieve WIND_AZIMUTH_TYPE property for FireEnv. \n", ERR_EFAILED);    
00316         }
00317         
00318     /* determine wind speed type and set the appropriate function pointer in FireEnv */
00319     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_WSPDTYP), (void *)&entry) )  {
00320         ERR_ERROR("Unable to retrieve WIND_SPEED_TYPE property for FireEnv. \n", ERR_EFAILED);
00321         }
00322     if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0)    {
00323         (*fe)->GetWindSpeedMpsFromProps = GetWindSpeedMpsFIXEDFromProps;
00324         }
00325     else if ( strcmp(entry->val, GetFireVal(VAL_RANDU)) == 0)   {
00326         (*fe)->GetWindSpeedMpsFromProps = GetWindSpeedMpsRANDUFromProps;
00327         }
00328     else if ( strcmp(entry->val, GetFireVal(VAL_RANDH)) == 0)   {
00329         (*fe)->GetWindSpeedMpsFromProps = GetWindSpeedMpsRANDHFromProps;   
00330         }       
00331     else if ( strcmp(entry->val, GetFireVal(VAL_SPATIAL)) == 0) {
00332         (*fe)->GetWindSpeedMpsFromProps = GetWindSpeedMpsSPATIALFromProps;
00333         }
00334     else    {
00335         FreeFireEnv(*fe);
00336         ERR_ERROR("Unable to retrieve WIND_SPEED_TYPE property for FireEnv. \n", ERR_EFAILED);  
00337         }
00338 
00339     /* determine dead fuel moisture type and set the appropriate function pointer in FireEnv */
00340     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_DFMTYP), (void *)&entry) )   {
00341         ERR_ERROR("Unable to retrieve DEAD_FUEL_MOIST_TYPE property for FireEnv. \n", ERR_EFAILED);
00342         }
00343     if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0)    {
00344         (*fe)->GetDeadFuelMoistFromProps = GetDeadFuelMoistFIXEDFromProps;
00345         }
00346     else if ( strcmp(entry->val, GetFireVal(VAL_RANDH)) == 0)   {
00347         (*fe)->GetDeadFuelMoistFromProps = GetDeadFuelMoistRANDHFromProps; 
00348         }       
00349     else if ( strcmp(entry->val, GetFireVal(VAL_SPATIAL)) == 0) {
00350         (*fe)->GetDeadFuelMoistFromProps = GetDeadFuelMoistSPATIALFromProps;
00351         }
00352     else    {
00353         FreeFireEnv(*fe);
00354         ERR_ERROR("Unable to retrieve DEAD_FUEL_MOIST_TYPE property for FireEnv. \n", ERR_EFAILED); 
00355         }
00356 
00357     /* determine live fuel moisture type and set the appropriate function pointer in FireEnv */
00358     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_LFMTYP), (void *)&entry) )   {
00359         ERR_ERROR("Unable to retrieve LIVE_FUEL_MOIST_TYPE property for FireEnv. \n", ERR_EFAILED);
00360         }
00361     if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0)    {
00362         (*fe)->GetLiveFuelMoistFromProps = GetLiveFuelMoistFIXEDFromProps;
00363         }
00364     else if ( strcmp(entry->val, GetFireVal(VAL_RANDH)) == 0)   {
00365         (*fe)->GetLiveFuelMoistFromProps = GetLiveFuelMoistRANDHFromProps; 
00366         }       
00367     else if ( strcmp(entry->val, GetFireVal(VAL_SPATIAL)) == 0) {
00368         (*fe)->GetLiveFuelMoistFromProps = GetLiveFuelMoistSPATIALFromProps;
00369         }
00370     else    {
00371         FreeFireEnv(*fe);
00372         ERR_ERROR("Unable to retrieve LIVE_FUEL_MOIST_TYPE property for FireEnv. \n", ERR_EFAILED); 
00373         }
00374                     
00375     /* determine Santa Ana type and set the appropriate function pointer in FireEnv */
00376     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SANAFREQ), (void *)&entry) ) {
00377         ERR_ERROR("Unable to retrieve SANTA_ANA_FREQUENCY_PER_YEAR property for FireEnv. \n", ERR_EFAILED);
00378         }
00379     (*fe)->IsSantaAnaNowFromProps = IsSantaAnaNowFromProps;
00380     (*fe)->GetSantaAnaEnvFromProps = GetSantaAnaEnvFromProps;
00381                         
00382     return ERR_SUCCESS;
00383     }
00384 
00385 int InitRandNumGenFromPropsFireConfig(ChHashTable * proptbl, void(*RandInit)(long int seed))    {
00386     long int seed = 0;
00387     KeyVal * entry  = NULL; 
00388     
00389     /* check args */
00390     if ( (proptbl == NULL) || (RandInit == NULL) )  {
00391         ERR_ERROR("Unable to initialize Random Number Generator from properties HashTable. \n", ERR_EINVAL);
00392         }
00393 
00394     /* retrieve random number seed type */      
00395     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMRNGSD), (void *)&entry) ) {
00396         ERR_ERROR("Unable to retrieve SIMULATION_RAND_NUM_SEED property. \n", ERR_EFAILED);
00397         }
00398         
00399     if ( strcmp(entry->val, GetFireVal(VAL_TABLE)) == 0 )   {
00400         RandInit(GetSeedRandRecordRandSeedTable());
00401         }
00402     else    {
00403         seed = atol(entry->val);
00404         RandInit(seed);
00405         }
00406         
00407     return ERR_SUCCESS;
00408     }
00409         
00410 void FireConfigDumpPropsToStream(ChHashTable * proptbl, FILE * fstream) {
00411     int i;
00412     KeyVal * entry = NULL;
00413     if ( proptbl != NULL && fstream != NULL )   {
00414         fprintf(fstream, "\n...Fire Simulation Configuration Properties Dump...\n");
00415         for(i = 0; i < PROP_UP_BOUND; i++)  {       
00416             if ( ChHashTableRetrieve(proptbl, GetFireProp((EnumFireProp)i), (void *)&entry) )   {
00417                 fprintf(fstream,"%-35s \t <WARNING! NOT INITIALIZED> \n", GetFireProp((EnumFireProp)i));
00418                 }
00419             else    {
00420                 fprintf(fstream,"%-35s \t %-35s \n", GetFireProp((EnumFireProp)i), entry->val);
00421                 }
00422             }
00423         fprintf(fstream, "\n");         
00424         }
00425     else    {
00426         ERR_ERROR_CONTINUE("Property table or stream supplied to FireConfigDump not initialized. \n", ERR_EINVAL);
00427         }
00428     return;
00429     }
00430     
00431 void FireConfigDumpFuelModelListToStream(List * fmlist, FILE * fstream) {
00432     ListElmt * lel          = NULL;
00433     FuelModel * fm          = NULL;
00434     
00435     if ( fmlist != NULL && fstream != NULL )    {
00436         /* retrieve and print FuelModels one-by-one */  
00437         lel = LIST_HEAD(fmlist);
00438         while( lel != NULL )    {
00439             fm = LIST_GET_DATA(lel);
00440             if ( fm != NULL )   {
00441                 FuelModelDumpToStream(fm, stdout);
00442                 }       
00443             lel = LIST_GET_NEXT_ELMT(lel);
00444             } 
00445         }
00446     return; 
00447     }
00448         
00449 /* end of FireConfig.c */

Generated at Fri Jun 22 00:46:50 2001 for HFire by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000