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