Skip to content

Commit

Permalink
Merge pull request #26 from MetOffice/feature/file_closure
Browse files Browse the repository at this point in the history
Feature/file closure
  • Loading branch information
phlndrwd authored Oct 24, 2023
2 parents 45c1e65 + 28dc225 commit 94662e8
Show file tree
Hide file tree
Showing 22 changed files with 131 additions and 42 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###############################################################################
# MONIO - Met Office NetCDF Input Output #
# #
# (C) Crown Copyright 2023 Met Office #
# (C) Crown Copyright 2023, Met Office. #
# #
# This software is licensed under the terms of the Apache Licence Version 2.0 #
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. #
Expand Down
9 changes: 9 additions & 0 deletions ci/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
###############################################################################
# MONIO - Met Office NetCDF Input Output #
# #
# (C) Crown Copyright 2023, Met Office. #
# #
# This software is licensed under the terms of the Apache Licence Version 2.0 #
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. #
###############################################################################

cmake_minimum_required( VERSION 3.20 FATAL_ERROR )

project( monio-ci VERSION 0.0.1 LANGUAGES C CXX Fortran )
Expand Down
11 changes: 8 additions & 3 deletions ci/build-and-lint
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/bash
#
# (C) Crown Copyright 2023, the Met Office. All rights reserved.
#
###############################################################################
# MONIO - Met Office NetCDF Input Output #
# #
# (C) Crown Copyright 2023, Met Office. #
# #
# This software is licensed under the terms of the Apache Licence Version 2.0 #
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. #
###############################################################################
set -euo pipefail

finally() {
Expand Down
11 changes: 8 additions & 3 deletions ci/build-and-test
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/bash
#
# (C) Crown Copyright 2023, the Met Office. All rights reserved.
#
###############################################################################
# MONIO - Met Office NetCDF Input Output #
# #
# (C) Crown Copyright 2023, Met Office. #
# #
# This software is licensed under the terms of the Apache Licence Version 2.0 #
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. #
###############################################################################
set -euxo pipefail

finally() {
Expand Down
8 changes: 8 additions & 0 deletions ci/get-ref-branch
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#!/bin/bash
###############################################################################
# MONIO - Met Office NetCDF Input Output #
# #
# (C) Crown Copyright 2023, Met Office. #
# #
# This software is licensed under the terms of the Apache Licence Version 2.0 #
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. #
###############################################################################
set -euxo pipefail
repo="$1" # expect path to a repo clone

Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###############################################################################
# MONIO - Met Office NetCDF Input Output #
# #
# (C) Crown Copyright 2023 Met Office #
# (C) Crown Copyright 2023, Met Office. #
# #
# This software is licensed under the terms of the Apache Licence Version 2.0 #
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. #
Expand Down
8 changes: 8 additions & 0 deletions src/monio/AtlasReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "Utils.h"
#include "UtilsAtlas.h"
#include "Monio.h"

monio::AtlasReader::AtlasReader(const eckit::mpi::Comm& mpiCommunicator,
const int mpiRankOwner):
Expand Down Expand Up @@ -59,6 +60,7 @@ void monio::AtlasReader::populateFieldWithDataContainer(atlas::Field& field,
break;
}
default: {
Monio::get().closeFiles();
utils::throwException("AtlasReader::populateFieldWithDataContainer()> "
"Data type not coded for...");
}
Expand Down Expand Up @@ -91,6 +93,7 @@ void monio::AtlasReader::populateFieldWithDataContainer(atlas::Field& field,
break;
}
default: {
Monio::get().closeFiles();
utils::throwException("AtlasReader::populateFieldWithDataContainer()> "
"Data type not coded for...");
}
Expand All @@ -107,6 +110,7 @@ void monio::AtlasReader::populateField(atlas::Field& field,
auto fieldView = atlas::array::make_view<T, 2>(field);
// Field with noFirstLevel == true should have been adjusted to have 70 levels.
if (noFirstLevel == true && field.levels() == consts::kVerticalFullSize) {
Monio::get().closeFiles();
utils::throwException("AtlasReader::populateField()> Field levels misconfiguration...");
// Only valid case for field with noFirstLevel == true. Field is adjusted to have 70 levels but
// read data still has enough to fill 71.
Expand All @@ -118,6 +122,7 @@ void monio::AtlasReader::populateField(atlas::Field& field,
if (std::size_t(index) <= dataVec.size()) {
fieldView(i, j - 1) = dataVec[index];
} else {
Monio::get().closeFiles();
utils::throwException("AtlasReader::populateField()> Calculated index exceeds size of "
"data for field \"" + field.name() + "\".");
}
Expand All @@ -132,6 +137,7 @@ void monio::AtlasReader::populateField(atlas::Field& field,
if (std::size_t(index) <= dataVec.size()) {
fieldView(i, j) = dataVec[index];
} else {
Monio::get().closeFiles();
utils::throwException("AtlasReader::populateField()> Calculated index exceeds size of "
"data for field \"" + field.name() + "\".");
}
Expand Down Expand Up @@ -170,6 +176,7 @@ void monio::AtlasReader::populateField(atlas::Field& field,
if (std::size_t(index) <= dataVec.size()) {
fieldView(i, j) = dataVec[index];
} else {
Monio::get().closeFiles();
utils::throwException("AtlasReader::populateField()> "
"Calculated index exceeds size of data.");
}
Expand All @@ -192,6 +199,7 @@ atlas::Field monio::AtlasReader::getReadField(atlas::Field& field,
if (atlasType != atlasType.KIND_REAL64 &&
atlasType != atlasType.KIND_REAL32 &&
atlasType != atlasType.KIND_INT32) {
Monio::get().closeFiles();
utils::throwException("AtlasReader::getReadField())> Data type not coded for...");
}
atlas::util::Config atlasOptions = atlas::option::name(field.name()) |
Expand Down
13 changes: 10 additions & 3 deletions src/monio/AtlasWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "DataContainerFloat.h"
#include "DataContainerInt.h"
#include "Metadata.h"
#include "Monio.h"
#include "Utils.h"
#include "UtilsAtlas.h"
#include "Writer.h"
Expand Down Expand Up @@ -202,7 +203,8 @@ void monio::AtlasWriter::populateDataContainerWithField(
break;
}
default: {
utils::throwException("AtlasWriter::populateDataContainerWithField()> "
Monio::get().closeFiles();
utils::throwException("AtlasWriter::populateDataContainerWithField()> "
"Data type not coded for...");
}
}
Expand Down Expand Up @@ -254,7 +256,8 @@ void monio::AtlasWriter::populateDataContainerWithField(
break;
}
default: {
utils::throwException("AtlasWriter::populateDataContainerWithField()> "
Monio::get().closeFiles();
utils::throwException("AtlasWriter::populateDataContainerWithField()> "
"Data type not coded for...");
}
}
Expand All @@ -268,7 +271,8 @@ void monio::AtlasWriter::populateDataVec(std::vector<T>& dataVec,
oops::Log::debug() << "AtlasWriter::populateDataVec() " << field.name() << std::endl;
int numLevels = field.levels();
if ((lfricToAtlasMap.size() * numLevels) != dataVec.size()) {
utils::throwException("AtlasWriter::populateDataVec()> "
Monio::get().closeFiles();
utils::throwException("AtlasWriter::populateDataVec()> "
"Data container is not configured for the expected data...");
}
auto fieldView = atlas::array::make_view<T, 2>(field);
Expand Down Expand Up @@ -323,10 +327,12 @@ atlas::Field monio::AtlasWriter::getWriteField(atlas::Field& field,
if (atlasType != atlasType.KIND_REAL64 &&
atlasType != atlasType.KIND_REAL32 &&
atlasType != atlasType.KIND_INT32) {
Monio::get().closeFiles();
utils::throwException("AtlasWriter::getWriteField())> Data type not coded for...");
}
// Erroneous case. For noFirstLevel == true field should have 70 levels
if (noFirstLevel == true && field.levels() == consts::kVerticalFullSize) {
Monio::get().closeFiles();
utils::throwException("AtlasWriter::getWriteField()> Field levels misconfiguration...");
}
// WARNING - This name-check is an LFRic-Lite specific convention...
Expand All @@ -350,6 +356,7 @@ atlas::Field monio::AtlasWriter::getWriteField(atlas::Field& field,
field.metadata().set("name", writeName);
}
} else {
Monio::get().closeFiles();
utils::throwException("AtlasWriter::getWriteField()> Field write name misconfiguration...");
}
return field;
Expand Down
6 changes: 3 additions & 3 deletions src/monio/AtlasWriter.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/******************************************************************************
* MONIO - Met Office NetCDF Input Output *
* *
* (C) Crown Copyright 2023, Met Office. All rights reserved. *
* (C) Crown Copyright 2023 Met Office *
* *
* This software is licensed under the terms of the 3-Clause BSD License *
* which can be obtained from https://opensource.org/license/bsd-3-clause/. *
* This software is licensed under the terms of the Apache Licence Version 2.0 *
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. *
******************************************************************************/
#pragma once

Expand Down
2 changes: 2 additions & 0 deletions src/monio/Data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "DataContainerDouble.h"
#include "DataContainerFloat.h"
#include "DataContainerInt.h"
#include "Monio.h"
#include "Utils.h"

namespace {
Expand Down Expand Up @@ -142,6 +143,7 @@ std::shared_ptr<monio::DataContainerBase>
if (it != dataContainers_.end()) {
return it->second;
} else {
Monio::get().closeFiles();
utils::throwException("DataContainer named \"" + name + "\" was not found.");
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/monio/DataContainerDouble.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdexcept>

#include "Constants.h"
#include "Monio.h"
#include "Utils.h"

monio::DataContainerDouble::DataContainerDouble(const std::string& name) :
Expand All @@ -30,6 +31,7 @@ const std::vector<double>& monio::DataContainerDouble::getData() const {

const double& monio::DataContainerDouble::getDatum(const size_t index) {
if (index > dataVector_.size()) {
Monio::get().closeFiles();
utils::throwException("DataContainerDouble::getDatum()> Passed index exceeds vector size...");
}

Expand Down
2 changes: 2 additions & 0 deletions src/monio/DataContainerFloat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdexcept>

#include "Constants.h"
#include "Monio.h"
#include "Utils.h"

monio::DataContainerFloat::DataContainerFloat(const std::string& name) :
Expand All @@ -30,6 +31,7 @@ const std::vector<float>& monio::DataContainerFloat::getData() const {

const float& monio::DataContainerFloat::getDatum(const size_t index) {
if (index > dataVector_.size()) {
Monio::get().closeFiles();
utils::throwException("DataContainerFloat::getDatum()> Passed index exceeds vector size...");
}
return dataVector_[index];
Expand Down
2 changes: 2 additions & 0 deletions src/monio/DataContainerInt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdexcept>

#include "Constants.h"
#include "Monio.h"
#include "Utils.h"

monio::DataContainerInt::DataContainerInt(const std::string& name) :
Expand All @@ -30,6 +31,7 @@ const std::vector<int>& monio::DataContainerInt::getData() const {

const int& monio::DataContainerInt::getDatum(const size_t index) {
if (index > dataVector_.size()) {
Monio::get().closeFiles();
utils::throwException("DataContainerInt::getDatum()> Passed index exceeds vector size...");
}
return dataVector_[index];
Expand Down
4 changes: 2 additions & 2 deletions src/monio/File.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ void monio::File::readAttributes(Metadata& metadata) {
metadata.addGlobalAttr(globAttr->getName(), globAttr);
}
} else {
utils::throwException(
"File::readAttributes()> Write file accessed for reading...");
close();
utils::throwException("File::readAttributes()> Write file accessed for reading...");
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/monio/Metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "AttributeDouble.h"
#include "AttributeInt.h"
#include "AttributeString.h"
#include "Monio.h"
#include "Utils.h"

monio::Metadata::Metadata() {
Expand Down Expand Up @@ -142,6 +143,7 @@ int monio::Metadata::getDimension(const std::string& dimName) {
if (isDimDefined(dimName) == true) {
return dimensions_.at(dimName);
} else {
Monio::get().closeFiles();
utils::throwException("Metadata::getDimension()> dimension \"" + dimName + "\" not found...");
}
}
Expand Down Expand Up @@ -170,6 +172,7 @@ std::shared_ptr<monio::Variable> monio::Metadata::getVariable(const std::string&
if (it != variables_.end()) {
return variables_.at(varName);
} else {
Monio::get().closeFiles();
utils::throwException("Metadata::getVariable()> variable \"" + varName + "\" not found...");
}
}
Expand All @@ -182,6 +185,7 @@ const std::shared_ptr<monio::Variable>
if (it != variables_.end()) {
return variables_.at(varName);
} else {
Monio::get().closeFiles();
utils::throwException("Metadata::getVariable()> variable \"" + varName + "\" not found...");
}
}
Expand Down Expand Up @@ -223,6 +227,7 @@ const std::vector<std::string> monio::Metadata::getVarStrAttrs(
varStrAttrs.push_back(attr);
}
if (varNames.size() != varStrAttrs.size()) {
Monio::get().closeFiles();
utils::throwException("Metadata::getVarStrAttrs()> "
"Unmatched number of variables and attributes...");
}
Expand Down Expand Up @@ -361,6 +366,7 @@ void monio::Metadata::deleteVariable(const std::string& varName) {
if (it != variables_.end()) {
variables_.erase(varName);
} else {
Monio::get().closeFiles();
utils::throwException("Metadata::deleteVariable()> Variable \"" + varName + "\" not found...");
}
}
Expand Down Expand Up @@ -432,6 +438,7 @@ void monio::Metadata::printVariables() {
break;
}
default: {
Monio::get().closeFiles();
utils::throwException("Metadata::printGlobalAttrs()> Data type not coded for...");
}
}
Expand Down Expand Up @@ -464,6 +471,7 @@ void monio::Metadata::printGlobalAttrs() {
break;
}
default: {
Monio::get().closeFiles();
utils::throwException("Metadata::printGlobalAttrs()> Data type not coded for...");
}
}
Expand Down
Loading

0 comments on commit 94662e8

Please sign in to comment.