[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" |
---|
| 10 | #include "xmlioserver_spl.hpp" |
---|
| 11 | #include "type.hpp" |
---|
| 12 | #include "context_client.hpp" |
---|
[459] | 13 | #include <set> |
---|
[219] | 14 | |
---|
[335] | 15 | namespace xios{ |
---|
[219] | 16 | |
---|
| 17 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 18 | |
---|
| 19 | CField::CField(void) |
---|
| 20 | : CObjectTemplate<CField>(), CFieldAttributes() |
---|
| 21 | , refObject(), baseRefObject() |
---|
| 22 | , grid(), file() |
---|
| 23 | , freq_operation(), freq_write() |
---|
[266] | 24 | , nstep(0) |
---|
[219] | 25 | , last_Write(), last_operation() |
---|
[459] | 26 | , foperation(), hasInstantData(false), hasExpression(false) |
---|
[460] | 27 | , active(false) , hasOutputFile(false),hasFieldOut(false), slotUpdateDate(NULL) |
---|
[459] | 28 | , processed(false) |
---|
[472] | 29 | { setVirtualVariableGroup() ; } |
---|
[219] | 30 | |
---|
| 31 | CField::CField(const StdString & id) |
---|
| 32 | : CObjectTemplate<CField>(id), CFieldAttributes() |
---|
| 33 | , refObject(), baseRefObject() |
---|
| 34 | , grid(), file() |
---|
| 35 | , freq_operation(), freq_write() |
---|
[266] | 36 | , nstep(0) |
---|
[219] | 37 | , last_Write(), last_operation() |
---|
[466] | 38 | , foperation(), hasInstantData(false), hasExpression(false) |
---|
[460] | 39 | , active(false), hasOutputFile(false), hasFieldOut(false), slotUpdateDate(NULL) |
---|
[459] | 40 | , processed(false) |
---|
[472] | 41 | { setVirtualVariableGroup() ; } |
---|
[219] | 42 | |
---|
| 43 | CField::~CField(void) |
---|
| 44 | { |
---|
[347] | 45 | // this->grid.reset() ; |
---|
| 46 | // this->file.reset() ; |
---|
[219] | 47 | this->foperation.reset() ; |
---|
[459] | 48 | if (hasExpression) delete expression ; |
---|
| 49 | if (slotUpdateDate!=NULL) delete slotUpdateDate ; |
---|
| 50 | |
---|
[219] | 51 | } |
---|
| 52 | |
---|
[472] | 53 | |
---|
| 54 | //---------------------------------------------------------------- |
---|
| 55 | |
---|
| 56 | void CField::setVirtualVariableGroup(CVariableGroup* newVVariableGroup) |
---|
| 57 | { |
---|
| 58 | this->vVariableGroup = newVVariableGroup; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void CField::setVirtualVariableGroup(void) |
---|
| 62 | { |
---|
| 63 | this->setVirtualVariableGroup(CVariableGroup::create()); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | CVariableGroup* CField::getVirtualVariableGroup(void) const |
---|
| 67 | { |
---|
| 68 | return (this->vVariableGroup); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | std::vector<CVariable*> CField::getAllVariables(void) const |
---|
| 73 | { |
---|
| 74 | return (this->vVariableGroup->getAllChildren()); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void CField::solveDescInheritance(bool apply, const CAttributeMap * const parent) |
---|
| 78 | { |
---|
| 79 | SuperClassAttribute::setAttributes(parent,apply); |
---|
| 80 | this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL); |
---|
| 81 | } |
---|
[219] | 82 | //---------------------------------------------------------------- |
---|
[472] | 83 | //---------------------------------------------------------------- |
---|
[219] | 84 | |
---|
| 85 | bool CField::updateDataServer |
---|
[343] | 86 | (const CDate & currDate, |
---|
[369] | 87 | const std::deque< CArray<double, 1>* > storedClient) |
---|
[219] | 88 | { |
---|
[343] | 89 | const CDate opeDate = *last_operation + freq_operation; |
---|
| 90 | const CDate writeDate = *last_Write + freq_write; |
---|
[278] | 91 | |
---|
| 92 | if (opeDate <= currDate) |
---|
[219] | 93 | { |
---|
[369] | 94 | if (this->data.numElements() != this->grid->storeIndex[0]->numElements()) |
---|
[278] | 95 | { |
---|
[369] | 96 | this->data.resize(this->grid->storeIndex[0] ->numElements()); |
---|
[278] | 97 | } |
---|
[369] | 98 | CArray<double,1> input(data.numElements()) ; |
---|
[278] | 99 | this->grid->inputFieldServer(storedClient, input); |
---|
[219] | 100 | (*this->foperation)(input); |
---|
| 101 | *last_operation = currDate; |
---|
| 102 | } |
---|
[278] | 103 | if (writeDate < (currDate + freq_operation)) |
---|
[219] | 104 | { |
---|
[278] | 105 | this->foperation->final(); |
---|
| 106 | this->incrementNStep(); |
---|
| 107 | *last_Write = writeDate; |
---|
| 108 | return (true); |
---|
[219] | 109 | } |
---|
| 110 | return (false); |
---|
| 111 | } |
---|
[300] | 112 | |
---|
| 113 | bool CField::dispatchEvent(CEventServer& event) |
---|
| 114 | { |
---|
| 115 | |
---|
| 116 | if (SuperClass::dispatchEvent(event)) return true ; |
---|
| 117 | else |
---|
| 118 | { |
---|
| 119 | switch(event.type) |
---|
| 120 | { |
---|
| 121 | case EVENT_ID_UPDATE_DATA : |
---|
| 122 | recvUpdateData(event) ; |
---|
| 123 | return true ; |
---|
| 124 | break ; |
---|
[472] | 125 | |
---|
| 126 | case EVENT_ID_ADD_VARIABLE : |
---|
| 127 | recvAddVariable(event) ; |
---|
| 128 | return true ; |
---|
| 129 | break ; |
---|
| 130 | |
---|
| 131 | case EVENT_ID_ADD_VARIABLE_GROUP : |
---|
| 132 | recvAddVariableGroup(event) ; |
---|
| 133 | return true ; |
---|
| 134 | break ; |
---|
[300] | 135 | |
---|
| 136 | default : |
---|
| 137 | ERROR("bool CField::dispatchEvent(CEventServer& event)",<<"Unknown Event") ; |
---|
| 138 | return false ; |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void CField::sendUpdateData(void) |
---|
| 144 | { |
---|
[347] | 145 | CContext* context = CContext::getCurrent() ; |
---|
[300] | 146 | CContextClient* client=context->client ; |
---|
| 147 | |
---|
| 148 | CEventClient event(getType(),EVENT_ID_UPDATE_DATA) ; |
---|
| 149 | |
---|
[369] | 150 | map<int,CArray<int, 1>* >::iterator it ; |
---|
[300] | 151 | list<shared_ptr<CMessage> > list_msg ; |
---|
[369] | 152 | list< CArray<double,1>* > list_data ; |
---|
[300] | 153 | |
---|
| 154 | for(it=grid->storeIndex_toSrv.begin();it!=grid->storeIndex_toSrv.end();it++) |
---|
| 155 | { |
---|
| 156 | int rank=(*it).first ; |
---|
[369] | 157 | CArray<int,1>& index = *(it->second) ; |
---|
| 158 | CArray<double,1> data_tmp(index.numElements()) ; |
---|
| 159 | |
---|
| 160 | for(int n=0;n<data_tmp.numElements();n++) data_tmp(n)=data(index(n)) ; |
---|
[300] | 161 | list_msg.push_back(shared_ptr<CMessage>(new CMessage)) ; |
---|
[369] | 162 | list_data.push_back(new CArray<double,1>(data_tmp)) ; |
---|
| 163 | *list_msg.back()<<getId()<<*list_data.back() ; |
---|
[300] | 164 | event.push(rank,grid->nbSenders[rank],*list_msg.back()) ; |
---|
| 165 | } |
---|
| 166 | client->sendEvent(event) ; |
---|
[369] | 167 | |
---|
| 168 | for(list< CArray<double,1>* >::iterator it=list_data.begin();it!=list_data.end();it++) delete *it ; |
---|
[300] | 169 | } |
---|
| 170 | |
---|
| 171 | void CField::recvUpdateData(CEventServer& event) |
---|
| 172 | { |
---|
| 173 | vector<int> ranks ; |
---|
| 174 | vector<CBufferIn*> buffers ; |
---|
| 175 | |
---|
| 176 | list<CEventServer::SSubEvent>::iterator it ; |
---|
| 177 | string fieldId ; |
---|
[219] | 178 | |
---|
[300] | 179 | for (it=event.subEvents.begin();it!=event.subEvents.end();++it) |
---|
| 180 | { |
---|
| 181 | int rank=it->rank; |
---|
| 182 | CBufferIn* buffer=it->buffer; |
---|
| 183 | *buffer>>fieldId ; |
---|
| 184 | ranks.push_back(rank) ; |
---|
| 185 | buffers.push_back(buffer) ; |
---|
| 186 | } |
---|
| 187 | get(fieldId)->recvUpdateData(ranks,buffers) ; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void CField::recvUpdateData(vector<int>& ranks, vector<CBufferIn*>& buffers) |
---|
| 191 | { |
---|
| 192 | |
---|
| 193 | if (data_srv.empty()) |
---|
| 194 | { |
---|
[369] | 195 | for(map<int, CArray<int, 1>* >::iterator it=grid->out_i_fromClient.begin();it!=grid->out_i_fromClient.end();it++) |
---|
[300] | 196 | { |
---|
| 197 | int rank=it->first ; |
---|
[369] | 198 | CArray<double,1> data_tmp(it->second->numElements()) ; |
---|
| 199 | data_srv.insert( pair<int, CArray<double,1>* >(rank, new CArray<double,1>(data_tmp) ) ) ; |
---|
| 200 | foperation_srv.insert(pair<int,boost::shared_ptr<func::CFunctor> >(rank,boost::shared_ptr<func::CFunctor>(new func::CInstant(*data_srv[rank])))) ; |
---|
[300] | 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
[347] | 204 | CContext* context = CContext::getCurrent() ; |
---|
[343] | 205 | const CDate & currDate = context->getCalendar()->getCurrentDate(); |
---|
| 206 | const CDate opeDate = *last_operation_srv + freq_operation_srv; |
---|
| 207 | const CDate writeDate = *last_Write_srv + freq_write_srv; |
---|
[300] | 208 | |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | if (opeDate <= currDate) |
---|
| 212 | { |
---|
| 213 | for(int n=0;n<ranks.size();n++) |
---|
| 214 | { |
---|
[369] | 215 | CArray<double,1> data_tmp ; |
---|
[300] | 216 | *buffers[n]>>data_tmp ; |
---|
| 217 | (*foperation_srv[ranks[n]])(data_tmp) ; |
---|
| 218 | } |
---|
| 219 | *last_operation_srv = currDate; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | if (writeDate < (currDate + freq_operation_srv)) |
---|
| 223 | { |
---|
| 224 | for(int n=0;n<ranks.size();n++) |
---|
| 225 | { |
---|
| 226 | this->foperation_srv[ranks[n]]->final(); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | *last_Write_srv = writeDate; |
---|
| 230 | writeField() ; |
---|
[334] | 231 | *lastlast_Write_srv=*last_Write_srv; |
---|
[300] | 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | void CField::writeField(void) |
---|
| 236 | { |
---|
[379] | 237 | if (! getRelFile()->allDomainEmpty ) |
---|
| 238 | if (! grid->domain->isEmpty() || getRelFile()->type == CFile::type_attr::one_file) |
---|
| 239 | { |
---|
| 240 | getRelFile()->checkFile(); |
---|
| 241 | this->incrementNStep(); |
---|
| 242 | getRelFile()->getDataOutput()->writeFieldData(CField::get(this)); |
---|
| 243 | } |
---|
[300] | 244 | } |
---|
[219] | 245 | //---------------------------------------------------------------- |
---|
| 246 | |
---|
[347] | 247 | void CField::setRelFile(CFile* _file) |
---|
[219] | 248 | { |
---|
[459] | 249 | this->file = _file; |
---|
| 250 | hasOutputFile=true ; |
---|
[219] | 251 | } |
---|
| 252 | |
---|
| 253 | //---------------------------------------------------------------- |
---|
| 254 | |
---|
| 255 | StdString CField::GetName(void) { return (StdString("field")); } |
---|
| 256 | StdString CField::GetDefName(void){ return (CField::GetName()); } |
---|
| 257 | ENodeType CField::GetType(void) { return (eField); } |
---|
| 258 | |
---|
| 259 | //---------------------------------------------------------------- |
---|
| 260 | |
---|
[347] | 261 | CGrid* CField::getRelGrid(void) const |
---|
[219] | 262 | { |
---|
| 263 | return (this->grid); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | //---------------------------------------------------------------- |
---|
| 267 | |
---|
[347] | 268 | CFile* CField::getRelFile(void) const |
---|
[219] | 269 | { |
---|
| 270 | return (this->file); |
---|
| 271 | } |
---|
[266] | 272 | |
---|
| 273 | StdSize CField::getNStep(void) const |
---|
| 274 | { |
---|
| 275 | return (this->nstep); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | void CField::incrementNStep(void) |
---|
| 279 | { |
---|
| 280 | this->nstep++; |
---|
| 281 | } |
---|
[321] | 282 | |
---|
| 283 | void CField::resetNStep(void) |
---|
| 284 | { |
---|
| 285 | this->nstep=0; |
---|
| 286 | } |
---|
[219] | 287 | |
---|
| 288 | //---------------------------------------------------------------- |
---|
| 289 | |
---|
[347] | 290 | CField* CField::getDirectFieldReference(void) const |
---|
[219] | 291 | { |
---|
| 292 | if (this->field_ref.isEmpty()) |
---|
| 293 | return (this->getBaseFieldReference()); |
---|
| 294 | |
---|
[346] | 295 | if (! CField::has(this->field_ref.getValue())) |
---|
[219] | 296 | ERROR("CField::getDirectFieldReference(void)", |
---|
| 297 | << "[ ref_name = " << this->field_ref.getValue() << "]" |
---|
| 298 | << " invalid field name !"); |
---|
| 299 | |
---|
[346] | 300 | return (CField::get(this->field_ref.getValue())); |
---|
[219] | 301 | } |
---|
| 302 | |
---|
| 303 | //---------------------------------------------------------------- |
---|
| 304 | |
---|
[347] | 305 | CField* CField::getBaseFieldReference(void) const |
---|
[219] | 306 | { |
---|
| 307 | return (baseRefObject); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | //---------------------------------------------------------------- |
---|
| 311 | |
---|
[347] | 312 | const std::vector<CField*>& CField::getAllReference(void) const |
---|
[219] | 313 | { |
---|
| 314 | return (refObject); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | //---------------------------------------------------------------- |
---|
| 318 | |
---|
| 319 | const StdString & CField::getBaseFieldId(void) const |
---|
| 320 | { |
---|
| 321 | return (this->getBaseFieldReference()->getId()); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | //---------------------------------------------------------------- |
---|
| 325 | |
---|
[343] | 326 | const CDuration & CField::getFreqOperation(void) const |
---|
[219] | 327 | { |
---|
| 328 | return (this->freq_operation); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | //---------------------------------------------------------------- |
---|
[343] | 332 | const CDuration & CField::getFreqWrite(void) const |
---|
[219] | 333 | { |
---|
| 334 | return (this->freq_write); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | //---------------------------------------------------------------- |
---|
| 338 | |
---|
| 339 | boost::shared_ptr<func::CFunctor> CField::getFieldOperation(void) const |
---|
| 340 | { |
---|
| 341 | return (this->foperation); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | //---------------------------------------------------------------- |
---|
| 345 | |
---|
| 346 | bool CField::hasDirectFieldReference(void) const |
---|
| 347 | { |
---|
[310] | 348 | return (!this->field_ref.isEmpty()); |
---|
[219] | 349 | } |
---|
| 350 | |
---|
[310] | 351 | bool CField::isActive(void) const |
---|
| 352 | { |
---|
| 353 | return (!this->refObject.empty()); |
---|
| 354 | } |
---|
[219] | 355 | //---------------------------------------------------------------- |
---|
| 356 | |
---|
[369] | 357 | CArray<double, 1> CField::getData(void) const |
---|
[219] | 358 | { |
---|
| 359 | return(this->data); |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | //---------------------------------------------------------------- |
---|
| 363 | |
---|
[343] | 364 | boost::shared_ptr<CDate> CField::getLastWriteDate(void) const |
---|
[219] | 365 | { |
---|
| 366 | return(this->last_Write); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | //---------------------------------------------------------------- |
---|
| 370 | |
---|
[343] | 371 | boost::shared_ptr<CDate> CField::getLastOperationDate(void) const |
---|
[219] | 372 | { |
---|
| 373 | return(this->last_operation); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | //---------------------------------------------------------------- |
---|
| 377 | |
---|
[459] | 378 | void CField::processEnabledField(void) |
---|
| 379 | { |
---|
| 380 | if (!processed) |
---|
| 381 | { |
---|
| 382 | processed=true ; |
---|
| 383 | solveRefInheritance(true) ; |
---|
| 384 | solveOperation() ; |
---|
| 385 | solveGridReference() ; |
---|
| 386 | |
---|
| 387 | if (hasDirectFieldReference()) baseRefObject->processEnabledField() ; |
---|
| 388 | buildExpression(); |
---|
| 389 | active=true; |
---|
| 390 | } |
---|
| 391 | } |
---|
| 392 | |
---|
[445] | 393 | void CField::solveRefInheritance(bool apply) |
---|
[219] | 394 | { |
---|
| 395 | std::set<CField *> sset; |
---|
[347] | 396 | CField* refer_sptr; |
---|
[219] | 397 | CField * refer_ptr = this; |
---|
| 398 | |
---|
[459] | 399 | if (this->hasDirectFieldReference()) baseRefObject = getDirectFieldReference(); |
---|
| 400 | else baseRefObject = CField::get(this); |
---|
[219] | 401 | |
---|
| 402 | while (refer_ptr->hasDirectFieldReference()) |
---|
| 403 | { |
---|
| 404 | refer_sptr = refer_ptr->getDirectFieldReference(); |
---|
[347] | 405 | refer_ptr = refer_sptr; |
---|
[219] | 406 | |
---|
| 407 | if(sset.end() != sset.find(refer_ptr)) |
---|
| 408 | { |
---|
| 409 | DEBUG (<< "Dépendance circulaire stoppée pour l'objet de type CField sur " |
---|
| 410 | << "\"" + refer_ptr->getId() + "\" !"); |
---|
| 411 | break; |
---|
| 412 | } |
---|
| 413 | |
---|
[445] | 414 | SuperClassAttribute::setAttributes(refer_ptr, apply); |
---|
[219] | 415 | sset.insert(refer_ptr); |
---|
[459] | 416 | //ym baseRefObject = refer_sptr; |
---|
[286] | 417 | //ym refObject.push_back(refer_sptr); |
---|
[219] | 418 | } |
---|
[459] | 419 | |
---|
| 420 | if (hasDirectFieldReference()) baseRefObject->addReference(this) ; |
---|
[219] | 421 | } |
---|
| 422 | |
---|
| 423 | //---------------------------------------------------------------- |
---|
| 424 | |
---|
| 425 | void CField::solveOperation(void) |
---|
| 426 | { |
---|
| 427 | using namespace func; |
---|
[459] | 428 | |
---|
[460] | 429 | if (!hasOutputFile && !hasFieldOut) return ; |
---|
[459] | 430 | |
---|
| 431 | StdString id ; |
---|
| 432 | if (hasId()) id=getId(); |
---|
| 433 | else if (!name.isEmpty()) id=name ; |
---|
| 434 | else if (hasDirectFieldReference()) id=baseRefObject->getId() ; |
---|
| 435 | |
---|
[347] | 436 | CContext* context = CContext::getCurrent(); |
---|
[436] | 437 | |
---|
| 438 | if (freq_op.isEmpty()) freq_op=string("1ts") ; |
---|
| 439 | |
---|
[459] | 440 | if (operation.isEmpty() ) |
---|
[219] | 441 | { |
---|
| 442 | ERROR("CField::solveOperation(void)", |
---|
| 443 | << "[ id = " << id << "]" |
---|
[421] | 444 | << "Impossible to define an operation for this field !"); |
---|
[219] | 445 | } |
---|
[278] | 446 | |
---|
| 447 | CDuration freq_offset_ = NoneDu; |
---|
| 448 | if (!freq_offset.isEmpty()) |
---|
| 449 | { |
---|
| 450 | freq_offset_ = CDuration::FromString(freq_offset.getValue()); |
---|
| 451 | } |
---|
| 452 | else |
---|
| 453 | { |
---|
| 454 | freq_offset.setValue(NoneDu.toString()); |
---|
| 455 | } |
---|
[219] | 456 | |
---|
[300] | 457 | // if (CXIOSManager::GetStatus() == CXIOSManager::LOC_SERVER) |
---|
| 458 | if (context->hasServer) |
---|
[219] | 459 | { |
---|
[460] | 460 | if (hasOutputFile) |
---|
| 461 | { |
---|
| 462 | this->freq_operation_srv =CDuration::FromString(this->file->output_freq.getValue()); |
---|
| 463 | this->freq_write_srv = CDuration::FromString(this->file->output_freq.getValue()); |
---|
| 464 | } |
---|
[343] | 465 | this->lastlast_Write_srv = boost::shared_ptr<CDate> |
---|
| 466 | (new CDate(context->getCalendar()->getInitDate())); |
---|
| 467 | this->last_Write_srv = boost::shared_ptr<CDate> |
---|
| 468 | (new CDate(context->getCalendar()->getInitDate())); |
---|
| 469 | this->last_operation_srv = boost::shared_ptr<CDate> |
---|
| 470 | (new CDate(context->getCalendar()->getInitDate())); |
---|
[300] | 471 | // this->foperation_srv = |
---|
| 472 | // boost::shared_ptr<func::CFunctor>(new CInstant(this->data_srv)); |
---|
[278] | 473 | |
---|
[460] | 474 | if (hasOutputFile) |
---|
| 475 | { |
---|
| 476 | const CDuration toffset = this->freq_operation_srv - freq_offset_ - context->getCalendar()->getTimeStep(); |
---|
| 477 | *this->last_operation_srv = *this->last_operation_srv - toffset; |
---|
| 478 | } |
---|
[219] | 479 | } |
---|
[300] | 480 | |
---|
[449] | 481 | // if (context->hasClient) |
---|
| 482 | // { |
---|
[219] | 483 | this->freq_operation = CDuration::FromString(freq_op.getValue()); |
---|
[460] | 484 | if (hasOutputFile) this->freq_write = CDuration::FromString(this->file->output_freq.getValue()); |
---|
| 485 | if (hasFieldOut) |
---|
| 486 | { |
---|
| 487 | this->freq_write = CDuration::FromString(this->fieldOut->freq_op.getValue()); |
---|
| 488 | } |
---|
[343] | 489 | this->last_Write = boost::shared_ptr<CDate> |
---|
| 490 | (new CDate(context->getCalendar()->getInitDate())); |
---|
| 491 | this->last_operation = boost::shared_ptr<CDate> |
---|
| 492 | (new CDate(context->getCalendar()->getInitDate())); |
---|
[278] | 493 | |
---|
[300] | 494 | const CDuration toffset = this->freq_operation - freq_offset_ - context->getCalendar()->getTimeStep(); |
---|
[278] | 495 | *this->last_operation = *this->last_operation - toffset; |
---|
[436] | 496 | |
---|
| 497 | if (operation.get()=="once") isOnceOperation=true ; |
---|
| 498 | else isOnceOperation=false; |
---|
| 499 | isFirstOperation=true; |
---|
[470] | 500 | |
---|
| 501 | |
---|
[219] | 502 | #define DECLARE_FUNCTOR(MType, mtype) \ |
---|
| 503 | if (operation.getValue().compare(#mtype) == 0) \ |
---|
| 504 | { \ |
---|
[470] | 505 | if (!detect_missing_value.isEmpty() && !default_value.isEmpty() && detect_missing_value==true) \ |
---|
| 506 | { \ |
---|
| 507 | boost::shared_ptr<func::CFunctor> foperation_(new C##MType(this->data,default_value)); \ |
---|
[473] | 508 | this->foperation = foperation_; \ |
---|
[470] | 509 | } \ |
---|
| 510 | else \ |
---|
| 511 | { \ |
---|
| 512 | boost::shared_ptr<func::CFunctor> foperation_(new C##MType(this->data)); \ |
---|
| 513 | this->foperation = foperation_; \ |
---|
| 514 | } \ |
---|
[219] | 515 | return; \ |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | #include "functor_type.conf" |
---|
| 519 | |
---|
| 520 | ERROR("CField::solveOperation(void)", |
---|
| 521 | << "[ operation = " << operation.getValue() << "]" |
---|
[421] | 522 | << "The operation is not defined !"); |
---|
[449] | 523 | // } |
---|
[436] | 524 | |
---|
| 525 | |
---|
[219] | 526 | } |
---|
| 527 | |
---|
| 528 | //---------------------------------------------------------------- |
---|
[369] | 529 | /* |
---|
[219] | 530 | void CField::fromBinary(StdIStream & is) |
---|
| 531 | { |
---|
| 532 | SuperClass::fromBinary(is); |
---|
| 533 | #define CLEAR_ATT(name_)\ |
---|
[369] | 534 | SuperClassAttribute::operator[](#name_)->reset() |
---|
[219] | 535 | |
---|
| 536 | CLEAR_ATT(domain_ref); |
---|
| 537 | CLEAR_ATT(axis_ref); |
---|
| 538 | #undef CLEAR_ATT |
---|
| 539 | |
---|
| 540 | } |
---|
[369] | 541 | */ |
---|
[219] | 542 | //---------------------------------------------------------------- |
---|
| 543 | |
---|
| 544 | void CField::solveGridReference(void) |
---|
| 545 | { |
---|
[347] | 546 | CDomain* domain; |
---|
| 547 | CAxis* axis; |
---|
[219] | 548 | |
---|
| 549 | if (!domain_ref.isEmpty()) |
---|
| 550 | { |
---|
[346] | 551 | if (CDomain::has(domain_ref.getValue())) |
---|
| 552 | domain = CDomain::get(domain_ref.getValue()) ; |
---|
[219] | 553 | else |
---|
| 554 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 555 | << "Reference to the domain \'" |
---|
| 556 | << domain_ref.getValue() << "\' is wrong") ; |
---|
[219] | 557 | } |
---|
| 558 | |
---|
| 559 | if (!axis_ref.isEmpty()) |
---|
| 560 | { |
---|
[346] | 561 | if (CAxis::has(axis_ref.getValue())) |
---|
| 562 | axis = CAxis::get(axis_ref.getValue()) ; |
---|
[219] | 563 | else |
---|
| 564 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 565 | << "Reference to the axis \'" |
---|
| 566 | << axis_ref.getValue() <<"\' is wrong") ; |
---|
[219] | 567 | } |
---|
| 568 | |
---|
| 569 | if (!grid_ref.isEmpty()) |
---|
| 570 | { |
---|
[346] | 571 | if (CGrid::has(grid_ref.getValue())) |
---|
| 572 | this->grid = CGrid::get(grid_ref.getValue()) ; |
---|
[219] | 573 | else |
---|
| 574 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 575 | << "Reference to the grid \'" |
---|
| 576 | << grid_ref.getValue() << "\' is wrong"); |
---|
[219] | 577 | } |
---|
[418] | 578 | |
---|
| 579 | if (grid_ref.isEmpty() && domain_ref.isEmpty()) |
---|
[219] | 580 | { |
---|
| 581 | ERROR("CField::solveGridReference(void)", |
---|
[421] | 582 | << "The horizontal domain for this field is not defined"); |
---|
[418] | 583 | |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | CType<string> goodDomain ; |
---|
| 587 | CType<string> goodAxis ; |
---|
| 588 | if (!grid_ref.isEmpty()) |
---|
| 589 | { |
---|
| 590 | if (!grid->domain_ref.isEmpty()) goodDomain=grid->domain_ref ; |
---|
| 591 | if (!grid->axis_ref.isEmpty()) goodAxis=grid->axis_ref ; |
---|
| 592 | } |
---|
| 593 | if (!domain_ref.isEmpty()) goodDomain=domain_ref ; |
---|
| 594 | if (!axis_ref.isEmpty()) goodAxis=axis_ref ; |
---|
| 595 | |
---|
| 596 | |
---|
| 597 | if (goodDomain.isEmpty()) |
---|
| 598 | { |
---|
[421] | 599 | ERROR("CField::solveGridReference(void)", << "The horizontal domain for this field is not defined"); |
---|
[418] | 600 | } |
---|
| 601 | else |
---|
| 602 | { |
---|
| 603 | if (CDomain::has(goodDomain)) domain = CDomain::get(goodDomain) ; |
---|
[421] | 604 | else ERROR("CField::solveGridReference(void)",<< "Reference to the domain \'"<<goodDomain.get() << "\' is wrong") ; |
---|
[418] | 605 | } |
---|
| 606 | |
---|
| 607 | if (!goodAxis.isEmpty()) |
---|
| 608 | { |
---|
| 609 | if (CAxis::has(goodAxis)) axis = CAxis::get(goodAxis) ; |
---|
[421] | 610 | else ERROR("CField::solveGridReference(void)", << "Reference to the axis \'" |
---|
| 611 | << goodAxis.get() <<"\' is wrong") ; |
---|
[418] | 612 | } |
---|
| 613 | |
---|
| 614 | bool nothingToDo=false ; |
---|
| 615 | |
---|
| 616 | if (!grid_ref.isEmpty()) |
---|
| 617 | { |
---|
| 618 | if (!grid->domain_ref.isEmpty() && goodDomain.get() == grid->domain_ref.get()) |
---|
| 619 | if (goodAxis.isEmpty()) nothingToDo=true ; |
---|
| 620 | else if (!grid->axis_ref.isEmpty()) |
---|
| 621 | if (grid->axis_ref.get()==goodAxis.get()) nothingToDo=true ; |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | if (!nothingToDo) |
---|
| 625 | { |
---|
| 626 | if (!goodAxis.isEmpty()) |
---|
| 627 | { |
---|
| 628 | this->grid = CGrid::createGrid(domain, axis) ; |
---|
| 629 | this->grid_ref.setValue(this->grid->getId()); |
---|
| 630 | } |
---|
| 631 | else |
---|
| 632 | { |
---|
| 633 | this->grid = CGrid::createGrid(domain) ; |
---|
| 634 | this->grid_ref.setValue(this->grid->getId()); |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | grid->solveReference() ; |
---|
[459] | 639 | |
---|
[219] | 640 | } |
---|
| 641 | |
---|
| 642 | |
---|
| 643 | ///------------------------------------------------------------------- |
---|
| 644 | |
---|
| 645 | template <> |
---|
| 646 | void CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void) |
---|
| 647 | { |
---|
| 648 | if (this->group_ref.isEmpty()) return; |
---|
| 649 | StdString gref = this->group_ref.getValue(); |
---|
| 650 | |
---|
[346] | 651 | if (!CFieldGroup::has(gref)) |
---|
[219] | 652 | ERROR("CGroupTemplate<CField, CFieldGroup, CFieldAttributes>::solveRefInheritance(void)", |
---|
| 653 | << "[ gref = " << gref << "]" |
---|
| 654 | << " invalid group name !"); |
---|
| 655 | |
---|
[347] | 656 | CFieldGroup* group = CFieldGroup::get(gref); |
---|
| 657 | CFieldGroup* owner = CFieldGroup::get(boost::polymorphic_downcast<CFieldGroup*>(this)); |
---|
[219] | 658 | |
---|
[347] | 659 | std::vector<CField*> allChildren = group->getAllChildren(); |
---|
| 660 | std::vector<CField*>::iterator |
---|
[219] | 661 | it = allChildren.begin(), end = allChildren.end(); |
---|
| 662 | |
---|
| 663 | for (; it != end; it++) |
---|
| 664 | { |
---|
[347] | 665 | CField* child = *it; |
---|
[346] | 666 | if (child->hasId()) owner->createChild()->field_ref.setValue(child->getId()) ; |
---|
| 667 | |
---|
[219] | 668 | } |
---|
| 669 | } |
---|
[300] | 670 | |
---|
[464] | 671 | void CField::scaleFactorAddOffset(double scaleFactor, double addOffset) |
---|
| 672 | { |
---|
| 673 | map<int, CArray<double,1>* >::iterator it; |
---|
| 674 | for(it=data_srv.begin();it!=data_srv.end();it++) *it->second = (*it->second -addOffset) * 1./scaleFactor ; |
---|
| 675 | } |
---|
| 676 | |
---|
[369] | 677 | void CField::outputField(CArray<double,3>& fieldOut) |
---|
[300] | 678 | { |
---|
[369] | 679 | map<int, CArray<double,1>* >::iterator it; |
---|
[300] | 680 | for(it=data_srv.begin();it!=data_srv.end();it++) |
---|
[369] | 681 | grid->outputField(it->first,*it->second, fieldOut) ; |
---|
[300] | 682 | |
---|
| 683 | } |
---|
| 684 | |
---|
[369] | 685 | void CField::outputField(CArray<double,2>& fieldOut) |
---|
[300] | 686 | { |
---|
[369] | 687 | map<int, CArray<double,1>* >::iterator it; |
---|
[219] | 688 | |
---|
[300] | 689 | for(it=data_srv.begin();it!=data_srv.end();it++) |
---|
| 690 | { |
---|
[369] | 691 | grid->outputField(it->first,*it->second, fieldOut) ; |
---|
[300] | 692 | } |
---|
| 693 | } |
---|
[219] | 694 | ///------------------------------------------------------------------- |
---|
| 695 | |
---|
[459] | 696 | void CField::parse(xml::CXMLNode & node) |
---|
| 697 | { |
---|
| 698 | SuperClass::parse(node); |
---|
[476] | 699 | if (! node.getContent(this->content)) |
---|
[472] | 700 | { |
---|
[476] | 701 | if (node.goToChildElement()) |
---|
[472] | 702 | { |
---|
[476] | 703 | do |
---|
| 704 | { |
---|
| 705 | if (node.getElementName()=="variable" || node.getElementName()=="variable_group") this->getVirtualVariableGroup()->parseChild(node); |
---|
| 706 | } while (node.goToNextElement()) ; |
---|
| 707 | node.goToParentElement(); |
---|
| 708 | } |
---|
[472] | 709 | } |
---|
[459] | 710 | } |
---|
| 711 | |
---|
| 712 | CArray<double,1>* CField::getInstantData(void) |
---|
| 713 | { |
---|
| 714 | if (!hasInstantData) |
---|
| 715 | { |
---|
| 716 | instantData.resize(grid->storeIndex_client.numElements()) ; |
---|
| 717 | hasInstantData=true ; |
---|
| 718 | } |
---|
| 719 | return &instantData ; |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | void CField::addReference(CField* field) |
---|
| 723 | { |
---|
| 724 | refObject.push_back(field) ; |
---|
| 725 | } |
---|
| 726 | |
---|
| 727 | void CField::addDependency(CField* field, int slotId) |
---|
| 728 | { |
---|
| 729 | fieldDependency.push_back(pair<CField*,int>(field,slotId)) ; |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | void CField::buildExpression(void) |
---|
| 733 | { |
---|
| 734 | if (content.size() > 0) |
---|
| 735 | { |
---|
| 736 | CSimpleNodeExpr* simpleExpr=parseExpr(content+'\0') ; |
---|
| 737 | expression=CFieldNode::newNode(simpleExpr) ; |
---|
| 738 | delete simpleExpr ; |
---|
[460] | 739 | set<string> instantFieldIds ; |
---|
| 740 | map<string,CField*> associatedInstantFieldIds ; |
---|
| 741 | expression->getInstantFieldIds(instantFieldIds) ; |
---|
| 742 | for (set<string>::iterator it=instantFieldIds.begin() ; it!=instantFieldIds.end();++it) |
---|
| 743 | { |
---|
| 744 | if (*it!="this") |
---|
| 745 | { |
---|
| 746 | if (CField::has(*it)) |
---|
| 747 | { |
---|
| 748 | CField* field=CField::get(*it) ; |
---|
| 749 | field->processEnabledField() ; |
---|
| 750 | associatedInstantFieldIds[*it]=field ; |
---|
| 751 | } |
---|
| 752 | else ERROR("void CField::buildExpression(void)",<<" Field "<<*it<<" does not exist") ; |
---|
| 753 | } |
---|
| 754 | } |
---|
[459] | 755 | |
---|
[460] | 756 | set<string> averageFieldIds ; |
---|
| 757 | map<string,CField*> associatedAverageFieldIds ; |
---|
[459] | 758 | |
---|
[460] | 759 | expression->getAverageFieldIds(averageFieldIds) ; |
---|
| 760 | for (set<string>::iterator it=averageFieldIds.begin() ; it!=averageFieldIds.end();++it) |
---|
| 761 | { |
---|
| 762 | if (CField::has(*it)) |
---|
| 763 | { |
---|
| 764 | CFieldGroup* root=CFieldGroup::get("field_definition") ; |
---|
| 765 | CField* averageField=root->createChild() ; |
---|
| 766 | CField* instantField=root->createChild() ; |
---|
| 767 | averageField->field_ref=*it ; |
---|
| 768 | averageField->hasFieldOut=true ; |
---|
| 769 | averageField->fieldOut=instantField ; |
---|
| 770 | instantField->freq_op=freq_op ; |
---|
| 771 | averageField-> processEnabledField() ; |
---|
| 772 | cout<<" outputfreq of averageField "<< freq_op <<" "<<instantField->freq_op<<" "<< averageField->freq_write << endl ; |
---|
| 773 | instantField->SuperClassAttribute::setAttributes(averageField, true); |
---|
| 774 | instantField->field_ref.reset() ; |
---|
| 775 | instantField->operation.reset() ; |
---|
| 776 | |
---|
| 777 | instantField-> processEnabledField() ; |
---|
| 778 | associatedAverageFieldIds[*it]=instantField ; |
---|
| 779 | } |
---|
| 780 | else ERROR("void CField::buildExpression(void)",<<" Field "<<*it<<" does not exist") ; |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | expression->reduce(this,associatedInstantFieldIds,associatedAverageFieldIds) ; |
---|
| 784 | |
---|
| 785 | slots.resize(instantFieldIds.size()+averageFieldIds.size()) ; |
---|
[459] | 786 | resetSlots() ; |
---|
| 787 | int slotId=0 ; |
---|
| 788 | set<CField*> fields ; |
---|
| 789 | expression->getFields(fields) ; |
---|
| 790 | for (set<CField*>::iterator it=fields.begin() ; it!=fields.end();++it,++slotId) (*it)->addDependency(this,slotId) ; |
---|
| 791 | hasExpression=true; |
---|
| 792 | } |
---|
| 793 | } |
---|
| 794 | |
---|
| 795 | void CField::resetSlots(void) |
---|
| 796 | { |
---|
| 797 | for(vector<bool>::iterator it=slots.begin();it!=slots.end();++it) *it=false ; |
---|
| 798 | } |
---|
| 799 | |
---|
| 800 | bool CField::slotsFull(void) |
---|
| 801 | { |
---|
| 802 | bool ret=true ; |
---|
| 803 | for(vector<bool>::iterator it=slots.begin();it!=slots.end();++it) ret &= *it; |
---|
| 804 | return ret ; |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | |
---|
| 808 | void CField::setSlot(int slotId) |
---|
| 809 | { |
---|
| 810 | CContext* context = CContext::getCurrent() ; |
---|
| 811 | const CDate & currDate = context->getCalendar()->getCurrentDate(); |
---|
| 812 | if (slotUpdateDate==NULL || currDate!=*slotUpdateDate) |
---|
| 813 | { |
---|
| 814 | resetSlots() ; |
---|
| 815 | if (slotUpdateDate==NULL) slotUpdateDate=new CDate(currDate) ; |
---|
| 816 | else *slotUpdateDate=currDate ; |
---|
| 817 | } |
---|
| 818 | slots[slotId]=true ; |
---|
| 819 | if (slotsFull()) |
---|
| 820 | { |
---|
| 821 | CArray<double,1> expr(expression->compute()) ; |
---|
| 822 | |
---|
| 823 | if (hasInstantData) |
---|
| 824 | { |
---|
| 825 | instantData=expr ; |
---|
| 826 | for(list< pair<CField *,int> >::iterator it=fieldDependency.begin(); it!=fieldDependency.end(); ++it) |
---|
| 827 | if (it->first!=this) it->first->setSlot(it->second) ; |
---|
| 828 | } |
---|
| 829 | |
---|
| 830 | if (hasOutputFile) updateDataFromExpression(expr) ; |
---|
| 831 | |
---|
| 832 | } |
---|
| 833 | } |
---|
[472] | 834 | |
---|
| 835 | |
---|
| 836 | |
---|
| 837 | CVariable* CField::addVariable(const string& id) |
---|
| 838 | { |
---|
| 839 | return vVariableGroup->createChild(id) ; |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | CVariableGroup* CField::addVariableGroup(const string& id) |
---|
| 843 | { |
---|
| 844 | return vVariableGroup->createChildGroup(id) ; |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | |
---|
| 848 | void CField::sendAddVariable(const string& id) |
---|
| 849 | { |
---|
| 850 | CContext* context=CContext::getCurrent() ; |
---|
| 851 | |
---|
| 852 | if (! context->hasServer ) |
---|
| 853 | { |
---|
| 854 | CContextClient* client=context->client ; |
---|
| 855 | |
---|
| 856 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE) ; |
---|
| 857 | if (client->isServerLeader()) |
---|
| 858 | { |
---|
| 859 | CMessage msg ; |
---|
| 860 | msg<<this->getId() ; |
---|
| 861 | msg<<id ; |
---|
| 862 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 863 | client->sendEvent(event) ; |
---|
| 864 | } |
---|
| 865 | else client->sendEvent(event) ; |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | } |
---|
| 869 | |
---|
| 870 | |
---|
| 871 | void CField::sendAddVariableGroup(const string& id) |
---|
| 872 | { |
---|
| 873 | CContext* context=CContext::getCurrent() ; |
---|
| 874 | if (! context->hasServer ) |
---|
| 875 | { |
---|
| 876 | CContextClient* client=context->client ; |
---|
| 877 | |
---|
| 878 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE_GROUP) ; |
---|
| 879 | if (client->isServerLeader()) |
---|
| 880 | { |
---|
| 881 | CMessage msg ; |
---|
| 882 | msg<<this->getId() ; |
---|
| 883 | msg<<id ; |
---|
| 884 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 885 | client->sendEvent(event) ; |
---|
| 886 | } |
---|
| 887 | else client->sendEvent(event) ; |
---|
| 888 | } |
---|
| 889 | |
---|
| 890 | } |
---|
| 891 | |
---|
| 892 | void CField::recvAddVariable(CEventServer& event) |
---|
| 893 | { |
---|
| 894 | |
---|
| 895 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 896 | string id; |
---|
| 897 | *buffer>>id ; |
---|
| 898 | get(id)->recvAddVariable(*buffer) ; |
---|
| 899 | } |
---|
| 900 | |
---|
| 901 | |
---|
| 902 | void CField::recvAddVariable(CBufferIn& buffer) |
---|
| 903 | { |
---|
| 904 | string id ; |
---|
| 905 | buffer>>id ; |
---|
| 906 | addVariable(id) ; |
---|
| 907 | } |
---|
| 908 | |
---|
| 909 | void CField::recvAddVariableGroup(CEventServer& event) |
---|
| 910 | { |
---|
| 911 | |
---|
| 912 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 913 | string id; |
---|
| 914 | *buffer>>id ; |
---|
| 915 | get(id)->recvAddVariableGroup(*buffer) ; |
---|
| 916 | } |
---|
| 917 | |
---|
| 918 | |
---|
| 919 | void CField::recvAddVariableGroup(CBufferIn& buffer) |
---|
| 920 | { |
---|
| 921 | string id ; |
---|
| 922 | buffer>>id ; |
---|
| 923 | addVariableGroup(id) ; |
---|
| 924 | } |
---|
| 925 | |
---|
| 926 | |
---|
| 927 | |
---|
[459] | 928 | |
---|
[335] | 929 | } // namespace xios |
---|