Skip to content

Commit

Permalink
Uses geopotential_height as altitude when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
tnipen committed May 27, 2015
1 parent f6989f4 commit d281800
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
25 changes: 21 additions & 4 deletions src/File/Arome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ FileArome::FileArome(std::string iFilename, bool iReadOnly) : FileNetcdf(iFilena

mLats = getLatLonVariable("latitude");
mLons = getLatLonVariable("longitude");
mElevs = getLatLonVariable("altitude");
if(hasVariableCore("surface_geopotential")) {
FieldPtr elevField = getFieldCore("surface_geopotential", 0);
mElevs.resize(getNumLat());
for(int i = 0; i < getNumLat(); i++) {
mElevs[i].resize(getNumLon());
for(int j = 0; j < getNumLon(); j++) {
float value = (*elevField)(i,j,0) / 9.81;
mElevs[i][j] = value;
}
}
std::cout << "Deriving altitude from geopotential height in " << getFilename() << std::endl;
}
else {
mElevs = getLatLonVariable("altitude");
}

if(hasVar("time")) {
NcVar* vTime = getVar("time");
Expand All @@ -42,9 +56,12 @@ FileArome::FileArome(std::string iFilename, bool iReadOnly) : FileNetcdf(iFilena
}

FieldPtr FileArome::getFieldCore(Variable::Type iVariable, int iTime) const {
std::string variable = getVariableName(iVariable);
std::string variableName = getVariableName(iVariable);
return getFieldCore(variableName, iTime);
}
FieldPtr FileArome::getFieldCore(std::string iVariable, int iTime) const {
// Not cached, retrieve data
NcVar* var = getVar(variable);
NcVar* var = getVar(iVariable);
int nLat = mNLat;
int nLon = mNLon;

Expand All @@ -70,7 +87,7 @@ FieldPtr FileArome::getFieldCore(Variable::Type iVariable, int iTime) const {
}
else {
std::stringstream ss;
ss << "Cannot read variable '" << variable << "' from '" << getFilename() << "'";
ss << "Cannot read variable '" << iVariable << "' from '" << getFilename() << "'";
Util::error(ss.str());
}
float* values = new float[nLat*nLon];
Expand Down
1 change: 1 addition & 0 deletions src/File/Arome.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FileArome : public FileNetcdf {
protected:
void writeCore(std::vector<Variable::Type> iVariables);
FieldPtr getFieldCore(Variable::Type iVariable, int iTime) const;
FieldPtr getFieldCore(std::string iVariable, int iTime) const;
vec2 getLatLonVariable(std::string iVariable) const;
int mDate;
};
Expand Down
7 changes: 5 additions & 2 deletions src/File/Netcdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ FileNetcdf::~FileNetcdf() {
}

bool FileNetcdf::hasVariableCore(Variable::Type iVariable) const {
NcError q(NcError::silent_nonfatal);
std::string variable = getVariableName(iVariable);
NcVar* var = mFile.get_var(variable.c_str());
return hasVariableCore(variable);
}
bool FileNetcdf::hasVariableCore(std::string iVariable) const {
NcError q(NcError::silent_nonfatal);
NcVar* var = mFile.get_var(iVariable.c_str());
return var != NULL;
}

Expand Down
6 changes: 4 additions & 2 deletions src/File/Netcdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class FileNetcdf : public File {
FileNetcdf(std::string iFilename, bool iReadOnly=false);
~FileNetcdf();

// Does this file contain the variable?
bool hasVariableCore(Variable::Type iVariable) const;
virtual std::string getVariableName(Variable::Type iVariable) const = 0;
//! Add attribute to a variable (overwrite if existing)
void setAttribute(NcVar* iVar, std::string iName, std::string iValue);
Expand All @@ -31,6 +29,10 @@ class FileNetcdf : public File {
float getOffset(NcVar* iVar) const;
NcFile mFile;

// Does this file contain the variable?
bool hasVariableCore(Variable::Type iVariable) const;
bool hasVariableCore(std::string iVariable) const;

NcDim* getDim(std::string iDim) const;
NcVar* getVar(std::string iVar) const;
bool hasDim(std::string iDim) const;
Expand Down

0 comments on commit d281800

Please sign in to comment.