------------------------------------------------------------------------------
| 2588 | |
| 2589 | //------------------------------------------------------------------------------ |
| 2590 | void vtkMPASReader::LoadTimeFieldData(vtkUnstructuredGrid* dataset) |
| 2591 | { |
| 2592 | vtkStringArray* array = nullptr; |
| 2593 | vtkFieldData* fd = dataset->GetFieldData(); |
| 2594 | if (!fd) |
| 2595 | { |
| 2596 | fd = vtkFieldData::New(); |
| 2597 | dataset->SetFieldData(fd); |
| 2598 | fd->Delete(); |
| 2599 | } |
| 2600 | |
| 2601 | if (vtkDataArray* da = fd->GetArray("Time")) |
| 2602 | { |
| 2603 | if (!(array = vtkArrayDownCast<vtkStringArray>(da))) |
| 2604 | { |
| 2605 | vtkWarningMacro("Not creating \"Time\" field data array: a data array " |
| 2606 | "with this name already exists."); |
| 2607 | return; |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | if (!array) |
| 2612 | { |
| 2613 | array = vtkStringArray::New(); |
| 2614 | array->SetName("Time"); |
| 2615 | fd->AddArray(array); |
| 2616 | array->Delete(); |
| 2617 | } |
| 2618 | |
| 2619 | // If the xtime variable exists, use its value at the current timestep: |
| 2620 | std::string time; |
| 2621 | int varid; |
| 2622 | if ((varid = this->Internals->nc_var_id("xtime", false)) != -1) |
| 2623 | { |
| 2624 | if (this->Internals->ValidateDimensions(varid, false, 2, "Time", "StrLen")) |
| 2625 | { |
| 2626 | int dimid = this->Internals->nc_dim_id("StrLen"); |
| 2627 | assert(dimid != -1); |
| 2628 | size_t strLen = 0; |
| 2629 | this->Internals->nc_err(nc_inq_dimlen(this->Internals->ncFile, dimid, &strLen)); |
| 2630 | if (strLen > 0) |
| 2631 | { |
| 2632 | time.resize(strLen); |
| 2633 | size_t start[] = { this->Internals->GetCursorForDimension(dimid), 0 }; |
| 2634 | size_t count[] = { 1, strLen }; |
| 2635 | if (this->Internals->nc_err( |
| 2636 | nc_get_vara_text(this->Internals->ncFile, varid, start, count, time.data()))) |
| 2637 | { |
| 2638 | // Trim off trailing whitespace: |
| 2639 | size_t realLength = time.find_last_not_of(' '); |
| 2640 | if (realLength != std::string::npos) |
| 2641 | { |
| 2642 | time.resize(realLength + 1); |
| 2643 | } |
| 2644 | } |
| 2645 | else |
| 2646 | { |
| 2647 | vtkWarningMacro("Error reading xtime variable from file."); |
no test coverage detected