[219] | 1 | #include "field.hpp" |
---|
| 2 | |
---|
[352] | 3 | #include "attribute_template.hpp" |
---|
| 4 | #include "object_template.hpp" |
---|
| 5 | #include "group_template.hpp" |
---|
[219] | 6 | |
---|
| 7 | #include "node_type.hpp" |
---|
| 8 | #include "calendar_util.hpp" |
---|
[352] | 9 | #include "message.hpp" |
---|
[591] | 10 | #include "xios_spl.hpp" |
---|
[352] | 11 | #include "type.hpp" |
---|
[638] | 12 | #include "timer.hpp" |
---|
[352] | 13 | #include "context_client.hpp" |
---|
[586] | 14 | #include "context_server.hpp" |
---|
[459] | 15 | #include <set> |
---|
[640] | 16 | #include "garbage_collector.hpp" |
---|
| 17 | #include "source_filter.hpp" |
---|
| 18 | #include "store_filter.hpp" |
---|
| 19 | #include "file_writer_filter.hpp" |
---|
[641] | 20 | #include "pass_through_filter.hpp" |
---|
[642] | 21 | #include "filter_expr_node.hpp" |
---|
| 22 | #include "lex_parser.hpp" |
---|
[643] | 23 | #include "temporal_filter.hpp" |
---|
[644] | 24 | #include "spatial_transform_filter.hpp" |
---|
[1025] | 25 | #include "file_server_writer_filter.hpp" |
---|
[219] | 26 | |
---|
[335] | 27 | namespace xios{ |
---|
[509] | 28 | |
---|
[219] | 29 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 30 | |
---|
| 31 | CField::CField(void) |
---|
| 32 | : CObjectTemplate<CField>(), CFieldAttributes() |
---|
| 33 | , grid(), file() |
---|
[707] | 34 | , written(false) |
---|
[645] | 35 | , nstep(0), nstepMax(0) |
---|
| 36 | , hasOutputFile(false) |
---|
[1129] | 37 | , domAxisScalarIds_(vector<StdString>(3,"")) |
---|
| 38 | , areAllReferenceSolved(false), isReferenceSolved(false), isReferenceSolvedAndTransformed(false) |
---|
[1417] | 39 | , isGridChecked(false) |
---|
[676] | 40 | , useCompressedOutput(false) |
---|
[1158] | 41 | , hasTimeInstant(false) |
---|
| 42 | , hasTimeCentered(false) |
---|
[1318] | 43 | , wasDataRequestedFromServer(false) |
---|
[1021] | 44 | , wasDataAlreadyReceivedFromServer(false) |
---|
[1358] | 45 | , mustAutoTrigger(false) |
---|
[1232] | 46 | , isEOF(false), nstepMaxRead(false) |
---|
[957] | 47 | { setVirtualVariableGroup(CVariableGroup::create(getId() + "_virtual_variable_group")); } |
---|
[219] | 48 | |
---|
[562] | 49 | CField::CField(const StdString& id) |
---|
[219] | 50 | : CObjectTemplate<CField>(id), CFieldAttributes() |
---|
| 51 | , grid(), file() |
---|
[707] | 52 | , written(false) |
---|
[645] | 53 | , nstep(0), nstepMax(0) |
---|
| 54 | , hasOutputFile(false) |
---|
[1129] | 55 | , domAxisScalarIds_(vector<StdString>(3,"")) |
---|
| 56 | , areAllReferenceSolved(false), isReferenceSolved(false), isReferenceSolvedAndTransformed(false) |
---|
[1417] | 57 | , isGridChecked(false) |
---|
[676] | 58 | , useCompressedOutput(false) |
---|
[1158] | 59 | , hasTimeInstant(false) |
---|
| 60 | , hasTimeCentered(false) |
---|
[1318] | 61 | , wasDataRequestedFromServer(false) |
---|
[1021] | 62 | , wasDataAlreadyReceivedFromServer(false) |
---|
[1358] | 63 | , mustAutoTrigger(false) |
---|
[1232] | 64 | , isEOF(false), nstepMaxRead(false) |
---|
[957] | 65 | { setVirtualVariableGroup(CVariableGroup::create(getId() + "_virtual_variable_group")); } |
---|
[219] | 66 | |
---|
| 67 | CField::~CField(void) |
---|
[645] | 68 | {} |
---|
[509] | 69 | |
---|
[472] | 70 | //---------------------------------------------------------------- |
---|
| 71 | |
---|
| 72 | void CField::setVirtualVariableGroup(CVariableGroup* newVVariableGroup) |
---|
[509] | 73 | { |
---|
| 74 | this->vVariableGroup = newVVariableGroup; |
---|
[472] | 75 | } |
---|
[509] | 76 | |
---|
[472] | 77 | CVariableGroup* CField::getVirtualVariableGroup(void) const |
---|
| 78 | { |
---|
[562] | 79 | return this->vVariableGroup; |
---|
[472] | 80 | } |
---|
| 81 | |
---|
| 82 | std::vector<CVariable*> CField::getAllVariables(void) const |
---|
| 83 | { |
---|
[562] | 84 | return this->vVariableGroup->getAllChildren(); |
---|
[472] | 85 | } |
---|
[509] | 86 | |
---|
[562] | 87 | void CField::solveDescInheritance(bool apply, const CAttributeMap* const parent) |
---|
[472] | 88 | { |
---|
[562] | 89 | SuperClassAttribute::setAttributes(parent, apply); |
---|
[472] | 90 | this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL); |
---|
| 91 | } |
---|
[219] | 92 | |
---|
[645] | 93 | //---------------------------------------------------------------- |
---|
[509] | 94 | |
---|
[598] | 95 | bool CField::dispatchEvent(CEventServer& event) |
---|
[300] | 96 | { |
---|
[562] | 97 | if (SuperClass::dispatchEvent(event)) return true; |
---|
[300] | 98 | else |
---|
| 99 | { |
---|
| 100 | switch(event.type) |
---|
| 101 | { |
---|
| 102 | case EVENT_ID_UPDATE_DATA : |
---|
[562] | 103 | recvUpdateData(event); |
---|
| 104 | return true; |
---|
| 105 | break; |
---|
[472] | 106 | |
---|
[598] | 107 | case EVENT_ID_READ_DATA : |
---|
| 108 | recvReadDataRequest(event); |
---|
| 109 | return true; |
---|
| 110 | break; |
---|
[509] | 111 | |
---|
[598] | 112 | case EVENT_ID_READ_DATA_READY : |
---|
| 113 | recvReadDataReady(event); |
---|
| 114 | return true; |
---|
| 115 | break; |
---|
[509] | 116 | |
---|
[598] | 117 | case EVENT_ID_ADD_VARIABLE : |
---|
| 118 | recvAddVariable(event); |
---|
| 119 | return true; |
---|
| 120 | break; |
---|
| 121 | |
---|
| 122 | case EVENT_ID_ADD_VARIABLE_GROUP : |
---|
| 123 | recvAddVariableGroup(event); |
---|
| 124 | return true; |
---|
| 125 | break; |
---|
| 126 | |
---|
[300] | 127 | default : |
---|
[562] | 128 | ERROR("bool CField::dispatchEvent(CEventServer& event)", << "Unknown Event"); |
---|
| 129 | return false; |
---|
[300] | 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
[509] | 133 | |
---|
[638] | 134 | void CField::sendUpdateData(const CArray<double,1>& data) |
---|
| 135 | { |
---|
[1158] | 136 | CTimer::get("Field : send data").resume(); |
---|
[638] | 137 | |
---|
| 138 | CContext* context = CContext::getCurrent(); |
---|
[1201] | 139 | CContextClient* client = (!context->hasServer) ? context->client : this->file->getContextClient(); |
---|
[1263] | 140 | int receiverSize = client->serverSize; |
---|
[638] | 141 | |
---|
[1201] | 142 | CEventClient event(getType(), EVENT_ID_UPDATE_DATA); |
---|
[1009] | 143 | |
---|
[1201] | 144 | map<int, CArray<int,1> >::iterator it; |
---|
| 145 | list<CMessage> list_msg; |
---|
| 146 | list<CArray<double,1> > list_data; |
---|
[1009] | 147 | |
---|
[1235] | 148 | if (!grid->doGridHaveDataDistributed(client)) |
---|
[1201] | 149 | { |
---|
| 150 | if (client->isServerLeader()) |
---|
| 151 | { |
---|
[1236] | 152 | for (it = grid->storeIndex_toSrv[client].begin(); it != grid->storeIndex_toSrv[client].end(); it++) |
---|
[1201] | 153 | { |
---|
| 154 | int rank = it->first; |
---|
| 155 | CArray<int,1>& index = it->second; |
---|
[1009] | 156 | |
---|
[1201] | 157 | list_msg.push_back(CMessage()); |
---|
| 158 | list_data.push_back(CArray<double,1>(index.numElements())); |
---|
[1009] | 159 | |
---|
[1201] | 160 | CArray<double,1>& data_tmp = list_data.back(); |
---|
| 161 | for (int n = 0; n < data_tmp.numElements(); n++) data_tmp(n) = data(index(n)); |
---|
[1009] | 162 | |
---|
[1201] | 163 | list_msg.back() << getId() << data_tmp; |
---|
| 164 | event.push(rank, 1, list_msg.back()); |
---|
| 165 | } |
---|
| 166 | client->sendEvent(event); |
---|
| 167 | } |
---|
| 168 | else client->sendEvent(event); |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
[1236] | 172 | for (it = grid->storeIndex_toSrv[client].begin(); it != grid->storeIndex_toSrv[client].end(); it++) |
---|
[1009] | 173 | { |
---|
[1201] | 174 | int rank = it->first; |
---|
| 175 | CArray<int,1>& index = it->second; |
---|
[1009] | 176 | |
---|
[1201] | 177 | list_msg.push_back(CMessage()); |
---|
| 178 | list_data.push_back(CArray<double,1>(index.numElements())); |
---|
[1009] | 179 | |
---|
[1201] | 180 | CArray<double,1>& data_tmp = list_data.back(); |
---|
| 181 | for (int n = 0; n < data_tmp.numElements(); n++) data_tmp(n) = data(index(n)); |
---|
[1009] | 182 | |
---|
[1201] | 183 | list_msg.back() << getId() << data_tmp; |
---|
[1263] | 184 | event.push(rank, grid->nbSenders[receiverSize][rank], list_msg.back()); |
---|
[1201] | 185 | } |
---|
| 186 | client->sendEvent(event); |
---|
| 187 | } |
---|
[1009] | 188 | |
---|
[1158] | 189 | CTimer::get("Field : send data").suspend(); |
---|
[1009] | 190 | } |
---|
| 191 | |
---|
[300] | 192 | void CField::recvUpdateData(CEventServer& event) |
---|
| 193 | { |
---|
[1025] | 194 | std::map<int,CBufferIn*> rankBuffers; |
---|
[509] | 195 | |
---|
[562] | 196 | list<CEventServer::SSubEvent>::iterator it; |
---|
| 197 | string fieldId; |
---|
[1158] | 198 | CTimer::get("Field : recv data").resume(); |
---|
[562] | 199 | for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it) |
---|
[300] | 200 | { |
---|
[562] | 201 | int rank = it->rank; |
---|
| 202 | CBufferIn* buffer = it->buffer; |
---|
| 203 | *buffer >> fieldId; |
---|
[1025] | 204 | rankBuffers[rank] = buffer; |
---|
[300] | 205 | } |
---|
[1025] | 206 | get(fieldId)->recvUpdateData(rankBuffers); |
---|
[1158] | 207 | CTimer::get("Field : recv data").suspend(); |
---|
[300] | 208 | } |
---|
[509] | 209 | |
---|
[1025] | 210 | void CField::recvUpdateData(std::map<int,CBufferIn*>& rankBuffers) |
---|
[300] | 211 | { |
---|
[983] | 212 | CContext* context = CContext::getCurrent(); |
---|
| 213 | |
---|
[1025] | 214 | size_t sizeData = 0; |
---|
| 215 | if (0 == recvDataSrv.numElements()) |
---|
[1129] | 216 | { |
---|
| 217 | CArray<int,1>& storeClient = grid->storeIndex_client; |
---|
[1025] | 218 | |
---|
[1136] | 219 | // Gather all data from different clients |
---|
[1129] | 220 | recvDataSrv.resize(storeClient.numElements()); |
---|
[1025] | 221 | recvFoperationSrv = boost::shared_ptr<func::CFunctor>(new func::CInstant(recvDataSrv)); |
---|
[300] | 222 | } |
---|
| 223 | |
---|
[1136] | 224 | CArray<double,1> recv_data_tmp(recvDataSrv.numElements()); |
---|
[562] | 225 | const CDate& currDate = context->getCalendar()->getCurrentDate(); |
---|
[1387] | 226 | const CDate opeDate = (last_operation_srv + context->getCalendar()->getTimeStep()) +freq_op + freq_operation_srv - freq_op - context->getCalendar()->getTimeStep(); |
---|
[300] | 227 | |
---|
| 228 | if (opeDate <= currDate) |
---|
| 229 | { |
---|
[1136] | 230 | for (map<int, CArray<size_t, 1> >::iterator it = grid->outLocalIndexStoreOnClient.begin(); it != grid->outLocalIndexStoreOnClient.end(); ++it) |
---|
[1129] | 231 | { |
---|
[1099] | 232 | CArray<double,1> tmp; |
---|
| 233 | CArray<size_t,1>& indexTmp = it->second; |
---|
[1025] | 234 | *(rankBuffers[it->first]) >> tmp; |
---|
[1099] | 235 | for (int idx = 0; idx < indexTmp.numElements(); ++idx) |
---|
| 236 | { |
---|
| 237 | recv_data_tmp(indexTmp(idx)) = tmp(idx); |
---|
[1136] | 238 | } |
---|
[300] | 239 | } |
---|
| 240 | } |
---|
[509] | 241 | |
---|
[1025] | 242 | this->setData(recv_data_tmp); |
---|
[1340] | 243 | // delete incomming flux for server only |
---|
| 244 | recvFoperationSrv.reset() ; |
---|
| 245 | recvDataSrv.reset() ; |
---|
[1025] | 246 | } |
---|
[509] | 247 | |
---|
[1025] | 248 | void CField::writeUpdateData(const CArray<double,1>& data) |
---|
| 249 | { |
---|
| 250 | CContext* context = CContext::getCurrent(); |
---|
[983] | 251 | |
---|
[1025] | 252 | const CDate& currDate = context->getCalendar()->getCurrentDate(); |
---|
[1387] | 253 | const CDate opeDate = (last_operation_srv + context->getCalendar()->getTimeStep()) + freq_op + freq_operation_srv - freq_op - context->getCalendar()->getTimeStep(); |
---|
| 254 | const CDate writeDate = last_Write_srv + freq_write_srv; |
---|
[1025] | 255 | |
---|
| 256 | if (opeDate <= currDate) |
---|
[983] | 257 | { |
---|
[1025] | 258 | (*recvFoperationSrv)(data); |
---|
| 259 | last_operation_srv = currDate; |
---|
[983] | 260 | } |
---|
[1025] | 261 | |
---|
| 262 | if (writeDate < (currDate + freq_operation_srv)) |
---|
[983] | 263 | { |
---|
[1025] | 264 | recvFoperationSrv->final(); |
---|
| 265 | last_Write_srv = writeDate; |
---|
[1129] | 266 | grid->computeWrittenIndex(); |
---|
[562] | 267 | writeField(); |
---|
[1025] | 268 | lastlast_Write_srv = last_Write_srv; |
---|
[300] | 269 | } |
---|
[1025] | 270 | } |
---|
[983] | 271 | |
---|
[300] | 272 | void CField::writeField(void) |
---|
| 273 | { |
---|
[1232] | 274 | if (!getRelFile()->isEmptyZone()) |
---|
[586] | 275 | { |
---|
[599] | 276 | if (grid->doGridHaveDataToWrite() || getRelFile()->type == CFile::type_attr::one_file) |
---|
[379] | 277 | { |
---|
[1136] | 278 | getRelFile()->checkWriteFile(); |
---|
[379] | 279 | this->incrementNStep(); |
---|
| 280 | getRelFile()->getDataOutput()->writeFieldData(CField::get(this)); |
---|
| 281 | } |
---|
[586] | 282 | } |
---|
[300] | 283 | } |
---|
[562] | 284 | |
---|
[1232] | 285 | /* |
---|
| 286 | Send a request for reading data. |
---|
| 287 | Client sends a request to server for demanding server to read data and send back to it. |
---|
| 288 | For now, this function is called only by client |
---|
| 289 | In the future, it can be called by level-1 servers |
---|
| 290 | \param [in] tsDataRequested timestamp when the call is made |
---|
| 291 | */ |
---|
[1158] | 292 | bool CField::sendReadDataRequest(const CDate& tsDataRequested) |
---|
[598] | 293 | { |
---|
| 294 | CContext* context = CContext::getCurrent(); |
---|
[1232] | 295 | // CContextClient* client = context->client; |
---|
| 296 | |
---|
| 297 | // This code is for future: If we want to read file with level-2 servers |
---|
[1071] | 298 | CContextClient* client = (!context->hasServer) ? context->client : this->file->getContextClient(); |
---|
[598] | 299 | |
---|
[1021] | 300 | lastDataRequestedFromServer = tsDataRequested; |
---|
[708] | 301 | |
---|
[1318] | 302 | // No need to send the request if we are sure that we are already at EOF |
---|
| 303 | if (!isEOF || context->getCalendar()->getCurrentDate() <= dateEOF) |
---|
[598] | 304 | { |
---|
[1158] | 305 | CEventClient event(getType(), EVENT_ID_READ_DATA); |
---|
| 306 | if (client->isServerLeader()) |
---|
| 307 | { |
---|
| 308 | CMessage msg; |
---|
| 309 | msg << getId(); |
---|
| 310 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 311 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 312 | event.push(*itRank, 1, msg); |
---|
| 313 | client->sendEvent(event); |
---|
| 314 | } |
---|
| 315 | else client->sendEvent(event); |
---|
[598] | 316 | } |
---|
[1158] | 317 | else |
---|
| 318 | serverSourceFilter->signalEndOfStream(tsDataRequested); |
---|
| 319 | |
---|
[1318] | 320 | wasDataRequestedFromServer = true; |
---|
| 321 | |
---|
[1158] | 322 | return !isEOF; |
---|
[598] | 323 | } |
---|
| 324 | |
---|
| 325 | /*! |
---|
| 326 | Send request new data read from file if need be, that is the current data is out-of-date. |
---|
| 327 | \return true if and only if some data was requested |
---|
| 328 | */ |
---|
| 329 | bool CField::sendReadDataRequestIfNeeded(void) |
---|
| 330 | { |
---|
| 331 | const CDate& currentDate = CContext::getCurrent()->getCalendar()->getCurrentDate(); |
---|
| 332 | |
---|
[1021] | 333 | bool dataRequested = false; |
---|
[1158] | 334 | |
---|
[1021] | 335 | while (currentDate >= lastDataRequestedFromServer) |
---|
[850] | 336 | { |
---|
[1021] | 337 | info(20) << "currentDate : " << currentDate << endl ; |
---|
| 338 | info(20) << "lastDataRequestedFromServer : " << lastDataRequestedFromServer << endl ; |
---|
| 339 | info(20) << "file->output_freq.getValue() : " << file->output_freq.getValue() << endl ; |
---|
| 340 | info(20) << "lastDataRequestedFromServer + file->output_freq.getValue() : " << lastDataRequestedFromServer + file->output_freq << endl ; |
---|
[873] | 341 | |
---|
[1158] | 342 | dataRequested |= sendReadDataRequest(lastDataRequestedFromServer + file->output_freq); |
---|
| 343 | } |
---|
[1021] | 344 | |
---|
| 345 | return dataRequested; |
---|
[598] | 346 | } |
---|
| 347 | |
---|
| 348 | void CField::recvReadDataRequest(CEventServer& event) |
---|
| 349 | { |
---|
| 350 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
| 351 | StdString fieldId; |
---|
| 352 | *buffer >> fieldId; |
---|
| 353 | get(fieldId)->recvReadDataRequest(); |
---|
| 354 | } |
---|
| 355 | |
---|
[1232] | 356 | /*! |
---|
| 357 | Receive data request sent from client and process it |
---|
| 358 | Every time server receives this request, it will try to read data and sent read data back to client |
---|
| 359 | At the moment, this function is called by server level 1 |
---|
| 360 | In the future, this should (only) be done by the last level servers. |
---|
| 361 | */ |
---|
[598] | 362 | void CField::recvReadDataRequest(void) |
---|
| 363 | { |
---|
| 364 | CContext* context = CContext::getCurrent(); |
---|
[1158] | 365 | CContextClient* client = context->client; |
---|
[598] | 366 | |
---|
| 367 | CEventClient event(getType(), EVENT_ID_READ_DATA_READY); |
---|
| 368 | std::list<CMessage> msgs; |
---|
| 369 | |
---|
[1232] | 370 | EReadField hasData = readField(); |
---|
[598] | 371 | |
---|
[1158] | 372 | map<int, CArray<double,1> >::iterator it; |
---|
[1235] | 373 | if (!grid->doGridHaveDataDistributed(client)) |
---|
[598] | 374 | { |
---|
[1021] | 375 | if (client->isServerLeader()) |
---|
| 376 | { |
---|
[1136] | 377 | if (0 != recvDataSrv.numElements()) |
---|
| 378 | { |
---|
[1021] | 379 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 380 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 381 | { |
---|
| 382 | msgs.push_back(CMessage()); |
---|
| 383 | CMessage& msg = msgs.back(); |
---|
| 384 | msg << getId(); |
---|
[1232] | 385 | switch (hasData) |
---|
| 386 | { |
---|
| 387 | case RF_DATA: |
---|
| 388 | msg << getNStep() - 1 << recvDataSrv; |
---|
| 389 | break; |
---|
| 390 | case RF_NODATA: |
---|
[1239] | 391 | msg << int(-2) << recvDataSrv; |
---|
[1232] | 392 | break; |
---|
| 393 | case RF_EOF: |
---|
| 394 | default: |
---|
| 395 | msg << int(-1); |
---|
| 396 | break; |
---|
| 397 | } |
---|
| 398 | |
---|
[1021] | 399 | event.push(*itRank, 1, msg); |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | client->sendEvent(event); |
---|
| 403 | } |
---|
| 404 | else |
---|
| 405 | { |
---|
| 406 | client->sendEvent(event); |
---|
| 407 | } |
---|
[598] | 408 | } |
---|
[1021] | 409 | else |
---|
| 410 | { |
---|
[1136] | 411 | for (map<int, CArray<size_t, 1> >::iterator it = grid->outLocalIndexStoreOnClient.begin(); |
---|
| 412 | it != grid->outLocalIndexStoreOnClient.end(); ++it) |
---|
[1021] | 413 | { |
---|
[1136] | 414 | CArray<size_t,1>& indexTmp = it->second; |
---|
| 415 | CArray<double,1> tmp(indexTmp.numElements()); |
---|
| 416 | for (int idx = 0; idx < indexTmp.numElements(); ++idx) |
---|
| 417 | { |
---|
| 418 | tmp(idx) = recvDataSrv(indexTmp(idx)); |
---|
| 419 | } |
---|
| 420 | |
---|
[1021] | 421 | msgs.push_back(CMessage()); |
---|
| 422 | CMessage& msg = msgs.back(); |
---|
| 423 | msg << getId(); |
---|
[1232] | 424 | switch (hasData) |
---|
| 425 | { |
---|
| 426 | case RF_DATA: |
---|
| 427 | msg << getNStep() - 1 << tmp; |
---|
| 428 | break; |
---|
| 429 | case RF_NODATA: |
---|
| 430 | msg << int(-2) << tmp; |
---|
| 431 | break; |
---|
| 432 | case RF_EOF: |
---|
| 433 | default: |
---|
| 434 | msg << int(-1); |
---|
| 435 | break; |
---|
| 436 | } |
---|
| 437 | |
---|
[1236] | 438 | event.push(it->first, grid->nbReadSenders[client][it->first], msg); |
---|
[1021] | 439 | } |
---|
| 440 | client->sendEvent(event); |
---|
| 441 | } |
---|
[599] | 442 | } |
---|
[598] | 443 | |
---|
[1232] | 444 | /*! |
---|
| 445 | Read field from a file. |
---|
| 446 | A field is read with the distribution of data on the server side |
---|
| 447 | \return State of field can be read from a file |
---|
| 448 | */ |
---|
| 449 | CField::EReadField CField::readField(void) |
---|
[599] | 450 | { |
---|
[1232] | 451 | CContext* context = CContext::getCurrent(); |
---|
[1136] | 452 | grid->computeWrittenIndex(); |
---|
[1232] | 453 | getRelFile()->initRead(); |
---|
| 454 | EReadField readState = RF_DATA; |
---|
| 455 | |
---|
| 456 | if (!getRelFile()->isEmptyZone()) |
---|
| 457 | { |
---|
| 458 | if (grid->doGridHaveDataToWrite() || getRelFile()->type == CFile::type_attr::one_file) |
---|
[599] | 459 | { |
---|
[1136] | 460 | if (0 == recvDataSrv.numElements()) |
---|
| 461 | { |
---|
| 462 | CArray<int,1>& storeClient = grid->storeIndex_client; |
---|
| 463 | recvDataSrv.resize(storeClient.numElements()); |
---|
[599] | 464 | } |
---|
[1232] | 465 | |
---|
| 466 | getRelFile()->checkReadFile(); |
---|
[599] | 467 | |
---|
| 468 | if (!nstepMax) |
---|
| 469 | { |
---|
| 470 | nstepMax = getRelFile()->getDataInput()->getFieldNbRecords(CField::get(this)); |
---|
| 471 | } |
---|
[873] | 472 | |
---|
[850] | 473 | this->incrementNStep(); |
---|
[599] | 474 | |
---|
[850] | 475 | if (getNStep() > nstepMax && (getRelFile()->cyclic.isEmpty() || !getRelFile()->cyclic) ) |
---|
[1232] | 476 | readState = RF_EOF; |
---|
[599] | 477 | |
---|
[1232] | 478 | if (RF_EOF != readState) |
---|
| 479 | getRelFile()->getDataInput()->readFieldData(CField::get(this)); |
---|
[599] | 480 | } |
---|
| 481 | } |
---|
[1232] | 482 | else |
---|
| 483 | { |
---|
| 484 | this->incrementNStep(); |
---|
| 485 | if (getNStep() > nstepMax && (getRelFile()->cyclic.isEmpty() || !getRelFile()->cyclic) ) |
---|
| 486 | readState = RF_EOF; |
---|
| 487 | else |
---|
| 488 | readState = RF_NODATA; |
---|
[599] | 489 | |
---|
[1232] | 490 | if (!nstepMaxRead) // This can be a bug if we try to read field from zero time record |
---|
| 491 | readState = RF_NODATA; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | if (!nstepMaxRead) |
---|
| 495 | { |
---|
[1240] | 496 | MPI_Allreduce(MPI_IN_PLACE, &nstepMax, 1, MPI_INT, MPI_MAX, context->server->intraComm); |
---|
[1232] | 497 | nstepMaxRead = true; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | return readState; |
---|
[598] | 501 | } |
---|
| 502 | |
---|
[1232] | 503 | /* |
---|
| 504 | Receive read data from server. |
---|
| 505 | At the moment, this function is called in the client side. |
---|
| 506 | In the future, this function can be called hiearachically (server n-1, server n -2, ..., client) |
---|
| 507 | \param event event containing read data |
---|
| 508 | */ |
---|
[598] | 509 | void CField::recvReadDataReady(CEventServer& event) |
---|
| 510 | { |
---|
| 511 | string fieldId; |
---|
| 512 | vector<int> ranks; |
---|
| 513 | vector<CBufferIn*> buffers; |
---|
| 514 | |
---|
| 515 | list<CEventServer::SSubEvent>::iterator it; |
---|
| 516 | for (it = event.subEvents.begin(); it != event.subEvents.end(); ++it) |
---|
| 517 | { |
---|
| 518 | ranks.push_back(it->rank); |
---|
| 519 | CBufferIn* buffer = it->buffer; |
---|
| 520 | *buffer >> fieldId; |
---|
| 521 | buffers.push_back(buffer); |
---|
| 522 | } |
---|
| 523 | get(fieldId)->recvReadDataReady(ranks, buffers); |
---|
| 524 | } |
---|
| 525 | |
---|
[1232] | 526 | /*! |
---|
| 527 | Receive read data from server |
---|
| 528 | \param [in] ranks Ranks of sending processes |
---|
| 529 | \param [in] buffers buffers containing read data |
---|
| 530 | */ |
---|
[598] | 531 | void CField::recvReadDataReady(vector<int> ranks, vector<CBufferIn*> buffers) |
---|
| 532 | { |
---|
| 533 | CContext* context = CContext::getCurrent(); |
---|
[640] | 534 | std::map<int, CArray<double,1> > data; |
---|
[1318] | 535 | const bool wasEOF = isEOF; |
---|
[640] | 536 | |
---|
[598] | 537 | for (int i = 0; i < ranks.size(); i++) |
---|
| 538 | { |
---|
| 539 | int rank = ranks[i]; |
---|
[1318] | 540 | int record; |
---|
[599] | 541 | *buffers[i] >> record; |
---|
[959] | 542 | isEOF = (record == int(-1)); |
---|
[598] | 543 | |
---|
[599] | 544 | if (!isEOF) |
---|
[640] | 545 | *buffers[i] >> data[rank]; |
---|
| 546 | else |
---|
| 547 | break; |
---|
| 548 | } |
---|
| 549 | |
---|
[1021] | 550 | if (wasDataAlreadyReceivedFromServer) |
---|
| 551 | lastDataReceivedFromServer = lastDataReceivedFromServer + file->output_freq; |
---|
| 552 | else |
---|
| 553 | { |
---|
| 554 | lastDataReceivedFromServer = context->getCalendar()->getInitDate(); |
---|
| 555 | wasDataAlreadyReceivedFromServer = true; |
---|
| 556 | } |
---|
| 557 | |
---|
[640] | 558 | if (isEOF) |
---|
[1318] | 559 | { |
---|
| 560 | if (!wasEOF) |
---|
| 561 | dateEOF = lastDataReceivedFromServer; |
---|
| 562 | |
---|
[1021] | 563 | serverSourceFilter->signalEndOfStream(lastDataReceivedFromServer); |
---|
[1318] | 564 | } |
---|
[640] | 565 | else |
---|
[1021] | 566 | serverSourceFilter->streamDataFromServer(lastDataReceivedFromServer, data); |
---|
[598] | 567 | } |
---|
| 568 | |
---|
[1318] | 569 | void CField::checkForLateDataFromServer(void) |
---|
| 570 | { |
---|
| 571 | CContext* context = CContext::getCurrent(); |
---|
| 572 | const CDate& currentDate = context->getCalendar()->getCurrentDate(); |
---|
| 573 | |
---|
| 574 | // Check if data previously requested has been received as expected |
---|
[1358] | 575 | if (wasDataRequestedFromServer && !isEOF) |
---|
[1318] | 576 | { |
---|
| 577 | CTimer timer("CField::checkForLateDataFromServer"); |
---|
| 578 | |
---|
[1322] | 579 | bool isDataLate; |
---|
| 580 | do |
---|
[1318] | 581 | { |
---|
[1322] | 582 | const CDate nextDataDue = wasDataAlreadyReceivedFromServer ? (lastDataReceivedFromServer + file->output_freq) : context->getCalendar()->getInitDate(); |
---|
[1358] | 583 | isDataLate = (nextDataDue <= currentDate); |
---|
[1318] | 584 | |
---|
[1322] | 585 | if (isDataLate) |
---|
| 586 | { |
---|
| 587 | timer.resume(); |
---|
[1318] | 588 | |
---|
[1322] | 589 | context->checkBuffersAndListen(); |
---|
[1318] | 590 | |
---|
[1322] | 591 | timer.suspend(); |
---|
| 592 | } |
---|
[1318] | 593 | } |
---|
[1322] | 594 | while (isDataLate && timer.getCumulatedTime() < CXios::recvFieldTimeout); |
---|
[1318] | 595 | |
---|
| 596 | if (isDataLate) |
---|
| 597 | ERROR("void CField::checkForLateDataFromServer(void)", |
---|
| 598 | << "Late data at timestep = " << currentDate); |
---|
| 599 | } |
---|
| 600 | } |
---|
| 601 | |
---|
[1358] | 602 | void CField::checkIfMustAutoTrigger(void) |
---|
| 603 | { |
---|
| 604 | mustAutoTrigger = serverSourceFilter ? serverSourceFilter->mustAutoTrigger() : false; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | void CField::autoTriggerIfNeeded(void) |
---|
| 608 | { |
---|
| 609 | if (mustAutoTrigger) |
---|
| 610 | serverSourceFilter->trigger(CContext::getCurrent()->getCalendar()->getCurrentDate()); |
---|
| 611 | } |
---|
| 612 | |
---|
[219] | 613 | //---------------------------------------------------------------- |
---|
| 614 | |
---|
[347] | 615 | void CField::setRelFile(CFile* _file) |
---|
[509] | 616 | { |
---|
[459] | 617 | this->file = _file; |
---|
[562] | 618 | hasOutputFile = true; |
---|
[219] | 619 | } |
---|
| 620 | |
---|
| 621 | //---------------------------------------------------------------- |
---|
| 622 | |
---|
[562] | 623 | StdString CField::GetName(void) { return StdString("field"); } |
---|
| 624 | StdString CField::GetDefName(void) { return CField::GetName(); } |
---|
| 625 | ENodeType CField::GetType(void) { return eField; } |
---|
[219] | 626 | |
---|
| 627 | //---------------------------------------------------------------- |
---|
| 628 | |
---|
[347] | 629 | CGrid* CField::getRelGrid(void) const |
---|
[509] | 630 | { |
---|
[562] | 631 | return this->grid; |
---|
[219] | 632 | } |
---|
| 633 | |
---|
| 634 | //---------------------------------------------------------------- |
---|
| 635 | |
---|
[347] | 636 | CFile* CField::getRelFile(void) const |
---|
[509] | 637 | { |
---|
[562] | 638 | return this->file; |
---|
[219] | 639 | } |
---|
[509] | 640 | |
---|
[952] | 641 | int CField::getNStep(void) const |
---|
[266] | 642 | { |
---|
[562] | 643 | return this->nstep; |
---|
[266] | 644 | } |
---|
[509] | 645 | |
---|
[645] | 646 | func::CFunctor::ETimeType CField::getOperationTimeType() const |
---|
| 647 | { |
---|
| 648 | return operationTimeType; |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | //---------------------------------------------------------------- |
---|
| 652 | |
---|
[266] | 653 | void CField::incrementNStep(void) |
---|
| 654 | { |
---|
| 655 | this->nstep++; |
---|
| 656 | } |
---|
[509] | 657 | |
---|
[952] | 658 | void CField::resetNStep(int nstep /*= 0*/) |
---|
[321] | 659 | { |
---|
[707] | 660 | this->nstep = nstep; |
---|
[321] | 661 | } |
---|
[219] | 662 | |
---|
[599] | 663 | void CField::resetNStepMax(void) |
---|
| 664 | { |
---|
| 665 | this->nstepMax = 0; |
---|
[1232] | 666 | nstepMaxRead = false; |
---|
[599] | 667 | } |
---|
| 668 | |
---|
[219] | 669 | //---------------------------------------------------------------- |
---|
| 670 | |
---|
[1158] | 671 | bool CField::isActive(bool atCurrentTimestep /*= false*/) const |
---|
[509] | 672 | { |
---|
[1158] | 673 | if (clientSourceFilter) |
---|
| 674 | return atCurrentTimestep ? clientSourceFilter->isDataExpected(CContext::getCurrent()->getCalendar()->getCurrentDate()) : true; |
---|
| 675 | else if (storeFilter) |
---|
| 676 | return true; |
---|
| 677 | else if (instantDataFilter) |
---|
| 678 | ERROR("bool CField::isActive(bool atCurrentTimestep)", |
---|
| 679 | << "Impossible to check if field [ id = " << getId() << " ] is active as it cannot be used to receive nor send data."); |
---|
| 680 | |
---|
| 681 | return false; |
---|
[310] | 682 | } |
---|
[562] | 683 | |
---|
[219] | 684 | //---------------------------------------------------------------- |
---|
[509] | 685 | |
---|
[707] | 686 | bool CField::wasWritten() const |
---|
| 687 | { |
---|
| 688 | return written; |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | void CField::setWritten() |
---|
| 692 | { |
---|
| 693 | written = true; |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | //---------------------------------------------------------------- |
---|
| 697 | |
---|
[676] | 698 | bool CField::getUseCompressedOutput() const |
---|
| 699 | { |
---|
| 700 | return useCompressedOutput; |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | void CField::setUseCompressedOutput() |
---|
| 704 | { |
---|
| 705 | useCompressedOutput = true; |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | //---------------------------------------------------------------- |
---|
| 709 | |
---|
[641] | 710 | boost::shared_ptr<COutputPin> CField::getInstantDataFilter() |
---|
| 711 | { |
---|
| 712 | return instantDataFilter; |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | //---------------------------------------------------------------- |
---|
| 716 | |
---|
[823] | 717 | /*! |
---|
| 718 | Build up graph of grids which plays role of destination and source in grid transformation |
---|
| 719 | This function should be called before \func solveGridReference() |
---|
| 720 | */ |
---|
| 721 | void CField::buildGridTransformationGraph() |
---|
| 722 | { |
---|
| 723 | CContext* context = CContext::getCurrent(); |
---|
[983] | 724 | if (context->hasClient && !context->hasServer) |
---|
[687] | 725 | { |
---|
[823] | 726 | if (grid && !grid->isTransformed() && hasDirectFieldReference() && grid != getDirectFieldReference()->grid) |
---|
| 727 | { |
---|
| 728 | grid->addTransGridSource(getDirectFieldReference()->grid); |
---|
| 729 | } |
---|
[687] | 730 | } |
---|
[823] | 731 | } |
---|
[687] | 732 | |
---|
[823] | 733 | /*! |
---|
| 734 | Generate a new grid destination if there are more than one grid source pointing to a same grid destination |
---|
| 735 | */ |
---|
| 736 | void CField::generateNewTransformationGridDest() |
---|
| 737 | { |
---|
| 738 | CContext* context = CContext::getCurrent(); |
---|
[983] | 739 | if (context->hasClient && !context->hasServer) |
---|
[823] | 740 | { |
---|
| 741 | std::map<CGrid*,std::pair<bool,StdString> >& gridSrcMap = grid->getTransGridSource(); |
---|
| 742 | if (1 < gridSrcMap.size()) |
---|
| 743 | { |
---|
| 744 | // Search for grid source |
---|
| 745 | CGrid* gridSrc = grid; |
---|
| 746 | CField* currField = this; |
---|
| 747 | std::vector<CField*> hieraField; |
---|
[687] | 748 | |
---|
[823] | 749 | while (currField->hasDirectFieldReference() && (gridSrc == grid)) |
---|
| 750 | { |
---|
| 751 | hieraField.push_back(currField); |
---|
| 752 | CField* tmp = currField->getDirectFieldReference(); |
---|
| 753 | currField = tmp; |
---|
| 754 | gridSrc = currField->grid; |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | if (gridSrcMap.end() != gridSrcMap.find(gridSrc)) |
---|
| 758 | { |
---|
| 759 | CGrid* gridTmp; |
---|
| 760 | std::pair<bool,StdString> newGridDest = gridSrcMap[gridSrc]; |
---|
| 761 | if (newGridDest.first) |
---|
| 762 | { |
---|
| 763 | StdString newIdGridDest = newGridDest.second; |
---|
| 764 | if (!CGrid::has(newIdGridDest)) |
---|
| 765 | { |
---|
| 766 | ERROR("CGrid* CGrid::generateNewTransformationGridDest()", |
---|
| 767 | << " Something wrong happened! Grid whose id " << newIdGridDest |
---|
| 768 | << "should exist "); |
---|
| 769 | } |
---|
| 770 | gridTmp = CGrid::get(newIdGridDest); |
---|
| 771 | } |
---|
| 772 | else |
---|
| 773 | { |
---|
| 774 | StdString newIdGridDest = CGrid::generateId(gridSrc, grid); |
---|
| 775 | gridTmp = CGrid::cloneGrid(newIdGridDest, grid); |
---|
| 776 | |
---|
| 777 | (gridSrcMap[gridSrc]).first = true; |
---|
| 778 | (gridSrcMap[gridSrc]).second = newIdGridDest; |
---|
| 779 | } |
---|
| 780 | |
---|
| 781 | // Update all descendants |
---|
| 782 | for (std::vector<CField*>::iterator it = hieraField.begin(); it != hieraField.end(); ++it) |
---|
| 783 | { |
---|
| 784 | (*it)->grid = gridTmp; |
---|
| 785 | (*it)->updateRef((*it)->grid); |
---|
| 786 | } |
---|
| 787 | } |
---|
| 788 | } |
---|
| 789 | } |
---|
| 790 | } |
---|
| 791 | |
---|
| 792 | void CField::updateRef(CGrid* grid) |
---|
| 793 | { |
---|
| 794 | if (!grid_ref.isEmpty()) grid_ref.setValue(grid->getId()); |
---|
| 795 | else |
---|
| 796 | { |
---|
| 797 | std::vector<CAxis*> axisTmp = grid->getAxis(); |
---|
| 798 | std::vector<CDomain*> domainTmp = grid->getDomains(); |
---|
| 799 | if ((1<axisTmp.size()) || (1<domainTmp.size())) |
---|
| 800 | ERROR("void CField::updateRef(CGrid* grid)", |
---|
| 801 | << "More than one domain or axis is available for domain_ref/axis_ref of field " << this->getId()); |
---|
| 802 | |
---|
| 803 | if ((!domain_ref.isEmpty()) && (domainTmp.empty())) |
---|
| 804 | ERROR("void CField::updateRef(CGrid* grid)", |
---|
| 805 | << "Incoherent between available domain and domain_ref of field " << this->getId()); |
---|
| 806 | if ((!axis_ref.isEmpty()) && (axisTmp.empty())) |
---|
| 807 | ERROR("void CField::updateRef(CGrid* grid)", |
---|
| 808 | << "Incoherent between available axis and axis_ref of field " << this->getId()); |
---|
| 809 | |
---|
| 810 | if (!domain_ref.isEmpty()) domain_ref.setValue(domainTmp[0]->getId()); |
---|
| 811 | if (!axis_ref.isEmpty()) axis_ref.setValue(axisTmp[0]->getId()); |
---|
| 812 | } |
---|
| 813 | } |
---|
[1025] | 814 | |
---|
[1129] | 815 | /*! |
---|
| 816 | Solve reference of all enabled fields even the source fields . |
---|
| 817 | In this step, we do transformations. |
---|
| 818 | */ |
---|
| 819 | void CField::solveAllEnabledFieldsAndTransform() |
---|
[1025] | 820 | { |
---|
| 821 | CContext* context = CContext::getCurrent(); |
---|
| 822 | bool hasClient = context->hasClient; |
---|
| 823 | bool hasServer = context->hasServer; |
---|
[823] | 824 | |
---|
[1129] | 825 | if (!isReferenceSolvedAndTransformed) |
---|
[1025] | 826 | { |
---|
[1129] | 827 | isReferenceSolvedAndTransformed = true; |
---|
[1025] | 828 | |
---|
| 829 | if (hasClient && !hasServer) |
---|
| 830 | { |
---|
| 831 | solveRefInheritance(true); |
---|
[1129] | 832 | if (hasDirectFieldReference()) getDirectFieldReference()->solveAllEnabledFieldsAndTransform(); |
---|
[1025] | 833 | } |
---|
| 834 | |
---|
| 835 | if (hasServer) |
---|
| 836 | solveServerOperation(); |
---|
| 837 | |
---|
| 838 | solveGridReference(); |
---|
| 839 | |
---|
| 840 | if (hasClient && !hasServer) |
---|
| 841 | { |
---|
| 842 | solveGenerateGrid(); |
---|
| 843 | buildGridTransformationGraph(); |
---|
| 844 | } |
---|
| 845 | |
---|
| 846 | solveGridDomainAxisRef(false); |
---|
| 847 | |
---|
| 848 | if (hasClient && !hasServer) |
---|
| 849 | { |
---|
| 850 | solveTransformedGrid(); |
---|
| 851 | } |
---|
| 852 | |
---|
| 853 | solveGridDomainAxisRef(false); |
---|
| 854 | } |
---|
| 855 | } |
---|
| 856 | |
---|
| 857 | void CField::checkGridOfEnabledFields() |
---|
| 858 | { |
---|
[1417] | 859 | if (!isGridChecked) |
---|
| 860 | { |
---|
| 861 | isGridChecked = true; |
---|
| 862 | solveCheckMaskIndex(false); |
---|
| 863 | } |
---|
[1025] | 864 | } |
---|
| 865 | |
---|
[1239] | 866 | void CField::sendGridComponentOfEnabledFields() |
---|
[1025] | 867 | { |
---|
| 868 | solveGridDomainAxisRef(true); |
---|
[1239] | 869 | // solveCheckMaskIndex(true); |
---|
[1025] | 870 | } |
---|
| 871 | |
---|
[1239] | 872 | void CField::sendGridOfEnabledFields() |
---|
| 873 | { |
---|
| 874 | // solveGridDomainAxisRef(true); |
---|
| 875 | solveCheckMaskIndex(true); |
---|
| 876 | } |
---|
[1025] | 877 | |
---|
[1239] | 878 | void CField::solveOnlyReferenceEnabledField(bool doSending2Server) |
---|
[1025] | 879 | { |
---|
| 880 | CContext* context = CContext::getCurrent(); |
---|
| 881 | if (!isReferenceSolved) |
---|
| 882 | { |
---|
| 883 | isReferenceSolved = true; |
---|
| 884 | |
---|
| 885 | if (context->hasClient && !context->hasServer) |
---|
| 886 | { |
---|
| 887 | solveRefInheritance(true); |
---|
| 888 | if (hasDirectFieldReference()) getDirectFieldReference()->solveOnlyReferenceEnabledField(false); |
---|
| 889 | } |
---|
[1144] | 890 | |
---|
[1025] | 891 | if (context->hasServer) |
---|
| 892 | solveServerOperation(); |
---|
| 893 | |
---|
| 894 | solveGridReference(); |
---|
[1308] | 895 | grid->solveDomainAxisRefInheritance(true); // make it again to solve grid reading from file |
---|
[1025] | 896 | |
---|
| 897 | if (context->hasClient && !context->hasServer) |
---|
| 898 | { |
---|
| 899 | solveGenerateGrid(); |
---|
| 900 | buildGridTransformationGraph(); |
---|
| 901 | } |
---|
| 902 | } |
---|
| 903 | } |
---|
| 904 | |
---|
[823] | 905 | void CField::solveAllReferenceEnabledField(bool doSending2Server) |
---|
| 906 | { |
---|
| 907 | CContext* context = CContext::getCurrent(); |
---|
| 908 | solveOnlyReferenceEnabledField(doSending2Server); |
---|
| 909 | |
---|
| 910 | if (!areAllReferenceSolved) |
---|
| 911 | { |
---|
| 912 | areAllReferenceSolved = true; |
---|
[1144] | 913 | |
---|
[983] | 914 | if (context->hasClient && !context->hasServer) |
---|
[823] | 915 | { |
---|
| 916 | solveRefInheritance(true); |
---|
| 917 | if (hasDirectFieldReference()) getDirectFieldReference()->solveAllReferenceEnabledField(false); |
---|
| 918 | } |
---|
[1158] | 919 | else if (context->hasServer) |
---|
[823] | 920 | solveServerOperation(); |
---|
| 921 | |
---|
| 922 | solveGridReference(); |
---|
| 923 | } |
---|
| 924 | |
---|
| 925 | solveGridDomainAxisRef(doSending2Server); |
---|
| 926 | |
---|
[1021] | 927 | if (context->hasClient && !context->hasServer) |
---|
[619] | 928 | { |
---|
| 929 | solveTransformedGrid(); |
---|
| 930 | } |
---|
[687] | 931 | |
---|
[823] | 932 | solveCheckMaskIndex(doSending2Server); |
---|
[509] | 933 | } |
---|
| 934 | |
---|
[1330] | 935 | std::map<int, StdSize> CField::getGridAttributesBufferSize(CContextClient* client, bool bufferForWriting /*= "false"*/) |
---|
[509] | 936 | { |
---|
[1330] | 937 | return grid->getAttributesBufferSize(client, bufferForWriting); |
---|
[509] | 938 | } |
---|
| 939 | |
---|
[1330] | 940 | std::map<int, StdSize> CField::getGridDataBufferSize(CContextClient* client, bool bufferForWriting /*= "false"*/) |
---|
[731] | 941 | { |
---|
[1330] | 942 | return grid->getDataBufferSize(client, getId(), bufferForWriting); |
---|
[731] | 943 | } |
---|
| 944 | |
---|
[1215] | 945 | size_t CField::getGlobalWrittenSize() |
---|
| 946 | { |
---|
| 947 | return grid->getGlobalWrittenSize(); |
---|
| 948 | } |
---|
| 949 | |
---|
[219] | 950 | //---------------------------------------------------------------- |
---|
| 951 | |
---|
[645] | 952 | void CField::solveServerOperation(void) |
---|
[219] | 953 | { |
---|
[640] | 954 | CContext* context = CContext::getCurrent(); |
---|
[509] | 955 | |
---|
[640] | 956 | if (!context->hasServer || !hasOutputFile) return; |
---|
| 957 | |
---|
[645] | 958 | if (freq_op.isEmpty()) |
---|
| 959 | freq_op.setValue(TimeStep); |
---|
[509] | 960 | |
---|
[538] | 961 | if (freq_offset.isEmpty()) |
---|
| 962 | freq_offset.setValue(NoneDu); |
---|
[219] | 963 | |
---|
[645] | 964 | freq_operation_srv = file->output_freq.getValue(); |
---|
| 965 | freq_write_srv = file->output_freq.getValue(); |
---|
[509] | 966 | |
---|
[651] | 967 | lastlast_Write_srv = context->getCalendar()->getInitDate(); |
---|
| 968 | last_Write_srv = context->getCalendar()->getInitDate(); |
---|
| 969 | last_operation_srv = context->getCalendar()->getInitDate(); |
---|
[509] | 970 | |
---|
[645] | 971 | const CDuration toffset = freq_operation_srv - freq_offset.getValue() - context->getCalendar()->getTimeStep(); |
---|
[651] | 972 | last_operation_srv = last_operation_srv - toffset; |
---|
[509] | 973 | |
---|
[645] | 974 | if (operation.isEmpty()) |
---|
| 975 | ERROR("void CField::solveServerOperation(void)", |
---|
| 976 | << "An operation must be defined for field \"" << getId() << "\"."); |
---|
[509] | 977 | |
---|
[645] | 978 | boost::shared_ptr<func::CFunctor> functor; |
---|
| 979 | CArray<double, 1> dummyData; |
---|
[598] | 980 | |
---|
[562] | 981 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
[645] | 982 | if (operation.getValue().compare(#mtype) == 0) \ |
---|
[470] | 983 | { \ |
---|
[645] | 984 | functor.reset(new func::C##MType(dummyData)); \ |
---|
| 985 | } |
---|
[509] | 986 | |
---|
[219] | 987 | #include "functor_type.conf" |
---|
[509] | 988 | |
---|
[645] | 989 | if (!functor) |
---|
| 990 | ERROR("void CField::solveServerOperation(void)", |
---|
| 991 | << "\"" << operation << "\" is not a valid operation."); |
---|
| 992 | |
---|
| 993 | operationTimeType = functor->timeType(); |
---|
[219] | 994 | } |
---|
[509] | 995 | |
---|
[219] | 996 | //---------------------------------------------------------------- |
---|
[640] | 997 | |
---|
| 998 | /*! |
---|
| 999 | * Constructs the graph filter for the field, enabling or not the data output. |
---|
| 1000 | * This method should not be called more than once with enableOutput equal to true. |
---|
| 1001 | * |
---|
| 1002 | * \param gc the garbage collector to use when building the filter graph |
---|
| 1003 | * \param enableOutput must be true when the field data is to be |
---|
| 1004 | * read by the client or/and written to a file |
---|
| 1005 | */ |
---|
| 1006 | void CField::buildFilterGraph(CGarbageCollector& gc, bool enableOutput) |
---|
[1144] | 1007 | { |
---|
[1129] | 1008 | if (!isReferenceSolvedAndTransformed) solveAllEnabledFieldsAndTransform(); |
---|
[1417] | 1009 | if (!isGridChecked) checkGridOfEnabledFields(); |
---|
[1201] | 1010 | |
---|
| 1011 | const bool detectMissingValues = (!detect_missing_value.isEmpty() && !default_value.isEmpty() && detect_missing_value == true); |
---|
| 1012 | const double defaultValue = detectMissingValues ? default_value : (!default_value.isEmpty() ? default_value : 0.0); |
---|
| 1013 | |
---|
[1025] | 1014 | CContext* context = CContext::getCurrent(); |
---|
| 1015 | bool hasWriterServer = context->hasServer && !context->hasClient; |
---|
| 1016 | bool hasIntermediateServer = context->hasServer && context->hasClient; |
---|
[641] | 1017 | |
---|
[1025] | 1018 | if (hasWriterServer) |
---|
[640] | 1019 | { |
---|
[1025] | 1020 | if (!instantDataFilter) |
---|
[1250] | 1021 | instantDataFilter = clientSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(gc, grid,true)); |
---|
[1025] | 1022 | |
---|
[1294] | 1023 | |
---|
| 1024 | // If the field data is to be read by the client or/and written to a file |
---|
[1025] | 1025 | if (enableOutput && !storeFilter && !fileWriterFilter) |
---|
| 1026 | { |
---|
| 1027 | if (file && (file->mode.isEmpty() || file->mode == CFile::mode_attr::write)) |
---|
| 1028 | { |
---|
| 1029 | fileServerWriterFilter = boost::shared_ptr<CFileServerWriterFilter>(new CFileServerWriterFilter(gc, this)); |
---|
| 1030 | instantDataFilter->connectOutput(fileServerWriterFilter, 0); |
---|
| 1031 | } |
---|
| 1032 | } |
---|
| 1033 | } |
---|
| 1034 | else if (hasIntermediateServer) |
---|
| 1035 | { |
---|
| 1036 | if (!instantDataFilter) |
---|
[1250] | 1037 | instantDataFilter = clientSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(gc, grid, true)); |
---|
[1025] | 1038 | |
---|
| 1039 | // If the field data is to be read by the client or/and written to a file |
---|
| 1040 | if (enableOutput && !storeFilter && !fileWriterFilter) |
---|
| 1041 | { |
---|
| 1042 | if (file && (file->mode.isEmpty() || file->mode == CFile::mode_attr::write)) |
---|
| 1043 | { |
---|
| 1044 | fileWriterFilter = boost::shared_ptr<CFileWriterFilter>(new CFileWriterFilter(gc, this)); |
---|
| 1045 | instantDataFilter->connectOutput(fileWriterFilter, 0); |
---|
| 1046 | } |
---|
| 1047 | } |
---|
| 1048 | } |
---|
| 1049 | else |
---|
| 1050 | { |
---|
| 1051 | // Start by building a filter which can provide the field's instant data |
---|
| 1052 | if (!instantDataFilter) |
---|
| 1053 | { |
---|
| 1054 | // Check if we have an expression to parse |
---|
[1158] | 1055 | if (hasExpression()) |
---|
[1025] | 1056 | { |
---|
[1158] | 1057 | boost::scoped_ptr<IFilterExprNode> expr(parseExpr(getExpression() + '\0')); |
---|
| 1058 | boost::shared_ptr<COutputPin> filter = expr->reduce(gc, *this); |
---|
[1021] | 1059 | |
---|
[1158] | 1060 | // Check if a spatial transformation is needed |
---|
| 1061 | if (!field_ref.isEmpty()) |
---|
[1021] | 1062 | { |
---|
[1158] | 1063 | CGrid* gridRef = CField::get(field_ref)->grid; |
---|
[1021] | 1064 | |
---|
[1158] | 1065 | if (grid && grid != gridRef && grid->hasTransform()) |
---|
| 1066 | { |
---|
[1201] | 1067 | std::pair<boost::shared_ptr<CFilter>, boost::shared_ptr<CFilter> > filters = CSpatialTransformFilter::buildFilterGraph(gc, gridRef, grid, detectMissingValues, defaultValue); |
---|
[1158] | 1068 | |
---|
| 1069 | filter->connectOutput(filters.first, 0); |
---|
| 1070 | filter = filters.second; |
---|
| 1071 | } |
---|
[1021] | 1072 | } |
---|
| 1073 | |
---|
[1158] | 1074 | instantDataFilter = filter; |
---|
[1025] | 1075 | } |
---|
| 1076 | // Check if we have a reference on another field |
---|
| 1077 | else if (!field_ref.isEmpty()) |
---|
| 1078 | instantDataFilter = getFieldReference(gc); |
---|
| 1079 | // Check if the data is to be read from a file |
---|
| 1080 | else if (file && !file->mode.isEmpty() && file->mode == CFile::mode_attr::read) |
---|
[1319] | 1081 | { |
---|
[1405] | 1082 | checkTimeAttributes(); |
---|
[1315] | 1083 | instantDataFilter = serverSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(gc, grid, true, freq_offset, true, |
---|
[1201] | 1084 | detectMissingValues, defaultValue)); |
---|
[1319] | 1085 | } |
---|
[1025] | 1086 | else // The data might be passed from the model |
---|
[1158] | 1087 | { |
---|
[1201] | 1088 | if (check_if_active.isEmpty()) check_if_active = false; |
---|
[1250] | 1089 | instantDataFilter = clientSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(gc, grid, false, NoneDu, false, |
---|
[1201] | 1090 | detectMissingValues, defaultValue)); |
---|
[1158] | 1091 | } |
---|
[1025] | 1092 | } |
---|
[640] | 1093 | |
---|
[1166] | 1094 | // If the field data is to be read by the client or/and written to a file |
---|
| 1095 | if (enableOutput && !storeFilter && !fileWriterFilter) |
---|
[640] | 1096 | { |
---|
[1248] | 1097 | if (!read_access.isEmpty() && read_access) |
---|
| 1098 | { |
---|
| 1099 | storeFilter = boost::shared_ptr<CStoreFilter>(new CStoreFilter(gc, CContext::getCurrent(), grid, |
---|
| 1100 | detectMissingValues, defaultValue)); |
---|
| 1101 | instantDataFilter->connectOutput(storeFilter, 0); |
---|
| 1102 | } |
---|
[640] | 1103 | |
---|
[1248] | 1104 | if (file && (file->mode.isEmpty() || file->mode == CFile::mode_attr::write)) |
---|
| 1105 | { |
---|
| 1106 | fileWriterFilter = boost::shared_ptr<CFileWriterFilter>(new CFileWriterFilter(gc, this)); |
---|
| 1107 | getTemporalDataFilter(gc, file->output_freq)->connectOutput(fileWriterFilter, 0); |
---|
| 1108 | } |
---|
[640] | 1109 | } |
---|
| 1110 | } |
---|
| 1111 | } |
---|
| 1112 | |
---|
[642] | 1113 | /*! |
---|
[737] | 1114 | * Returns the filter needed to handle the field reference. |
---|
| 1115 | * This method should only be called when building the filter graph corresponding to the field. |
---|
| 1116 | * |
---|
| 1117 | * \param gc the garbage collector to use |
---|
| 1118 | * \return the output pin corresponding to the field reference |
---|
| 1119 | */ |
---|
[1158] | 1120 | boost::shared_ptr<COutputPin> CField::getFieldReference(CGarbageCollector& gc) |
---|
| 1121 | { |
---|
| 1122 | if (instantDataFilter || field_ref.isEmpty()) |
---|
| 1123 | ERROR("COutputPin* CField::getFieldReference(CGarbageCollector& gc)", |
---|
| 1124 | "Impossible to get the field reference for a field which has already been parsed or which does not have a field_ref."); |
---|
[737] | 1125 | |
---|
[1158] | 1126 | CField* fieldRef = CField::get(field_ref); |
---|
| 1127 | fieldRef->buildFilterGraph(gc, false); |
---|
[737] | 1128 | |
---|
[1158] | 1129 | std::pair<boost::shared_ptr<CFilter>, boost::shared_ptr<CFilter> > filters; |
---|
| 1130 | // Check if a spatial transformation is needed |
---|
| 1131 | if (grid && grid != fieldRef->grid && grid->hasTransform()) |
---|
| 1132 | { |
---|
| 1133 | bool hasMissingValue = (!detect_missing_value.isEmpty() && !default_value.isEmpty() && detect_missing_value == true); |
---|
| 1134 | double defaultValue = hasMissingValue ? default_value : (!default_value.isEmpty() ? default_value : 0.0); |
---|
| 1135 | filters = CSpatialTransformFilter::buildFilterGraph(gc, fieldRef->grid, grid, hasMissingValue, defaultValue); |
---|
| 1136 | } |
---|
| 1137 | else |
---|
| 1138 | filters.first = filters.second = boost::shared_ptr<CFilter>(new CPassThroughFilter(gc)); |
---|
[873] | 1139 | |
---|
[1158] | 1140 | fieldRef->getInstantDataFilter()->connectOutput(filters.first, 0); |
---|
[737] | 1141 | |
---|
[1158] | 1142 | return filters.second; |
---|
| 1143 | } |
---|
[737] | 1144 | |
---|
| 1145 | /*! |
---|
| 1146 | * Returns the filter needed to handle a self reference in the field's expression. |
---|
| 1147 | * If the needed filter does not exist, it is created, otherwise it is reused. |
---|
[642] | 1148 | * This method should only be called when building the filter graph corresponding |
---|
| 1149 | * to the field's expression. |
---|
| 1150 | * |
---|
| 1151 | * \param gc the garbage collector to use |
---|
| 1152 | * \return the output pin corresponding to a self reference |
---|
| 1153 | */ |
---|
| 1154 | boost::shared_ptr<COutputPin> CField::getSelfReference(CGarbageCollector& gc) |
---|
| 1155 | { |
---|
[1021] | 1156 | if (instantDataFilter || !hasExpression()) |
---|
[642] | 1157 | ERROR("COutputPin* CField::getSelfReference(CGarbageCollector& gc)", |
---|
| 1158 | "Impossible to add a self reference to a field which has already been parsed or which does not have an expression."); |
---|
| 1159 | |
---|
[737] | 1160 | if (!selfReferenceFilter) |
---|
| 1161 | { |
---|
[1201] | 1162 | const bool detectMissingValues = (!detect_missing_value.isEmpty() && !default_value.isEmpty() && detect_missing_value == true); |
---|
| 1163 | const double defaultValue = detectMissingValues ? default_value : (!default_value.isEmpty() ? default_value : 0.0); |
---|
| 1164 | |
---|
[741] | 1165 | if (file && !file->mode.isEmpty() && file->mode == CFile::mode_attr::read) |
---|
| 1166 | { |
---|
| 1167 | if (!serverSourceFilter) |
---|
[1319] | 1168 | { |
---|
[1405] | 1169 | checkTimeAttributes(); |
---|
[1315] | 1170 | serverSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(gc, grid, true, freq_offset, true, |
---|
[1201] | 1171 | detectMissingValues, defaultValue)); |
---|
[1319] | 1172 | } |
---|
[1201] | 1173 | |
---|
[741] | 1174 | selfReferenceFilter = serverSourceFilter; |
---|
| 1175 | } |
---|
| 1176 | else if (!field_ref.isEmpty()) |
---|
[1021] | 1177 | { |
---|
| 1178 | CField* fieldRef = CField::get(field_ref); |
---|
| 1179 | fieldRef->buildFilterGraph(gc, false); |
---|
| 1180 | selfReferenceFilter = fieldRef->getInstantDataFilter(); |
---|
| 1181 | } |
---|
[737] | 1182 | else |
---|
| 1183 | { |
---|
| 1184 | if (!clientSourceFilter) |
---|
[1158] | 1185 | { |
---|
[1201] | 1186 | if (check_if_active.isEmpty()) check_if_active = false; |
---|
[1241] | 1187 | clientSourceFilter = boost::shared_ptr<CSourceFilter>(new CSourceFilter(gc, grid, true, NoneDu, false, |
---|
[1201] | 1188 | detectMissingValues, defaultValue)); |
---|
[1158] | 1189 | } |
---|
[642] | 1190 | |
---|
[737] | 1191 | selfReferenceFilter = clientSourceFilter; |
---|
| 1192 | } |
---|
| 1193 | } |
---|
| 1194 | |
---|
| 1195 | return selfReferenceFilter; |
---|
[642] | 1196 | } |
---|
| 1197 | |
---|
[643] | 1198 | /*! |
---|
| 1199 | * Returns the temporal filter corresponding to the field's temporal operation |
---|
| 1200 | * for the specified operation frequency. The filter is created if it does not |
---|
| 1201 | * exist, otherwise it is reused. |
---|
| 1202 | * |
---|
| 1203 | * \param gc the garbage collector to use |
---|
| 1204 | * \param outFreq the operation frequency, i.e. the frequency at which the output data will be computed |
---|
| 1205 | * \return the output pin corresponding to the requested temporal filter |
---|
| 1206 | */ |
---|
| 1207 | boost::shared_ptr<COutputPin> CField::getTemporalDataFilter(CGarbageCollector& gc, CDuration outFreq) |
---|
| 1208 | { |
---|
| 1209 | std::map<CDuration, boost::shared_ptr<COutputPin> >::iterator it = temporalDataFilters.find(outFreq); |
---|
| 1210 | |
---|
| 1211 | if (it == temporalDataFilters.end()) |
---|
| 1212 | { |
---|
| 1213 | if (operation.isEmpty()) |
---|
| 1214 | ERROR("void CField::getTemporalDataFilter(CGarbageCollector& gc, CDuration outFreq)", |
---|
| 1215 | << "An operation must be defined for field \"" << getId() << "\"."); |
---|
| 1216 | |
---|
[1405] | 1217 | checkTimeAttributes(&outFreq); |
---|
[1315] | 1218 | |
---|
[1440] | 1219 | const bool detectMissingValues = (!detect_missing_value.isEmpty() && detect_missing_value == true); |
---|
[643] | 1220 | boost::shared_ptr<CTemporalFilter> temporalFilter(new CTemporalFilter(gc, operation, |
---|
| 1221 | CContext::getCurrent()->getCalendar()->getInitDate(), |
---|
[1440] | 1222 | freq_op, freq_offset, outFreq, detectMissingValues)); |
---|
[1278] | 1223 | |
---|
[643] | 1224 | instantDataFilter->connectOutput(temporalFilter, 0); |
---|
| 1225 | |
---|
| 1226 | it = temporalDataFilters.insert(std::make_pair(outFreq, temporalFilter)).first; |
---|
| 1227 | } |
---|
| 1228 | |
---|
| 1229 | return it->second; |
---|
| 1230 | } |
---|
| 1231 | |
---|
[1158] | 1232 | /*! |
---|
| 1233 | * Returns the temporal filter corresponding to the field's temporal operation |
---|
| 1234 | * for the specified operation frequency. |
---|
| 1235 | * |
---|
| 1236 | * \param gc the garbage collector to use |
---|
| 1237 | * \param outFreq the operation frequency, i.e. the frequency at which the output data will be computed |
---|
| 1238 | * \return the output pin corresponding to the requested temporal filter |
---|
| 1239 | */ |
---|
| 1240 | |
---|
| 1241 | boost::shared_ptr<COutputPin> CField::getSelfTemporalDataFilter(CGarbageCollector& gc, CDuration outFreq) |
---|
| 1242 | { |
---|
| 1243 | if (instantDataFilter || !hasExpression()) |
---|
| 1244 | ERROR("COutputPin* CField::getSelfTemporalDataFilter(CGarbageCollector& gc)", |
---|
| 1245 | "Impossible to add a self reference to a field which has already been parsed or which does not have an expression."); |
---|
| 1246 | |
---|
| 1247 | if (!selfReferenceFilter) getSelfReference(gc) ; |
---|
| 1248 | |
---|
| 1249 | if (serverSourceFilter || clientSourceFilter) |
---|
| 1250 | { |
---|
| 1251 | if (operation.isEmpty()) |
---|
| 1252 | ERROR("void CField::getSelfTemporalDataFilter(CGarbageCollector& gc, CDuration outFreq)", |
---|
| 1253 | << "An operation must be defined for field \"" << getId() << "\"."); |
---|
| 1254 | |
---|
[1405] | 1255 | checkTimeAttributes(&outFreq); |
---|
[1315] | 1256 | |
---|
[1440] | 1257 | const bool detectMissingValues = (!detect_missing_value.isEmpty() && detect_missing_value == true); |
---|
[1158] | 1258 | boost::shared_ptr<CTemporalFilter> temporalFilter(new CTemporalFilter(gc, operation, |
---|
| 1259 | CContext::getCurrent()->getCalendar()->getInitDate(), |
---|
[1440] | 1260 | freq_op, freq_offset, outFreq, detectMissingValues)); |
---|
[1278] | 1261 | |
---|
[1158] | 1262 | selfReferenceFilter->connectOutput(temporalFilter, 0); |
---|
| 1263 | return temporalFilter ; |
---|
| 1264 | } |
---|
| 1265 | else if (!field_ref.isEmpty()) |
---|
| 1266 | { |
---|
| 1267 | CField* fieldRef = CField::get(field_ref); |
---|
| 1268 | fieldRef->buildFilterGraph(gc, false); |
---|
| 1269 | return fieldRef->getTemporalDataFilter(gc, outFreq) ; |
---|
| 1270 | } |
---|
| 1271 | } |
---|
| 1272 | |
---|
[640] | 1273 | //---------------------------------------------------------------- |
---|
[369] | 1274 | /* |
---|
[562] | 1275 | void CField::fromBinary(StdIStream& is) |
---|
[219] | 1276 | { |
---|
| 1277 | SuperClass::fromBinary(is); |
---|
| 1278 | #define CLEAR_ATT(name_)\ |
---|
[369] | 1279 | SuperClassAttribute::operator[](#name_)->reset() |
---|
[219] | 1280 | |
---|
| 1281 | CLEAR_ATT(domain_ref); |
---|
| 1282 | CLEAR_ATT(axis_ref); |
---|
| 1283 | #undef CLEAR_ATT |
---|
| 1284 | |
---|
| 1285 | } |
---|
[369] | 1286 | */ |
---|
[219] | 1287 | //---------------------------------------------------------------- |
---|
| 1288 | |
---|
| 1289 | void CField::solveGridReference(void) |
---|
| 1290 | { |
---|
[887] | 1291 | if (grid_ref.isEmpty() && domain_ref.isEmpty() && axis_ref.isEmpty() && scalar_ref.isEmpty()) |
---|
[742] | 1292 | { |
---|
| 1293 | ERROR("CField::solveGridReference(void)", |
---|
[770] | 1294 | << "A grid must be defined for field '" << getFieldOutputName() << "' ."); |
---|
[742] | 1295 | } |
---|
[887] | 1296 | else if (!grid_ref.isEmpty() && (!domain_ref.isEmpty() || !axis_ref.isEmpty() || !scalar_ref.isEmpty())) |
---|
[219] | 1297 | { |
---|
[744] | 1298 | ERROR("CField::solveGridReference(void)", |
---|
[887] | 1299 | << "Field '" << getFieldOutputName() << "' has both a grid and a domain/axis/scalar." << std::endl |
---|
| 1300 | << "Please define either 'grid_ref' or 'domain_ref'/'axis_ref'/'scalar_ref'."); |
---|
[219] | 1301 | } |
---|
| 1302 | |
---|
[744] | 1303 | if (grid_ref.isEmpty()) |
---|
[219] | 1304 | { |
---|
[744] | 1305 | std::vector<CDomain*> vecDom; |
---|
| 1306 | std::vector<CAxis*> vecAxis; |
---|
[887] | 1307 | std::vector<CScalar*> vecScalar; |
---|
[894] | 1308 | std::vector<int> axisDomainOrderTmp; |
---|
| 1309 | |
---|
[744] | 1310 | if (!domain_ref.isEmpty()) |
---|
| 1311 | { |
---|
[823] | 1312 | StdString tmp = domain_ref.getValue(); |
---|
[744] | 1313 | if (CDomain::has(domain_ref)) |
---|
[894] | 1314 | { |
---|
[744] | 1315 | vecDom.push_back(CDomain::get(domain_ref)); |
---|
[894] | 1316 | axisDomainOrderTmp.push_back(2); |
---|
| 1317 | } |
---|
[744] | 1318 | else |
---|
[219] | 1319 | ERROR("CField::solveGridReference(void)", |
---|
[744] | 1320 | << "Invalid reference to domain '" << domain_ref.getValue() << "'."); |
---|
| 1321 | } |
---|
[219] | 1322 | |
---|
[744] | 1323 | if (!axis_ref.isEmpty()) |
---|
[742] | 1324 | { |
---|
[744] | 1325 | if (CAxis::has(axis_ref)) |
---|
[894] | 1326 | { |
---|
[744] | 1327 | vecAxis.push_back(CAxis::get(axis_ref)); |
---|
[894] | 1328 | axisDomainOrderTmp.push_back(1); |
---|
| 1329 | } |
---|
[744] | 1330 | else |
---|
| 1331 | ERROR("CField::solveGridReference(void)", |
---|
| 1332 | << "Invalid reference to axis '" << axis_ref.getValue() << "'."); |
---|
[742] | 1333 | } |
---|
[744] | 1334 | |
---|
[887] | 1335 | if (!scalar_ref.isEmpty()) |
---|
| 1336 | { |
---|
| 1337 | if (CScalar::has(scalar_ref)) |
---|
[894] | 1338 | { |
---|
[887] | 1339 | vecScalar.push_back(CScalar::get(scalar_ref)); |
---|
[894] | 1340 | axisDomainOrderTmp.push_back(0); |
---|
| 1341 | } |
---|
[887] | 1342 | else |
---|
| 1343 | ERROR("CField::solveGridReference(void)", |
---|
| 1344 | << "Invalid reference to scalar '" << scalar_ref.getValue() << "'."); |
---|
| 1345 | } |
---|
[894] | 1346 | |
---|
| 1347 | CArray<int,1> axisDomainOrder(axisDomainOrderTmp.size()); |
---|
| 1348 | for (int idx = 0; idx < axisDomainOrderTmp.size(); ++idx) |
---|
| 1349 | { |
---|
| 1350 | axisDomainOrder(idx) = axisDomainOrderTmp[idx]; |
---|
| 1351 | } |
---|
[887] | 1352 | |
---|
[745] | 1353 | // Warning: the gridId shouldn't be set as the grid_ref since it could be inherited |
---|
[894] | 1354 | StdString gridId = CGrid::generateId(vecDom, vecAxis, vecScalar,axisDomainOrder); |
---|
[745] | 1355 | if (CGrid::has(gridId)) |
---|
| 1356 | this->grid = CGrid::get(gridId); |
---|
| 1357 | else |
---|
[894] | 1358 | this->grid = CGrid::createGrid(gridId, vecDom, vecAxis, vecScalar,axisDomainOrder); |
---|
[219] | 1359 | } |
---|
[586] | 1360 | else |
---|
| 1361 | { |
---|
[744] | 1362 | if (CGrid::has(grid_ref)) |
---|
| 1363 | this->grid = CGrid::get(grid_ref); |
---|
| 1364 | else |
---|
| 1365 | ERROR("CField::solveGridReference(void)", |
---|
| 1366 | << "Invalid reference to grid '" << grid_ref.getValue() << "'."); |
---|
[586] | 1367 | } |
---|
[509] | 1368 | } |
---|
[459] | 1369 | |
---|
[509] | 1370 | void CField::solveGridDomainAxisRef(bool checkAtt) |
---|
| 1371 | { |
---|
| 1372 | grid->solveDomainAxisRef(checkAtt); |
---|
[219] | 1373 | } |
---|
| 1374 | |
---|
[509] | 1375 | void CField::solveCheckMaskIndex(bool doSendingIndex) |
---|
| 1376 | { |
---|
| 1377 | grid->checkMaskIndex(doSendingIndex); |
---|
| 1378 | } |
---|
[219] | 1379 | |
---|
[619] | 1380 | void CField::solveTransformedGrid() |
---|
| 1381 | { |
---|
[746] | 1382 | if (grid && !grid->isTransformed() && hasDirectFieldReference() && grid != getDirectFieldReference()->grid) |
---|
[790] | 1383 | { |
---|
| 1384 | std::vector<CGrid*> grids; |
---|
| 1385 | // Source grid |
---|
| 1386 | grids.push_back(getDirectFieldReference()->grid); |
---|
| 1387 | // Intermediate grids |
---|
| 1388 | if (!grid_path.isEmpty()) |
---|
| 1389 | { |
---|
| 1390 | std::string gridId; |
---|
| 1391 | size_t start = 0, end; |
---|
| 1392 | |
---|
| 1393 | do |
---|
| 1394 | { |
---|
| 1395 | end = grid_path.getValue().find(',', start); |
---|
| 1396 | if (end != std::string::npos) |
---|
| 1397 | { |
---|
| 1398 | gridId = grid_path.getValue().substr(start, end - start); |
---|
| 1399 | start = end + 1; |
---|
| 1400 | } |
---|
| 1401 | else |
---|
| 1402 | gridId = grid_path.getValue().substr(start); |
---|
| 1403 | |
---|
| 1404 | if (!CGrid::has(gridId)) |
---|
| 1405 | ERROR("void CField::solveTransformedGrid()", |
---|
| 1406 | << "Invalid grid_path, the grid '" << gridId << "' does not exist."); |
---|
| 1407 | |
---|
| 1408 | grids.push_back(CGrid::get(gridId)); |
---|
| 1409 | } |
---|
| 1410 | while (end != std::string::npos); |
---|
| 1411 | } |
---|
| 1412 | // Destination grid |
---|
| 1413 | grids.push_back(grid); |
---|
| 1414 | |
---|
| 1415 | for (size_t i = 0, count = grids.size() - 1; i < count; ++i) |
---|
| 1416 | { |
---|
| 1417 | CGrid *gridSrc = grids[i]; |
---|
| 1418 | CGrid *gridDest = grids[i + 1]; |
---|
| 1419 | if (!gridDest->isTransformed()) |
---|
| 1420 | gridDest->transformGrid(gridSrc); |
---|
| 1421 | } |
---|
| 1422 | } |
---|
[934] | 1423 | else if (grid && grid->hasTransform() && !grid->isTransformed()) |
---|
| 1424 | { |
---|
[1021] | 1425 | // Temporarily deactivate the self-transformation of grid |
---|
[1386] | 1426 | // grid->transformGrid(grid); |
---|
[934] | 1427 | } |
---|
[619] | 1428 | } |
---|
| 1429 | |
---|
[687] | 1430 | void CField::solveGenerateGrid() |
---|
| 1431 | { |
---|
[746] | 1432 | if (grid && !grid->isTransformed() && hasDirectFieldReference() && grid != getDirectFieldReference()->grid) |
---|
[687] | 1433 | grid->completeGrid(getDirectFieldReference()->grid); |
---|
[775] | 1434 | else |
---|
| 1435 | grid->completeGrid(); |
---|
[687] | 1436 | } |
---|
| 1437 | |
---|
[775] | 1438 | void CField::solveGridDomainAxisBaseRef() |
---|
| 1439 | { |
---|
| 1440 | grid->solveDomainAxisRef(false); |
---|
| 1441 | grid->solveDomainAxisBaseRef(); |
---|
| 1442 | } |
---|
| 1443 | |
---|
[219] | 1444 | ///------------------------------------------------------------------- |
---|
| 1445 | |
---|
| 1446 | template <> |
---|
[562] | 1447 | void CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void) |
---|
[219] | 1448 | { |
---|
| 1449 | if (this->group_ref.isEmpty()) return; |
---|
| 1450 | StdString gref = this->group_ref.getValue(); |
---|
| 1451 | |
---|
[346] | 1452 | if (!CFieldGroup::has(gref)) |
---|
[219] | 1453 | ERROR("CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)", |
---|
| 1454 | << "[ gref = " << gref << "]" |
---|
| 1455 | << " invalid group name !"); |
---|
| 1456 | |
---|
[347] | 1457 | CFieldGroup* group = CFieldGroup::get(gref); |
---|
| 1458 | CFieldGroup* owner = CFieldGroup::get(boost::polymorphic_downcast<CFieldGroup*>(this)); |
---|
[219] | 1459 | |
---|
[347] | 1460 | std::vector<CField*> allChildren = group->getAllChildren(); |
---|
[562] | 1461 | std::vector<CField*>::iterator it = allChildren.begin(), end = allChildren.end(); |
---|
[509] | 1462 | |
---|
[219] | 1463 | for (; it != end; it++) |
---|
| 1464 | { |
---|
[347] | 1465 | CField* child = *it; |
---|
[562] | 1466 | if (child->hasId()) owner->createChild()->field_ref.setValue(child->getId()); |
---|
[509] | 1467 | |
---|
[219] | 1468 | } |
---|
| 1469 | } |
---|
[509] | 1470 | |
---|
[464] | 1471 | void CField::scaleFactorAddOffset(double scaleFactor, double addOffset) |
---|
| 1472 | { |
---|
[1136] | 1473 | recvDataSrv = (recvDataSrv - addOffset) / scaleFactor; |
---|
[464] | 1474 | } |
---|
[509] | 1475 | |
---|
[599] | 1476 | void CField::invertScaleFactorAddOffset(double scaleFactor, double addOffset) |
---|
| 1477 | { |
---|
[1136] | 1478 | recvDataSrv = recvDataSrv * scaleFactor + addOffset; |
---|
[599] | 1479 | } |
---|
| 1480 | |
---|
[567] | 1481 | void CField::outputField(CArray<double,1>& fieldOut) |
---|
[1136] | 1482 | { |
---|
[1129] | 1483 | CArray<size_t,1>& outIndexClient = grid->localIndexToWriteOnClient; |
---|
| 1484 | CArray<size_t,1>& outIndexServer = grid->localIndexToWriteOnServer; |
---|
| 1485 | for (size_t idx = 0; idx < outIndexServer.numElements(); ++idx) |
---|
| 1486 | { |
---|
| 1487 | fieldOut(outIndexServer(idx)) = recvDataSrv(outIndexClient(idx)); |
---|
| 1488 | } |
---|
[1136] | 1489 | } |
---|
[1129] | 1490 | |
---|
[1136] | 1491 | void CField::inputField(CArray<double,1>& fieldIn) |
---|
| 1492 | { |
---|
| 1493 | CArray<size_t,1>& outIndexClient = grid->localIndexToWriteOnClient; |
---|
| 1494 | CArray<size_t,1>& outIndexServer = grid->localIndexToWriteOnServer; |
---|
| 1495 | for (size_t idx = 0; idx < outIndexServer.numElements(); ++idx) |
---|
[599] | 1496 | { |
---|
[1136] | 1497 | recvDataSrv(outIndexClient(idx)) = fieldIn(outIndexServer(idx)); |
---|
[599] | 1498 | } |
---|
| 1499 | |
---|
| 1500 | } |
---|
| 1501 | |
---|
[676] | 1502 | void CField::outputCompressedField(CArray<double,1>& fieldOut) |
---|
| 1503 | { |
---|
[1143] | 1504 | CArray<size_t,1>& outIndexClient = grid->localIndexToWriteOnClient; |
---|
| 1505 | CArray<size_t,1>& outIndexServer = grid->localIndexToWriteOnServer; |
---|
| 1506 | for (size_t idx = 0; idx < outIndexServer.numElements(); ++idx) |
---|
[676] | 1507 | { |
---|
[1143] | 1508 | fieldOut((idx)) = recvDataSrv(outIndexClient(idx)); |
---|
[676] | 1509 | } |
---|
| 1510 | } |
---|
| 1511 | |
---|
[219] | 1512 | ///------------------------------------------------------------------- |
---|
| 1513 | |
---|
[562] | 1514 | void CField::parse(xml::CXMLNode& node) |
---|
[459] | 1515 | { |
---|
[1363] | 1516 | string newContent ; |
---|
[459] | 1517 | SuperClass::parse(node); |
---|
[1363] | 1518 | if (node.goToChildElement()) |
---|
[472] | 1519 | { |
---|
[1363] | 1520 | do |
---|
[472] | 1521 | { |
---|
[1363] | 1522 | if (node.getElementName() == "variable" || node.getElementName() == "variable_group") this->getVirtualVariableGroup()->parseChild(node); |
---|
| 1523 | else if (node.getElementName() == "expr") if (node.getContent(newContent)) content+=newContent ; |
---|
| 1524 | } while (node.goToNextElement()); |
---|
| 1525 | node.goToParentElement(); |
---|
[472] | 1526 | } |
---|
[1363] | 1527 | if (node.getContent(newContent)) content=newContent ; |
---|
[459] | 1528 | } |
---|
[509] | 1529 | |
---|
| 1530 | /*! |
---|
| 1531 | This function retrieves Id of corresponding domain_ref and axis_ref (if any) |
---|
| 1532 | of a field. In some cases, only domain exists but axis doesn't |
---|
| 1533 | \return pair of Domain and Axis id |
---|
| 1534 | */ |
---|
[887] | 1535 | const std::vector<StdString>& CField::getRefDomainAxisIds() |
---|
[569] | 1536 | { |
---|
| 1537 | CGrid* cgPtr = getRelGrid(); |
---|
| 1538 | if (NULL != cgPtr) |
---|
| 1539 | { |
---|
| 1540 | std::vector<StdString>::iterator it; |
---|
| 1541 | if (!domain_ref.isEmpty()) |
---|
| 1542 | { |
---|
| 1543 | std::vector<StdString> domainList = cgPtr->getDomainList(); |
---|
| 1544 | it = std::find(domainList.begin(), domainList.end(), domain_ref.getValue()); |
---|
[887] | 1545 | if (domainList.end() != it) domAxisScalarIds_[0] = *it; |
---|
[569] | 1546 | } |
---|
[472] | 1547 | |
---|
[569] | 1548 | if (!axis_ref.isEmpty()) |
---|
| 1549 | { |
---|
| 1550 | std::vector<StdString> axisList = cgPtr->getAxisList(); |
---|
| 1551 | it = std::find(axisList.begin(), axisList.end(), axis_ref.getValue()); |
---|
[887] | 1552 | if (axisList.end() != it) domAxisScalarIds_[1] = *it; |
---|
[569] | 1553 | } |
---|
[887] | 1554 | |
---|
| 1555 | if (!scalar_ref.isEmpty()) |
---|
| 1556 | { |
---|
| 1557 | std::vector<StdString> scalarList = cgPtr->getScalarList(); |
---|
| 1558 | it = std::find(scalarList.begin(), scalarList.end(), scalar_ref.getValue()); |
---|
| 1559 | if (scalarList.end() != it) domAxisScalarIds_[2] = *it; |
---|
| 1560 | } |
---|
[569] | 1561 | } |
---|
[887] | 1562 | return (domAxisScalarIds_); |
---|
[569] | 1563 | } |
---|
| 1564 | |
---|
[472] | 1565 | CVariable* CField::addVariable(const string& id) |
---|
| 1566 | { |
---|
[562] | 1567 | return vVariableGroup->createChild(id); |
---|
[472] | 1568 | } |
---|
| 1569 | |
---|
| 1570 | CVariableGroup* CField::addVariableGroup(const string& id) |
---|
| 1571 | { |
---|
[562] | 1572 | return vVariableGroup->createChildGroup(id); |
---|
[472] | 1573 | } |
---|
| 1574 | |
---|
[1294] | 1575 | void CField::setContextClient(CContextClient* contextClient) |
---|
| 1576 | { |
---|
[1344] | 1577 | CContext* context = CContext::getCurrent(); |
---|
[1294] | 1578 | client = contextClient; |
---|
[1344] | 1579 | if (context->hasClient) |
---|
| 1580 | { |
---|
| 1581 | // A grid is sent by a client (both for read or write) or by primary server (write only) |
---|
| 1582 | if (context->hasServer) |
---|
| 1583 | { |
---|
| 1584 | if (file->mode.isEmpty() || (!file->mode.isEmpty() && file->mode == CFile::mode_attr::write)) |
---|
| 1585 | grid->setContextClient(contextClient); |
---|
| 1586 | } |
---|
| 1587 | else |
---|
| 1588 | grid->setContextClient(contextClient); |
---|
| 1589 | } |
---|
[1294] | 1590 | } |
---|
| 1591 | |
---|
| 1592 | CContextClient* CField::getContextClient() |
---|
| 1593 | { |
---|
| 1594 | return client; |
---|
| 1595 | } |
---|
| 1596 | |
---|
[1021] | 1597 | void CField::sendAddAllVariables(CContextClient* client) |
---|
[1009] | 1598 | { |
---|
| 1599 | std::vector<CVariable*> allVar = getAllVariables(); |
---|
| 1600 | std::vector<CVariable*>::const_iterator it = allVar.begin(); |
---|
| 1601 | std::vector<CVariable*>::const_iterator itE = allVar.end(); |
---|
| 1602 | |
---|
| 1603 | for (; it != itE; ++it) |
---|
| 1604 | { |
---|
[1021] | 1605 | this->sendAddVariable((*it)->getId(), client); |
---|
| 1606 | (*it)->sendAllAttributesToServer(client); |
---|
| 1607 | (*it)->sendValue(client); |
---|
[1009] | 1608 | } |
---|
| 1609 | } |
---|
| 1610 | |
---|
[1021] | 1611 | void CField::sendAddVariable(const string& id, CContextClient* client) |
---|
| 1612 | { |
---|
| 1613 | sendAddItem(id, (int)EVENT_ID_ADD_VARIABLE, client); |
---|
| 1614 | } |
---|
| 1615 | |
---|
[1144] | 1616 | void CField::sendAddVariableGroup(const string& id, CContextClient* client) |
---|
[472] | 1617 | { |
---|
[1144] | 1618 | sendAddItem(id, (int)EVENT_ID_ADD_VARIABLE_GROUP, client); |
---|
[472] | 1619 | } |
---|
[509] | 1620 | |
---|
[472] | 1621 | void CField::recvAddVariable(CEventServer& event) |
---|
| 1622 | { |
---|
[509] | 1623 | |
---|
[562] | 1624 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
[472] | 1625 | string id; |
---|
[562] | 1626 | *buffer >> id; |
---|
| 1627 | get(id)->recvAddVariable(*buffer); |
---|
[472] | 1628 | } |
---|
[509] | 1629 | |
---|
[472] | 1630 | void CField::recvAddVariable(CBufferIn& buffer) |
---|
| 1631 | { |
---|
[562] | 1632 | string id; |
---|
| 1633 | buffer >> id; |
---|
| 1634 | addVariable(id); |
---|
[472] | 1635 | } |
---|
| 1636 | |
---|
| 1637 | void CField::recvAddVariableGroup(CEventServer& event) |
---|
| 1638 | { |
---|
[509] | 1639 | |
---|
[562] | 1640 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
[472] | 1641 | string id; |
---|
[562] | 1642 | *buffer >> id; |
---|
| 1643 | get(id)->recvAddVariableGroup(*buffer); |
---|
[472] | 1644 | } |
---|
[509] | 1645 | |
---|
[472] | 1646 | void CField::recvAddVariableGroup(CBufferIn& buffer) |
---|
| 1647 | { |
---|
[562] | 1648 | string id; |
---|
| 1649 | buffer >> id; |
---|
| 1650 | addVariableGroup(id); |
---|
[472] | 1651 | } |
---|
| 1652 | |
---|
[1021] | 1653 | /*! |
---|
[1278] | 1654 | * Check on freq_off and freq_op attributes. |
---|
| 1655 | */ |
---|
[1405] | 1656 | void CField::checkTimeAttributes(CDuration* freqOp) |
---|
[1278] | 1657 | { |
---|
[1405] | 1658 | bool isFieldRead = file && !file->mode.isEmpty() && file->mode == CFile::mode_attr::read; |
---|
| 1659 | bool isFieldWrite = file && ( file->mode.isEmpty() || file->mode == CFile::mode_attr::write); |
---|
[1315] | 1660 | if (isFieldRead && operation.getValue() != "instant") |
---|
[1405] | 1661 | ERROR("void CField::checkTimeAttributes(void)", |
---|
[1315] | 1662 | << "Unsupported operation for field '" << getFieldOutputName() << "'." << std::endl |
---|
| 1663 | << "Currently only \"instant\" is supported for fields read from file.") |
---|
| 1664 | |
---|
[1278] | 1665 | if (freq_op.isEmpty()) |
---|
| 1666 | { |
---|
[1315] | 1667 | if (operation.getValue() == "instant") |
---|
[1405] | 1668 | { |
---|
| 1669 | if (isFieldRead || isFieldWrite) freq_op.setValue(file->output_freq.getValue()); |
---|
| 1670 | else freq_op=*freqOp ; |
---|
| 1671 | } |
---|
[1278] | 1672 | else |
---|
[1292] | 1673 | freq_op.setValue(TimeStep); |
---|
[1278] | 1674 | } |
---|
[1306] | 1675 | if (freq_offset.isEmpty()) |
---|
[1315] | 1676 | freq_offset.setValue(isFieldRead ? NoneDu : (freq_op.getValue() - TimeStep)); |
---|
[1278] | 1677 | } |
---|
| 1678 | |
---|
| 1679 | /*! |
---|
[1021] | 1680 | * Returns string arithmetic expression associated to the field. |
---|
| 1681 | * \return if content is defined return content string, otherwise, if "expr" attribute is defined, return expr string. |
---|
| 1682 | */ |
---|
| 1683 | const string& CField::getExpression(void) |
---|
| 1684 | { |
---|
| 1685 | if (!expr.isEmpty() && content.empty()) |
---|
| 1686 | { |
---|
| 1687 | content = expr; |
---|
| 1688 | expr.reset(); |
---|
| 1689 | } |
---|
| 1690 | |
---|
| 1691 | return content; |
---|
| 1692 | } |
---|
| 1693 | |
---|
| 1694 | bool CField::hasExpression(void) const |
---|
| 1695 | { |
---|
| 1696 | return (!expr.isEmpty() || !content.empty()); |
---|
| 1697 | } |
---|
| 1698 | |
---|
| 1699 | |
---|
[540] | 1700 | DEFINE_REF_FUNC(Field,field) |
---|
[335] | 1701 | } // namespace xios |
---|