Main Page   Compound List   File List   Compound Members   File Members  

FuelsRegrowth.c

Go to the documentation of this file.
00001 
00025 #include "FuelsRegrowth.h"
00026 
00027 int GetFuelsRegrowthFIXEDFromProps(ChHashTable * proptbl, GridData * std_age, GridData ** fuels)    {
00028     IntTwoDArray * farr     = NULL;             /* temp array used to initialize fuels */
00029     KeyVal * entry          = NULL;             /* key/val instances from table */
00030     int domain_rows, domain_cols, fixed_fnum;
00031     int i, j, adata;
00032     
00033     /* check args */
00034     if ( proptbl == NULL || std_age == NULL )   {
00035         ERR_ERROR("Arguments supplied to initialize fuels data invalid. \n", ERR_EINVAL);
00036         }
00037 
00038     /* retrieve fixed num */
00039     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_FIXFNUM), (void *)&entry) 
00040             || strcmp(entry->val, GetFireVal(VAL_NULL)) == 0 )  {
00041         ERR_ERROR("Unable to retrieve FUELS_FIXED_MODEL_NUM property. \n", ERR_EFAILED);
00042         }
00043     fixed_fnum = atoi(entry->val);
00044 
00045     /* get dimensions of fuels from stand age */    
00046     domain_rows = std_age->ghdr->nrows; 
00047     domain_cols = std_age->ghdr->ncols;
00048         
00049     /* create temp array to initialize fuels GridData */
00050     if ( (farr = InitIntTwoDArraySizeEmpty(domain_rows, domain_cols)) == NULL ) {
00051         ERR_ERROR("Unable to allocate memory for fuels TwoDArray. \n", ERR_ENOMEM);
00052         }
00053 
00054     /* insert fuels values into array */
00055     for(i = 0; i < domain_rows; i++)    {
00056         for(j = 0; j < domain_cols; j++)    {
00057             GRID_DATA_GET_DATA(std_age, i, j, adata);
00058             if ( adata == std_age->ghdr->NODATA_value ) {
00059                 INTTWODARRAY_SET_DATA(farr, i, j, std_age->ghdr->NODATA_value);
00060                 }
00061             else    {
00062                 INTTWODARRAY_SET_DATA(farr, i, j, fixed_fnum);
00063                 }
00064             }
00065         }
00066 
00067     /* initialize the fuels GridData from array */
00068     if ( (*fuels = InitGridDataFromIntTwoDArray(farr, std_age->ghdr->xllcorner, std_age->ghdr->yllcorner, 
00069                                 std_age->ghdr->cellsize, std_age->ghdr->NODATA_value)) == NULL )    {
00070         FreeIntTwoDArray(farr);
00071         ERR_ERROR("Unable to allocate memory for fuels GridData. \n", ERR_ENOMEM);
00072         }
00073         
00074     /* free the temp array */
00075     FreeIntTwoDArray(farr);
00076                         
00077     return ERR_SUCCESS;
00078     }
00079 
00080 int GetFuelsRegrowthSTATICFromProps(ChHashTable * proptbl, GridData * std_age, GridData ** fuels)   {
00081     if ( proptbl == NULL || std_age == NULL )   {
00082         ERR_ERROR("Arguments supplied to initialize fuels data invalid. \n", ERR_EINVAL);
00083         }
00084         
00085     /* retrieve fuels data */
00086     *fuels = GetGridDataFromPropsFireGridData(proptbl, FIRE_GRIDDATA_FUELS_STATIC_DATA);
00087         
00088     return ERR_SUCCESS;
00089     }
00090 
00091 int GetFuelsRegrowthPNVFromProps(ChHashTable * proptbl, GridData * std_age, GridData ** fuels)  {
00092     GridData * pnv          = NULL;             /* (temp) pnv spatial data used to initialize fuels */
00093     IntTwoDArray * rgr_tbl  = NULL;             /* (temp) parsed index table references pnv to fuel model num */
00094     IntTwoDArray * farr     = NULL;             /* (temp) underlying array of fuel values */
00095     KeyVal * entry          = NULL;             /* key/val instances from properties table */
00096     FILE * fstream          = NULL;             /* file ptr to rgr file */
00097     int domain_rows, domain_cols;
00098     int i, j, r;
00099     int pnv_cell = 0, age_cell = 0, fnum_cell = 0;
00100     int found_fnum = 0;
00101 
00102     /* check args */        
00103     if ( proptbl == NULL || std_age == NULL )   {
00104         ERR_ERROR("Arguments supplied to initialize fuels data invalid. \n", ERR_EINVAL);
00105         }
00106 
00107     /* retrieve rgr file name */
00108     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_RGRFILE), (void *)&entry)
00109             || strcmp(entry->val, GetFireVal(VAL_NULL)) == 0 )  {
00110         ERR_ERROR("Unable to retrieve FUELS_PNV_RGR_FILE property. \n", ERR_EFAILED);
00111         }
00112         
00113     /* open rgr file stream */
00114     if ( (fstream = fopen(((char *)entry->val), "r")) == NULL ) {
00115         ERR_ERROR("Unable to open rgr file. \n", ERR_EIOFAIL);
00116         }
00117 
00118     /* initialize table of rgr values to cross reference pnv numbers to fuel model numbers */
00119     if ( (rgr_tbl = GetIntTwoDArrayTableFStreamIO(fstream, FUELS_REGROWTH_SEP_CHARS, 
00120             FUELS_REGROWTH_RGR_COMMENT_CHAR)) == NULL ) {
00121         fclose(fstream);
00122         ERR_ERROR("Unable to allocate memory for list backing rgr table. \n", ERR_EBADFUNC);
00123         }
00124     fclose(fstream);
00125 
00126     /* get dimensions of fuels from stand age */    
00127     domain_rows = std_age->ghdr->nrows; 
00128     domain_cols = std_age->ghdr->ncols;
00129     
00130     /* create temp fuels array to initialize a fuels GridData */
00131     if ( (farr = InitIntTwoDArraySizeEmpty(domain_rows, domain_cols)) == NULL ) {
00132         FreeIntTwoDArray(rgr_tbl);
00133         ERR_ERROR("Unable to allocate memory for fuels TwoDArray. \n", ERR_ENOMEM);
00134         }
00135         
00136     /* create temp pnv grid containing the potential natural vegetation class numbers */
00137     if ( (pnv = GetGridDataFromPropsFireGridData(proptbl, FIRE_GRIDDATA_FUELS_PNV_DATA)) == NULL )  {
00138         FreeIntTwoDArray(farr);
00139         FreeIntTwoDArray(rgr_tbl);  
00140         ERR_ERROR("Unable to initialize PNV raster spatial dataset. \n", ERR_EBADFUNC);
00141         }
00142 
00143     /* iterate through the pnv grid assigning fuel model numbers to fuels array based upon rgr table */
00144     for(i = 0; i < domain_rows; i++)    {
00145         for(j = 0; j < domain_cols; j++)    {
00146             /* retrieve stand age at cell */
00147             GRID_DATA_GET_DATA(std_age, i, j, age_cell);
00148             /* try to assign a fuel model num */
00149             if (  age_cell != std_age->ghdr->NODATA_value ) {
00150                 found_fnum = 0;
00151                 /* retrieve pnv at cell */
00152                 GRID_DATA_GET_DATA(pnv, i, j, pnv_cell);
00153                 /* retrieve the fuel model num from rgr_tbl */
00154                 for(r = 0; r < INTTWODARRAY_SIZE_ROW(rgr_tbl); r++) {
00155                     if ( INTTWODARRAY_GET_DATA(rgr_tbl, r, 0) == pnv_cell ) {
00156                         found_fnum = 1;
00157                         if ( age_cell > (INTTWODARRAY_SIZE_COL(rgr_tbl) - 1) )
00158                             /* age exceeds largest age in rgr table */
00159                             fnum_cell = INTTWODARRAY_GET_DATA(rgr_tbl, r, (INTTWODARRAY_SIZE_COL(rgr_tbl) - 1));
00160                         else
00161                             /* index age directly from rgr table */
00162                             fnum_cell = INTTWODARRAY_GET_DATA(rgr_tbl, r, age_cell);
00163                         break;
00164                         }
00165                     }
00166                 /* assign fuel model num to fuels array */
00167                 if ( found_fnum == 1 )  {
00168                     INTTWODARRAY_SET_DATA(farr, i, j, fnum_cell);
00169                     }
00170                 /* pnv num not found in rgr_tbl */
00171                 else    {
00172                     FreeIntTwoDArray(rgr_tbl);
00173                     FreeIntTwoDArray(farr);
00174                     FreeGridData(pnv);
00175                     ERR_ERROR("Unable to find pnv number in rgr table. \n", ERR_EFAILED);
00176                     }
00177                 }
00178             else    {
00179                 /* assign NO DATA */            
00180                 INTTWODARRAY_SET_DATA(farr, i, j, std_age->ghdr->NODATA_value);
00181                 }
00182             }
00183         }
00184 
00185     /* initialize the fuels GridData from array */
00186     if ( (*fuels = InitGridDataFromIntTwoDArray(farr, std_age->ghdr->xllcorner, std_age->ghdr->yllcorner, 
00187                                 std_age->ghdr->cellsize, std_age->ghdr->NODATA_value)) == NULL )    {
00188         FreeIntTwoDArray(rgr_tbl);
00189         FreeIntTwoDArray(farr);
00190         FreeGridData(pnv);
00191         ERR_ERROR("Unable to allocate memory for fuels GridData. \n", ERR_ENOMEM);
00192         }
00193         
00194     /* free memory associated with temp data structures */
00195     FreeIntTwoDArray(rgr_tbl);
00196     FreeIntTwoDArray(farr); 
00197     FreeGridData(pnv);
00198     
00199     return ERR_SUCCESS;
00200     }
00201             
00202 /* end of FuelsRegrowth.c */

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