Skip to content

Commit

Permalink
Use the netCDF type transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalhorton committed May 23, 2018
1 parent f7ce875 commit ccd9d8a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 48 deletions.
62 changes: 32 additions & 30 deletions src/shared_base/core/asFileNetcdf.cpp
Expand Up @@ -1291,9 +1291,10 @@ void asFileNetcdf::GetVarArray(const wxString &varName, const size_t indexStart[

// Check the given type
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_SHORT)
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."),
varName, (int) nctype, (int) NC_SHORT));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vara_short(m_fileId, varId, indexStart, indexCount, pValue);
Expand Down Expand Up @@ -1323,9 +1324,10 @@ void asFileNetcdf::GetVarArray(const wxString &varName, const size_t indexStart[

// Check the given type
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_INT)
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."),
varName, (int) nctype, (int) NC_INT));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vara_int(m_fileId, varId, indexStart, indexCount, pValue);
Expand Down Expand Up @@ -1355,10 +1357,10 @@ void asFileNetcdf::GetVarArray(const wxString &varName, const size_t indexStart[

// Check the given type
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_FLOAT)
asThrowException(
wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."), varName,
(int) nctype, (int) NC_FLOAT));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vara_float(m_fileId, varId, indexStart, indexCount, pValue);
Expand Down Expand Up @@ -1388,10 +1390,10 @@ void asFileNetcdf::GetVarArray(const wxString &varName, const size_t indexStart[

// Check the given type
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_DOUBLE)
asThrowException(
wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."), varName,
(int) nctype, (int) NC_DOUBLE));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vara_double(m_fileId, varId, indexStart, indexCount, pValue);
Expand Down Expand Up @@ -1421,10 +1423,10 @@ void asFileNetcdf::GetVarSample(const wxString &varName, const size_t indexStart

// Check the given type
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_SHORT)
asThrowException(
wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."), varName,
(int) nctype, (int) NC_SHORT));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vars_short(m_fileId, varId, indexStart, indexCount, indexStride, pValue);
Expand Down Expand Up @@ -1455,10 +1457,10 @@ void asFileNetcdf::GetVarSample(const wxString &varName, const size_t indexStart

// Check the given type
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_INT)
asThrowException(
wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."), varName,
(int) nctype, (int) NC_INT));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vars_int(m_fileId, varId, indexStart, indexCount, indexStride, pValue);
Expand Down Expand Up @@ -1489,10 +1491,10 @@ void asFileNetcdf::GetVarSample(const wxString &varName, const size_t indexStart

// Check the given type. Allow the short type here.
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_FLOAT)
asThrowException(
wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."), varName,
(int) nctype, (int) NC_FLOAT));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vars_float(m_fileId, varId, indexStart, indexCount, indexStride, pValue);
Expand Down Expand Up @@ -1523,10 +1525,10 @@ void asFileNetcdf::GetVarSample(const wxString &varName, const size_t indexStart

// Check the given type
nc_type nctype = m_struct.vars[varId].type;
if (nctype != NC_DOUBLE)
asThrowException(
wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (%d)."), varName,
(int) nctype, (int) NC_DOUBLE));
if (nctype != NC_FLOAT && nctype != NC_SHORT && nctype != NC_DOUBLE && nctype != NC_INT && nctype != NC_INT64) {
asThrowException(wxString::Format(_("The variable (%s) type (%d) in file doesn't match the desired type (numeric)."),
varName, (int) nctype));
}

// Get value
m_status = nc_get_vars_double(m_fileId, varId, indexStart, indexCount, indexStride, pValue);
Expand Down
20 changes: 2 additions & 18 deletions src/shared_base/core/asPredictor.cpp
Expand Up @@ -930,12 +930,6 @@ int *asPredictor::GetIndexesCountGrib(int iArea) const

bool asPredictor::GetDataFromFile(asFileNetcdf &ncFile, vvva2f &compositeData)
{
bool isShort = (ncFile.GetVarType(m_fileVarName) == NC_SHORT);
bool isFloat = (ncFile.GetVarType(m_fileVarName) == NC_FLOAT);

if (!isShort && !isFloat) {
wxLogError(_("Loading data other than short or float is not implemented yet."));
}

// Check if scaling is needed
bool scalingNeeded = true;
Expand All @@ -955,16 +949,12 @@ bool asPredictor::GetDataFromFile(asFileNetcdf &ncFile, vvva2f &compositeData)

// Create the arrays to receive the data
vf dataF;
vs dataS;

// Resize the arrays to store the new data
unsigned int totLength = (unsigned int) m_fInd.memberCount * m_fInd.timeArrayCount *
m_fInd.areas[iArea].latCount * m_fInd.areas[iArea].lonCount;
wxASSERT(totLength > 0);
dataF.resize(totLength);
if (isShort) {
dataS.resize(totLength);
}

// Fill empty beginning with NaNs
int indexBegining = 0;
Expand Down Expand Up @@ -993,14 +983,8 @@ bool asPredictor::GetDataFromFile(asFileNetcdf &ncFile, vvva2f &compositeData)
}

// In the netCDF Common Data Language, variables are printed with the outermost dimension first and the innermost dimension last.
if (isFloat) {
ncFile.GetVarSample(m_fileVarName, GetIndexesStartNcdf(iArea), GetIndexesCountNcdf(iArea),
GetIndexesStrideNcdf(), &dataF[indexBegining]);
} else if (isShort) {
ncFile.GetVarSample(m_fileVarName, GetIndexesStartNcdf(iArea), GetIndexesCountNcdf(iArea),
GetIndexesStrideNcdf(), &dataS[indexBegining]);
dataF = vf(dataS.begin(), dataS.end());
}
ncFile.GetVarSample(m_fileVarName, GetIndexesStartNcdf(iArea), GetIndexesCountNcdf(iArea),
GetIndexesStrideNcdf(), &dataF[indexBegining]);

// Keep data for later treatment
vectData.push_back(dataF);
Expand Down

0 comments on commit ccd9d8a

Please sign in to comment.