PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/data/processingwpdatasource.cpp

https://gitlab.com/wxmetvis/met.3d
C++ | 298 lines | 179 code | 51 blank | 68 comment | 22 complexity | 35548d3cb27d7915f23cb9366b6369c4 MD5 | raw file
  1. /******************************************************************************
  2. **
  3. ** This file is part of Met.3D -- a research environment for the
  4. ** three-dimensional visual exploration of numerical ensemble weather
  5. ** prediction data.
  6. **
  7. ** Copyright 2018-2020 Marc Rautenhaus [*, previously +]
  8. ** Copyright 2018 Bianca Tost [+]
  9. **
  10. ** * Regional Computing Center, Visual Data Analysis Group
  11. ** Universitaet Hamburg, Hamburg, Germany
  12. **
  13. ** + Computer Graphics and Visualization Group
  14. ** Technische Universitaet Muenchen, Garching, Germany
  15. **
  16. ** Met.3D is free software: you can redistribute it and/or modify
  17. ** it under the terms of the GNU General Public License as published by
  18. ** the Free Software Foundation, either version 3 of the License, or
  19. ** (at your option) any later version.
  20. **
  21. ** Met.3D is distributed in the hope that it will be useful,
  22. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. ** GNU General Public License for more details.
  25. **
  26. ** You should have received a copy of the GNU General Public License
  27. ** along with Met.3D. If not, see <http://www.gnu.org/licenses/>.
  28. **
  29. *******************************************************************************/
  30. #include "processingwpdatasource.h"
  31. // standard library imports
  32. #include <iostream>
  33. #include "assert.h"
  34. // related third party imports
  35. #include <log4cplus/loggingmacros.h>
  36. // local application imports
  37. #include "util/mutil.h"
  38. #include "util/mexception.h"
  39. using namespace std;
  40. namespace Met3D
  41. {
  42. /******************************************************************************
  43. *** CONSTRUCTOR / DESTRUCTOR ***
  44. *******************************************************************************/
  45. MProcessingWeatherPredictionDataSource::MProcessingWeatherPredictionDataSource()
  46. : MWeatherPredictionDataSource()
  47. {
  48. }
  49. /******************************************************************************
  50. *** PUBLIC METHODS ***
  51. *******************************************************************************/
  52. /******************************************************************************
  53. *** PROTECTED METHODS ***
  54. *******************************************************************************/
  55. MStructuredGrid*
  56. MProcessingWeatherPredictionDataSource::createAndInitializeResultGrid(
  57. MStructuredGrid *templateGrid)
  58. {
  59. MStructuredGrid *result = nullptr;
  60. switch (templateGrid->leveltype)
  61. {
  62. case PRESSURE_LEVELS_3D:
  63. result = new MRegularLonLatStructuredPressureGrid(
  64. templateGrid->nlevs, templateGrid->nlats,
  65. templateGrid->nlons);
  66. break;
  67. case HYBRID_SIGMA_PRESSURE_3D:
  68. result = new MLonLatHybridSigmaPressureGrid(
  69. templateGrid->nlevs, templateGrid->nlats,
  70. templateGrid->nlons);
  71. break;
  72. case AUXILIARY_PRESSURE_3D:
  73. {
  74. MLonLatAuxiliaryPressureGrid *templateGridAux =
  75. dynamic_cast<MLonLatAuxiliaryPressureGrid*>(templateGrid);
  76. result = new MLonLatAuxiliaryPressureGrid(
  77. templateGrid->nlevs, templateGrid->nlats,
  78. templateGrid->nlons, templateGridAux->reverseLevels);
  79. break;
  80. }
  81. case POTENTIAL_VORTICITY_2D:
  82. break;
  83. case SURFACE_2D:
  84. result = new MRegularLonLatGrid(templateGrid->nlats,
  85. templateGrid->nlons);
  86. break;
  87. case LOG_PRESSURE_LEVELS_3D:
  88. result = new MRegularLonLatLnPGrid(
  89. templateGrid->nlevs, templateGrid->nlats,
  90. templateGrid->nlons);
  91. break;
  92. default:
  93. break;
  94. }
  95. if (result == nullptr)
  96. {
  97. QString msg = QString("ERROR: Cannot intialize result grid. Level "
  98. "type %1 not implemented.")
  99. .arg(MStructuredGrid::verticalLevelTypeToString(
  100. templateGrid->leveltype));
  101. LOG4CPLUS_ERROR(mlog, msg.toStdString());
  102. throw MInitialisationError(msg.toStdString(), __FILE__, __LINE__);
  103. }
  104. // Set horizontal grid type and copy coordinate axes.
  105. result->setHorizontalGridType(templateGrid->getHorizontalGridType());
  106. for (unsigned int i = 0; i < templateGrid->nlons; i++)
  107. result->lons[i] = templateGrid->lons[i];
  108. for (unsigned int j = 0; j < templateGrid->nlats; j++)
  109. result->lats[j] = templateGrid->lats[j];
  110. for (unsigned int i = 0; i < templateGrid->nlevs; i++)
  111. result->levels[i] = templateGrid->levels[i];
  112. result->setAvailableMembers(templateGrid->getAvailableMembers());
  113. if (templateGrid->leveltype == HYBRID_SIGMA_PRESSURE_3D)
  114. {
  115. // Special treatment for hybrid model levels: copy ak/bk coeffs.
  116. MLonLatHybridSigmaPressureGrid *hybtemplate =
  117. dynamic_cast<MLonLatHybridSigmaPressureGrid*>(templateGrid);
  118. MLonLatHybridSigmaPressureGrid *hybresult =
  119. dynamic_cast<MLonLatHybridSigmaPressureGrid*>(result);
  120. for (unsigned int i = 0; i < hybtemplate->nlevs; i++)
  121. {
  122. hybresult->ak_hPa[i] = hybtemplate->ak_hPa[i];
  123. hybresult->bk[i] = hybtemplate->bk[i];
  124. }
  125. // Take care of the surface grid: use the surface grid of the template
  126. // grid.
  127. hybresult->surfacePressure = hybtemplate->getSurfacePressureGrid();
  128. // Increase the reference counter for this field (as done above by
  129. // containsData() or storeData()). NOTE: The field is released in
  130. // the destructor of "result" -- the reference is kept for the
  131. // entire lifetime of "result" to make sure the psfc field is not
  132. // deleted while "result" is still in memory.
  133. if ( !hybresult->surfacePressure->increaseReferenceCounter() )
  134. {
  135. // This should not happen.
  136. QString msg = QString("This is embarrassing: The data item "
  137. "for request %1 should have been in "
  138. "cache.").arg(
  139. hybresult->surfacePressure->getGeneratingRequest());
  140. throw MMemoryError(msg.toStdString(), __FILE__, __LINE__);
  141. }
  142. } // grid is hybrid sigma pressure
  143. else if (templateGrid->leveltype == AUXILIARY_PRESSURE_3D)
  144. {
  145. // Special treatment for auxiliary pressure levels: copy pointer to
  146. // auxiliary 3D pressure field.
  147. MLonLatAuxiliaryPressureGrid *auxptemplate =
  148. dynamic_cast<MLonLatAuxiliaryPressureGrid*>(templateGrid);
  149. MLonLatAuxiliaryPressureGrid *auxresult =
  150. dynamic_cast<MLonLatAuxiliaryPressureGrid*>(result);
  151. auxresult->auxPressureField_hPa =
  152. auxptemplate->getAuxiliaryPressureFieldGrid();
  153. // Increase the reference counter for this field (as done above by
  154. // containsData() or storeData()). NOTE: The field is released in
  155. // the destructor of "result" -- the reference is kept for the
  156. // entire lifetime of "result" to make sure the pressure field is
  157. // not deleted while "result" is still in memory.
  158. if ( !auxresult->auxPressureField_hPa->increaseReferenceCounter() )
  159. {
  160. // This should not happen.
  161. QString msg = QString("This is embarrassing: The data item "
  162. "for request %1 should have been in "
  163. "cache.").arg(
  164. auxresult->auxPressureField_hPa->getGeneratingRequest());
  165. LOG4CPLUS_ERROR(mlog, msg.toStdString());
  166. throw MMemoryError(msg.toStdString(), __FILE__, __LINE__);
  167. }
  168. } // grid is auxiliary pressure 3D
  169. return result;
  170. }
  171. /******************************************************************************
  172. *** CONSTRUCTOR / DESTRUCTOR ***
  173. *******************************************************************************/
  174. MSingleInputProcessingWeatherPredictionDataSource::MSingleInputProcessingWeatherPredictionDataSource()
  175. : MProcessingWeatherPredictionDataSource()
  176. {
  177. }
  178. /******************************************************************************
  179. *** PUBLIC METHODS ***
  180. *******************************************************************************/
  181. void MSingleInputProcessingWeatherPredictionDataSource::setInputSource(
  182. MWeatherPredictionDataSource* s)
  183. {
  184. inputSource = s;
  185. registerInputSource(inputSource);
  186. enablePassThrough(s);
  187. }
  188. QList<MVerticalLevelType>
  189. MSingleInputProcessingWeatherPredictionDataSource::availableLevelTypes()
  190. {
  191. assert(inputSource != nullptr);
  192. return inputSource->availableLevelTypes();
  193. }
  194. QStringList
  195. MSingleInputProcessingWeatherPredictionDataSource::availableVariables(
  196. MVerticalLevelType levelType)
  197. {
  198. assert(inputSource != nullptr);
  199. return inputSource->availableVariables(levelType);
  200. }
  201. QSet<unsigned int>
  202. MSingleInputProcessingWeatherPredictionDataSource::availableEnsembleMembers(
  203. MVerticalLevelType levelType, const QString& variableName)
  204. {
  205. assert(inputSource != nullptr);
  206. return inputSource->availableEnsembleMembers(levelType, variableName);
  207. }
  208. QList<QDateTime>
  209. MSingleInputProcessingWeatherPredictionDataSource::availableInitTimes(
  210. MVerticalLevelType levelType, const QString& variableName)
  211. {
  212. assert(inputSource != nullptr);
  213. return inputSource->availableInitTimes(levelType, variableName);
  214. }
  215. QList<QDateTime>
  216. MSingleInputProcessingWeatherPredictionDataSource::availableValidTimes(
  217. MVerticalLevelType levelType,
  218. const QString& variableName, const QDateTime& initTime)
  219. {
  220. assert(inputSource != nullptr);
  221. return inputSource->availableValidTimes(levelType, variableName, initTime);
  222. }
  223. QString MSingleInputProcessingWeatherPredictionDataSource::variableLongName(
  224. MVerticalLevelType levelType,
  225. const QString& variableName)
  226. {
  227. assert(inputSource != nullptr);
  228. return inputSource->variableLongName(levelType, variableName);
  229. }
  230. QString MSingleInputProcessingWeatherPredictionDataSource::variableStandardName(
  231. MVerticalLevelType levelType,
  232. const QString& variableName)
  233. {
  234. assert(inputSource != nullptr);
  235. return inputSource->variableStandardName(levelType, variableName);
  236. }
  237. QString MSingleInputProcessingWeatherPredictionDataSource::variableUnits(
  238. MVerticalLevelType levelType,
  239. const QString& variableName)
  240. {
  241. assert(inputSource != nullptr);
  242. return inputSource->variableUnits(levelType, variableName);
  243. }
  244. /******************************************************************************
  245. *** PROTECTED METHODS ***
  246. *******************************************************************************/
  247. } // namespace Met3D