cfinput.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file cfinput.h
  3. /// \brief Input module for CF conforming NetCDF files
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date$
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #ifndef LPJ_GUESS_CFINPUT_H
  10. #define LPJ_GUESS_CFINPUT_H
  11. #ifdef HAVE_NETCDF
  12. #include "cruinput.h"
  13. #include "guessnc.h"
  14. #include <memory>
  15. #include <limits>
  16. class CFInput : public InputModule {
  17. public:
  18. CFInput();
  19. ~CFInput();
  20. void init();
  21. /// See base class for documentation about this function's responsibilities
  22. bool getgridcell(Gridcell& gridcell);
  23. /// See base class for documentation about this function's responsibilities
  24. bool getclimate(Gridcell& gridcell);
  25. /// See base class for documentation about this function's responsibilities
  26. void getlandcover(Gridcell& gridcell);
  27. /// Obtains land management data for one day
  28. void getmanagement(Gridcell& gridcell) {management_input.getmanagement(gridcell, landcover_input);}
  29. static const int NYEAR_SPINUP_DATA=30;
  30. private:
  31. /// Land cover input module
  32. LandcoverInput landcover_input;
  33. /// Management input module
  34. ManagementInput management_input;
  35. struct Coord {
  36. // Type for storing grid cell longitude, latitude and description text
  37. int rlon;
  38. int rlat;
  39. int landid;
  40. std::string descrip;
  41. };
  42. /// The grid cells to simulate
  43. std::vector<Coord> gridlist;
  44. /// The current grid cell to simulate
  45. std::vector<Coord>::iterator current_gridcell;
  46. /// Loads data from NetCDF files for current grid cell
  47. /** Returns the coordinates for the current grid cell, for
  48. * the closest CRU grid cell and the soilcode for the cell.
  49. * \returns whether it was possible to load data and find nearby CRU cell */
  50. bool load_data_from_files(double& lon, double& lat,
  51. double& cru_lon, double& cru_lat,
  52. int& soilcode);
  53. /// Gets the first few years of data from cf_var and puts it into spinup_data
  54. void load_spinup_data(const GuessNC::CF::GridcellOrderedVariable* cf_var,
  55. GenericSpinupData& spinup_data);
  56. /// Gets data for one year, for one variable.
  57. /** Returns either 12 or 365/366 values (depending on LPJ-GUESS year length, not
  58. * data set year length). Gets the values from spinup and/or historic period. */
  59. void get_yearly_data(std::vector<double>& data,
  60. const GenericSpinupData& spinup,
  61. GuessNC::CF::GridcellOrderedVariable* cf_historic,
  62. int& historic_timestep);
  63. /// Fills one array of daily values with forcing data for the current year
  64. void populate_daily_array(double* daily,
  65. const GenericSpinupData& spinup,
  66. GuessNC::CF::GridcellOrderedVariable* cf_historic,
  67. int& historic_timestep,
  68. double minimum = -std::numeric_limits<double>::max(),
  69. double maximum = std::numeric_limits<double>::max());
  70. /// Same as populate_daily_array, but for precipitation which is special
  71. /** Uses number of wet days if available and handles extensive/intensive conversion */
  72. void populate_daily_prec_array(long& seed);
  73. /// Fills dtemp, dprec, etc. with forcing data for the current year
  74. void populate_daily_arrays(long& seed);
  75. /// \returns all (used) variables
  76. std::vector<GuessNC::CF::GridcellOrderedVariable*> all_variables() const;
  77. /// Yearly CO2 data read from file
  78. /**
  79. * This object is indexed with calendar years, so to get co2 value for
  80. * year 1990, use co2[1990]. See documentation for GlobalCO2File for
  81. * more information.
  82. */
  83. GlobalCO2File co2;
  84. // The variables
  85. GuessNC::CF::GridcellOrderedVariable* cf_temp;
  86. GuessNC::CF::GridcellOrderedVariable* cf_prec;
  87. GuessNC::CF::GridcellOrderedVariable* cf_insol;
  88. GuessNC::CF::GridcellOrderedVariable* cf_wetdays;
  89. GuessNC::CF::GridcellOrderedVariable* cf_min_temp;
  90. GuessNC::CF::GridcellOrderedVariable* cf_max_temp;
  91. // Spinup data for each variable
  92. GenericSpinupData spinup_temp;
  93. GenericSpinupData spinup_prec;
  94. GenericSpinupData spinup_insol;
  95. GenericSpinupData spinup_wetdays;
  96. GenericSpinupData spinup_min_temp;
  97. GenericSpinupData spinup_max_temp;
  98. /// Temperature for current gridcell and current year (deg C)
  99. double dtemp[Date::MAX_YEAR_LENGTH];
  100. /// Precipitation for current gridcell and current year (mm/day)
  101. double dprec[Date::MAX_YEAR_LENGTH];
  102. /// Insolation for current gridcell and current year (\see instype)
  103. double dinsol[Date::MAX_YEAR_LENGTH];
  104. /// Daily N deposition for one year
  105. double dndep[Date::MAX_YEAR_LENGTH];
  106. /// Minimum temperature for current gridcell and current year (deg C)
  107. double dmin_temp[Date::MAX_YEAR_LENGTH];
  108. /// Maximum temperature for current gridcell and current year (deg C)
  109. double dmax_temp[Date::MAX_YEAR_LENGTH];
  110. /// Whether the forcing data for precipitation is an extensive quantity
  111. /** If given as an amount (kg m-2) per timestep it is extensive, if it's
  112. * given as a mean rate (kg m-2 s-1) it is an intensive quantity */
  113. bool extensive_precipitation;
  114. // Current timestep in CF files
  115. int historic_timestep_temp;
  116. int historic_timestep_prec;
  117. int historic_timestep_insol;
  118. int historic_timestep_wetdays;
  119. int historic_timestep_min_temp;
  120. int historic_timestep_max_temp;
  121. /// Path to CRU binary archive
  122. xtring file_cru;
  123. /// Nitrogen deposition forcing for current gridcell
  124. Lamarque::NDepData ndep;
  125. /// Nitrogen deposition time series to use (historic,rcp26,...)
  126. std::string ndep_timeseries;
  127. // Timers for keeping track of progress through the simulation
  128. Timer tprogress,tmute;
  129. static const int MUTESEC=20; // minimum number of sec to wait between progress messages
  130. };
  131. #endif // HAVE_NETCDF
  132. #endif // LPJ_GUESS_CFINPUT_H