functions.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <grass/gis.h>
  5. #include <math.h>
  6. #include "local_proto.h"
  7. extern CELL f_c(CELL);
  8. extern FCELL f_f(FCELL);
  9. extern DCELL f_d(DCELL);
  10. //#define TRUE 0
  11. //#define FALSE 1
  12. /* constant definition */
  13. //#define k_sb 4.903 //[MJ/m2*h] Stefan Bolzman constant
  14. #define cp 1.013 //[kJ/kg*°C] specific heat of moist air
  15. #define epsilon 0.622 //[-] ratio of molecular weigth of water to dry air
  16. #define Po 101.3 //[kPa] atmospheric pressure at sea level
  17. #define Tko 293.16 //[K] reference temperature at sea level
  18. #define eta 0.0065 //[K/m] constant lapse rate
  19. #define Ao 0 //[m] altitude at sea level
  20. #define g 9.81 //[m/s] gravitational accelleration
  21. #define R 287 //[J/kg*K] specific gas constant
  22. #define Zw 2 //[m] height of wind measurements
  23. #define Zh 2 //[m] height of humidity measurements
  24. #define k 0.41 //[-] Von Karman constant
  25. DCELL calc_ETp(DCELL T, DCELL Z, DCELL u2, DCELL Rn, int night, DCELL Rh,
  26. DCELL hc)
  27. {
  28. DCELL ea, delta, gamma, gstar, lambda;
  29. DCELL P, ra, d, Zom, Zoh, G, ETrad, u10, rs, ed, Tkv, rho, ETaero, ETp;
  30. /* calculus: mean saturation vapoure pressure [KPa] */
  31. ea = 0.61078 * exp((17.27 * T) / (T + 237.3));
  32. /* calculus: slope of vapoure pressure curve [KPa/°C] */
  33. delta = (4098 * ea) / pow((237.3 + T), 2);
  34. /* calculus: latent heat vapourisation [MJ/kg] */
  35. lambda = 2.501 - (0.002361 * T);
  36. /* calculus: atmospheric pressure [KPa] */
  37. P = Po * pow(((Tko - eta * (Z - Ao)) / Tko), (g / (eta * R)));
  38. /* calculus: psichiometric constant [kPa/°C] */
  39. gamma = ((cp * P) / (epsilon * lambda)) * 0.001;
  40. /* calculus: aerodynamic resistance [s/m] */
  41. if (hc < 2) {
  42. d = (2 / 3) * hc;
  43. Zom = 0.123 * hc;
  44. Zoh = 0.1 * Zom;
  45. ra = (log((Zw - d) / Zom) * log((Zh - d) / Zoh)) / (k * k * u2);
  46. }
  47. else {
  48. u10 = u2 * (log((67.8 * 10) - 5.42)) / 4.87;
  49. ra = 94 / u10;
  50. }
  51. /* calculus: surface resistance [s/m] */
  52. rs = 100 / (0.5 * 24 * hc);
  53. /*calculus: modified psichiometric constant [kPa/°C] */
  54. gstar = gamma * (1 + (rs / ra));
  55. /*calculus: net radiation [MJ/m2*d] */
  56. /*Rn derived from r.sun */
  57. /*calculus: soil heat flux [MJ/m2*d] */
  58. if (night == FALSE)
  59. G = 0.1 * Rn;
  60. else
  61. G = 0.5 * Rn;
  62. /* calculus: radiation term [mm/h] */
  63. /* ETrad = (delta/(delta+gstar))*((Rn-G)/(lambda*1000000)); */
  64. ETrad = (delta / (delta + gstar)) * ((Rn - G) / lambda); /* torna da analisi dimensionale */
  65. /* calculus: actual vapoure pressure [kPa] */
  66. ed = Rh * ea / 100;
  67. /* calculus: virtual temperature [°C] */
  68. Tkv = (T + 273.15) / (1 - (0.378 * ed / P));
  69. /* calculus: atmospheric density [Kg/m^3] */
  70. rho = P / (Tkv * R / 100);
  71. /* calculus: aerodynamic term [mm/h] */
  72. /* ETaero = (0.001/lambda)*(1/(delta+gstar))*(rho*cp/ra)*(ea-ed); */
  73. ETaero = (3.6 / lambda) * (1 / (delta + gstar)) * (rho * cp / ra) * (ea - ed); /* torna da analisi dimensionale */
  74. /* calculus: potential evapotranspiration [mm/h] */
  75. ETp = ETrad + ETaero;
  76. return ETp;
  77. }
  78. DCELL calc_openwaterETp(DCELL T, DCELL Z, DCELL u2, DCELL Rn, int day,
  79. DCELL Rh, DCELL hc)
  80. {
  81. DCELL ea, delta, gamma, lambda;
  82. DCELL P, ed, ETaero, ETp;
  83. /* calculus: mean saturation vapoure pressure [KPa] */
  84. ea = 0.61078 * exp((17.27 * T) / (T + 237.3));
  85. /* calculus: slope of vapoure pressure curve [KPa/°C] */
  86. delta = (4098 * ea) / pow((237.3 + T), 2);
  87. /* calculus: latent heat vapourisation [MJ/kg] */
  88. lambda = 2.501 - (0.002361 * T);
  89. /* calculus: atmospheric pressure [KPa] */
  90. P = Po * pow(((Tko - eta * (Z - Ao)) / Tko), (g / (eta * R)));
  91. /* calculus: di psichiometric constant [kPa/°C] */
  92. gamma = ((cp * P) / (epsilon * lambda)) * 0.001;
  93. /*calculus: net radiation [MJ/m2*h] */
  94. /*Rn derived from r.sun
  95. /*calculus: actual vapoure pressure [kPa] */
  96. ed = Rh * ea / 100;
  97. /*calculus: aerodynamic term [mm/h] */
  98. /*ETaero = 0.35*(0.5+(0.621375*u2/100))*7.500638*(ea-ed); */
  99. /*to convert mm/d to mm/h it results: */
  100. ETaero =
  101. (0.35 / 24) * (0.5 + (0.621375 * u2 / 100)) * 7.500638 * (ea - ed);
  102. /*calculus: potential evapotranspiration [mm/h] */
  103. ETp = (((Rn * delta) / lambda) + (gamma * ETaero)) / (delta + gamma);
  104. return ETp;
  105. }
  106. /*
  107. DCELL calc_ETp_WaSiM(){
  108. //calculus of saturation vapour pressure at the temperature T: es[hPa]
  109. //with T[°C]
  110. es = 6.1078*exp((17.27*T)/(237.3+T);
  111. //tangent of the saturated vapour pressure curve: D[hPa/K] with T[°C]
  112. D = (25029/pow((273.3+T),2))*exp((17.27*T)/(237.3+T);
  113. //air pressure at level hM
  114. P = 1013*exp(-hM/(7991+29.33*Tv));
  115. //calculus of lambda [KJ/Kg] with T[°C]
  116. lambda = 2500.8 - 2.372*T;
  117. //calculus psichiometric constant gamma [hPa/°C] with cp=1.005[KJ/(Kg*K)]
  118. gamma = ((1.005*P)/(0.622*lambda))*0.001;
  119. //
  120. */