00001 /* ***************************************************** */ 00002 /* Compute relative humidity from specific humidity. */ 00003 /* spechum_to_hr.c */ 00004 /* ***************************************************** */ 00005 /* Author: Christian Page, CERFACS, Toulouse, France. */ 00006 /* ***************************************************** */ 00011 /* LICENSE BEGIN 00012 00013 Copyright Cerfacs (Christian Page) (2015) 00014 00015 christian.page@cerfacs.fr 00016 00017 This software is a computer program whose purpose is to downscale climate 00018 scenarios using a statistical methodology based on weather regimes. 00019 00020 This software is governed by the CeCILL license under French law and 00021 abiding by the rules of distribution of free software. You can use, 00022 modify and/ or redistribute the software under the terms of the CeCILL 00023 license as circulated by CEA, CNRS and INRIA at the following URL 00024 "http://www.cecill.info". 00025 00026 As a counterpart to the access to the source code and rights to copy, 00027 modify and redistribute granted by the license, users are provided only 00028 with a limited warranty and the software's author, the holder of the 00029 economic rights, and the successive licensors have only limited 00030 liability. 00031 00032 In this respect, the user's attention is drawn to the risks associated 00033 with loading, using, modifying and/or developing or reproducing the 00034 software by the user in light of its specific status of free software, 00035 that may mean that it is complicated to manipulate, and that also 00036 therefore means that it is reserved for developers and experienced 00037 professionals having in-depth computer knowledge. Users are therefore 00038 encouraged to load and test the software's suitability as regards their 00039 requirements in conditions enabling the security of their systems and/or 00040 data to be ensured and, more generally, to use and operate it in the 00041 same conditions as regards security. 00042 00043 The fact that you are presently reading this means that you have had 00044 knowledge of the CeCILL license and that you accept its terms. 00045 00046 LICENSE END */ 00047 00048 00049 00050 00051 00052 00053 00054 #include <utils.h> 00055 00057 void 00058 spechum_to_hr(double *hur, double *tas, double *hus, double *pmsl, double fillvalue, int ni, int nj) { 00059 00070 int i; /* Loop counter */ 00071 00072 double curtas; /* Current temperature value */ 00073 double curhus; /* Current specific humidity value */ 00074 double mixr; /* Mixing ratio */ 00075 double es; /* Saturation vapor pressure */ 00076 double fact; /* Factor */ 00077 00078 for (i=0; i<(ni*nj); i++) { 00079 00080 curtas = tas[i]; 00081 if (curtas != fillvalue) { 00082 curhus = hus[i] * 1000.0; 00083 00084 /* Begin by calculating the mixing ratio Q/(1.-Q/1000.) */ 00085 mixr = curhus / (1.0 - (curhus / 1000.0)); 00086 /* Compute relative humidity from the mixing ratio */ 00087 /* ; Mw*e e 00088 ; W (mixing ratio) = m_h2o/m_dry = -------- = Mw/Md * --- 00089 ; Md*(p-e) p-e 00090 ; 00091 ; RH (rel. hum.) = e/esat(T)*100. 00092 */ 00093 /* Compute saturation vapor pressure in hPa */ 00094 /* ; Formula with T = temperature in K 00095 ; esat = exp( -6763.6/(T+T0) - 4.9283*alog((T+T0)) + 54.2190 ) 00096 00097 ; Formula close to that of Magnus, 1844 with temperature TC in Celsius 00098 ; ESAT = 6.1078 * EXP( 17.2693882 * TC / (TC + 237.3) ) ; TC in Celsius 00099 00100 ; or Emanuel's formula (also approximation in form of Magnus' formula, 00101 ; 1844), which was taken from Bolton, Mon. Wea. Rev. 108, 1046-1053, 1980. 00102 ; This formula is very close to Goff and Gratch with differences of 00103 ; less than 0.25% between -50 and 0 deg C (and only 0.4% at -60degC) 00104 ; esat=6.112*EXP(17.67*TC/(243.5+TC)) 00105 00106 ; WMO reference formula is that of Goff and Gratch (1946), slightly 00107 ; modified by Goff in 1965: 00108 */ 00109 es = 1013.250 * pow( 10.0, ( 10.79586* (1.0-K_TKELVIN/curtas) - 00110 5.02808 * log10(curtas/K_TKELVIN) + 00111 1.50474 * 0.0001 * 00112 (1.0 - pow(10.0, (-8.29692*((curtas/K_TKELVIN)-1.0))) ) + 00113 0.42873 * 0.001 * 00114 (pow(10.0, (4.76955*(1.0-K_TKELVIN/curtas)))-1.0) - 2.2195983) ); 00115 fact = mixr / 1000.0 * (K_MD/K_MW); 00116 /* Use Standard Pressure for now, given altitude. 00117 For more precise values we should use instead a pressure field close to the weather type... */ 00118 hur[i] = pmsl[i] / es * fact / (1.0 + fact) * 100.0; 00119 if (hur[i] > 100.0) hur[i] = 100.0; 00120 if (hur[i] < 0.0) hur[i] = 0.0; 00121 } 00122 else 00123 hur[i] = fillvalue; 00124 } 00125 }