Main Page   Compound List   File List   Compound Members   File Members  

FireExport.c

Go to the documentation of this file.
00001 
00025 #include "FireExport.h"
00026 
00027 /*
00028  *********************************************************
00029  * NON PUBLIC FUNCTIONS
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;             /* initialized structure */
00041     KeyVal * entry                      = NULL;             /* key/val instances from table */
00042     
00043     /* check args */
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     /* allocate memory */               
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     /* assign an export frequency enumeration */    
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     /* write out all headers for text files */
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     /* set all members to NULL */
00078     fe->ft                          = NULL;
00079     fe->fyr                         = NULL;
00080     fe->fuels                       = NULL;
00081     fe->std_age                     = NULL;
00082     /* set all function pointers */
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 /* INCLUDES SUPPORT FOR EXPORTING IMAGES FROM SIMULATION USING GD LIBRARY */
00091 
00092     return fe; 
00093     }
00094     
00095 int FireExportSpatialData(ChHashTable * proptbl, FireExport * fe)   {
00096     /* static variables used to store state across function calls */
00097     static int smonth               = 0;
00098     static int sday                 = 0;
00099     static int shour                = 0;
00100     /* stack variables */
00101     int do_export                   = 0;
00102     
00103     /* check args */
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     /* determine if export occurs */
00112     switch(fe->exp_freq)    {
00113         case EnumFreqTimestep:
00114             /* always export when freq is timestep */
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                 /* set {month, day} for future calls */         
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         /* export fire ids */   
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         /* export fuels */
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         /* export stand age */
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         /* export fire id images */ 
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;             /* key/val instances from table */
00167     char fid_fname[FIRE_EXPORT_DEFAULT_FILENAME_SIZE]   = {'\0'};
00168     int mt = 0;
00169 
00170     /* check args */    
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     /* return if no export options specified */
00176     if  ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0)    {
00177         return ERR_SUCCESS;
00178         }
00179     
00180     /* generate name for output asc file of format fidYYYYMMDDHHHH.asc */
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     /* export data */
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;             /* key/val instances from table */
00195     char fl_fname[FIRE_EXPORT_DEFAULT_FILENAME_SIZE]    = {'\0'};
00196     int mt = 0;
00197 
00198     /* check args */    
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     /* return if no export options specified */
00204     if  ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0)    {
00205         return ERR_SUCCESS;
00206         }
00207     
00208     /* generate name for output asc file of format flYYYYMMDDHHHH.asc */
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     /* export data */
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;             /* key/val instances from table */
00222     char sa_fname[FIRE_EXPORT_DEFAULT_FILENAME_SIZE]    = {'\0'};
00223     int mt = 0;
00224 
00225     /* check args */    
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     /* return if no export options specified */
00231     if  ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0)    {
00232         return ERR_SUCCESS;
00233         }
00234     
00235     /* generate name for output asc file of format saYYYYMMDDHHHH.asc */
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     /* export data */
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; /* key/val props */
00249     FILE * fstream                  = NULL;                         /* file stream */
00250     
00251     /* check args */    
00252     if ( proptbl == NULL )  {
00253         ERR_ERROR("Unable to retrieve any text file export properties. \n", ERR_EINVAL);
00254         }
00255         
00256     /* retrieve text file export args */
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     /* write ig loc header if property contains user specifed value */
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     /* write fire area header if property contains user specifed value */
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     /* write fire perimeter header if property contains user specifed value */
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     /* write santa ana header if property contains user specifed value */
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;             /* key/val instances from table */
00305     FILE * fstream                  = NULL;             /* file stream */
00306     int mt = 0;
00307     
00308     /* check args */    
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     /* return if no export options specified */
00314     if  ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0)    {
00315         return ERR_SUCCESS;
00316         }
00317         
00318     /* open append stream to ignition location file */
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     /* formatted output: YYYY, MM, DD, HHHH, XXXXX, YYYYY */
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;             /* key/val instances from table */
00332     FILE * fstream                  = NULL;             /* file stream */
00333         
00334     /* check args */    
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     /* return if no export options specified */
00340     if  ( strcmp(entry->val, GetFireVal(VAL_NULL)) == 0)    {
00341         return ERR_SUCCESS;
00342         }
00343         
00344     /* open append stream to santa ana file */
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     /* formatted output: YYYY, MM, DD, DURATION */
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 /* end of FireExport.c */

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