[219] | 1 | #include "file.hpp" |
---|
| 2 | |
---|
[352] | 3 | #include "attribute_template.hpp" |
---|
| 4 | #include "object_template.hpp" |
---|
| 5 | #include "group_template.hpp" |
---|
[219] | 6 | #include "object_factory.hpp" |
---|
[300] | 7 | #include "context.hpp" |
---|
| 8 | #include "context_server.hpp" |
---|
| 9 | #include "nc4_data_output.hpp" |
---|
[599] | 10 | #include "nc4_data_input.hpp" |
---|
[318] | 11 | #include "calendar_util.hpp" |
---|
| 12 | #include "date.hpp" |
---|
[352] | 13 | #include "message.hpp" |
---|
| 14 | #include "type.hpp" |
---|
[591] | 15 | #include "xios_spl.hpp" |
---|
[352] | 16 | #include "context_client.hpp" |
---|
[382] | 17 | #include "mpi.hpp" |
---|
[1205] | 18 | #include "timer.hpp" |
---|
[219] | 19 | |
---|
[1328] | 20 | |
---|
[335] | 21 | namespace xios { |
---|
[509] | 22 | |
---|
[878] | 23 | /// ////////////////////// Dfinitions ////////////////////// /// |
---|
[219] | 24 | |
---|
| 25 | CFile::CFile(void) |
---|
| 26 | : CObjectTemplate<CFile>(), CFileAttributes() |
---|
[1328] | 27 | , vFieldGroup(), data_out(), enabledFields() |
---|
[775] | 28 | , allDomainEmpty(false), isOpen(false) |
---|
[509] | 29 | { |
---|
[957] | 30 | setVirtualFieldGroup(CFieldGroup::create(getId() + "_virtual_field_group")); |
---|
| 31 | setVirtualVariableGroup(CVariableGroup::create(getId() + "_virtual_variable_group")); |
---|
[472] | 32 | } |
---|
[219] | 33 | |
---|
| 34 | CFile::CFile(const StdString & id) |
---|
| 35 | : CObjectTemplate<CFile>(id), CFileAttributes() |
---|
[1328] | 36 | , vFieldGroup(), data_out(), enabledFields() |
---|
[775] | 37 | , allDomainEmpty(false), isOpen(false) |
---|
[509] | 38 | { |
---|
[957] | 39 | setVirtualFieldGroup(CFieldGroup::create(getId() + "_virtual_field_group")); |
---|
| 40 | setVirtualVariableGroup(CVariableGroup::create(getId() + "_virtual_variable_group")); |
---|
[472] | 41 | } |
---|
[219] | 42 | |
---|
| 43 | CFile::~CFile(void) |
---|
| 44 | { /* Ne rien faire de plus */ } |
---|
| 45 | |
---|
| 46 | ///--------------------------------------------------------------- |
---|
[509] | 47 | //! Get name of file |
---|
[219] | 48 | StdString CFile::GetName(void) { return (StdString("file")); } |
---|
| 49 | StdString CFile::GetDefName(void){ return (CFile::GetName()); } |
---|
| 50 | ENodeType CFile::GetType(void) { return (eFile); } |
---|
| 51 | |
---|
| 52 | //---------------------------------------------------------------- |
---|
[773] | 53 | |
---|
[1090] | 54 | const StdString CFile::getFileOutputName(void) const |
---|
[773] | 55 | { |
---|
[1090] | 56 | return (name.isEmpty() ? getId() : name) + (name_suffix.isEmpty() ? StdString("") : name_suffix.getValue()); |
---|
[773] | 57 | } |
---|
| 58 | |
---|
| 59 | //---------------------------------------------------------------- |
---|
[509] | 60 | /*! |
---|
[599] | 61 | \brief Get data writer object. |
---|
[509] | 62 | Each enabled file in xml represents a physical netcdf file. |
---|
[599] | 63 | This function allows to access the data writer object. |
---|
| 64 | \return data writer object. |
---|
[509] | 65 | */ |
---|
[337] | 66 | boost::shared_ptr<CDataOutput> CFile::getDataOutput(void) const |
---|
[219] | 67 | { |
---|
[599] | 68 | return data_out; |
---|
[219] | 69 | } |
---|
| 70 | |
---|
[509] | 71 | /*! |
---|
[599] | 72 | \brief Get data reader object. |
---|
| 73 | Each enabled file in xml represents a physical netcdf file. |
---|
| 74 | This function allows to access the data reader object. |
---|
| 75 | \return data reader object. |
---|
| 76 | */ |
---|
| 77 | boost::shared_ptr<CDataInput> CFile::getDataInput(void) const |
---|
| 78 | { |
---|
| 79 | return data_in; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /*! |
---|
[509] | 83 | \brief Get virtual field group |
---|
| 84 | In each file, there always exists a field group which is the ancestor of all |
---|
| 85 | fields in the file. This is considered be virtual because it is created automatically during |
---|
| 86 | file initialization and it normally doesn't appear on xml file |
---|
| 87 | \return Pointer to field group |
---|
| 88 | */ |
---|
[347] | 89 | CFieldGroup* CFile::getVirtualFieldGroup(void) const |
---|
[219] | 90 | { |
---|
| 91 | return (this->vFieldGroup); |
---|
| 92 | } |
---|
| 93 | |
---|
[509] | 94 | /*! |
---|
| 95 | \brief Get virtual variable group |
---|
| 96 | In each file, there always exists a variable group which is the ancestor of all |
---|
| 97 | variable in the file. This is considered be virtual because it is created automatically during |
---|
| 98 | file initialization and it normally doesn't appear on xml file |
---|
| 99 | \return Pointer to variable group |
---|
| 100 | */ |
---|
[472] | 101 | CVariableGroup* CFile::getVirtualVariableGroup(void) const |
---|
| 102 | { |
---|
| 103 | return (this->vVariableGroup); |
---|
| 104 | } |
---|
| 105 | |
---|
[509] | 106 | //! Get all fields of a file |
---|
[347] | 107 | std::vector<CField*> CFile::getAllFields(void) const |
---|
[219] | 108 | { |
---|
| 109 | return (this->vFieldGroup->getAllChildren()); |
---|
| 110 | } |
---|
[509] | 111 | |
---|
| 112 | //! Get all variables of a file |
---|
[472] | 113 | std::vector<CVariable*> CFile::getAllVariables(void) const |
---|
| 114 | { |
---|
| 115 | return (this->vVariableGroup->getAllChildren()); |
---|
| 116 | } |
---|
[219] | 117 | |
---|
| 118 | //---------------------------------------------------------------- |
---|
[509] | 119 | /*! |
---|
| 120 | \brief Get all enabled fields of file |
---|
| 121 | A field is considered to be enabled if it fullfil these conditions: it is enabled, inside a enabled file |
---|
| 122 | and its own level is not larger than file output level. |
---|
| 123 | \param [in] default_outputlevel default value output level of file |
---|
| 124 | \param [in] default_level default value level of field |
---|
| 125 | \param [in] default_enabled flag determine by default if field is enabled |
---|
| 126 | \return Vector of pointers of enabled fields |
---|
| 127 | */ |
---|
| 128 | std::vector<CField*> CFile::getEnabledFields(int default_outputlevel, |
---|
[347] | 129 | int default_level, |
---|
| 130 | bool default_enabled) |
---|
[219] | 131 | { |
---|
| 132 | if (!this->enabledFields.empty()) |
---|
| 133 | return (this->enabledFields); |
---|
| 134 | |
---|
| 135 | const int _outputlevel = |
---|
| 136 | (!output_level.isEmpty()) ? output_level.getValue() : default_outputlevel; |
---|
[347] | 137 | std::vector<CField*>::iterator it; |
---|
[219] | 138 | this->enabledFields = this->getAllFields(); |
---|
| 139 | |
---|
[347] | 140 | std::vector<CField*> newEnabledFields; |
---|
[509] | 141 | |
---|
[651] | 142 | for ( it = this->enabledFields.begin(); it != this->enabledFields.end(); it++ ) |
---|
[219] | 143 | { |
---|
[878] | 144 | if (!(*it)->enabled.isEmpty()) // Si l'attribut 'enabled' est dfini ... |
---|
[219] | 145 | { |
---|
[300] | 146 | if (! (*it)->enabled.getValue()) continue; |
---|
| 147 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 148 | } |
---|
[878] | 149 | else // Si l'attribut 'enabled' n'est pas dfini ... |
---|
[219] | 150 | { |
---|
[651] | 151 | if (!default_enabled) continue; |
---|
[300] | 152 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 153 | } |
---|
| 154 | |
---|
[878] | 155 | if (!(*it)->level.isEmpty()) // Si l'attribut 'level' est dfini ... |
---|
[219] | 156 | { |
---|
[651] | 157 | if ((*it)->level.getValue() > _outputlevel) continue; |
---|
[300] | 158 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 159 | } |
---|
[878] | 160 | else // Si l'attribut 'level' n'est pas dfini ... |
---|
[219] | 161 | { |
---|
[651] | 162 | if (default_level > _outputlevel) continue; |
---|
[300] | 163 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 164 | } |
---|
[509] | 165 | |
---|
[651] | 166 | // CField* field_tmp=(*it).get(); |
---|
| 167 | // shared_ptr<CField> sptfield=*it; |
---|
| 168 | // field_tmp->refObject.push_back(sptfield); |
---|
| 169 | newEnabledFields.push_back(*it); |
---|
[286] | 170 | // Le champ est finalement actif, on y ajoute sa propre reference. |
---|
[459] | 171 | // (*it)->refObject.push_back(*it); |
---|
[878] | 172 | // Le champ est finalement actif, on y ajoute la rfrence au champ de base. |
---|
[346] | 173 | (*it)->setRelFile(CFile::get(this)); |
---|
[219] | 174 | } |
---|
[651] | 175 | enabledFields = newEnabledFields; |
---|
[219] | 176 | |
---|
| 177 | return (this->enabledFields); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | //---------------------------------------------------------------- |
---|
[509] | 181 | //! Change virtual field group to a new one |
---|
[347] | 182 | void CFile::setVirtualFieldGroup(CFieldGroup* newVFieldGroup) |
---|
[509] | 183 | { |
---|
| 184 | this->vFieldGroup = newVFieldGroup; |
---|
[219] | 185 | } |
---|
| 186 | |
---|
[509] | 187 | //! Change virtual variable group to new one |
---|
[472] | 188 | void CFile::setVirtualVariableGroup(CVariableGroup* newVVariableGroup) |
---|
[509] | 189 | { |
---|
| 190 | this->vVariableGroup = newVVariableGroup; |
---|
[472] | 191 | } |
---|
| 192 | |
---|
[219] | 193 | //---------------------------------------------------------------- |
---|
[318] | 194 | bool CFile::isSyncTime(void) |
---|
| 195 | { |
---|
[651] | 196 | CContext* context = CContext::getCurrent(); |
---|
| 197 | const CDate& currentDate = context->calendar->getCurrentDate(); |
---|
[538] | 198 | if (!sync_freq.isEmpty()) |
---|
[318] | 199 | { |
---|
[651] | 200 | if (lastSync + sync_freq.getValue() < currentDate) |
---|
[318] | 201 | { |
---|
[651] | 202 | lastSync = currentDate; |
---|
| 203 | return true; |
---|
[318] | 204 | } |
---|
| 205 | } |
---|
[651] | 206 | return false; |
---|
[318] | 207 | } |
---|
[509] | 208 | |
---|
| 209 | //! Initialize a file in order to write into it |
---|
[321] | 210 | void CFile::initFile(void) |
---|
| 211 | { |
---|
[651] | 212 | CContext* context = CContext::getCurrent(); |
---|
| 213 | const CDate& currentDate = context->calendar->getCurrentDate(); |
---|
| 214 | CContextServer* server = context->server; |
---|
[509] | 215 | |
---|
[651] | 216 | lastSync = currentDate; |
---|
| 217 | lastSplit = currentDate; |
---|
[702] | 218 | if (!split_freq.isEmpty()) |
---|
| 219 | { |
---|
[1090] | 220 | StdString keySuffix("CContext_"+CContext::getCurrent()->getId()+"::CFile_"+getFileOutputName()+"::") ; |
---|
| 221 | if (context->registryIn->foundKey(keySuffix+"splitStart") && context->registryIn->foundKey(keySuffix+"splitEnd")) |
---|
[702] | 222 | { |
---|
| 223 | CDate savedSplitStart(*context->getCalendar()), savedSplitEnd(*context->getCalendar()); |
---|
[1092] | 224 | context->registryIn->getKey(keySuffix+"splitStart", savedSplitStart); |
---|
| 225 | context->registryIn->getKey(keySuffix+"splitEnd", savedSplitEnd); |
---|
[702] | 226 | |
---|
| 227 | if (savedSplitStart <= lastSplit && lastSplit <= savedSplitEnd) |
---|
| 228 | lastSplit = savedSplitStart; |
---|
| 229 | } |
---|
| 230 | } |
---|
[651] | 231 | isOpen = false; |
---|
[379] | 232 | |
---|
[651] | 233 | allDomainEmpty = true; |
---|
[567] | 234 | |
---|
[952] | 235 | // if (!record_offset.isEmpty() && record_offset < 0) |
---|
| 236 | // ERROR("void CFile::initFile(void)", |
---|
| 237 | // "Invalid 'record_offset', this attribute cannot be negative."); |
---|
| 238 | const int recordOffset = record_offset.isEmpty() ? 0 : record_offset; |
---|
[757] | 239 | |
---|
[996] | 240 | // set<CAxis*> setAxis; |
---|
| 241 | // set<CDomain*> setDomains; |
---|
| 242 | set<StdString> setAxis; |
---|
| 243 | set<StdString> setDomains; |
---|
| 244 | |
---|
[379] | 245 | std::vector<CField*>::iterator it, end = this->enabledFields.end(); |
---|
[609] | 246 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
[379] | 247 | { |
---|
| 248 | CField* field = *it; |
---|
[609] | 249 | allDomainEmpty &= !field->grid->doGridHaveDataToWrite(); |
---|
| 250 | std::vector<CAxis*> vecAxis = field->grid->getAxis(); |
---|
| 251 | for (size_t i = 0; i < vecAxis.size(); ++i) |
---|
[996] | 252 | setAxis.insert(vecAxis[i]->getAxisOutputName()); |
---|
| 253 | // setAxis.insert(vecAxis[i]); |
---|
[609] | 254 | std::vector<CDomain*> vecDomains = field->grid->getDomains(); |
---|
| 255 | for (size_t i = 0; i < vecDomains.size(); ++i) |
---|
[996] | 256 | setDomains.insert(vecDomains[i]->getDomainOutputName()); |
---|
| 257 | // setDomains.insert(vecDomains[i]); |
---|
[757] | 258 | |
---|
| 259 | field->resetNStep(recordOffset); |
---|
[379] | 260 | } |
---|
[609] | 261 | nbAxis = setAxis.size(); |
---|
| 262 | nbDomains = setDomains.size(); |
---|
[379] | 263 | |
---|
[509] | 264 | // create sub communicator for file |
---|
[651] | 265 | int color = allDomainEmpty ? 0 : 1; |
---|
[1328] | 266 | ep_lib::MPI_Comm_split(server->intraComm, color, server->intraCommRank, &fileComm); |
---|
| 267 | if (allDomainEmpty) ep_lib::MPI_Comm_free(&fileComm); |
---|
[692] | 268 | |
---|
[989] | 269 | // if (time_counter.isEmpty()) time_counter.setValue(time_counter_attr::centered); |
---|
[802] | 270 | if (time_counter_name.isEmpty()) time_counter_name = "time_counter"; |
---|
[321] | 271 | } |
---|
[509] | 272 | |
---|
| 273 | //! Verify state of a file |
---|
[321] | 274 | void CFile::checkFile(void) |
---|
| 275 | { |
---|
[599] | 276 | if (mode.isEmpty() || mode.getValue() == mode_attr::write) |
---|
| 277 | { |
---|
[1205] | 278 | CTimer::get("Files : create headers").resume(); |
---|
[599] | 279 | if (!isOpen) createHeader(); |
---|
[1205] | 280 | CTimer::get("Files : create headers").suspend(); |
---|
[599] | 281 | checkSync(); |
---|
| 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
[1205] | 285 | CTimer::get("Files : open headers").resume(); |
---|
[599] | 286 | if (!isOpen) openInReadMode(); |
---|
[1205] | 287 | CTimer::get("Files : open headers").suspend(); |
---|
[599] | 288 | } |
---|
| 289 | checkSplit(); |
---|
[321] | 290 | } |
---|
[509] | 291 | |
---|
| 292 | /*! |
---|
| 293 | \brief Verify if synchronisation should be done |
---|
| 294 | If syn option is enabled, syn frequence and current time will be used to |
---|
| 295 | calculate the moment to syn file(s) |
---|
| 296 | \return True if it is the moment to synchronize file, otherwise false |
---|
| 297 | */ |
---|
[321] | 298 | bool CFile::checkSync(void) |
---|
| 299 | { |
---|
[651] | 300 | CContext* context = CContext::getCurrent(); |
---|
| 301 | const CDate& currentDate = context->calendar->getCurrentDate(); |
---|
[538] | 302 | if (!sync_freq.isEmpty()) |
---|
[321] | 303 | { |
---|
[651] | 304 | if (lastSync + sync_freq.getValue() <= currentDate) |
---|
[321] | 305 | { |
---|
[651] | 306 | lastSync = currentDate; |
---|
| 307 | data_out->syncFile(); |
---|
| 308 | return true; |
---|
[321] | 309 | } |
---|
| 310 | } |
---|
[651] | 311 | return false; |
---|
[321] | 312 | } |
---|
[509] | 313 | |
---|
| 314 | /*! |
---|
| 315 | \brief Verify if splitting should be done |
---|
| 316 | If split option is enabled, split frequence and current time will be used to |
---|
| 317 | calculate the moment to split file |
---|
| 318 | \return True if it is the moment to split file, otherwise false |
---|
| 319 | */ |
---|
[321] | 320 | bool CFile::checkSplit(void) |
---|
| 321 | { |
---|
[651] | 322 | CContext* context = CContext::getCurrent(); |
---|
| 323 | const CDate& currentDate = context->calendar->getCurrentDate(); |
---|
[538] | 324 | if (!split_freq.isEmpty()) |
---|
[321] | 325 | { |
---|
[651] | 326 | if (currentDate > lastSplit + split_freq.getValue()) |
---|
[321] | 327 | { |
---|
[651] | 328 | lastSplit = lastSplit + split_freq.getValue(); |
---|
[347] | 329 | std::vector<CField*>::iterator it, end = this->enabledFields.end(); |
---|
[599] | 330 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
| 331 | { |
---|
| 332 | (*it)->resetNStep(); |
---|
| 333 | (*it)->resetNStepMax(); |
---|
| 334 | } |
---|
| 335 | if (mode.isEmpty() || mode.getValue() == mode_attr::write) |
---|
[651] | 336 | createHeader(); |
---|
[599] | 337 | else |
---|
| 338 | openInReadMode(); |
---|
[651] | 339 | return true; |
---|
[321] | 340 | } |
---|
| 341 | } |
---|
[651] | 342 | return false; |
---|
[321] | 343 | } |
---|
[509] | 344 | |
---|
| 345 | /*! |
---|
| 346 | \brief Create header of netcdf file |
---|
| 347 | There are some information to fill in header of each netcdf. |
---|
| 348 | */ |
---|
[300] | 349 | void CFile::createHeader(void) |
---|
[219] | 350 | { |
---|
[651] | 351 | CContext* context = CContext::getCurrent(); |
---|
| 352 | CContextServer* server = context->server; |
---|
[509] | 353 | |
---|
[379] | 354 | if (!allDomainEmpty) |
---|
[219] | 355 | { |
---|
[773] | 356 | StdString filename = getFileOutputName(); |
---|
[875] | 357 | |
---|
| 358 | // determine splitting format in the file name : firstPart%start_date%middlePart%end_date%lastPart |
---|
| 359 | |
---|
| 360 | std::string strStartDate="%start_date%" ; |
---|
| 361 | std::string strEndDate="%end_date%" ; |
---|
| 362 | |
---|
| 363 | std::string firstPart ; |
---|
| 364 | std::string middlePart ; |
---|
| 365 | std::string lastPart ; |
---|
| 366 | size_t pos1, pos2 ; |
---|
| 367 | bool hasStartDate=false ; |
---|
[920] | 368 | bool hasEndDate=false ; |
---|
| 369 | bool hasSplit = (!split_freq.isEmpty()); |
---|
[875] | 370 | |
---|
| 371 | pos1=filename.find(strStartDate) ; |
---|
| 372 | if (pos1!=std::string::npos) |
---|
| 373 | { |
---|
| 374 | firstPart=filename.substr(0,pos1) ; |
---|
| 375 | pos1+=strStartDate.size() ; |
---|
| 376 | hasStartDate=true ; |
---|
| 377 | } |
---|
| 378 | else pos1=0 ; |
---|
| 379 | |
---|
| 380 | pos2=filename.find(strEndDate,pos1) ; |
---|
| 381 | if (pos2!=std::string::npos) |
---|
| 382 | { |
---|
[1011] | 383 | middlePart=filename.substr(pos1,pos2-pos1) ; |
---|
[875] | 384 | pos2+=strEndDate.size() ; |
---|
| 385 | lastPart=filename.substr(pos2,filename.size()-pos2) ; |
---|
| 386 | hasEndDate=true ; |
---|
| 387 | } |
---|
| 388 | else middlePart=filename.substr(pos1,filename.size()) ; |
---|
| 389 | |
---|
| 390 | if (!hasStartDate && !hasEndDate) |
---|
| 391 | { |
---|
| 392 | hasStartDate=true ; |
---|
| 393 | hasEndDate=true; |
---|
| 394 | firstPart=middlePart ; |
---|
[920] | 395 | if (hasSplit) firstPart +="_"; |
---|
[875] | 396 | middlePart="-" ; |
---|
| 397 | } |
---|
[920] | 398 | |
---|
[300] | 399 | StdOStringStream oss; |
---|
[538] | 400 | |
---|
[431] | 401 | if (!split_freq.isEmpty()) |
---|
| 402 | { |
---|
[1205] | 403 | CDate split_start ; |
---|
| 404 | CDate splitEnd ; |
---|
| 405 | if (!split_start_offset.isEmpty()) split_start=lastSplit + split_start_offset ; |
---|
| 406 | else split_start=lastSplit ; |
---|
[702] | 407 | |
---|
[1205] | 408 | splitEnd = lastSplit + split_freq ; |
---|
| 409 | if (!split_last_date.isEmpty()) |
---|
| 410 | { |
---|
| 411 | CDate splitLastDate=CDate::FromString(split_last_date,*CContext::getCurrent()->getCalendar()) ; |
---|
| 412 | if( splitLastDate < splitEnd) splitEnd=splitLastDate ; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | if (!split_end_offset.isEmpty()) splitEnd = splitEnd + split_end_offset; |
---|
| 416 | else splitEnd = splitEnd - 1 * Second; |
---|
| 417 | |
---|
[651] | 418 | string splitFormat; |
---|
[431] | 419 | if (split_freq_format.isEmpty()) |
---|
| 420 | { |
---|
[538] | 421 | if (split_freq.getValue().second != 0) splitFormat = "%y%mo%d%h%mi%s"; |
---|
| 422 | else if (split_freq.getValue().minute != 0) splitFormat = "%y%mo%d%h%mi"; |
---|
| 423 | else if (split_freq.getValue().hour != 0) splitFormat = "%y%mo%d%h"; |
---|
| 424 | else if (split_freq.getValue().day != 0) splitFormat = "%y%mo%d"; |
---|
| 425 | else if (split_freq.getValue().month != 0) splitFormat = "%y%mo"; |
---|
| 426 | else splitFormat = "%y"; |
---|
[431] | 427 | } |
---|
[651] | 428 | else splitFormat = split_freq_format; |
---|
[702] | 429 | |
---|
[875] | 430 | oss << firstPart ; |
---|
[1205] | 431 | if (hasStartDate) oss << split_start.getStr(splitFormat) ; |
---|
[875] | 432 | oss << middlePart ; |
---|
| 433 | if (hasEndDate) oss << splitEnd.getStr(splitFormat); |
---|
| 434 | oss << lastPart ; |
---|
[702] | 435 | |
---|
[1090] | 436 | StdString keySuffix("CContext_"+CContext::getCurrent()->getId()+"::CFile_"+getFileOutputName()+"::") ; |
---|
| 437 | context->registryOut->setKey(keySuffix+"splitStart", lastSplit); |
---|
| 438 | context->registryOut->setKey(keySuffix+"splitEnd", splitEnd); |
---|
[431] | 439 | } |
---|
[875] | 440 | else oss<<firstPart<<lastPart ; |
---|
[509] | 441 | |
---|
[528] | 442 | bool append = !this->append.isEmpty() && this->append.getValue(); |
---|
[567] | 443 | |
---|
[517] | 444 | bool useClassicFormat = !format.isEmpty() && format == format_attr::netcdf4_classic; |
---|
[878] | 445 | bool useCFConvention = convention.isEmpty() || convention == convention_attr::CF; |
---|
[517] | 446 | |
---|
[651] | 447 | bool multifile = true; |
---|
[300] | 448 | if (!type.isEmpty()) |
---|
| 449 | { |
---|
[651] | 450 | if (type == type_attr::one_file) multifile = false; |
---|
| 451 | else if (type == type_attr::multiple_file) multifile = true; |
---|
[379] | 452 | |
---|
[509] | 453 | } |
---|
[379] | 454 | #ifndef USING_NETCDF_PAR |
---|
| 455 | if (!multifile) |
---|
| 456 | { |
---|
[651] | 457 | info(0) << "!!! Warning -> Using non parallel version of netcdf, switching in multiple_file mode for file : " << filename << " ..." << endl; |
---|
| 458 | multifile = true; |
---|
[379] | 459 | } |
---|
| 460 | #endif |
---|
[509] | 461 | if (multifile) |
---|
[300] | 462 | { |
---|
[651] | 463 | int commSize, commRank; |
---|
[1328] | 464 | ep_lib::MPI_Comm_size(fileComm, &commSize); |
---|
| 465 | ep_lib::MPI_Comm_rank(fileComm, &commRank); |
---|
[509] | 466 | |
---|
| 467 | if (server->intraCommSize > 1) |
---|
[375] | 468 | { |
---|
[651] | 469 | oss << "_" ; |
---|
| 470 | int width=0; int n = commSize-1; |
---|
| 471 | while (n != 0) { n = n / 10; width++;} |
---|
[509] | 472 | if (!min_digits.isEmpty()) |
---|
[651] | 473 | if (width < min_digits) width = min_digits; |
---|
| 474 | oss.width(width); |
---|
| 475 | oss.fill('0'); |
---|
| 476 | oss << right << commRank; |
---|
[375] | 477 | } |
---|
[300] | 478 | } |
---|
| 479 | oss << ".nc"; |
---|
| 480 | |
---|
[526] | 481 | bool isCollective = par_access.isEmpty() || par_access == par_access_attr::collective; |
---|
| 482 | |
---|
| 483 | if (isOpen) data_out->closeFile(); |
---|
| 484 | |
---|
[1334] | 485 | data_out = boost::shared_ptr<CDataOutput>(new CNc4DataOutput(this, oss.str(), append, useClassicFormat, useCFConvention, |
---|
[1328] | 486 | fileComm, multifile, isCollective, time_counter_name)); |
---|
[528] | 487 | isOpen = true; |
---|
[300] | 488 | |
---|
[528] | 489 | data_out->writeFile(CFile::get(this)); |
---|
| 490 | |
---|
| 491 | // Do not recreate the file structure if opening an existing file |
---|
| 492 | if (!data_out->IsInAppendMode()) |
---|
| 493 | { |
---|
| 494 | std::vector<CField*>::iterator it, end = this->enabledFields.end(); |
---|
| 495 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
| 496 | { |
---|
[347] | 497 | CField* field = *it; |
---|
[300] | 498 | this->data_out->writeFieldGrid(field); |
---|
[528] | 499 | } |
---|
| 500 | this->data_out->writeTimeDimension(); |
---|
[509] | 501 | |
---|
[528] | 502 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
| 503 | { |
---|
[347] | 504 | CField* field = *it; |
---|
[1096] | 505 | this->data_out->writeFieldTimeAxis(field); |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
| 509 | { |
---|
| 510 | CField* field = *it; |
---|
[300] | 511 | this->data_out->writeField(field); |
---|
[528] | 512 | } |
---|
[509] | 513 | |
---|
[651] | 514 | vector<CVariable*> listVars = getAllVariables(); |
---|
[528] | 515 | for (vector<CVariable*>::iterator it = listVars.begin(); it != listVars.end(); it++) |
---|
| 516 | this->data_out->writeAttribute(*it); |
---|
[509] | 517 | |
---|
[528] | 518 | this->data_out->definition_end(); |
---|
| 519 | } |
---|
[1098] | 520 | else |
---|
| 521 | { |
---|
| 522 | // check time axis even in append mode |
---|
| 523 | std::vector<CField*>::iterator it, end = this->enabledFields.end(); |
---|
| 524 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
| 525 | { |
---|
| 526 | CField* field = *it; |
---|
| 527 | this->data_out->writeFieldTimeAxis(field); |
---|
| 528 | } |
---|
| 529 | } |
---|
[219] | 530 | } |
---|
| 531 | } |
---|
| 532 | |
---|
[599] | 533 | /*! |
---|
| 534 | \brief Open an existing NetCDF file in read-only mode |
---|
| 535 | */ |
---|
| 536 | void CFile::openInReadMode(void) |
---|
| 537 | { |
---|
| 538 | CContext* context = CContext::getCurrent(); |
---|
[651] | 539 | CContextServer* server = context->server; |
---|
[599] | 540 | |
---|
| 541 | if (!allDomainEmpty) |
---|
| 542 | { |
---|
[773] | 543 | StdString filename = getFileOutputName(); |
---|
[599] | 544 | StdOStringStream oss; |
---|
| 545 | oss << filename; |
---|
| 546 | |
---|
| 547 | if (!split_freq.isEmpty()) |
---|
| 548 | { |
---|
| 549 | string splitFormat; |
---|
| 550 | if (split_freq_format.isEmpty()) |
---|
| 551 | { |
---|
| 552 | if (split_freq.getValue().second != 0) splitFormat = "%y%mo%d%h%mi%s"; |
---|
| 553 | else if (split_freq.getValue().minute != 0) splitFormat = "%y%mo%d%h%mi"; |
---|
| 554 | else if (split_freq.getValue().hour != 0) splitFormat = "%y%mo%d%h"; |
---|
| 555 | else if (split_freq.getValue().day != 0) splitFormat = "%y%mo%d"; |
---|
| 556 | else if (split_freq.getValue().month != 0) splitFormat = "%y%mo"; |
---|
| 557 | else splitFormat = "%y"; |
---|
| 558 | } |
---|
[651] | 559 | else splitFormat = split_freq_format; |
---|
| 560 | oss << "_" << lastSplit.getStr(splitFormat) |
---|
| 561 | << "-" << (lastSplit + split_freq.getValue() - 1 * Second).getStr(splitFormat); |
---|
[599] | 562 | } |
---|
| 563 | |
---|
| 564 | bool multifile = true; |
---|
| 565 | if (!type.isEmpty()) |
---|
| 566 | { |
---|
| 567 | if (type == type_attr::one_file) multifile = false; |
---|
| 568 | else if (type == type_attr::multiple_file) multifile = true; |
---|
| 569 | } |
---|
| 570 | #ifndef USING_NETCDF_PAR |
---|
| 571 | if (!multifile) |
---|
| 572 | { |
---|
| 573 | info(0) << "!!! Warning -> Using non parallel version of netcdf, switching in multiple_file mode for file : " << filename << " ..." << endl; |
---|
| 574 | multifile = true; |
---|
| 575 | } |
---|
| 576 | #endif |
---|
| 577 | if (multifile) |
---|
| 578 | { |
---|
| 579 | int commSize, commRank; |
---|
[1328] | 580 | ep_lib::MPI_Comm_size(fileComm, &commSize); |
---|
| 581 | ep_lib::MPI_Comm_rank(fileComm, &commRank); |
---|
[599] | 582 | |
---|
| 583 | if (server->intraCommSize > 1) |
---|
| 584 | { |
---|
[651] | 585 | oss << "_"; |
---|
[599] | 586 | int width = 0, n = commSize - 1; |
---|
| 587 | while (n != 0) { n = n / 10; width++; } |
---|
| 588 | if (!min_digits.isEmpty() && width < min_digits) |
---|
| 589 | width = min_digits; |
---|
| 590 | oss.width(width); |
---|
| 591 | oss.fill('0'); |
---|
| 592 | oss << right << commRank; |
---|
| 593 | } |
---|
| 594 | } |
---|
| 595 | oss << ".nc"; |
---|
| 596 | |
---|
| 597 | bool isCollective = par_access.isEmpty() || par_access == par_access_attr::collective; |
---|
| 598 | |
---|
| 599 | if (isOpen) data_out->closeFile(); |
---|
[1334] | 600 | if (time_counter_name.isEmpty()) data_in = boost::shared_ptr<CDataInput>(new CNc4DataInput(oss.str(), fileComm, multifile, isCollective)); |
---|
| 601 | else data_in = boost::shared_ptr<CDataInput>(new CNc4DataInput(oss.str(), fileComm, multifile, isCollective, time_counter_name)); |
---|
[599] | 602 | isOpen = true; |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
[509] | 606 | //! Close file |
---|
[286] | 607 | void CFile::close(void) |
---|
| 608 | { |
---|
[379] | 609 | if (!allDomainEmpty) |
---|
[509] | 610 | if (isOpen) |
---|
[321] | 611 | { |
---|
[599] | 612 | if (mode.isEmpty() || mode.getValue() == mode_attr::write) |
---|
| 613 | this->data_out->closeFile(); |
---|
| 614 | else |
---|
| 615 | this->data_in->closeFile(); |
---|
[321] | 616 | } |
---|
[1328] | 617 | // if (fileComm != MPI_COMM_NULL) MPI_Comm_free(&fileComm); |
---|
| 618 | //if (fileComm.mpi_comm != ::MPI_COMM_NULL) MPI_Comm_free(&fileComm); |
---|
[286] | 619 | } |
---|
[219] | 620 | //---------------------------------------------------------------- |
---|
| 621 | |
---|
[775] | 622 | void CFile::readAttributesOfEnabledFieldsInReadMode() |
---|
| 623 | { |
---|
| 624 | if (enabledFields.empty()) return; |
---|
| 625 | |
---|
| 626 | // Just check file and try to open it |
---|
| 627 | CContext* context = CContext::getCurrent(); |
---|
| 628 | CContextClient* client=context->client; |
---|
| 629 | |
---|
[805] | 630 | // It would probably be better to call initFile() somehow |
---|
[1328] | 631 | ep_lib::MPI_Comm_dup(client->intraComm, &fileComm); |
---|
[805] | 632 | if (time_counter_name.isEmpty()) time_counter_name = "time_counter"; |
---|
| 633 | |
---|
[1328] | 634 | checkFile(); |
---|
[805] | 635 | |
---|
[775] | 636 | for (int idx = 0; idx < enabledFields.size(); ++idx) |
---|
| 637 | { |
---|
| 638 | // First of all, find out which domain and axis associated with this field |
---|
| 639 | enabledFields[idx]->solveGridReference(); |
---|
| 640 | |
---|
| 641 | // Read attributes of domain and axis from this file |
---|
| 642 | this->data_in->readFieldAttributesMetaData(enabledFields[idx]); |
---|
| 643 | |
---|
| 644 | // Now complete domain and axis associated with this field |
---|
| 645 | enabledFields[idx]->solveGenerateGrid(); |
---|
| 646 | |
---|
| 647 | // Read necessary value from file |
---|
[1334] | 648 | #pragma omp critical (_func) |
---|
| 649 | { |
---|
| 650 | this->data_in->readFieldAttributesValues(enabledFields[idx]); |
---|
| 651 | } |
---|
[775] | 652 | // Fill attributes for base reference |
---|
| 653 | enabledFields[idx]->solveGridDomainAxisBaseRef(); |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | // Now everything is ok, close it |
---|
[1153] | 657 | close(); |
---|
[775] | 658 | } |
---|
| 659 | |
---|
| 660 | |
---|
[509] | 661 | /*! |
---|
| 662 | \brief Parse xml file and write information into file object |
---|
| 663 | \param [in] node xmld node corresponding in xml file |
---|
| 664 | */ |
---|
[219] | 665 | void CFile::parse(xml::CXMLNode & node) |
---|
| 666 | { |
---|
| 667 | SuperClass::parse(node); |
---|
[509] | 668 | |
---|
[472] | 669 | if (node.goToChildElement()) |
---|
| 670 | { |
---|
| 671 | do |
---|
| 672 | { |
---|
| 673 | if (node.getElementName()=="field" || node.getElementName()=="field_group") this->getVirtualFieldGroup()->parseChild(node); |
---|
| 674 | else if (node.getElementName()=="variable" || node.getElementName()=="variable_group") this->getVirtualVariableGroup()->parseChild(node); |
---|
[651] | 675 | } while (node.goToNextElement()); |
---|
[472] | 676 | node.goToParentElement(); |
---|
[219] | 677 | } |
---|
[472] | 678 | |
---|
[219] | 679 | } |
---|
| 680 | //---------------------------------------------------------------- |
---|
| 681 | |
---|
[509] | 682 | /*! |
---|
| 683 | \brief Represent a file in form of string with all its info |
---|
| 684 | \return String |
---|
| 685 | */ |
---|
[219] | 686 | StdString CFile::toString(void) const |
---|
| 687 | { |
---|
| 688 | StdOStringStream oss; |
---|
| 689 | |
---|
| 690 | oss << "<" << CFile::GetName() << " "; |
---|
| 691 | if (this->hasId()) |
---|
| 692 | oss << " id=\"" << this->getId() << "\" "; |
---|
| 693 | oss << SuperClassAttribute::toString() << ">" << std::endl; |
---|
[347] | 694 | if (this->getVirtualFieldGroup() != NULL) |
---|
[219] | 695 | oss << *this->getVirtualFieldGroup() << std::endl; |
---|
| 696 | oss << "</" << CFile::GetName() << " >"; |
---|
| 697 | return (oss.str()); |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | //---------------------------------------------------------------- |
---|
[509] | 701 | |
---|
| 702 | /*! |
---|
| 703 | \brief Find all inheritace among objects in a file. |
---|
| 704 | \param [in] apply (true) write attributes of parent into ones of child if they are empty |
---|
| 705 | (false) write attributes of parent into a new container of child |
---|
| 706 | \param [in] parent |
---|
| 707 | */ |
---|
[445] | 708 | void CFile::solveDescInheritance(bool apply, const CAttributeMap * const parent) |
---|
[219] | 709 | { |
---|
[445] | 710 | SuperClassAttribute::setAttributes(parent,apply); |
---|
[509] | 711 | this->getVirtualFieldGroup()->solveDescInheritance(apply, NULL); |
---|
[472] | 712 | this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL); |
---|
[219] | 713 | } |
---|
| 714 | |
---|
| 715 | //---------------------------------------------------------------- |
---|
| 716 | |
---|
[509] | 717 | /*! |
---|
| 718 | \brief Resolve all reference of active fields. |
---|
| 719 | In order to know exactly which data each active field has, a search for all its |
---|
| 720 | reference to find its parents or/and its base reference object must be done. Moreover |
---|
| 721 | during this search, there are some information that can only be sent to server AFTER |
---|
| 722 | all information of active fields are created on server side, e.g: checking mask or index |
---|
| 723 | \param [in] sendToServer: Send all info to server (true) or only a part of it (false) |
---|
| 724 | */ |
---|
[823] | 725 | void CFile::solveOnlyRefOfEnabledFields(bool sendToServer) |
---|
| 726 | { |
---|
| 727 | int size = this->enabledFields.size(); |
---|
| 728 | for (int i = 0; i < size; ++i) |
---|
| 729 | { |
---|
| 730 | this->enabledFields[i]->solveOnlyReferenceEnabledField(sendToServer); |
---|
| 731 | // this->enabledFields[i]->buildGridTransformationGraph(); |
---|
| 732 | } |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | void CFile::generateNewTransformationGridDest() |
---|
| 736 | { |
---|
| 737 | int size = this->enabledFields.size(); |
---|
| 738 | for (int i = 0; i < size; ++i) |
---|
| 739 | { |
---|
| 740 | this->enabledFields[i]->generateNewTransformationGridDest(); |
---|
| 741 | } |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | /*! |
---|
| 745 | \brief Resolve all reference of active fields. |
---|
| 746 | In order to know exactly which data each active field has, a search for all its |
---|
| 747 | reference to find its parents or/and its base reference object must be done. Moreover |
---|
| 748 | during this search, there are some information that can only be sent to server AFTER |
---|
| 749 | all information of active fields are created on server side, e.g: checking mask or index |
---|
| 750 | \param [in] sendToServer: Send all info to server (true) or only a part of it (false) |
---|
| 751 | */ |
---|
[509] | 752 | void CFile::solveAllRefOfEnabledFields(bool sendToServer) |
---|
[459] | 753 | { |
---|
[509] | 754 | int size = this->enabledFields.size(); |
---|
| 755 | for (int i = 0; i < size; ++i) |
---|
| 756 | { |
---|
| 757 | this->enabledFields[i]->solveAllReferenceEnabledField(sendToServer); |
---|
| 758 | } |
---|
[459] | 759 | } |
---|
[509] | 760 | |
---|
| 761 | /*! |
---|
[640] | 762 | * Constructs the filter graph for each active field. |
---|
| 763 | * |
---|
| 764 | * \param gc the garbage collector to use when building the filter graph |
---|
| 765 | */ |
---|
| 766 | void CFile::buildFilterGraphOfEnabledFields(CGarbageCollector& gc) |
---|
| 767 | { |
---|
| 768 | int size = this->enabledFields.size(); |
---|
| 769 | for (int i = 0; i < size; ++i) |
---|
| 770 | { |
---|
| 771 | this->enabledFields[i]->buildFilterGraph(gc, true); |
---|
| 772 | } |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | /*! |
---|
[598] | 776 | Prefetching the data for enabled fields read from file. |
---|
| 777 | */ |
---|
| 778 | void CFile::prefetchEnabledReadModeFields(void) |
---|
| 779 | { |
---|
| 780 | if (mode.isEmpty() || mode.getValue() != mode_attr::read) |
---|
| 781 | return; |
---|
| 782 | |
---|
| 783 | int size = this->enabledFields.size(); |
---|
| 784 | for (int i = 0; i < size; ++i) |
---|
[1013] | 785 | this->enabledFields[i]->sendReadDataRequest(CContext::getCurrent()->getCalendar()->getCurrentDate()); |
---|
[598] | 786 | } |
---|
| 787 | |
---|
| 788 | /*! |
---|
[1328] | 789 | Do all post timestep operations for enabled fields in read mode: |
---|
| 790 | - Prefetch the data read from file when needed |
---|
| 791 | - Check that the data excepted from server has been received |
---|
[598] | 792 | */ |
---|
[1328] | 793 | void CFile::doPostTimestepOperationsForEnabledReadModeFields(void) |
---|
[598] | 794 | { |
---|
| 795 | if (mode.isEmpty() || mode.getValue() != mode_attr::read) |
---|
| 796 | return; |
---|
| 797 | |
---|
| 798 | int size = this->enabledFields.size(); |
---|
| 799 | for (int i = 0; i < size; ++i) |
---|
[1328] | 800 | { |
---|
| 801 | this->enabledFields[i]->checkForLateDataFromServer(); |
---|
[598] | 802 | this->enabledFields[i]->sendReadDataRequestIfNeeded(); |
---|
[1328] | 803 | } |
---|
[598] | 804 | } |
---|
| 805 | |
---|
[445] | 806 | void CFile::solveFieldRefInheritance(bool apply) |
---|
[219] | 807 | { |
---|
[878] | 808 | // Rsolution des hritages par rfrence de chacun des champs contenus dans le fichier. |
---|
[347] | 809 | std::vector<CField*> allF = this->getAllFields(); |
---|
[219] | 810 | for (unsigned int i = 0; i < allF.size(); i++) |
---|
[445] | 811 | allF[i]->solveRefInheritance(apply); |
---|
[219] | 812 | } |
---|
| 813 | |
---|
| 814 | //---------------------------------------------------------------- |
---|
| 815 | |
---|
[509] | 816 | /*! |
---|
| 817 | \brief Add a field into file. |
---|
| 818 | A field is added into file and it will be written out if the file is enabled and |
---|
| 819 | level of this field is smaller than level_output. A new field won't be created if one |
---|
| 820 | with id has already existed |
---|
| 821 | \param [in] id String identity of new field |
---|
| 822 | \return Pointer to added (or already existed) field |
---|
| 823 | */ |
---|
[347] | 824 | CField* CFile::addField(const string& id) |
---|
[300] | 825 | { |
---|
[651] | 826 | return vFieldGroup->createChild(id); |
---|
[300] | 827 | } |
---|
[219] | 828 | |
---|
[509] | 829 | /*! |
---|
| 830 | \brief Add a field group into file. |
---|
| 831 | A field group is added into file and it will play a role as parents for fields. |
---|
| 832 | A new field group won't be created if one with id has already existed |
---|
| 833 | \param [in] id String identity of new field group |
---|
| 834 | \return Pointer to added (or already existed) field group |
---|
| 835 | */ |
---|
[347] | 836 | CFieldGroup* CFile::addFieldGroup(const string& id) |
---|
[300] | 837 | { |
---|
[651] | 838 | return vFieldGroup->createChildGroup(id); |
---|
[300] | 839 | } |
---|
[509] | 840 | |
---|
| 841 | /*! |
---|
| 842 | \brief Add a variable into file. |
---|
| 843 | A variable is added into file and if one with id has already existed, pointer to |
---|
| 844 | it will be returned. |
---|
| 845 | Variable as long as attributes are information container of file. |
---|
| 846 | However, whereas attributes are "fixed" information, variables provides a more flexible way to user |
---|
| 847 | to fill in (extra) information for a file. |
---|
| 848 | \param [in] id String identity of new variable |
---|
| 849 | \return Pointer to added (or already existed) variable |
---|
| 850 | */ |
---|
[472] | 851 | CVariable* CFile::addVariable(const string& id) |
---|
| 852 | { |
---|
[651] | 853 | return vVariableGroup->createChild(id); |
---|
[472] | 854 | } |
---|
| 855 | |
---|
[509] | 856 | /*! |
---|
| 857 | \brief Add a variable group into file. |
---|
| 858 | A variable group is added into file and it will play a role as parents for variables. |
---|
| 859 | A new variable group won't be created if one with id has already existed |
---|
| 860 | \param [in] id String identity of new variable group |
---|
| 861 | \return Pointer to added (or already existed) variable group |
---|
| 862 | */ |
---|
[472] | 863 | CVariableGroup* CFile::addVariableGroup(const string& id) |
---|
| 864 | { |
---|
[651] | 865 | return vVariableGroup->createChildGroup(id); |
---|
[472] | 866 | } |
---|
[509] | 867 | |
---|
| 868 | /*! |
---|
| 869 | \brief Send a message to create a field on server side |
---|
| 870 | \param[in] id String identity of field that will be created on server |
---|
| 871 | */ |
---|
[300] | 872 | void CFile::sendAddField(const string& id) |
---|
| 873 | { |
---|
[651] | 874 | CContext* context = CContext::getCurrent(); |
---|
[509] | 875 | |
---|
[300] | 876 | if (! context->hasServer ) |
---|
| 877 | { |
---|
[651] | 878 | CContextClient* client = context->client; |
---|
[300] | 879 | |
---|
[651] | 880 | CEventClient event(this->getType(),EVENT_ID_ADD_FIELD); |
---|
[300] | 881 | if (client->isServerLeader()) |
---|
| 882 | { |
---|
[651] | 883 | CMessage msg; |
---|
| 884 | msg << this->getId(); |
---|
| 885 | msg << id; |
---|
[595] | 886 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 887 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 888 | event.push(*itRank,1,msg); |
---|
[651] | 889 | client->sendEvent(event); |
---|
[300] | 890 | } |
---|
[651] | 891 | else client->sendEvent(event); |
---|
[300] | 892 | } |
---|
[509] | 893 | |
---|
[300] | 894 | } |
---|
[509] | 895 | |
---|
| 896 | /*! |
---|
| 897 | \brief Send a message to create a field group on server side |
---|
| 898 | \param[in] id String identity of field group that will be created on server |
---|
| 899 | */ |
---|
[300] | 900 | void CFile::sendAddFieldGroup(const string& id) |
---|
| 901 | { |
---|
[651] | 902 | CContext* context = CContext::getCurrent(); |
---|
[300] | 903 | if (! context->hasServer ) |
---|
| 904 | { |
---|
[651] | 905 | CContextClient* client = context->client; |
---|
[300] | 906 | |
---|
[651] | 907 | CEventClient event(this->getType(),EVENT_ID_ADD_FIELD_GROUP); |
---|
[300] | 908 | if (client->isServerLeader()) |
---|
| 909 | { |
---|
[651] | 910 | CMessage msg; |
---|
| 911 | msg << this->getId(); |
---|
| 912 | msg << id; |
---|
[595] | 913 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 914 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 915 | event.push(*itRank,1,msg); |
---|
[651] | 916 | client->sendEvent(event); |
---|
[300] | 917 | } |
---|
[651] | 918 | else client->sendEvent(event); |
---|
[300] | 919 | } |
---|
[509] | 920 | |
---|
[300] | 921 | } |
---|
[509] | 922 | |
---|
| 923 | /*! |
---|
| 924 | \brief Receive a message annoucing the creation of a field on server side |
---|
| 925 | \param[in] event Received event |
---|
| 926 | */ |
---|
[300] | 927 | void CFile::recvAddField(CEventServer& event) |
---|
| 928 | { |
---|
[509] | 929 | |
---|
[651] | 930 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
[300] | 931 | string id; |
---|
[651] | 932 | *buffer>>id; |
---|
| 933 | get(id)->recvAddField(*buffer); |
---|
[300] | 934 | } |
---|
[509] | 935 | |
---|
| 936 | /*! |
---|
| 937 | \brief Receive a message annoucing the creation of a field on server side |
---|
| 938 | \param[in] buffer Buffer containing message |
---|
| 939 | */ |
---|
[300] | 940 | void CFile::recvAddField(CBufferIn& buffer) |
---|
| 941 | { |
---|
[651] | 942 | string id; |
---|
| 943 | buffer>>id; |
---|
| 944 | addField(id); |
---|
[300] | 945 | } |
---|
| 946 | |
---|
[509] | 947 | /*! |
---|
| 948 | \brief Receive a message annoucing the creation of a field group on server side |
---|
| 949 | \param[in] event Received event |
---|
| 950 | */ |
---|
[300] | 951 | void CFile::recvAddFieldGroup(CEventServer& event) |
---|
| 952 | { |
---|
[509] | 953 | |
---|
[651] | 954 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
[300] | 955 | string id; |
---|
[651] | 956 | *buffer>>id; |
---|
| 957 | get(id)->recvAddFieldGroup(*buffer); |
---|
[300] | 958 | } |
---|
[509] | 959 | |
---|
| 960 | /*! |
---|
| 961 | \brief Receive a message annoucing the creation of a field group on server side |
---|
| 962 | \param[in] buffer Buffer containing message |
---|
| 963 | */ |
---|
[300] | 964 | void CFile::recvAddFieldGroup(CBufferIn& buffer) |
---|
| 965 | { |
---|
[651] | 966 | string id; |
---|
| 967 | buffer>>id; |
---|
| 968 | addFieldGroup(id); |
---|
[300] | 969 | } |
---|
| 970 | |
---|
[509] | 971 | /*! |
---|
| 972 | \brief Send messages to duplicate all variables on server side |
---|
| 973 | Because each variable has also its attributes. So first thing to do is replicate |
---|
| 974 | all these attributes on server side. Because variable can have a value, the second thing |
---|
| 975 | is to duplicate this value on server, too. |
---|
| 976 | */ |
---|
| 977 | void CFile::sendAddAllVariables() |
---|
| 978 | { |
---|
[957] | 979 | std::vector<CVariable*> allVar = getAllVariables(); |
---|
| 980 | std::vector<CVariable*>::const_iterator it = allVar.begin(); |
---|
| 981 | std::vector<CVariable*>::const_iterator itE = allVar.end(); |
---|
| 982 | |
---|
| 983 | for (; it != itE; ++it) |
---|
[509] | 984 | { |
---|
[957] | 985 | this->sendAddVariable((*it)->getId()); |
---|
| 986 | (*it)->sendAllAttributesToServer(); |
---|
| 987 | (*it)->sendValue(); |
---|
[509] | 988 | } |
---|
| 989 | } |
---|
[472] | 990 | |
---|
[509] | 991 | /*! |
---|
| 992 | \brief Send a message to create a variable on server side |
---|
| 993 | A variable always belongs to a variable group |
---|
| 994 | \param[in] id String identity of variable that will be created on server |
---|
| 995 | */ |
---|
[472] | 996 | void CFile::sendAddVariable(const string& id) |
---|
| 997 | { |
---|
[651] | 998 | CContext* context = CContext::getCurrent(); |
---|
[509] | 999 | |
---|
[472] | 1000 | if (! context->hasServer ) |
---|
| 1001 | { |
---|
[651] | 1002 | CContextClient* client = context->client; |
---|
[472] | 1003 | |
---|
[651] | 1004 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE); |
---|
[472] | 1005 | if (client->isServerLeader()) |
---|
| 1006 | { |
---|
[651] | 1007 | CMessage msg; |
---|
| 1008 | msg << this->getId(); |
---|
| 1009 | msg << id; |
---|
[595] | 1010 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 1011 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 1012 | event.push(*itRank,1,msg); |
---|
[651] | 1013 | client->sendEvent(event); |
---|
[472] | 1014 | } |
---|
[651] | 1015 | else client->sendEvent(event); |
---|
[472] | 1016 | } |
---|
[509] | 1017 | |
---|
[472] | 1018 | } |
---|
[509] | 1019 | |
---|
| 1020 | /*! |
---|
| 1021 | \brief Send a message to create a variable group on server side |
---|
| 1022 | \param[in] id String identity of variable group that will be created on server |
---|
| 1023 | */ |
---|
[472] | 1024 | void CFile::sendAddVariableGroup(const string& id) |
---|
| 1025 | { |
---|
[651] | 1026 | CContext* context = CContext::getCurrent(); |
---|
[472] | 1027 | if (! context->hasServer ) |
---|
| 1028 | { |
---|
[651] | 1029 | CContextClient* client = context->client; |
---|
[472] | 1030 | |
---|
[651] | 1031 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE_GROUP); |
---|
[472] | 1032 | if (client->isServerLeader()) |
---|
| 1033 | { |
---|
[651] | 1034 | CMessage msg; |
---|
| 1035 | msg << this->getId(); |
---|
| 1036 | msg << id; |
---|
[595] | 1037 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
| 1038 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 1039 | event.push(*itRank,1,msg); |
---|
[651] | 1040 | client->sendEvent(event); |
---|
[472] | 1041 | } |
---|
[651] | 1042 | else client->sendEvent(event); |
---|
[472] | 1043 | } |
---|
[509] | 1044 | |
---|
[472] | 1045 | } |
---|
[509] | 1046 | |
---|
| 1047 | /*! |
---|
| 1048 | \brief Receive a message annoucing the creation of a variable on server side |
---|
| 1049 | \param[in] event Received event |
---|
| 1050 | */ |
---|
[472] | 1051 | void CFile::recvAddVariable(CEventServer& event) |
---|
| 1052 | { |
---|
[509] | 1053 | |
---|
[651] | 1054 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
[472] | 1055 | string id; |
---|
[651] | 1056 | *buffer>>id; |
---|
| 1057 | get(id)->recvAddVariable(*buffer); |
---|
[472] | 1058 | } |
---|
[509] | 1059 | |
---|
| 1060 | /*! |
---|
| 1061 | \brief Receive a message annoucing the creation of a variable on server side |
---|
| 1062 | \param[in] buffer Buffer containing message |
---|
| 1063 | */ |
---|
[472] | 1064 | void CFile::recvAddVariable(CBufferIn& buffer) |
---|
| 1065 | { |
---|
[651] | 1066 | string id; |
---|
| 1067 | buffer>>id; |
---|
| 1068 | addVariable(id); |
---|
[472] | 1069 | } |
---|
| 1070 | |
---|
[509] | 1071 | /*! |
---|
| 1072 | \brief Receive a message annoucing the creation of a variable group on server side |
---|
| 1073 | \param[in] event Received event |
---|
| 1074 | */ |
---|
[472] | 1075 | void CFile::recvAddVariableGroup(CEventServer& event) |
---|
| 1076 | { |
---|
[509] | 1077 | |
---|
[651] | 1078 | CBufferIn* buffer = event.subEvents.begin()->buffer; |
---|
[472] | 1079 | string id; |
---|
[651] | 1080 | *buffer>>id; |
---|
| 1081 | get(id)->recvAddVariableGroup(*buffer); |
---|
[472] | 1082 | } |
---|
[509] | 1083 | |
---|
| 1084 | /*! |
---|
| 1085 | \brief Receive a message annoucing the creation of a variable group on server side |
---|
| 1086 | \param[in] buffer Buffer containing message |
---|
| 1087 | */ |
---|
[472] | 1088 | void CFile::recvAddVariableGroup(CBufferIn& buffer) |
---|
| 1089 | { |
---|
[651] | 1090 | string id; |
---|
| 1091 | buffer>>id; |
---|
| 1092 | addVariableGroup(id); |
---|
[472] | 1093 | } |
---|
| 1094 | |
---|
[509] | 1095 | /*! |
---|
| 1096 | \brief Sending all active (enabled) fields from client to server. |
---|
| 1097 | Each field is identified uniquely by its string identity. Not only should we |
---|
| 1098 | send the id to server but also we need to send ids of reference domain and reference axis. |
---|
| 1099 | With these two id, it's easier to make reference to grid where all data should be written. |
---|
| 1100 | Remark: This function must be called AFTER all active (enabled) files have been created on the server side |
---|
| 1101 | */ |
---|
| 1102 | void CFile::sendEnabledFields() |
---|
| 1103 | { |
---|
[770] | 1104 | size_t size = this->enabledFields.size(); |
---|
| 1105 | for (size_t i = 0; i < size; ++i) |
---|
[509] | 1106 | { |
---|
[770] | 1107 | CField* field = this->enabledFields[i]; |
---|
| 1108 | this->sendAddField(field->getId()); |
---|
[1328] | 1109 | field->checkAttributes(); |
---|
[770] | 1110 | field->sendAllAttributesToServer(); |
---|
| 1111 | field->sendAddAllVariables(); |
---|
[509] | 1112 | } |
---|
| 1113 | } |
---|
[472] | 1114 | |
---|
[509] | 1115 | /*! |
---|
| 1116 | \brief Dispatch event received from client |
---|
| 1117 | Whenever a message is received in buffer of server, it will be processed depending on |
---|
| 1118 | its event type. A new event type should be added in the switch list to make sure |
---|
| 1119 | it processed on server side. |
---|
| 1120 | \param [in] event: Received message |
---|
| 1121 | */ |
---|
[300] | 1122 | bool CFile::dispatchEvent(CEventServer& event) |
---|
| 1123 | { |
---|
[651] | 1124 | if (SuperClass::dispatchEvent(event)) return true; |
---|
[300] | 1125 | else |
---|
| 1126 | { |
---|
| 1127 | switch(event.type) |
---|
| 1128 | { |
---|
| 1129 | case EVENT_ID_ADD_FIELD : |
---|
[651] | 1130 | recvAddField(event); |
---|
| 1131 | return true; |
---|
| 1132 | break; |
---|
[509] | 1133 | |
---|
[300] | 1134 | case EVENT_ID_ADD_FIELD_GROUP : |
---|
[651] | 1135 | recvAddFieldGroup(event); |
---|
| 1136 | return true; |
---|
| 1137 | break; |
---|
[509] | 1138 | |
---|
[472] | 1139 | case EVENT_ID_ADD_VARIABLE : |
---|
[651] | 1140 | recvAddVariable(event); |
---|
| 1141 | return true; |
---|
| 1142 | break; |
---|
[509] | 1143 | |
---|
[472] | 1144 | case EVENT_ID_ADD_VARIABLE_GROUP : |
---|
[651] | 1145 | recvAddVariableGroup(event); |
---|
| 1146 | return true; |
---|
| 1147 | break; |
---|
[300] | 1148 | default : |
---|
[651] | 1149 | ERROR("bool CFile::dispatchEvent(CEventServer& event)", << "Unknown Event"); |
---|
| 1150 | return false; |
---|
[300] | 1151 | } |
---|
| 1152 | } |
---|
| 1153 | } |
---|
[509] | 1154 | |
---|
| 1155 | |
---|
| 1156 | |
---|
| 1157 | |
---|
[219] | 1158 | ///--------------------------------------------------------------- |
---|
| 1159 | |
---|
[335] | 1160 | } // namespace xios |
---|