Main Page   Compound List   File List   Compound Members   File Members  

WindSpd.c

Go to the documentation of this file.
00001 
00025 #include "WindSpd.h"
00026  
00027 int GetWindSpeedMpsFIXEDFromProps(ChHashTable * proptbl, double fbedhgtm, int month, int day, int hour,
00028                                                 unsigned int row, unsigned int col, double * wspmps)        {
00029     /* static variables used to store state across function calls */
00030     static int smonth               = 0;
00031     static int sday                 = 0;
00032     static int shour                = 0;
00033     static double swsp              = 0.0;
00034     static DblTwoDArray * swsp_tbl  = NULL;
00035     /* stack variables */
00036     KeyVal * entry                  = NULL;             /* key/val instances from table */
00037     FILE * fstream                  = NULL;             /* file stream */
00038     char * units                    = NULL;
00039     int rec;
00040 
00041     if ( (smonth != month) || (sday != day) || (shour != hour) )    {
00042         /* new wind speed needed */
00043         if (swsp_tbl == NULL )  {
00044             *wspmps = swsp;
00045             /* table of fixed values only needs to be initialized once */
00046             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_WSPDFFILE), (void *)&entry) )    {
00047                 ERR_ERROR("Unable to retrieve WIND_SPEED_FIXED_FILE property. \n", ERR_EINVAL);
00048                 }
00049             /* open fixed speed file */
00050             if ( (fstream = fopen((char *) entry->val, "r")) == NULL )  {
00051                 ERR_ERROR("Unable to open file containing fixed Wind Speeds. \n", ERR_EIOFAIL);
00052                 }
00053             /* retrieve string indicating units windspeed stored in */
00054             units = GetValFromKeyStringFStreamIO(fstream, WIND_SPD_WSP_KEYWORD_UNITS, 
00055                                     WIND_SPD_SEP_CHARS, WIND_SPD_WSP_COMMENT_CHAR);
00056             /* create the table of values */            
00057             if ( (swsp_tbl = GetDblTwoDArrayTableFStreamIO(fstream, 
00058                     WIND_SPD_SEP_CHARS, WIND_SPD_WSP_COMMENT_CHAR)) == NULL )   {
00059                 free(units);
00060                 fclose(fstream);
00061                 ERR_ERROR("Unable to initialize fixed Wind Speed table. \n", ERR_EFAILED);
00062                 }
00063             /* convert values in table to mps */
00064             if ( ConvertWindSpeedUnitsToMps(swsp_tbl, units) )  {
00065                 fclose(fstream);
00066                 ERR_ERROR("Unable to convert values in fixed Wind Speed table to mps. \n", ERR_EFAILED);
00067                 }
00068             /* cleanup */
00069             free(units);
00070             fclose(fstream);
00071             }
00072         /* find record in table */
00073         row = DBLTWODARRAY_SIZE_ROW(swsp_tbl);
00074         for(rec = 0; rec < row; rec++)  {
00075             /* find matching month and day */
00076             if ( DBLTWODARRAY_GET_DATA(swsp_tbl, rec, WIND_SPD_MO_WSP_TBL_INDEX) == month 
00077                  && DBLTWODARRAY_GET_DATA(swsp_tbl, rec, WIND_SPD_DY_WSP_TBL_INDEX) == day )    {
00078                 /* retrieve wsp on that month and day */
00079                 col = WIND_SPD_HR_TO_WSP_TBL_INDEX(hour);
00080                 swsp = DBLTWODARRAY_GET_DATA(swsp_tbl, rec, col);
00081                 break;
00082                 }
00083             }
00084         /* set {month, day, hour} for future calls */
00085         smonth = month;
00086         sday = day;
00087         shour = hour;
00088         }
00089     
00090     *wspmps = WIND_SPD_REF_HGT_TO_MIDFLAME(swsp, WIND_SPD_RAWS_REF_HGT_METERS, fbedhgtm);
00091                 
00092     return ERR_SUCCESS;
00093     }
00094 
00095 int GetWindSpeedMpsRANDUFromProps(ChHashTable * proptbl, double fbedhgtm, int month, int day, int hour,
00096                                                 unsigned int row, unsigned int col, double * wspmps)        {
00097     /* static variables used to store state across function calls */
00098     static int smonth               = 0;
00099     static int sday                 = 0;
00100     static int shour                = 0;
00101     static double swsp              = 0.0;
00102 
00103     /* WA: stop warnings from unused vars */
00104     row = col = 0;
00105     if ( proptbl == NULL )  {
00106         ERR_ERROR("Simulation properties table not initialized. \n", ERR_EINVAL);
00107         }
00108 
00109     if ( (smonth != month) || (sday != day) || (shour != hour) )    {   
00110         /* new wind speed needed */
00111         swsp = randu(WIND_SPD_RANDU_MIN_MPS, WIND_SPD_RANDU_MAX_MPS);
00112         /* set {month, day, hour} for future calls */
00113         smonth = month;
00114         sday = day;
00115         shour = hour;       
00116         }
00117     
00118     *wspmps = WIND_SPD_REF_HGT_TO_MIDFLAME(swsp, WIND_SPD_RAWS_REF_HGT_METERS, fbedhgtm);
00119                 
00120     return ERR_SUCCESS;
00121     }
00122 
00123 int GetWindSpeedMpsRANDHFromProps(ChHashTable * proptbl, double fbedhgtm, int month, int day, int hour,
00124                                                 unsigned int row, unsigned int col, double * wspmps)        {                                       
00125     /* static variables used to store state across function calls */
00126     static int smonth               = 0;
00127     static int sday                 = 0;
00128     static int shour                = 0;
00129     static double swsp              = 0.0;
00130     static DblTwoDArray * swsp_tbl  = NULL;
00131     /* stack variables */
00132     KeyVal * entry                  = NULL;             /* key/val instances from table */
00133     FILE * fstream                  = NULL;             /* file stream */
00134     char * units                    = NULL;
00135 
00136     if ( (smonth != month) || (sday != day) || (shour != hour) )    {
00137         /* new wind speed needed */
00138         if (swsp_tbl == NULL )  {
00139             *wspmps = swsp;     
00140             /* table of fixed values only needs to be initialized once */
00141             if ( ChHashTableRetrieve(proptbl, GetFireProp(PROP_WSPDHFILE), (void *)&entry) )    {
00142                 ERR_ERROR("Unable to retrieve WIND_SPEED_HISTORICAL_FILE property. \n", ERR_EINVAL);
00143                 }
00144             /* open fixed speed file */
00145             if ( (fstream = fopen((char *) entry->val, "r")) == NULL )  {
00146                 ERR_ERROR("Unable to open file containing historical Wind Speeds. \n", ERR_EIOFAIL);
00147                 }
00148             /* retrieve string indicating units windspeed stored in */
00149             units = GetValFromKeyStringFStreamIO(fstream, WIND_SPD_WSP_KEYWORD_UNITS, 
00150                                     WIND_SPD_SEP_CHARS, WIND_SPD_WSP_COMMENT_CHAR);
00151             /* create the table of values */
00152             if ( (swsp_tbl = GetDblTwoDArrayTableFStreamIO(fstream, 
00153                     WIND_SPD_SEP_CHARS, WIND_SPD_WSP_COMMENT_CHAR)) == NULL )   {
00154                 free(units);
00155                 fclose(fstream);
00156                 ERR_ERROR("Unable to initialize historical Wind Speed table. \n", ERR_EFAILED);
00157                 }
00158             /* convert values in table to mps */
00159             if ( ConvertWindSpeedUnitsToMps(swsp_tbl, units) )  {
00160                 fclose(fstream);
00161                 ERR_ERROR("Unable to convert values in historical Wind Speed table to mps. \n", ERR_EFAILED);
00162                 }
00163             /* cleanup */
00164             free(units);
00165             fclose(fstream);                
00166             }
00167         /* find wind azimuth from random record in table that is not NO DATA */
00168         do  {
00169             /* retrieve a record at random from table within range 0 to num_recs */
00170             /* convert u.r.n. range 0-max to range a-b = a + (((b-a)*rand)/randmax) */
00171             /*row = 0 + (((DBLTWODARRAY_SIZE_ROW(swsp_tbl) - 0) * randi(0)) / LONG_MAX);*/
00172             /* appears to be flawed bc always returns 0 */
00173             row = randi(0) % DBLTWODARRAY_SIZE_ROW(swsp_tbl);           
00174             col = WIND_SPD_HR_TO_WSP_TBL_INDEX(hour);
00175             /* retrieve waz from random recno using current hour */
00176             swsp = DBLTWODARRAY_GET_DATA(swsp_tbl, row, col);       
00177             } while( swsp == WIND_SPD_WSP_NO_DATA_VALUE );
00178         /* set {month, day, hour} for future calls */
00179         smonth = month;
00180         sday = day;
00181         shour = hour;
00182         }
00183         
00184     *wspmps = WIND_SPD_REF_HGT_TO_MIDFLAME(swsp, WIND_SPD_RAWS_REF_HGT_METERS, fbedhgtm);
00185                 
00186     return ERR_SUCCESS; 
00187     }
00188     
00189 int GetWindSpeedMpsSPATIALFromProps(ChHashTable * proptbl, double fbedhgtm, int month, int day, int hour, 
00190                                                 unsigned int row, unsigned int col, double * wspmps)        {
00191     /* WA: stop warnings from unused vars */
00192     month = day = hour = row = col = fbedhgtm = *wspmps = 0;
00193     if ( proptbl == NULL )  {
00194         ERR_ERROR("Simulation properties table not initialized. \n", ERR_EINVAL);
00195         }
00196     
00197     ERR_ERROR("SPATIAL option for WIND_SPEED_TYPE not yet implemented. \n", ERR_EUNIMPL);
00198     }
00199 
00200 double ConvertWindSpeedAtRefHgtToArbitraryHgt(double wsmps, double refhgtm, double hgtm)    {
00201     return wsmps / log(((refhgtm + (0.36*hgtm)) / (0.13*hgtm)));
00202     }
00203 
00204 /*
00205  * Visibility:
00206  * global
00207  *
00208  * Description:
00209  * This function converts the units of windspeed provided as a DblTwoDArray of values, from
00210  * values given in the form of the keywords to the common denominator units of meters per
00211  * second.  The unit keyword expected is either WIND_SPD_WSP_KEYWORD_MILEPHR or 
00212  * WIND_SPD_WSP_KEYWORD_KMPHR.
00213  *
00214  * Arguments:
00215  * wsp_tbl- two-dimensional array of double values corresponding to windspeed
00216  * units- units windspeed values are stored as
00217  *
00218  * Returns:
00219  * ERR_SUCCESS (0) if operation successful, an error code otherwise.
00220  * Best use of this facility is as follows...
00221  * int error_status = CallFunctionXXX();
00222  * if ( error_status)  something bad happened
00223  */
00224 int ConvertWindSpeedUnitsToMps(DblTwoDArray * wsp_tbl, const char * units)  {
00225     int rec, hr;
00226     double wsp2cvrt;
00227 
00228     /* check args */
00229     if ( wsp_tbl == NULL || units == NULL ) {
00230         ERR_ERROR("Arguments to convert values in WindSpeed table are invalid. \n", ERR_EINVAL);
00231         }
00232 
00233     if ( strcmp(units, WIND_SPD_WSP_KEYWORD_MILEPHR) == 0 ) {
00234         /* convert all hourly values not equal to NO DATA from mphr to mps */
00235         for(rec = 0; rec < DBLTWODARRAY_SIZE_ROW(wsp_tbl); rec++)   {
00236             for(hr = 0; hr < 24; hr++)  {
00237                 if ( (wsp2cvrt = DBLTWODARRAY_GET_DATA(wsp_tbl, rec, WIND_SPD_HR_TO_WSP_TBL_INDEX(hr))) !=
00238                             WIND_SPD_WSP_NO_DATA_VALUE )    {
00239                     DBLTWODARRAY_SET_DATA(wsp_tbl, rec, WIND_SPD_HR_TO_WSP_TBL_INDEX(hr), 
00240                                                 UNITS_MILEPHR_TO_MPSEC(wsp2cvrt));
00241                     }
00242                 }
00243             }
00244         }
00245     else if ( strcmp(units, WIND_SPD_WSP_KEYWORD_KMPHR) == 0 )  {
00246         /* convert all hourly values not equal to NO DATA from kmphr to mps */
00247         for(rec = 0; rec < DBLTWODARRAY_SIZE_ROW(wsp_tbl); rec++)   {
00248             for(hr = 0; hr < 24; hr++)  {
00249                 if ( (wsp2cvrt = DBLTWODARRAY_GET_DATA(wsp_tbl, rec, WIND_SPD_HR_TO_WSP_TBL_INDEX(hr))) !=
00250                             WIND_SPD_WSP_NO_DATA_VALUE )    {
00251                     DBLTWODARRAY_SET_DATA(wsp_tbl, rec, WIND_SPD_HR_TO_WSP_TBL_INDEX(hr), 
00252                                                 UNITS_KMPHR_TO_MPSEC(wsp2cvrt));
00253                     }
00254                 }
00255             }
00256         }
00257     else    {
00258         ERR_ERROR("Unable to determine units in Wind Speed table, values not converted. \n", ERR_EDOM);
00259         }
00260 
00261     return ERR_SUCCESS;
00262     }
00263                                                             
00264 /* end of WindSpd.c */

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