00001
00025 #include "FuelsRegrowth.h"
00026
00027 int GetFuelsRegrowthFIXEDFromProps(ChHashTable * proptbl, GridData * std_age, GridData ** fuels) {
00028 IntTwoDArray * farr = NULL;
00029 KeyVal * entry = NULL;
00030 int domain_rows, domain_cols, fixed_fnum;
00031 int i, j, adata;
00032
00033
00034 if ( proptbl == NULL || std_age == NULL ) {
00035 ERR_ERROR("Arguments supplied to initialize fuels data invalid. \n", ERR_EINVAL);
00036 }
00037
00038
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
00046 domain_rows = std_age->ghdr->nrows;
00047 domain_cols = std_age->ghdr->ncols;
00048
00049
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
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
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
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
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;
00093 IntTwoDArray * rgr_tbl = NULL;
00094 IntTwoDArray * farr = NULL;
00095 KeyVal * entry = NULL;
00096 FILE * fstream = NULL;
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
00103 if ( proptbl == NULL || std_age == NULL ) {
00104 ERR_ERROR("Arguments supplied to initialize fuels data invalid. \n", ERR_EINVAL);
00105 }
00106
00107
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
00114 if ( (fstream = fopen(((char *)entry->val), "r")) == NULL ) {
00115 ERR_ERROR("Unable to open rgr file. \n", ERR_EIOFAIL);
00116 }
00117
00118
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
00127 domain_rows = std_age->ghdr->nrows;
00128 domain_cols = std_age->ghdr->ncols;
00129
00130
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
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
00144 for(i = 0; i < domain_rows; i++) {
00145 for(j = 0; j < domain_cols; j++) {
00146
00147 GRID_DATA_GET_DATA(std_age, i, j, age_cell);
00148
00149 if ( age_cell != std_age->ghdr->NODATA_value ) {
00150 found_fnum = 0;
00151
00152 GRID_DATA_GET_DATA(pnv, i, j, pnv_cell);
00153
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
00159 fnum_cell = INTTWODARRAY_GET_DATA(rgr_tbl, r, (INTTWODARRAY_SIZE_COL(rgr_tbl) - 1));
00160 else
00161
00162 fnum_cell = INTTWODARRAY_GET_DATA(rgr_tbl, r, age_cell);
00163 break;
00164 }
00165 }
00166
00167 if ( found_fnum == 1 ) {
00168 INTTWODARRAY_SET_DATA(farr, i, j, fnum_cell);
00169 }
00170
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
00180 INTTWODARRAY_SET_DATA(farr, i, j, std_age->ghdr->NODATA_value);
00181 }
00182 }
00183 }
00184
00185
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
00195 FreeIntTwoDArray(rgr_tbl);
00196 FreeIntTwoDArray(farr);
00197 FreeGridData(pnv);
00198
00199 return ERR_SUCCESS;
00200 }
00201
00202