demoinput.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file demoinput.cpp
  3. /// \brief LPJ-GUESS input module for a toy data set (for demonstration purposes)
  4. ///
  5. ///
  6. /// \author Ben Smith
  7. /// $Date: 2013-11-22 10:56:18 +0100 (Fri, 22 Nov 2013) $
  8. ///
  9. ///////////////////////////////////////////////////////////////////////////////////////
  10. #include "config.h"
  11. #include "demoinput.h"
  12. #include "driver.h"
  13. #include "outputchannel.h"
  14. #include <stdio.h>
  15. REGISTER_INPUT_MODULE("demo", DemoInput)
  16. // Anonymous namespace for variables and functions with file scope
  17. namespace {
  18. // File names for temperature, precipitation, sunshine and soil code driver files
  19. xtring file_temp,file_prec,file_sun,file_soil;
  20. // LPJ soil code
  21. int soilcode;
  22. /// Interpolates monthly data to quasi-daily values.
  23. void interp_climate(double* mtemp, double* mprec, double* msun, double* mdtr,
  24. double* dtemp, double* dprec, double* dsun, double* ddtr) {
  25. interp_monthly_means_conserve(mtemp, dtemp, date.is_leap(date.year));
  26. interp_monthly_totals_conserve(mprec, dprec, date.is_leap(date.year), 0);
  27. interp_monthly_means_conserve(msun, dsun, date.is_leap(date.year), 0, 100);
  28. interp_monthly_means_conserve(mdtr, ddtr, date.is_leap(date.year), 0);
  29. }
  30. } // namespace
  31. DemoInput::DemoInput()
  32. : nyear(1) {
  33. // Declare instruction file parameters
  34. declare_parameter("nyear", &nyear, 1, 10000, "Number of simulation years to run after spinup");
  35. }
  36. bool DemoInput::read_from_file(Coord coord, xtring fname, const char* format,
  37. double monthly[12], bool soil /* = false */) {
  38. double dlon, dlat;
  39. int elev;
  40. FILE* in = fopen(fname, "r");
  41. if (!in) {
  42. fail("readenv: could not open %s for input", (char*)fname);
  43. }
  44. bool foundgrid = false;
  45. while (!feof(in) && !foundgrid) {
  46. if (!soil) {
  47. readfor(in, format, &dlon, &dlat, &elev, monthly);
  48. } else {
  49. readfor(in, format, &dlon, &dlat, &soilcode);
  50. }
  51. foundgrid = equal(coord.lon, dlon) && equal(coord.lat, dlat);
  52. }
  53. fclose(in);
  54. if (!foundgrid) {
  55. dprintf("readenv: could not find record for (%g,%g) in %s",
  56. coord.lon, coord.lat, (char*)fname);
  57. }
  58. return foundgrid;
  59. }
  60. bool DemoInput::readenv(Coord coord, long& seed) {
  61. // Searches for environmental data in driver temperature, precipitation,
  62. // sunshine and soil code files for the grid cell whose coordinates are given by
  63. // 'coord'. Data are written to arrays mtemp, mprec, msun and the variable
  64. // soilcode, which are defined as global variables in this file
  65. // The temperature, precipitation and sunshine files (Cramer & Leemans,
  66. // unpublished) should be in ASCII text format and contain one-line records for the
  67. // climate (mean monthly temperature, mean monthly percentage sunshine or total
  68. // monthly precipitation) of a particular 0.5 x 0.5 degree grid cell. Elevation is
  69. // also included in each grid cell record.
  70. // The following sample record from the temperature file:
  71. // " -4400 8300 293-298-311-316-239-105 -7 26 -3 -91-184-239-277"
  72. // corresponds to the following data:
  73. // longitude 44 deg (-=W)
  74. // latitude 83 deg (+=N)
  75. // elevation 293 m
  76. // mean monthly temperatures (deg C) -29.8 (Jan), -31.1 (Feb), ..., -27.7 (Dec)
  77. // The following sample record from the precipitation file:
  78. // " 12750 -200 223 190 165 168 239 415 465 486 339 218 162 149 180"
  79. // corresponds to the following data:
  80. // longitude 127.5 deg (+=E)
  81. // latitude 20 deg (-=S)
  82. // elevation 223 m
  83. // monthly precipitation sum (mm) 190 (Jan), 165 (Feb), ..., 180 (Dec)
  84. // The following sample record from the sunshine file:
  85. // " 2600 7000 293 0 20 38 37 31 28 28 25 21 17 9 7"
  86. // corresponds to the following data:
  87. // longitude 26 deg (+=E)
  88. // latitude 70 deg (+=N)
  89. // elevation 293 m
  90. // monthly mean %age of full sunshine 0 (Jan), 20 (Feb), ..., 7 (Dec)
  91. // The LPJ soil code file is in ASCII text format and contains one-line records for
  92. // each grid cell in the form:
  93. // <lon> <lat> <soilcode>
  94. // where <lon> = longitude as a floating point number (-=W, +=E)
  95. // <lat> = latitude as a floating point number (-=S, +=N)
  96. // <soilcode> = integer in the range 0 (no soil) to 9 (see function
  97. // soilparameters in driver module)
  98. // The fields in each record are separated by spaces
  99. double mtemp[12]; // monthly mean temperature (deg C)
  100. double mprec[12]; // monthly precipitation sum (mm)
  101. double msun[12]; // monthly mean percentage sunshine values
  102. double mwet[12]={31,28,31,30,31,30,31,31,30,31,30,31}; // number of rain days per month
  103. double mdtr[12]; // monthly mean diurnal temperature range (oC)
  104. for(int m=0; m<12; m++) {
  105. mdtr[m] = 0.;
  106. if (ifbvoc) {
  107. dprintf("WARNING: No data available for dtr in sample data set!\nNo daytime temperature correction for BVOC calculations applied.");
  108. }
  109. }
  110. bool gridfound = read_from_file(coord, file_temp, "f6.2,f5.2,i4,12f4.1", mtemp);
  111. if(gridfound)
  112. gridfound = read_from_file(coord, file_prec, "f6.2,f5.2,i4,12f4", mprec);
  113. if(gridfound)
  114. gridfound = read_from_file(coord, file_sun, "f6.2,f5.2,i4,12f3", msun);
  115. if(gridfound)
  116. gridfound = read_from_file(coord, file_soil, "f,f,i", msun, true); // msun is not used here: just dummy
  117. if(gridfound) {
  118. // Interpolate monthly values for environmental drivers to daily values
  119. // (relevant daily values will be sent to the framework each simulation
  120. // day in function getclimate, below)
  121. interp_climate(mtemp, mprec, msun, mdtr, dtemp, dprec, dsun, ddtr);
  122. // Recalculate precipitation values using weather generator
  123. // (from Dieter Gerten 021121)
  124. prdaily(mprec, dprec, mwet, seed);
  125. }
  126. return gridfound;
  127. }
  128. void DemoInput::init() {
  129. // DESCRIPTION
  130. // Initialises input (e.g. opening files), and reads in the gridlist
  131. //
  132. // Reads list of grid cells and (optional) description text from grid list file
  133. // This file should consist of any number of one-line records in the format:
  134. // <longitude> <latitude> [<description>]
  135. double dlon,dlat;
  136. bool eof=false;
  137. xtring descrip;
  138. // Read list of grid coordinates and store in global Coord object 'gridlist'
  139. // Retrieve name of grid list file as read from ins file
  140. xtring file_gridlist=param["file_gridlist"].str;
  141. FILE* in_grid=fopen(file_gridlist,"r");
  142. if (!in_grid) fail("initio: could not open %s for input",(char*)file_gridlist);
  143. gridlist.killall();
  144. first_call = true;
  145. while (!eof) {
  146. // Read next record in file
  147. eof=!readfor(in_grid,"f,f,a#",&dlon,&dlat,&descrip);
  148. if (!eof && !(dlon==0.0 && dlat==0.0)) { // ignore blank lines at end (if any)
  149. Coord& c=gridlist.createobj(); // add new coordinate to grid list
  150. c.lon=dlon;
  151. c.lat=dlat;
  152. c.descrip=descrip;
  153. }
  154. }
  155. fclose(in_grid);
  156. // Retrieve specified CO2 value as read from ins file
  157. co2=param["co2"].num;
  158. // Retrieve specified N value as read from ins file
  159. ndep=param["ndep"].num;
  160. // Open landcover files
  161. landcover_input.init();
  162. // Open management files
  163. management_input.init();
  164. // Retrieve input file names as read from ins file
  165. file_temp=param["file_temp"].str;
  166. file_prec=param["file_prec"].str;
  167. file_sun=param["file_sun"].str;
  168. file_soil=param["file_soil"].str;
  169. // Set timers
  170. tprogress.init();
  171. tmute.init();
  172. tprogress.settimer();
  173. tmute.settimer(MUTESEC);
  174. }
  175. bool DemoInput::getgridcell(Gridcell& gridcell) {
  176. // See base class for documentation about this function's responsibilities
  177. // Select coordinates for next grid cell in linked list
  178. bool gridfound = false;
  179. bool LUerror = false;
  180. // Make sure we use the first gridcell in the first call to this function,
  181. // and then step through the gridlist in subsequent calls.
  182. if (first_call) {
  183. gridlist.firstobj();
  184. // Note that first_call is static, so this assignment is remembered
  185. // across function calls.
  186. first_call = false;
  187. }
  188. else gridlist.nextobj();
  189. if (gridlist.isobj) {
  190. while(!gridfound) {
  191. // Retrieve coordinate of next grid cell from linked list
  192. Coord& c = gridlist.getobj();
  193. // Load environmental data for this grid cell from files
  194. if(run_landcover) {
  195. LUerror = landcover_input.loadlandcover(gridlist.getobj().lon, gridlist.getobj().lat);
  196. if(!LUerror)
  197. LUerror = management_input.loadmanagement(gridlist.getobj().lon, gridlist.getobj().lat);
  198. }
  199. if (!LUerror) {
  200. gridfound = readenv(c, gridcell.seed);
  201. } else {
  202. gridlist.nextobj();
  203. if(!gridlist.isobj)
  204. return false;
  205. }
  206. }
  207. dprintf("\nCommencing simulation for stand at (%g,%g)",gridlist.getobj().lon,
  208. gridlist.getobj().lat);
  209. if (gridlist.getobj().descrip!="") dprintf(" (%s)\n\n",
  210. (char*)gridlist.getobj().descrip);
  211. else dprintf("\n\n");
  212. // Tell framework the coordinates of this grid cell
  213. gridcell.set_coordinates(gridlist.getobj().lon, gridlist.getobj().lat);
  214. // The insolation data will be sent (in function getclimate, below)
  215. // as percentage sunshine
  216. gridcell.climate.instype=SUNSHINE;
  217. // Tell framework the soil type of this grid cell
  218. soilparameters(gridcell.soiltype,soilcode);
  219. // For Windows shell - clear graphical output
  220. // (ignored on other platforms)
  221. clear_all_graphs();
  222. return true; // simulate this stand
  223. }
  224. return false; // no more stands
  225. }
  226. void DemoInput::getlandcover(Gridcell& gridcell) {
  227. landcover_input.getlandcover(gridcell);
  228. landcover_input.get_land_transitions(gridcell);
  229. }
  230. bool DemoInput::getclimate(Gridcell& gridcell) {
  231. // See base class for documentation about this function's responsibilities
  232. double progress;
  233. Climate& climate = gridcell.climate;
  234. // Send environmental values for today to framework
  235. climate.dndep = ndep / (365.0 * 10000.0);
  236. climate.co2 = co2;
  237. climate.temp = dtemp[date.day];
  238. climate.prec = dprec[date.day];
  239. climate.insol = dsun[date.day];
  240. // bvoc
  241. climate.dtr=ddtr[date.day];
  242. // First day of year only ...
  243. if (date.day == 0) {
  244. // Return false if last year was the last for the simulation
  245. if (date.year==nyear_spinup+nyear) return false;
  246. // Progress report to user and update timer
  247. if (tmute.getprogress()>=1.0) {
  248. progress=(double)(gridlist.getobj().id*(nyear_spinup+nyear)
  249. +date.year)/(double)(gridlist.nobj*(nyear_spinup+nyear));
  250. tprogress.setprogress(progress);
  251. dprintf("%3d%% complete, %s elapsed, %s remaining\n",(int)(progress*100.0),
  252. tprogress.elapsed.str,tprogress.remaining.str);
  253. tmute.settimer(MUTESEC);
  254. }
  255. }
  256. return true;
  257. }
  258. DemoInput::~DemoInput() {
  259. // Performs memory deallocation, closing of files or other "cleanup" functions.
  260. }