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