Main Page   Compound List   File List   Compound Members   File Members  

SantaAna.c

Go to the documentation of this file.
00001 
00025 #include "SantaAna.h"
00026 
00027 IsSantaAnaNowFromProps(ChHashTable * proptbl, int year, int month, int day)     {
00028     KeyVal * entry                  = NULL;             /* key/val instances from table */
00029     /* stack variables */
00030     double sa_freq_yr               = 0.0;              /* number of Santa Ana events per year (avg) */
00031     int st_mo, st_dy, end_mo, end_dy;                   /* start and end month and day of simulation */
00032     /* static variables used to store state across function calls */
00033     static int smonth               = 0;
00034     static int sday                 = 0;
00035     static int syear                = 0;    
00036     static double sprob_sa          = -1.0;             /* daily annual Santa Ana probability */
00037     static int sis_sa_now           = 0;                /* flag set during Santa Ana event */
00038     static int sexp_sa_dy           = 0;                /* counter tracking expired days during current Santa Ana */
00039 
00040     /* check args */
00041     if ( proptbl == NULL )  {
00042         ERR_ERROR_CONTINUE("Arguments supplied to determine Santa Ana occurence invalid. \n", ERR_EINVAL);
00043         return 0;
00044         }
00045 
00046     /* retrieve Santa Ana frequency per year */
00047     if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SANAFREQ), (void *)&entry) ) {
00048         ERR_ERROR_CONTINUE("Unable to retrieve SANTA_ANA_FREQUENCY_PER_YEAR property. \n", ERR_EINVAL);
00049         return 0;
00050         }
00051     sa_freq_yr = atof((char *) entry->val);     
00052     
00053     if( UNITS_FP_GT_ZERO(sa_freq_yr) && ((smonth != month) || (sday != day)) )  {
00054         /* retrieve num days in simulation year and calculate Santa Ana probability, only done once */
00055         if ( UNITS_FP_LT_ZERO(sprob_sa) )   {
00056             /* find simulation start month */       
00057             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTMO), (void *)&entry) )  {
00058                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_START_MONTH property. \n", ERR_EINVAL);
00059                 return 0;
00060                 }
00061             st_mo = atoi((char *) entry->val);
00062             /* find simulation start day */
00063             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMSTDY), (void *)&entry) )  {
00064                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_START_DAY property. \n", ERR_EINVAL);
00065                 return 0;
00066                 }
00067             st_dy = atoi((char *) entry->val);
00068             /* find simulation end month */
00069             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDMO), (void *)&entry) )  {
00070                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_END_MONTH property. \n", ERR_EINVAL);
00071                 return 0;
00072                 }
00073             end_mo = atoi((char *) entry->val);
00074             /* find simulation end day */
00075             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SIMEDDY), (void *)&entry) )  {
00076                 ERR_ERROR_CONTINUE("Unable to retrieve SIMULATION_END_DAY property. \n", ERR_EINVAL);
00077                 return 0;
00078                 }
00079             end_dy = atoi((char *) entry->val);
00080             /* calculate daily annual probability */             
00081             sprob_sa = sa_freq_yr / FireTimerGetDaysDifftime(st_mo, st_dy, end_mo, end_dy);
00082             }
00083         /* is there an ongoing Santa Ana event */
00084         if ( sis_sa_now == 1 )  {
00085             /* increment num of days that have expired during current Santa Ana */
00086             sexp_sa_dy++;
00087             /* retrieve duration of all Santa Anas */
00088             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SANANUMD), (void *)&entry) ) {
00089                 ERR_ERROR_CONTINUE("Unable to retrieve SANTA_ANA_NUM_DAYS_DURATION property. \n", ERR_EINVAL);
00090                 return 0;
00091                 }
00092             /* the length of the current Santa Ana has reached the limit of its duration or a new year has started */
00093             if ( atoi((char *) entry->val) == sexp_sa_dy  || syear != year )    {
00094                 sexp_sa_dy = 0;
00095                 sis_sa_now = 0;
00096                 }
00097             }
00098         /* draw a uniform random number and determine if santa ana occurs */
00099         else    {
00100             /* Santa Ana occurs */
00101             if ( randu(0.0, 1.0) < sprob_sa )   {
00102                 /* set Santa Ana flag for subsequent calls to this method */
00103                 sis_sa_now = 1;         
00104                 /* write occurence into user-specified Santa Ana event occurences file */
00105                 if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SANANUMD), (void *)&entry) ) {
00106                     ERR_ERROR_CONTINUE("Unable to retrieve SANTA_ANA_NUM_DAYS_DURATION property. \n", ERR_EINVAL);
00107                     return 0;
00108                     }           
00109                 if ( FireExportSantaAnaEvtTxtFile(proptbl, atoi((char *) entry->val), year, month, day) )   {
00110                     ERR_ERROR_CONTINUE("Unable to insert Santa Ana event into EXPORT_SANTA_ANA_EVT_FILE. \n", ERR_EIOFAIL);
00111                     return 0;
00112                     }
00113                 }
00114             }
00115         /* set {month, day, year} for future calls */
00116         smonth = month;
00117         sday = day;
00118         syear = year;
00119         }       
00120     
00121     return sis_sa_now;
00122     }
00123 
00124 int GetSantaAnaEnvFromProps(ChHashTable * proptbl, int month, int day, int hour,
00125                                     double * waz, double fbedhgtm, double * wspmps,
00126                                     double * d1hfm, double * d10hfm, double * d100hfm)      {
00127     /* static variables used to store state across function calls */
00128     static int smonth               = 0;
00129     static int sday                 = 0;
00130     static int shour                = 0;
00131     static int srec                 = 0;
00132     static double swaz              = 0.0;
00133     static DblTwoDArray * swaz_tbl  = NULL; 
00134     static double swsp              = 0.0;
00135     static DblTwoDArray * swsp_tbl  = NULL;
00136     static double sd1hfm            = 0.0;
00137     static double sd10hfm           = 0.0;
00138     static double sd100hfm          = 0.0;
00139     static DblTwoDArray * sd10h_tbl = NULL;
00140     /* stack variables */   
00141     KeyVal * entry                  = NULL;             /* key/val instances from table */  
00142     FILE * fstream                  = NULL;             /* file stream */
00143     char * units                    = NULL;
00144                                                 
00145     if ( (smonth != month) || (sday != day) || (shour != hour) )    {
00146         /* new wind azimuth table needed, only done once */
00147         if (swaz_tbl == NULL )  {
00148             /* initialize returned vars in case table not created */        
00149             *waz = swaz;        
00150             /* table of fixed values only needs to be initialized once */
00151             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SANAWAZF), (void *)&entry) ) {
00152                 ERR_ERROR("Unable to retrieve SANTA_ANA_WIND_AZIMUTH_FILE property. \n", ERR_EINVAL);
00153                 }
00154             /* open fixed azimuth file */
00155             if ( (fstream = fopen((char *) entry->val, "r")) == NULL )  {
00156                 ERR_ERROR("Unable to open file containing Santa Ana Wind Azimuths. \n", ERR_EIOFAIL);
00157                 }
00158             /* create the table of values */
00159             if ( (swaz_tbl = GetDblTwoDArrayTableFStreamIO(fstream, 
00160                     WIND_AZIMUTH_SEP_CHARS, WIND_AZIMUTH_WAZ_COMMENT_CHAR)) == NULL )   {
00161                 fclose(fstream);
00162                 ERR_ERROR("Unable to initialize Santa Ana Wind Azimuth table. \n", ERR_EFAILED);
00163                 }
00164             fclose(fstream);            
00165             }
00166         /* new wind speed table needed, only done once */
00167         if (swsp_tbl == NULL )  {
00168             /* initialize returned vars in case table not created */        
00169             *wspmps = swsp;     
00170             /* table of fixed values only needs to be initialized once */
00171             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SANAWSPDF), (void *)&entry) )    {
00172                 ERR_ERROR("Unable to retrieve SANTA_ANA_WIND_SPEED_FILE property. \n", ERR_EINVAL);
00173                 }
00174             /* open fixed speed file */
00175             if ( (fstream = fopen((char *) entry->val, "r")) == NULL )  {
00176                 ERR_ERROR("Unable to open file containing Santa Ana Wind Speeds. \n", ERR_EIOFAIL);
00177                 }
00178             /* retrieve string indicating units windspeed stored in */
00179             units = GetValFromKeyStringFStreamIO(fstream, WIND_SPD_WSP_KEYWORD_UNITS, 
00180                                     WIND_SPD_SEP_CHARS, WIND_SPD_WSP_COMMENT_CHAR);
00181             /* create the table of values */
00182             if ( (swsp_tbl = GetDblTwoDArrayTableFStreamIO(fstream, 
00183                     WIND_SPD_SEP_CHARS, WIND_SPD_WSP_COMMENT_CHAR)) == NULL )   {
00184                 free(units);
00185                 fclose(fstream);
00186                 ERR_ERROR("Unable to initialize Santa Ana Wind Speed table. \n", ERR_EFAILED);
00187                 }
00188             /* convert values in table to mps */
00189             if ( ConvertWindSpeedUnitsToMps(swsp_tbl, units) )  {
00190                 fclose(fstream);
00191                 ERR_ERROR("Unable to convert values in historical Wind Speed table to mps. \n", ERR_EFAILED);
00192                 }
00193             /* cleanup */
00194             free(units);
00195             fclose(fstream);        
00196             }
00197         /* new dead fuel moistures table needed, only done once */
00198         if (sd10h_tbl == NULL ) {
00199             /* initialize returned vars in case table not created */
00200             *d1hfm = sd1hfm;
00201             *d10hfm = sd10hfm;
00202             *d100hfm = sd100hfm;        
00203             /* table of fixed values only needs to be initialized once */
00204             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_SANADFMF), (void *)&entry) ) {
00205                 ERR_ERROR("Unable to retrieve SANTA_ANA_DEAD_FUEL_MOIST_FILE property. \n", ERR_EINVAL);
00206                 }
00207             /* open fixed azimuth file */
00208             if ( (fstream = fopen((char *) entry->val, "r")) == NULL )  {
00209                 ERR_ERROR("Unable to open file containing Santa Ana Dead Fuel Moisture. \n", ERR_EIOFAIL);
00210                 }
00211             /* create the table of values */
00212             if ( (sd10h_tbl = GetDblTwoDArrayTableFStreamIO(fstream, 
00213                     DEAD_FUEL_MOIST_10H_SEP_CHARS, DEAD_FUEL_MOIST_10H_COMMENT_CHAR)) == NULL ) {
00214                 fclose(fstream);
00215                 ERR_ERROR("Unable to initialize Santa Ana Dead Fuel Moisture table. \n", ERR_EFAILED);
00216                 }
00217             fclose(fstream);        
00218             }
00219         
00220         /* new record index required */
00221         if ( (smonth != month) || (sday != day) )   {
00222             if ( (DBLTWODARRAY_SIZE_ROW(swaz_tbl) != DBLTWODARRAY_SIZE_ROW(swsp_tbl)) 
00223                     && (DBLTWODARRAY_SIZE_ROW(swsp_tbl) != DBLTWODARRAY_SIZE_ROW(sd10h_tbl)) )  {
00224                 ERR_ERROR("Unable to retrieve Santa Ana conditions, table record numbers unequal. \n", ERR_EINVAL);
00225                 }
00226             srec = randi(0) % DBLTWODARRAY_SIZE_ROW(swaz_tbl);
00227             }
00228 
00229         /* new environmental variables required */
00230         if ( UNITS_FP_GT_ZERO(DBLTWODARRAY_GET_DATA(swaz_tbl, srec, SANTA_ANA_HR_TO_TBL_INDEX(hour))) )
00231             swaz = DBLTWODARRAY_GET_DATA(swaz_tbl, srec, SANTA_ANA_HR_TO_TBL_INDEX(hour));
00232         if ( UNITS_FP_GT_ZERO(DBLTWODARRAY_GET_DATA(swsp_tbl, srec, SANTA_ANA_HR_TO_TBL_INDEX(hour))) )
00233             swsp = DBLTWODARRAY_GET_DATA(swsp_tbl, srec, SANTA_ANA_HR_TO_TBL_INDEX(hour));
00234         sd10hfm = DBLTWODARRAY_GET_DATA(sd10h_tbl, srec, SANTA_ANA_HR_TO_TBL_INDEX(hour)) / 100;
00235         if ( !UNITS_FP_GT_ZERO(sd10hfm) )
00236             sd10hfm = 0.01;
00237         sd1hfm = sd10hfm - 0.02;
00238         if ( !UNITS_FP_GT_ZERO(sd1hfm) )
00239             sd1hfm = 0.01;
00240         sd100hfm = sd10hfm + 0.02;
00241         if ( !UNITS_FP_GT_ZERO(sd100hfm) )
00242             sd100hfm = 0.01;
00243                 
00244         /* set {month, day, hour} for future calls */
00245         smonth = month;
00246         sday = day;
00247         shour = hour;           
00248         }
00249 
00250     *waz = swaz;
00251     *wspmps = WIND_SPD_REF_HGT_TO_MIDFLAME(swsp, WIND_SPD_RAWS_REF_HGT_METERS, fbedhgtm);
00252     *d1hfm = sd1hfm;
00253     *d10hfm = sd10hfm;
00254     *d100hfm = sd100hfm;
00255                 
00256     return ERR_SUCCESS;
00257     }
00258 
00259 /* end of SantaAna.c */

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