cru_ts30.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file cru.cpp
  3. /// \brief Functions for reading the CRU-NCEP data set
  4. ///
  5. /// $Date: 2013-11-04 16:30:55 +0100 (Mon, 04 Nov 2013) $
  6. ///
  7. ///////////////////////////////////////////////////////////////////////////////////////
  8. #include "config.h"
  9. #include "cru_ts30.h"
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <vector>
  13. // header files for the CRU-NCEP data archives
  14. #include "cruncep_1901_2015.h"
  15. #include "cruncep_1901_2015misc.h"
  16. namespace CRU_TS30 {
  17. bool searchcru(char* cruark,double dlon,double dlat,int& soilcode,
  18. double mtemp[NYEAR_HIST][12],
  19. double mprec[NYEAR_HIST][12],
  20. double msun[NYEAR_HIST][12]) {
  21. // !!!! NEW VERSION OF THIS FUNCTION - guess2008 - NEW VERSION OF THIS FUNCTION !!!!
  22. // Please note the new function signature.
  23. // Archive object. Definition in new header file, cru.h
  24. Cruncep_1901_2015Archive ark;
  25. int y,m;
  26. // Try block to catch any unexpected errors
  27. try {
  28. Cruncep_1901_2015 data; // struct to hold the data
  29. bool success = ark.open(cruark);
  30. if (success) {
  31. bool flag = ark.rewind();
  32. if (!flag) {
  33. ark.close(); // I.e. we opened it but we couldn't rewind
  34. return false;
  35. }
  36. }
  37. else
  38. return false;
  39. // The CRU archive index hold lons & lats as whole doubles * 10
  40. data.lon = dlon;
  41. data.lat = dlat;
  42. // Read the CRU data into the data struct
  43. success =ark.getindex(data);
  44. if (!success) {
  45. ark.close();
  46. return false;
  47. }
  48. // Transfer the data from the data struct to the arrays.
  49. soilcode=(int)data.soilcode[0];
  50. for (y = 0; y < NYEAR_HIST; y++) {
  51. for (m=0;m<12;m++) {
  52. mtemp[y][m] = data.mtemp[y*12+m]; // now degC
  53. mprec[y][m] = data.mprec[y*12+m]; // mm (sum over month)
  54. // Limit very low precip amounts because negligible precipitation causes problems
  55. // in the prdaily function (infinite loops).
  56. if (mprec[y][m] <= 1.0) mprec[y][m] = 0.0;
  57. msun[y][m] = data.mswrad[y*12+m]; // shortwave radiation
  58. }
  59. }
  60. // Close the archive
  61. ark.close();
  62. return true;
  63. }
  64. catch(...) {
  65. // Unknown error.
  66. return false;
  67. }
  68. }
  69. bool searchcru_misc(char* cruark,double dlon,double dlat,int& elevation,
  70. double mfrs[NYEAR_HIST][12],
  71. double mwet[NYEAR_HIST][12],
  72. double mdtr[NYEAR_HIST][12]) {
  73. // Please note the new function signature.
  74. // Archive object
  75. Cruncep_1901_2015miscArchive ark;
  76. int y,m;
  77. // Try block to catch any unexpected errors
  78. try {
  79. Cruncep_1901_2015misc data;
  80. bool success = ark.open(cruark);
  81. if (success) {
  82. bool flag = ark.rewind();
  83. if (!flag) {
  84. ark.close(); // I.e. we opened it but we couldn't rewind
  85. return false;
  86. }
  87. }
  88. else
  89. return false;
  90. // The CRU archive index hold lons & lats as whole doubles * 10
  91. data.lon = dlon;
  92. data.lat = dlat;
  93. // Read the CRU data into the data struct
  94. success =ark.getindex(data);
  95. if (!success) {
  96. ark.close();
  97. return false;
  98. }
  99. // Transfer the data from the data struct to the arrays.
  100. // Note that the multipliers are NOT the same as in searchcru above!
  101. elevation=(int)data.elv[0]; // km * 1000?
  102. for (y = 0; y < NYEAR_HIST; y++) {
  103. for (m=0;m<12;m++) {
  104. // guess2008 - catch rounding errors
  105. mfrs[y][m] = data.mfrs[y*12+m]; // days
  106. if (mfrs[y][m] < 0.1)
  107. mfrs[y][m] = 0.0; // Catches rounding errors
  108. mwet[y][m] = data.mwet[y*12+m]; // days
  109. if (mwet[y][m] <= 0.1)
  110. mwet[y][m] = 0.0; // Catches rounding errors
  111. mdtr[y][m] = data.mdtr[y*12+m]; // degC
  112. // For some reason there are negative dtr values in
  113. // the CRU binaries(!). Set these to zero for now.
  114. mdtr[y][m] = max(0.0, mdtr[y][m]);
  115. /*
  116. If vapour pressure is needed:
  117. mvap[y][m] = data.mvap[y*12+m];
  118. */
  119. }
  120. }
  121. // Close the archive
  122. ark.close();
  123. return true;
  124. }
  125. catch(...) {
  126. // Unknown error.
  127. return false;
  128. }
  129. }
  130. bool findnearestCRUdata(double searchradius, char* cruark, double& lon, double& lat,
  131. int& scode,
  132. double hist_mtemp1[NYEAR_HIST][12],
  133. double hist_mprec1[NYEAR_HIST][12],
  134. double hist_msun1[NYEAR_HIST][12]) {
  135. // First try the exact coordinate
  136. if (searchcru(cruark, lon, lat, scode, hist_mtemp1, hist_mprec1, hist_msun1)) {
  137. return true;
  138. }
  139. if (searchradius == 0) {
  140. // Don't try to search
  141. return false;
  142. }
  143. // Search all coordinates in a square around (lon, lat), but first go down to
  144. // multiple of 0.5
  145. double center_lon = floor(lon*2)/2 + 0.25;
  146. double center_lat = floor(lat*2)/2 + 0.25;
  147. // Enumerate all coordinates within the square, place them in a vector of
  148. // pairs where the first element is distance from center to allow easy
  149. // sorting.
  150. using std::pair;
  151. using std::make_pair;
  152. typedef pair<double, double> point;
  153. std::vector<pair<double, point> > search_points;
  154. const double STEP = 0.5;
  155. const double EPS = 1e-15;
  156. for (double y = center_lon-searchradius; y <= center_lon+searchradius+EPS; y += STEP) {
  157. for (double x = center_lat-searchradius; x <= center_lat+searchradius+EPS; x += STEP) {
  158. double xdist = x - lat;
  159. double ydist = y - lon;
  160. double dist = sqrt(xdist*xdist + ydist*ydist);
  161. if (dist <= searchradius + EPS) {
  162. search_points.push_back(make_pair(dist, make_pair(y, x)));
  163. }
  164. }
  165. }
  166. // Sort by increasing distance
  167. std::sort(search_points.begin(), search_points.end());
  168. // Find closest coordinate which can be found in CRU
  169. for (unsigned int i = 0; i < search_points.size(); i++) {
  170. point search_point = search_points[i].second;
  171. double search_lon = search_point.first;
  172. double search_lat = search_point.second;
  173. if (searchcru(cruark, search_lon, search_lat, scode,
  174. hist_mtemp1, hist_mprec1, hist_msun1)) {
  175. lon = search_lon;
  176. lat = search_lat;
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. }