00001
00025 #include "Ignition.h"
00026
00027 int IsIgnitionNowFIXEDFromProps(ChHashTable * proptbl) {
00028 KeyVal * entry = NULL;
00029 static int fixed_ig_occured = 0;
00030
00031
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
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;
00048 int st_mo, st_dy, end_mo, end_dy;
00049 double ig_freq_yr, timestep;
00050
00051 static double sprob_ig_ts = -1.0;
00052
00053
00054 if ( proptbl == NULL ) {
00055 ERR_ERROR_CONTINUE("Arguments supplied to determine Ignition Frequency invalid. \n", ERR_EINVAL);
00056 return 0;
00057 }
00058
00059
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
00068 if ( UNITS_FP_LT_ZERO(sprob_ig_ts) ) {
00069
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
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
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
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
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
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
00107 return 0;
00108 }
00109
00110
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;
00119 DblTwoDArray * da = NULL;
00120 FILE * fstream = NULL;
00121 ListElmt * lel = NULL;
00122 double * rw = NULL;
00123 int i;
00124
00125
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
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
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
00143 *rwxylist = InitListEmpty(free);
00144 for(i = 0; i < DBLTWODARRAY_SIZE_ROW(da); i++) {
00145
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
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
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
00175 if ( proptbl == NULL || fy == NULL ) {
00176 ERR_ERROR("Arguments supplied to determine Ignition Location invalid. \n", ERR_EINVAL);
00177 }
00178
00179
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
00186 for (num_trials = 0; num_trials < IGNITION_RANDU_MAX_TRIALS; num_trials++) {
00187
00188 rwx = randu(llx, urx);
00189 rwy = randu(lly, ury);
00190
00191
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
00198 if ( id != FIRE_YEAR_ID_UNBURNABLE ) {
00199 break;
00200 }
00201 }
00202
00203
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
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;
00226 ListElmt * lel = NULL;
00227 double * rwx, * rwy;
00228 int i, j;
00229 double xyprob;
00230 long int num_trials;
00231
00232
00233 if ( proptbl == NULL || fy == NULL ) {
00234 ERR_ERROR("Arguments supplied to determine Ignition Location invalid. \n", ERR_EINVAL);
00235 }
00236
00237
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
00243 for (num_trials = 0; num_trials < IGNITION_RANDS_MAX_TRIALS; num_trials++) {
00244
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
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
00262 GRID_DATA_GET_DATA(igprob, i, j, xyprob);
00263
00264 if ( randu(0.0,1.0) < xyprob ) {
00265 break;
00266 }
00267
00268 FreeList(*rwxylist);
00269 }
00270
00271
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