lamarquendep.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file lamarquendep.cpp
  3. /// \brief Functionality for reading the Lamarque Nitrogen deposition data set
  4. ///
  5. /// $Date$
  6. ///
  7. ///////////////////////////////////////////////////////////////////////////////////////
  8. #include "config.h"
  9. #include "lamarquendep.h"
  10. #include "shell.h"
  11. #include "guessmath.h"
  12. #include "guessstring.h"
  13. #include <stdio.h>
  14. #include <string>
  15. // header files for reading binary data archive of global nitrogen deposition
  16. #include "GlobalNitrogenDeposition.h"
  17. #include "GlobalNitrogenDepositionRCP26.h"
  18. #include "GlobalNitrogenDepositionRCP45.h"
  19. #include "GlobalNitrogenDepositionRCP60.h"
  20. #include "GlobalNitrogenDepositionRCP85.h"
  21. namespace Lamarque {
  22. timeseriestype parse_timeseries(const std::string& str) {
  23. std::string strupper = to_upper(str);
  24. if (strupper == "HISTORIC") {
  25. return HISTORIC;
  26. }
  27. else if (strupper == "RCP26") {
  28. return RCP26;
  29. }
  30. else if (strupper == "RCP45") {
  31. return RCP45;
  32. }
  33. else if (strupper == "RCP60") {
  34. return RCP60;
  35. }
  36. else if (strupper == "RCP85") {
  37. return RCP85;
  38. }
  39. else if (strupper == "FIXED") {
  40. return FIXED;
  41. }
  42. else {
  43. fail("Unrecognized timeseries type: %s", str.c_str());
  44. return FIXED;
  45. }
  46. }
  47. const double convert = 1e-7; // converting from gN ha-1 to kgN m-2
  48. NDepData::NDepData() : timeseries(FIXED) {
  49. set_to_pre_industrial();
  50. }
  51. // Template function for getting the scenario data,
  52. // using different FastArchive classes depending on RCP
  53. template<typename ArchiveType, typename RecordType>
  54. void get_scenario(const char* file_ndep, const char* scen_suffix,
  55. double lon, double lat,
  56. double NHxDryDep[NYEAR_SCENNDEP][12],
  57. double NHxWetDep[NYEAR_SCENNDEP][12],
  58. double NOyDryDep[NYEAR_SCENNDEP][12],
  59. double NOyWetDep[NYEAR_SCENNDEP][12]) {
  60. xtring scenario_filename = file_ndep;
  61. // insert the scenario suffix (e.g. "RCP26") into the filename
  62. long position = scenario_filename.find(".bin");
  63. if (position != -1) {
  64. scenario_filename = scenario_filename.left(position) + xtring(scen_suffix) + ".bin";
  65. }
  66. else {
  67. fail("Invalid filename for ndep archive: %s", file_ndep);
  68. }
  69. ArchiveType ark;
  70. if (!ark.open((char*)scenario_filename)) {
  71. fail("Could not open %s for input", (char*)scenario_filename);
  72. }
  73. RecordType rec;
  74. rec.longitude = lon;
  75. rec.latitude = lat;
  76. if (!ark.getindex(rec)) {
  77. // The coordinate wasn't found in the archive
  78. ark.close();
  79. fail("Grid cell not found in %s", (char*)scenario_filename);
  80. }
  81. else {
  82. // Found the record, get the values
  83. for (int y = 0; y < NYEAR_SCENNDEP; y++) {
  84. for (int m = 0; m < 12; m++) {
  85. NHxDryDep[y][m] = rec.NHxDry[y * 12 + m] * convert;
  86. NHxWetDep[y][m] = rec.NHxWet[y * 12 + m] * convert;
  87. NOyDryDep[y][m] = rec.NOyDry[y * 12 + m] * convert;
  88. NOyWetDep[y][m] = rec.NOyWet[y * 12 + m] * convert;
  89. }
  90. }
  91. ark.close();
  92. }
  93. }
  94. void NDepData::getndep(const char* file_ndep,
  95. double lon, double lat,
  96. timeseriestype timeseries) {
  97. this->timeseries = timeseries;
  98. if (std::string(file_ndep) == "" || timeseries == FIXED) {
  99. set_to_pre_industrial();
  100. this->timeseries = FIXED;
  101. }
  102. else {
  103. // read in historic (and possibly scenario)
  104. GlobalNitrogenDepositionArchive ark;
  105. if (!ark.open(file_ndep)) {
  106. fail("Could not open %s for input", (char*)file_ndep);
  107. }
  108. GlobalNitrogenDeposition rec;
  109. rec.longitude = lon;
  110. rec.latitude = lat;
  111. if (!ark.getindex(rec)) {
  112. ark.close();
  113. fail("Grid cell not found in %s", (char*)file_ndep);
  114. }
  115. // Found the record, get the values
  116. for (int y=0; y<NYEAR_HISTNDEP; y++) {
  117. for (int m=0; m<12; m++) {
  118. NHxDryDep[y][m] = rec.NHxDry[y*12+m] * convert;
  119. NHxWetDep[y][m] = rec.NHxWet[y*12+m] * convert;
  120. NOyDryDep[y][m] = rec.NOyDry[y*12+m] * convert;
  121. NOyWetDep[y][m] = rec.NOyWet[y*12+m] * convert;
  122. }
  123. }
  124. ark.close();
  125. // scenario?
  126. if (timeseries != HISTORIC && timeseries != FIXED) {
  127. double scen_NHxDryDep[NYEAR_SCENNDEP][12];
  128. double scen_NHxWetDep[NYEAR_SCENNDEP][12];
  129. double scen_NOyDryDep[NYEAR_SCENNDEP][12];
  130. double scen_NOyWetDep[NYEAR_SCENNDEP][12];
  131. switch (timeseries) {
  132. case RCP26:
  133. get_scenario<GlobalNitrogenDepositionRCP26Archive,
  134. GlobalNitrogenDepositionRCP26>(file_ndep, "RCP26",
  135. lon, lat,
  136. scen_NHxDryDep,
  137. scen_NHxWetDep,
  138. scen_NOyDryDep,
  139. scen_NOyWetDep);
  140. break;
  141. case RCP45:
  142. get_scenario<GlobalNitrogenDepositionRCP45Archive,
  143. GlobalNitrogenDepositionRCP45>(file_ndep, "RCP45",
  144. lon, lat,
  145. scen_NHxDryDep,
  146. scen_NHxWetDep,
  147. scen_NOyDryDep,
  148. scen_NOyWetDep);
  149. break;
  150. case RCP60:
  151. get_scenario<GlobalNitrogenDepositionRCP60Archive,
  152. GlobalNitrogenDepositionRCP60>(file_ndep, "RCP60",
  153. lon, lat,
  154. scen_NHxDryDep,
  155. scen_NHxWetDep,
  156. scen_NOyDryDep,
  157. scen_NOyWetDep);
  158. break;
  159. case RCP85:
  160. get_scenario<GlobalNitrogenDepositionRCP85Archive,
  161. GlobalNitrogenDepositionRCP85>(file_ndep, "RCP85",
  162. lon, lat,
  163. scen_NHxDryDep,
  164. scen_NHxWetDep,
  165. scen_NOyDryDep,
  166. scen_NOyWetDep);
  167. break;
  168. default:
  169. // shouldn't happen
  170. fail("Unexpected timeseriestype!");
  171. }
  172. // for the overlapping decade, use mean value of historic and scenario
  173. for (int m = 0; m < 12; m++) {
  174. NHxDryDep[NYEAR_HISTNDEP-1][m] = mean(NHxDryDep[NYEAR_HISTNDEP-1][m],
  175. scen_NHxDryDep[0][m]);
  176. NHxWetDep[NYEAR_HISTNDEP-1][m] = mean(NHxWetDep[NYEAR_HISTNDEP-1][m],
  177. scen_NHxWetDep[0][m]);
  178. NOyDryDep[NYEAR_HISTNDEP-1][m] = mean(NOyDryDep[NYEAR_HISTNDEP-1][m],
  179. scen_NOyDryDep[0][m]);
  180. NOyWetDep[NYEAR_HISTNDEP-1][m] = mean(NOyWetDep[NYEAR_HISTNDEP-1][m],
  181. scen_NOyWetDep[0][m]);
  182. }
  183. // for the rest, simply copy from scenario
  184. for (int y = 1; y < NYEAR_SCENNDEP; y++) {
  185. for (int m = 0; m < 12; m++) {
  186. NHxDryDep[NYEAR_HISTNDEP-1+y][m] = scen_NHxDryDep[y][m];
  187. NHxWetDep[NYEAR_HISTNDEP-1+y][m] = scen_NHxWetDep[y][m];
  188. NOyDryDep[NYEAR_HISTNDEP-1+y][m] = scen_NOyDryDep[y][m];
  189. NOyWetDep[NYEAR_HISTNDEP-1+y][m] = scen_NOyWetDep[y][m];
  190. }
  191. }
  192. }
  193. }
  194. }
  195. // ecev3
  196. bool NDepData::getndep_nearest(const char* file_ndep,
  197. double lon, double lat,
  198. timeseriestype timeseries) {
  199. this->timeseries = timeseries;
  200. // ecev3 - exclude Antarctic and Greenland?
  201. bool isGreenland = lat < 85.0 && lat > 60.0 && lon > -70.0 && lon < -20.0;
  202. bool isAntarctica = lat < -65.0;
  203. if (std::string(file_ndep) == "" || timeseries == FIXED || isGreenland || isAntarctica) {
  204. set_to_pre_industrial();
  205. this->timeseries = FIXED;
  206. }
  207. else {
  208. // read in historic (and possibly scenario)
  209. GlobalNitrogenDepositionArchive ark;
  210. if (!ark.open(file_ndep)) {
  211. fail("Could not open %s for input", (char*)file_ndep);
  212. }
  213. // ecev3 - increase radius
  214. double searchradius = 60.0; // hardcode search radius to 3 degrees
  215. // These are the points we're looking for.
  216. double lamarquelon, lamarquelat;
  217. // Search all coordinates in a square around (lon, lat), but first go down to
  218. // multiple of 0.5 - ecev3 - add 0.25
  219. double center_lon = floor(lon*2)/2 + 0.25;
  220. double center_lat = floor(lat*2)/2 + 0.25;
  221. // Enumerate all coordinates within the square, place them in a vector of
  222. // pairs where the first element is distance from center to allow easy
  223. // sorting.
  224. using std::pair;
  225. using std::make_pair;
  226. typedef pair<double, double> point;
  227. std::vector<pair<double, point> > search_points;
  228. const double STEP = 0.5;
  229. const double EPS = 1e-15;
  230. for (double y = center_lon-searchradius; y <= center_lon+searchradius+EPS; y += STEP) {
  231. for (double x = center_lat-searchradius; x <= center_lat+searchradius+EPS; x += STEP) {
  232. double xdist = x-center_lat;
  233. double ydist = y-center_lon;
  234. double dist = sqrt(xdist*xdist + ydist*ydist);
  235. if (dist <= searchradius + EPS) {
  236. search_points.push_back(make_pair(dist, make_pair(y, x)));
  237. }
  238. }
  239. }
  240. // Sort by increasing distance
  241. std::sort(search_points.begin(), search_points.end());
  242. GlobalNitrogenDeposition rec;
  243. // Find closest coordinate which can be found in the main Ndep binary file
  244. bool indexfound = false;
  245. for (unsigned int i = 0; i < search_points.size(); i++) {
  246. if (!indexfound) {
  247. point search_point = search_points[i].second;
  248. rec.longitude = search_point.first;
  249. rec.latitude = search_point.second;
  250. if (ark.getindex(rec)) {
  251. lamarquelon = rec.longitude;
  252. lamarquelat = rec.latitude;
  253. indexfound = true;
  254. }
  255. }
  256. }
  257. if (!indexfound) {
  258. printf("LPJG ERROR - Could not find Ndep data in N dep binary file for lon=%g, lat=%g\n",lon,lat);
  259. ark.close();
  260. return false;
  261. }
  262. // Found the record, get the values
  263. for (int y=0; y<NYEAR_HISTNDEP; y++) {
  264. for (int m=0; m<12; m++) {
  265. NHxDryDep[y][m] = rec.NHxDry[y*12+m] * convert;
  266. NHxWetDep[y][m] = rec.NHxWet[y*12+m] * convert;
  267. NOyDryDep[y][m] = rec.NOyDry[y*12+m] * convert;
  268. NOyWetDep[y][m] = rec.NOyWet[y*12+m] * convert;
  269. }
  270. }
  271. ark.close();
  272. // scenario?
  273. if (timeseries != HISTORIC && timeseries != FIXED) {
  274. double scen_NHxDryDep[NYEAR_SCENNDEP][12];
  275. double scen_NHxWetDep[NYEAR_SCENNDEP][12];
  276. double scen_NOyDryDep[NYEAR_SCENNDEP][12];
  277. double scen_NOyWetDep[NYEAR_SCENNDEP][12];
  278. switch (timeseries) {
  279. case RCP26:
  280. get_scenario<GlobalNitrogenDepositionRCP26Archive,
  281. GlobalNitrogenDepositionRCP26>(file_ndep, "RCP26",
  282. lamarquelon, lamarquelat,
  283. scen_NHxDryDep,
  284. scen_NHxWetDep,
  285. scen_NOyDryDep,
  286. scen_NOyWetDep);
  287. break;
  288. case RCP45:
  289. get_scenario<GlobalNitrogenDepositionRCP45Archive,
  290. GlobalNitrogenDepositionRCP45>(file_ndep, "RCP45",
  291. lamarquelon, lamarquelat,
  292. scen_NHxDryDep,
  293. scen_NHxWetDep,
  294. scen_NOyDryDep,
  295. scen_NOyWetDep);
  296. break;
  297. case RCP60:
  298. get_scenario<GlobalNitrogenDepositionRCP60Archive,
  299. GlobalNitrogenDepositionRCP60>(file_ndep, "RCP60",
  300. lamarquelon, lamarquelat,
  301. scen_NHxDryDep,
  302. scen_NHxWetDep,
  303. scen_NOyDryDep,
  304. scen_NOyWetDep);
  305. break;
  306. case RCP85:
  307. get_scenario<GlobalNitrogenDepositionRCP85Archive,
  308. GlobalNitrogenDepositionRCP85>(file_ndep, "RCP85",
  309. lamarquelon, lamarquelat,
  310. scen_NHxDryDep,
  311. scen_NHxWetDep,
  312. scen_NOyDryDep,
  313. scen_NOyWetDep);
  314. break;
  315. default:
  316. // shouldn't happen
  317. fail("Unexpected timeseriestype!");
  318. }
  319. // for the overlapping decade, use mean value of historic and scenario
  320. for (int m = 0; m < 12; m++) {
  321. NHxDryDep[NYEAR_HISTNDEP-1][m] = mean(NHxDryDep[NYEAR_HISTNDEP-1][m],
  322. scen_NHxDryDep[0][m]);
  323. NHxWetDep[NYEAR_HISTNDEP-1][m] = mean(NHxWetDep[NYEAR_HISTNDEP-1][m],
  324. scen_NHxWetDep[0][m]);
  325. NOyDryDep[NYEAR_HISTNDEP-1][m] = mean(NOyDryDep[NYEAR_HISTNDEP-1][m],
  326. scen_NOyDryDep[0][m]);
  327. NOyWetDep[NYEAR_HISTNDEP-1][m] = mean(NOyWetDep[NYEAR_HISTNDEP-1][m],
  328. scen_NOyWetDep[0][m]);
  329. }
  330. // for the rest, simply copy from scenario
  331. for (int y = 1; y < NYEAR_SCENNDEP; y++) {
  332. for (int m = 0; m < 12; m++) {
  333. NHxDryDep[NYEAR_HISTNDEP-1+y][m] = scen_NHxDryDep[y][m];
  334. NHxWetDep[NYEAR_HISTNDEP-1+y][m] = scen_NHxWetDep[y][m];
  335. NOyDryDep[NYEAR_HISTNDEP-1+y][m] = scen_NOyDryDep[y][m];
  336. NOyWetDep[NYEAR_HISTNDEP-1+y][m] = scen_NOyWetDep[y][m];
  337. }
  338. }
  339. }
  340. }
  341. return true;
  342. }
  343. void NDepData::get_one_calendar_year(int calendar_year,
  344. double mndrydep[12],
  345. double mnwetdep[12]) {
  346. int ndep_year = 0;
  347. if (timeseries != FIXED && calendar_year >= Lamarque::FIRSTHISTYEARNDEP) {
  348. ndep_year = (int)((calendar_year - Lamarque::FIRSTHISTYEARNDEP)/10);
  349. }
  350. if (timeseries == HISTORIC && ndep_year >= NYEAR_HISTNDEP) {
  351. fail("Tried to get ndep for year %d (not included in Lamarque historic ndep data set)",
  352. calendar_year);
  353. }
  354. else if (timeseries != FIXED && ndep_year >= NYEAR_TOTNDEP) {
  355. fail("Tried to get ndep for year %d (not included in Lamarque historic or scenario ndep data set)",
  356. calendar_year);
  357. }
  358. for (int m = 0; m < 12; m++) {
  359. mndrydep[m] = NHxDryDep[ndep_year][m] + NOyDryDep[ndep_year][m];
  360. mnwetdep[m] = NHxWetDep[ndep_year][m] + NOyWetDep[ndep_year][m];
  361. }
  362. }
  363. void NDepData::set_to_pre_industrial() {
  364. // pre-industrial total nitrogen depostion set to 2 kgN/ha/year [kgN m-2]
  365. double dailyndep = 2000.0 / (4 * 365) * convert;
  366. for (int y=0; y<NYEAR_TOTNDEP; y++) {
  367. for (int m=0; m<12; m++) {
  368. NHxDryDep[y][m] = dailyndep;
  369. NHxWetDep[y][m] = dailyndep;
  370. NOyDryDep[y][m] = dailyndep;
  371. NOyWetDep[y][m] = dailyndep;
  372. }
  373. }
  374. }
  375. }