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