00001
00025 #include "FireExport.h"
00026
00027
00028
00029
00030
00031
00032
00033 int FireExportFireIDAscRaster(ChHashTable * proptbl, FireYear * fyr, FireTimer * ft);
00034
00035 int FireExportFuelsAscRaster(ChHashTable * proptbl, GridData * fuels, FireTimer * ft);
00036
00037 int FireExportStandAgeAscRaster(ChHashTable * proptbl, GridData * std_age, FireTimer * ft);
00038
00039 FireExport * InitFireExport(ChHashTable * proptbl) {
00040 FireExport * fe = NULL;
00041 KeyVal * entry = NULL;
00042
00043
00044 if ( proptbl == NULL || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPFREQ), (void *)&entry) ) {
00045 ERR_ERROR_CONTINUE("Unable to retrieve EXPORT_FREQUENCY property. \n", ERR_EINVAL);
00046 return fe;
00047 }
00048
00049 if ( (fe = (FireExport *) malloc(sizeof(FireExport))) == NULL ) {
00050 ERR_ERROR_CONTINUE("Unable to initialize FireExport, memory allocation failed. \n", ERR_ENOMEM);
00051 return fe;
00052 }
00053
00054
00055 if ( strcmp(entry->val, GetFireVal(VAL_TIMESTEP)) == 0 ) {
00056 fe->exp_freq = EnumFreqTimestep;
00057 }
00058 else if ( strcmp(entry->val, GetFireVal(VAL_DAILY)) == 0 ) {
00059 fe->exp_freq = EnumFreqDaily;
00060 }
00061 else if ( strcmp(entry->val, GetFireVal(VAL_ANNUAL)) == 0 ) {
00062 fe->exp_freq = EnumFreqAnnual;
00063 }
00064 else {
00065 ERR_ERROR_CONTINUE("Unable to initialize FireExport, EXPORT_FREQUENCY property incorrect. \n", ERR_EINVAL);
00066 FreeFireExport(fe);
00067 return fe;
00068 }
00069
00070
00071 if ( FireExportInitTxtFileHeaders(proptbl) ) {
00072 ERR_ERROR_CONTINUE("Unable to initialize FireExport, EXPORT textfile properties incorrect. \n", ERR_EINVAL);
00073 FreeFireExport(fe);
00074 return fe;
00075 }
00076
00077
00078 fe->ft = NULL;
00079 fe->fyr = NULL;
00080 fe->fuels = NULL;
00081 fe->std_age = NULL;
00082
00083 fe->FireExportFireIDAscRaster = FireExportFireIDAscRaster;
00084 fe->FireExportFuelsAscRaster = FireExportFuelsAscRaster;
00085 fe->FireExportStandAgeAscRaster = FireExportStandAgeAscRaster;
00086 #ifdef USING_GD
00087 fe->FireExportFireIDPng = FireExportFireIDPng;
00088 #else
00089 fe->FireExportFireIDPng = NULL;
00090 #endif
00091
00092 return fe;
00093 }
00094
00095 int FireExportSpatialData(ChHashTable * proptbl, FireExport * fe) {
00096
00097 static int smonth = 0;
00098 static int sday = 0;
00099 static int shour = 0;
00100
00101 int do_export = 0;
00102
00103
00104 if ( proptbl == NULL || fe == NULL ) {
00105 ERR_ERROR("Unable to retrieve FireExport information. \n", ERR_EINVAL);
00106 }
00107 if ( fe->ft == NULL ) {
00108 ERR_ERROR("Must have a FireTimer set in order to use FireExport. \n", ERR_EINVAL);
00109 }
00110
00111
00112 switch(fe->exp_freq) {
00113 case EnumFreqTimestep:
00114
00115 do_export = 1;
00116 break;
00117 case EnumFreqDaily:
00118 if ( (smonth != fe->ft->sim_cur_mo) || (sday != fe->ft->sim_cur_dy) ) {
00119 do_export = 1;
00120
00121 smonth = fe->ft->sim_cur_mo;
00122 sday = fe->ft->sim_cur_dy;
00123 }
00124 break;
00125 case EnumFreqAnnual:
00126 if ( FireTimerIsSimCurYearTimeExpired(fe->ft) ) {
00127 do_export = 1;
00128 }
00129 break;
00130 default:
00131 do_export = 0;
00132 break;
00133 }
00134
00135 if ( do_export == 1 ) {
00136
00137 if ( fe->fyr != NULL && fe->FireExportFireIDAscRaster != NULL ) {
00138 if ( fe->FireExportFireIDAscRaster(proptbl, fe->fyr, fe->ft) ) {
00139 ERR_ERROR("Unable to export fire ids raster. \n", ERR_EBADFUNC);
00140 }
00141 }
00142
00143 if ( fe->fuels != NULL && fe->FireExportFuelsAscRaster != NULL ) {
00144 if ( fe->FireExportFuelsAscRaster(proptbl, fe->fuels, fe->ft) ) {
00145 ERR_ERROR("Unable to export fuels raster. \n", ERR_EBADFUNC);
00146 }
00147 }
00148
00149 if ( fe->std_age != NULL && fe->FireExportStandAgeAscRaster != NULL ) {
00150 if ( fe->FireExportStandAgeAscRaster(proptbl, fe->std_age, fe->ft) ) {
00151 ERR_ERROR("Unable to export stand age raster. \n", ERR_EBADFUNC);
00152 }
00153 }
00154
00155 if ( fe->fyr != NULL && fe->FireExportFireIDPng != NULL ) {
00156 if ( fe->FireExportFireIDPng(proptbl, fe->fyr, fe->ft) ) {
00157 ERR_ERROR("Unable to export fire ids images. \n", ERR_EBADFUNC);
00158 }
00159 }
00160 }
00161
00162 return ERR_SUCCESS;
00163 }
00164
00165 int FireExportFireIDAscRaster(ChHashTable * proptbl, FireYear * fyr, FireTimer * ft) {
00166 KeyVal * entry = NULL;
00167 char fid_fname[FIRE_EXPORT_DEFAULT_FILENAME_SIZE] = {'\0'};
00168 int mt = 0;
00169
00170
00171 if ( proptbl == NULL || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPFIDDIR), (void *)&entry) ) {
00172 ERR_ERROR("Unable to retrieve EXPORT_FIRE_ID_RASTER_DIR property. \n", ERR_EINVAL);
00173 }
00174
00175
00176 if ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0) {
00177 return ERR_SUCCESS;
00178 }
00179
00180
00181 mt = FIRE_TIMER_GET_MILITARY_TIME(ft);
00182 sprintf(fid_fname, "%s\\fid%d%02d%02d%04d.asc", entry->val, ft->sim_cur_yr, ft->sim_cur_mo, ft->sim_cur_dy, mt);
00183
00184
00185 if ( ExportIntTwoDArrayAsAsciiRaster(fyr->id, fyr->xllcorner, fyr->yllcorner,
00186 fyr->cellsize, FIRE_YEAR_ID_UNBURNABLE, fid_fname) ) {
00187 ERR_ERROR("Unable to export fire ID data in function FireExportFireIDAscRaster. \n", ERR_EBADFUNC);
00188 }
00189
00190 return ERR_SUCCESS;
00191 }
00192
00193 int FireExportFuelsAscRaster(ChHashTable * proptbl, GridData * fuels, FireTimer * ft) {
00194 KeyVal * entry = NULL;
00195 char fl_fname[FIRE_EXPORT_DEFAULT_FILENAME_SIZE] = {'\0'};
00196 int mt = 0;
00197
00198
00199 if ( proptbl == NULL || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPFUELDIR), (void *)&entry) ) {
00200 ERR_ERROR("Unable to retrieve EXPORT_FUELS_RASTER_DIR property. \n", ERR_EINVAL);
00201 }
00202
00203
00204 if ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0) {
00205 return ERR_SUCCESS;
00206 }
00207
00208
00209 mt = FIRE_TIMER_GET_MILITARY_TIME(ft);
00210 sprintf(fl_fname, "%s\\fl%d%02d%02d%04d.asc", entry->val, ft->sim_cur_yr, ft->sim_cur_mo, ft->sim_cur_dy, mt);
00211
00212
00213 if ( ExportGridDataAsAsciiRaster(fuels, fl_fname) ) {
00214 ERR_ERROR("Unable to export fuels data in function FireExportFuelsAscRaster. \n", ERR_EBADFUNC);
00215 }
00216
00217 return ERR_SUCCESS;
00218 }
00219
00220 int FireExportStandAgeAscRaster(ChHashTable * proptbl, GridData * std_age, FireTimer * ft) {
00221 KeyVal * entry = NULL;
00222 char sa_fname[FIRE_EXPORT_DEFAULT_FILENAME_SIZE] = {'\0'};
00223 int mt = 0;
00224
00225
00226 if ( proptbl == NULL || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPSAGEDIR), (void *)&entry) ) {
00227 ERR_ERROR("Unable to retrieve EXPORT_STAND_AGE_RASTER_DIR property. \n", ERR_EINVAL);
00228 }
00229
00230
00231 if ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0) {
00232 return ERR_SUCCESS;
00233 }
00234
00235
00236 mt = FIRE_TIMER_GET_MILITARY_TIME(ft);
00237 sprintf(sa_fname, "%s\\sa%d%02d%02d%04d.asc", entry->val, ft->sim_cur_yr, ft->sim_cur_mo, ft->sim_cur_dy, mt);
00238
00239
00240 if ( ExportGridDataAsAsciiRaster(std_age, sa_fname) ) {
00241 ERR_ERROR("Unable to export stand age data in function FireExportStandAgeAscRaster. \n", ERR_EBADFUNC);
00242 }
00243
00244 return ERR_SUCCESS;
00245 }
00246
00247 int FireExportInitTxtFileHeaders(ChHashTable * proptbl) {
00248 KeyVal * igloc_fname, * far_fname, * fperm_fname, * sana_fname;
00249 FILE * fstream = NULL;
00250
00251
00252 if ( proptbl == NULL ) {
00253 ERR_ERROR("Unable to retrieve any text file export properties. \n", ERR_EINVAL);
00254 }
00255
00256
00257 if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPIGLCF), (void *)&igloc_fname)
00258 || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPFAREAF), (void *)&far_fname)
00259 || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPFPERMF), (void *)&fperm_fname)
00260 || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPSANAEVF), (void *)&sana_fname) ) {
00261 ERR_ERROR("unable to retrieve one of the text file EXPORT properties. \n", ERR_EINVAL);
00262 }
00263
00264
00265 if ( strcmp(igloc_fname->val, GetFireVal(VAL_NULL)) != 0) {
00266 if ( (fstream = fopen((char *) igloc_fname->val, "a")) == NULL ) {
00267 ERR_ERROR("Unable to insert header into Ignition Location file. \n", ERR_EIOFAIL);
00268 }
00269 fprintf(fstream, "YYYY, MM, DD, HHHH, X, Y \n");
00270 fclose(fstream);
00271 }
00272
00273
00274 if ( strcmp(far_fname->val, GetFireVal(VAL_NULL)) != 0) {
00275 if ( (fstream = fopen((char *) far_fname->val, "a")) == NULL ) {
00276 ERR_ERROR("Unable to insert header into Fire Area file. \n", ERR_EIOFAIL);
00277 }
00278 fprintf(fstream, "YYYY, FID, Area \n");
00279 fclose(fstream);
00280 }
00281
00282
00283 if ( strcmp(fperm_fname->val, GetFireVal(VAL_NULL)) != 0) {
00284 if ( (fstream = fopen((char *) fperm_fname->val, "a")) == NULL ) {
00285 ERR_ERROR("Unable to insert header into Fire Perimeter file. \n", ERR_EIOFAIL);
00286 }
00287 fprintf(fstream, "YYYY, FID, Perimeter \n");
00288 fclose(fstream);
00289 }
00290
00291
00292 if ( strcmp(sana_fname->val, GetFireVal(VAL_NULL)) != 0) {
00293 if ( (fstream = fopen((char *) sana_fname->val, "a")) == NULL ) {
00294 ERR_ERROR("Unable to insert header into Santa Ana event occurences file. \n", ERR_EIOFAIL);
00295 }
00296 fprintf(fstream, "YYYY, MM, DD, DURATION \n");
00297 fclose(fstream);
00298 }
00299
00300 return ERR_SUCCESS;
00301 }
00302
00303 int FireExportIgLocsTxtFile(ChHashTable * proptbl, double rwx, double rwy, FireTimer * ft) {
00304 KeyVal * entry = NULL;
00305 FILE * fstream = NULL;
00306 int mt = 0;
00307
00308
00309 if ( proptbl == NULL || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPIGLCF), (void *)&entry) ) {
00310 ERR_ERROR("Unable to retrieve EXPORT_IGNITION_LOCS_FILE property. \n", ERR_EINVAL);
00311 }
00312
00313
00314 if ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0) {
00315 return ERR_SUCCESS;
00316 }
00317
00318
00319 if ( (fstream = fopen((char *) entry->val, "a")) == NULL ) {
00320 ERR_ERROR("Unable to append to file containing Ignition Locations. \n", ERR_EIOFAIL);
00321 }
00322
00323 mt = FIRE_TIMER_GET_MILITARY_TIME(ft);
00324 fprintf(fstream, "%d, %02d, %02d, %04d, %0.2f, %0.2f \n", ft->sim_cur_yr, ft->sim_cur_mo, ft->sim_cur_dy, mt, rwx, rwy);
00325 fclose(fstream);
00326
00327 return ERR_SUCCESS;
00328 }
00329
00330 int FireExportSantaAnaEvtTxtFile(ChHashTable * proptbl, int duration, int year, int month, int day) {
00331 KeyVal * entry = NULL;
00332 FILE * fstream = NULL;
00333
00334
00335 if ( proptbl == NULL || ChHashTableRetrieve(proptbl, GetFireProp(PROP_EXPSANAEVF), (void *)&entry) ) {
00336 ERR_ERROR("Unable to retrieve EXPORT_SANTA_ANA_EVT_FILE property. \n", ERR_EINVAL);
00337 }
00338
00339
00340 if ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0) {
00341 return ERR_SUCCESS;
00342 }
00343
00344
00345 if ( (fstream = fopen((char *) entry->val, "a")) == NULL ) {
00346 ERR_ERROR("Unable to append to file containing Santa Ana event occurences. \n", ERR_EIOFAIL);
00347 }
00348
00349 fprintf(fstream, "%d, %02d, %02d, %d \n", year, month, day, duration);
00350 fclose(fstream);
00351
00352 return ERR_SUCCESS;
00353 }
00354
00355 void FreeFireExport(FireExport * fe) {
00356 if ( fe != NULL ) {
00357 free(fe);
00358 }
00359 fe = NULL;
00360 return;
00361 }
00362
00363