Main Page   Compound List   File List   Compound Members   File Members  

Ignition.c

Go to the documentation of this file.
00001 
00025 #include "Ignition.h"
00026     
00027 int IsIgnitionNowFIXEDFromProps(ChHashTable * proptbl)  {
00028     KeyVal * entry          = NULL;             /* key/val instances from table */
00029     static int fixed_ig_occured = 0;
00030     
00031     /* check args and retrieve frequency per day */
00032     if ( proptbl == NULL || ChHashTableRetrieve(proptbl, GetFireProp(PROP_IGTYP), (void *)&entry) ) {
00033         ERR_ERROR_CONTINUE("Unable to retrieve IGNITION_TYPE property. \n", ERR_EINVAL);
00034         return 0;
00035         }
00036         
00037     /* test if FIXED ignition specified */
00038     if ( strcmp(entry->val, GetFireVal(VAL_FIXED)) == 0 && fixed_ig_occured == 0)   {
00039         fixed_ig_occured = 1;
00040         return 1;   
00041         }
00042 
00043     return 0;
00044     }
00045 
00046 int IsIgnitionNowRANDFromProps(ChHashTable * proptbl)   {
00047     KeyVal * entry                  = NULL;             /* key/val instances from table */
00048     int st_mo, st_dy, end_mo, end_dy;                   /* start and end month and day of simulation */     
00049     double ig_freq_yr, timestep;                        /* ig freq and length of sim timestep */
00050     /* static variables used to store state across function calls */    
00051     static double sprob_ig_ts       = -1.0;             /* ignition probability per timestep */ 
00052     
00053     /* check args */
00054     if ( proptbl == NULL )  {
00055         ERR_ERROR_CONTINUE("Arguments supplied to determine Ignition Frequency invalid. \n", ERR_EINVAL);
00056         return 0;
00057         }
00058 
00059     /* retrieve frequency per year */       
00060     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_IGFREQYR), (void *)&entry) ) {
00061         ERR_ERROR_CONTINUE("Unable to retrieve IGNITION_FREQUENCY_PER_YEAR property. \n", ERR_EINVAL);
00062         return 0;
00063         }
00064     ig_freq_yr = atof((char *) entry->val); 
00065     
00066     if ( UNITS_FP_GT_ZERO(ig_freq_yr) ) {
00067         /* retrieve num days in simulation year and calculate ignition probability per timestep, only done once */
00068         if ( UNITS_FP_LT_ZERO(sprob_ig_ts) )    {
00069             /* retrieve timestep */
00070             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMTSSEC), (void *)&entry) ) {
00071                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_TIMESTEP_SECS property. \n", ERR_EINVAL);
00072                 return 0;
00073                 }   
00074             timestep = atof(entry->val);        
00075             /* find simulation start month */       
00076             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTMO), (void *)&entry) )  {
00077                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_START_MONTH property. \n", ERR_EINVAL);
00078                 return 0;
00079                 }
00080             st_mo = atoi((char *) entry->val);
00081             /* find simulation start day */
00082             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTDY), (void *)&entry) )  {
00083                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_START_DAY property. \n", ERR_EINVAL);
00084                 return 0;
00085                 }
00086             st_dy = atoi((char *) entry->val);
00087             /* find simulation end month */
00088             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDMO), (void *)&entry) )  {
00089                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_END_MONTH property. \n", ERR_EINVAL);
00090                 return 0;
00091                 }
00092             end_mo = atoi((char *) entry->val);
00093             /* find simulation end day */
00094             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDDY), (void *)&entry) )  {
00095                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_END_DAY property. \n", ERR_EINVAL);
00096                 return 0;
00097                 }
00098             end_dy = atoi((char *) entry->val);
00099             /* calculate probability per timestep */             
00100             sprob_ig_ts = ig_freq_yr / 
00101                           FireTimerGetDaysDifftime(st_mo, st_dy, end_mo, end_dy) / 
00102                           (IGNITION_SECS_PER_DAY / timestep);
00103             }
00104         }
00105     else    {
00106         /* ignition frequency per year < 0, ignitions not possible */
00107         return 0;       
00108         }
00109         
00110     /* test for ignition */
00111     if ( randu(0.0, 1.0) < (sprob_ig_ts) )
00112         return 1;
00113 
00114     return 0;
00115     }
00116 
00117 int GetIgnitionLocFIXEDFromProps(ChHashTable * proptbl, FireYear * fy, List ** rwxylist)    {
00118     KeyVal * entry          = NULL;             /* key/val instances from table */  
00119     DblTwoDArray * da       = NULL;             /* (temp) array of ignition locations retrieved from file */
00120     FILE * fstream          = NULL;
00121     ListElmt * lel          = NULL;
00122     double * rw             = NULL;
00123     int i;
00124     
00125     /* check args and retrieve fixed ig file  */
00126     if ( proptbl == NULL || fy == NULL 
00127             || ChHashTableRetrieve(proptbl, GetFireProp(PROP_IGFILE), (void *)&entry) ) {
00128         ERR_ERROR("Arguments supplied to determine Ignition Location invalid. \n", ERR_EINVAL);
00129         }   
00130     
00131     /* open fixed igntion file */
00132     if ( (fstream = fopen((char *) entry->val, "r")) == NULL )  {
00133         ERR_ERROR("Unable to open file containing fixed Ignition Locations. \n", ERR_EIOFAIL);
00134         }
00135 
00136     /* retrieve array of real world xy coordinates */   
00137     if ( (da = GetDblTwoDArrayTableFStreamIO(fstream, IGNITION_SEP_CHARS, IGNITION_IGS_COMMENT_CHAR)) == NULL ) {
00138         fclose(fstream);
00139         ERR_ERROR("Unable to parse fixed Ignition Locations from file. \n", ERR_EIOFAIL);
00140         }
00141     
00142     /* insert real world xy coordinates from array in list */
00143     *rwxylist = InitListEmpty(free);
00144     for(i = 0; i < DBLTWODARRAY_SIZE_ROW(da); i++)  {
00145         /* get real world x coordinate */
00146         if ((rw = (double *)malloc(sizeof(double))) != NULL)    {
00147             *rw = DBLTWODARRAY_GET_DATA(da, i, 0);
00148             lel = LIST_TAIL(*rwxylist);
00149             ListInsertNext(*rwxylist, lel, rw);
00150             }
00151         /* get real world y coordinate */
00152         if ((rw = (double *)malloc(sizeof(double))) != NULL)    {
00153             *rw = DBLTWODARRAY_GET_DATA(da, i, 1);
00154             lel = LIST_TAIL(*rwxylist);
00155             ListInsertNext(*rwxylist, lel, rw);
00156             }
00157         }
00158     
00159     /* free memory */
00160     fclose(fstream);
00161     FreeDblTwoDArray(da);
00162 
00163     return ERR_SUCCESS;
00164     }
00165 
00166 int GetIgnitionLocRANDUFromProps(ChHashTable * proptbl, FireYear * fy, List ** rwxylist)    {
00167     ListElmt * lel      = NULL;
00168     double * rw         = NULL;
00169     double llx, lly, urx, ury;
00170     double rwx, rwy;
00171     int id;
00172     long int num_trials;
00173     
00174     /* check args */
00175     if ( proptbl == NULL || fy == NULL )    {
00176         ERR_ERROR("Arguments supplied to determine Ignition Location invalid. \n", ERR_EINVAL);
00177         }
00178     
00179     /* set up xy range to extract random numbers over */
00180     llx = fy->xllcorner;
00181     urx = llx + (INTTWODARRAY_SIZE_COL(fy->id) * fy->cellsize);
00182     lly = fy->yllcorner;
00183     ury = lly + (INTTWODARRAY_SIZE_ROW(fy->id) * fy->cellsize);
00184 
00185     /* keep retrieving numbers until one inside MASK found or num_trials exceeded */
00186     for (num_trials = 0; num_trials < IGNITION_RANDU_MAX_TRIALS; num_trials++)  {
00187         /* generate urn within range [llx, urx] and [lly, ury] */
00188         rwx = randu(llx, urx);
00189         rwy = randu(lly, ury);
00190         
00191         /* return id at urn */
00192         if ( FireYearGetCellIDRealWorld(fy, rwx, rwy, &id) )    {
00193             ERR_ERROR_CONTINUE("Unable to retrieve fire ID, repeating Ignition trial. \n", ERR_EBADFUNC);
00194             continue;
00195             }
00196         
00197         /* if id corresponds to burnable area, we are done */
00198         if ( id != FIRE_YEAR_ID_UNBURNABLE )    {
00199             break;
00200             }       
00201         }
00202     
00203     /* alert user trials exceeded */
00204     if ( num_trials == IGNITION_RANDU_MAX_TRIALS )  {
00205         ERR_ERROR("Unable to generate random Ignition Location, trials exceeded. \n", ERR_EMAXITER);
00206         }
00207 
00208     /* initialize returned structure */
00209     *rwxylist = InitListEmpty(free);
00210     if ((rw = (double *)malloc(sizeof(double))) != NULL)    {
00211         *rw = rwx;
00212         lel = LIST_TAIL(*rwxylist);
00213         ListInsertNext(*rwxylist, lel, rw);
00214         }
00215     if ((rw = (double *)malloc(sizeof(double))) != NULL)    {
00216         *rw = rwy;
00217         lel = LIST_TAIL(*rwxylist);     
00218         ListInsertNext(*rwxylist, lel, rw);
00219         }
00220                                 
00221     return ERR_SUCCESS;
00222     }
00223 
00224 int GetIgnitionLocRANDSFromProps(ChHashTable * proptbl, FireYear * fy, List ** rwxylist)    {
00225     GridData * igprob   = NULL;                         /* (temp) grid of ignition probabilities */
00226     ListElmt * lel      = NULL;
00227     double * rwx, * rwy;
00228     int i, j;   
00229     double xyprob;
00230     long int num_trials;
00231         
00232     /* check args */
00233     if ( proptbl == NULL || fy == NULL )    {
00234         ERR_ERROR("Arguments supplied to determine Ignition Location invalid. \n", ERR_EINVAL);
00235         }
00236 
00237     /* initialize temporary ignition probability Grid */    
00238     if ( (igprob = GetGridDataFromPropsFireGridData(proptbl, FIRE_GRIDDATA_IGNITION_RSP_DATA)) == NULL )    {
00239         ERR_ERROR("Unable to initialize ignition probability raster spatial dataset. \n", ERR_EBADFUNC);
00240         }
00241 
00242     /* keep retrieving numbers until ignition within probability or num_trials exceeded */  
00243     for (num_trials = 0; num_trials < IGNITION_RANDS_MAX_TRIALS; num_trials++)  {
00244         /* get ignition location in real world units */
00245         if ( GetIgnitionLocRANDUFromProps(proptbl, fy, rwxylist) )  {
00246             ERR_ERROR_CONTINUE("Unable to determine random Ignition Location, repeating trial. \n", ERR_EFAILED);
00247             continue;
00248             }
00249         lel = LIST_HEAD(*rwxylist);
00250         rwx = LIST_GET_DATA(lel);
00251         lel = LIST_GET_NEXT_ELMT(lel);
00252         rwy = LIST_GET_DATA(lel);
00253         /* transform real world units to grid cell index */
00254         if ( CoordTransRealWorldToRaster(*rwx, *rwy, fy->cellsize, fy->cellsize,
00255             COORD_TRANS_XLLCORNER_TO_XULCNTR(fy->xllcorner, fy->cellsize), 
00256             COORD_TRANS_YLLCORNER_TO_YULCNTR(fy->yllcorner, fy->cellsize, INTTWODARRAY_SIZE_ROW(fy->id)), &i, &j) )     {
00257             FreeList(*rwxylist);
00258             ERR_ERROR_CONTINUE("Unable to transform Ignition Location, repeating trial. \n", ERR_EFAILED);
00259             continue;           
00260             }
00261         /* retrieve probability at location */
00262         GRID_DATA_GET_DATA(igprob, i, j, xyprob);
00263         /* test probability against u.r.n.g */
00264         if ( randu(0.0,1.0) < xyprob )  {
00265             break;
00266             }
00267         /* trial failed, try again */
00268         FreeList(*rwxylist);
00269         }
00270     
00271     /* alert user trials exceeded */
00272     if ( num_trials == IGNITION_RANDU_MAX_TRIALS )  {
00273         ERR_ERROR("Unable to generate random Ignition Location, trials exceeded. \n", ERR_EMAXITER);
00274         }
00275                                 
00276     return ERR_SUCCESS;
00277     }
00278     
00279 /* end of Ignition.c */

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