WSL/SLF GitLab Repository

Commit af549e40 authored by Mathias Bavay's avatar Mathias Bavay
Browse files

Small clean up for the specific/relative humidity conversions. Small code...

Small clean up for the specific/relative humidity conversions. Small code clean up (mostly indentation) for NetCDF.
parent 7bc48460
......@@ -805,7 +805,8 @@ double Atmosphere::DewPointtoRh(double TD, double TA, const bool& force_water)
double Atmosphere::specToRelHumidity(const double& altitude, const double& TA, const double& qi)
{
const double SatVaporDensity = waterVaporDensity(TA, waterSaturationPressure(TA));
const double RH = (qi/(1.-qi))*stdDryAirDensity(altitude, TA)/SatVaporDensity;
const double dryAir_density = stdDryAirDensity(altitude, TA);
const double RH = qi/(1.-qi) * dryAir_density/SatVaporDensity;
if(RH>1.) return 1.;
else return RH;
......@@ -821,9 +822,10 @@ double Atmosphere::specToRelHumidity(const double& altitude, const double& TA, c
double Atmosphere::relToSpecHumidity(const double& altitude, const double& TA, const double& RH)
{
const double dryAir_density = stdDryAirDensity(altitude, TA);
const double wetAir_density = RH * waterVaporDensity(TA,waterSaturationPressure(TA));
const double qi = 1./( dryAir_density/wetAir_density+1. );
return qi;
const double SatVaporDensity = waterVaporDensity(TA, waterSaturationPressure(TA));
const double qi_inv = dryAir_density/(RH*SatVaporDensity) + 1.;
return 1./qi_inv;
}
} //namespace
/***********************************************************************************/
/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/* Copyright 2014 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/***********************************************************************************/
/* This file is part of MeteoIO.
MeteoIO is free software: you can redistribute it and/or modify
......@@ -16,8 +16,15 @@
along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
*/
#include "NetCDFIO.h"
#include <meteoio/ResamplingAlgorithms2D.h>
#include <meteoio/meteostats/libinterpol1D.h>
#include <meteoio/Timer.h>
#include <meteoio/MathOptim.h>
#include <meteoio/plugins/libncpp.h>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
using namespace ncpp; // wrappers for libnetcdf
......@@ -140,14 +147,14 @@ bool NetCDFIO::initStaticData()
return true;
}
NetCDFIO::NetCDFIO(const std::string& configfile) : cfg(configfile), coordin(""), coordinparam(""), coordout(""), coordoutparam(""),
NetCDFIO::NetCDFIO(const std::string& configfile) : cfg(configfile), coordin(), coordinparam(), coordout(), coordoutparam(),
in_dflt_TZ(0.), out_dflt_TZ(0.), in_strict(false), out_strict(false), vecMetaData()
{
IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
parseInputOutputSection();
}
NetCDFIO::NetCDFIO(const Config& cfgreader) : cfg(cfgreader), coordin(""), coordinparam(""), coordout(""), coordoutparam(""),
NetCDFIO::NetCDFIO(const Config& cfgreader) : cfg(cfgreader), coordin(), coordinparam(), coordout(), coordoutparam(),
in_dflt_TZ(0.), out_dflt_TZ(0.), in_strict(false), out_strict(false), vecMetaData()
{
IOUtils::getProjectionParameters(cfg, coordin, coordinparam, coordout, coordoutparam);
......@@ -181,7 +188,7 @@ void NetCDFIO::read2DGrid(Grid2DObject& grid_out, const std::string& arguments)
void NetCDFIO::read2DGrid(Grid2DObject& grid_out, const MeteoGrids::Parameters& parameter, const Date& date)
{
string filename("");
string filename;
cfg.getValue("GRID2DFILE", "Input", filename);
const string varname = get_varname(parameter);
......@@ -311,7 +318,7 @@ double NetCDFIO::calculate_cellsize(const size_t& latlen, const size_t& lonlen,
void NetCDFIO::readDEM(DEMObject& dem_out)
{
string filename(""), varname("");
string filename, varname;
cfg.getValue("DEMFILE", "Input", filename);
cfg.getValue("DEMVAR", "Input", varname);
......@@ -338,7 +345,7 @@ void NetCDFIO::readStationData(const Date&, std::vector<StationData>& vecStation
return;
}
string filename("");
string filename;
cfg.getValue("METEOFILE", "Input", filename);
int ncid;
......@@ -422,7 +429,7 @@ void NetCDFIO::readMeteoData(const Date& dateStart, const Date& dateEnd, std::ve
{
vecMeteo.clear();
string filename("");
string filename;
cfg.getValue("METEOFILE", "Input", filename);
int ncid;
......@@ -559,9 +566,9 @@ void NetCDFIO::copy_data(const int& ncid, const std::map<std::string, size_t>& m
}
}
// Go through all variables present in the NetCDF dataset that have the correct dimensions. A map called
// map_parameters will associate all parameters present with MeteoData parameters or IOUtils::npos). If
// the CNRM parameter does not have a corresponding parameter in the meteo_data object we can add a new
// Go through all variables present in the NetCDF dataset that have the correct dimensions. A map called
// map_parameters will associate all parameters present with MeteoData parameters or IOUtils::npos). If
// the CNRM parameter does not have a corresponding parameter in the meteo_data object we can add a new
// parameter (e.g. cnrm_theorsw) or if the situation is more complex (e.g. rainfall is measured with two
// parameters) we deal with the situation in copy_data().
void NetCDFIO::get_parameters(const int& ncid, std::map<std::string, size_t>& map_parameters, MeteoData& meteo_data)
......@@ -575,7 +582,7 @@ void NetCDFIO::get_parameters(const int& ncid, std::map<std::string, size_t>& ma
for (vector<string>::const_iterator it = present_parameters.begin(); it != present_parameters.end(); ++it) {
const string& name = *it;
//cout << "Found parameter: " << name << endl;
//cout << "Found parameter: " << name << endl;
// Check if parameter exists in paramname, which holds strict CNRM parameters
map<string, size_t>::const_iterator strict_it = paramname.find(name);
......@@ -701,7 +708,7 @@ void NetCDFIO::writeMeteoData(const std::vector< std::vector<MeteoData> >& vecMe
const size_t number_of_records = vecMeteo[0].size();
string filename("");
string filename;
cfg.getValue("METEOFILE", "Output", filename);
int ncid, did_time, vid_time, did_points;
......@@ -772,8 +779,8 @@ void NetCDFIO::copy_data(const size_t& number_of_stations, const size_t& number_
simple_copy = true;
}
for (size_t ii=0; ii<number_of_stations; ii++) {
for (size_t jj=0; jj<number_of_records; jj++) {
for (size_t ii=0; ii<number_of_stations; ++ii) {
for (size_t jj=0; jj<number_of_records; ++jj) {
const double& value = vecMeteo[ii][jj](param);
if (value == IOUtils::nodata) {
......@@ -878,9 +885,9 @@ void NetCDFIO::get_parameters(const std::vector< std::vector<MeteoData> >& vecMe
//Check consistency, dates must be existent everywhere
bool inconsistent = false;
for (size_t ii=0; ii<vecMeteo.size(); ii++) {
for (size_t ii=0; ii<vecMeteo.size(); ++ii) {
if (number_of_records != vecMeteo[ii].size()) inconsistent = true;
for (size_t jj=0; jj<vecMeteo[ii].size(); jj++) {
for (size_t jj=0; jj<vecMeteo[ii].size(); ++jj) {
const MeteoData& meteo_data = vecMeteo[ii][jj];
if (dates[jj] != meteo_data.date.getModifiedJulianDate()) inconsistent = true;
......@@ -894,7 +901,7 @@ void NetCDFIO::get_parameters(const std::vector< std::vector<MeteoData> >& vecMe
}
//Check which parameters are in use
for (size_t kk=0; kk<nr_of_parameters; kk++) {
for (size_t kk=0; kk<nr_of_parameters; ++kk) {
if (!vec_param_in_use[kk]){
if (meteo_data(kk) != IOUtils::nodata){
vec_param_in_use[kk] = true;
......@@ -907,7 +914,7 @@ void NetCDFIO::get_parameters(const std::vector< std::vector<MeteoData> >& vecMe
if (inconsistent) throw IOException("Inconsistent dates in vecMeteo between different stations", AT);
for (size_t kk=0; kk<nr_of_parameters; kk++) {
for (size_t kk=0; kk<nr_of_parameters; ++kk) {
if (vec_param_in_use[kk])
map_param_name[kk] = vec_param_name[kk];
}
......@@ -938,7 +945,7 @@ void NetCDFIO::write2DGrid(const Grid2DObject& grid_in, const std::string& argum
void NetCDFIO::write2DGrid(const Grid2DObject& grid_in, const MeteoGrids::Parameters& parameter, const Date& date)
{
string filename("");
string filename;
cfg.getValue("GRID2DFILE", "Output", filename);
const string varname = get_varname(parameter);
......@@ -1060,8 +1067,8 @@ void NetCDFIO::create_time_dimension(const int& ncid, int& did_time, int& vid_ti
void NetCDFIO::fill_data(const Grid2DObject& grid, double*& data)
{
for (size_t kk=0; kk<grid.nrows; kk++) {
for (size_t ll=0; ll<grid.ncols; ll++) {
for (size_t kk=0; kk<grid.nrows; ++kk) {
for (size_t ll=0; ll<grid.ncols; ++ll) {
data[kk*grid.ncols + ll] = grid.grid2D(ll,kk);
}
}
......@@ -1176,11 +1183,11 @@ void NetCDFIO::calculate_dimensions(const Grid2DObject& grid, double*& lat_array
// The method to use interval*ii is consistent with the corresponding
// calculation of the Grid2DObject::gridify method -> numerical stability
for (size_t ii=1; ii<grid.nrows; ii++) {
for (size_t ii=1; ii<grid.nrows; ++ii) {
lat_array[ii] = lat_array[0] + lat_interval*ii;
}
for (size_t ii=1; ii<grid.ncols; ii++) {
for (size_t ii=1; ii<grid.ncols; ++ii) {
lon_array[ii] = lon_array[0] + lon_interval*ii;
}
}
......@@ -1205,12 +1212,12 @@ void NetCDFIO::check_consistency(const int& ncid, const Grid2DObject& grid, doub
read_data(ncid, cf_latitude, vid_lat, lat);
read_data(ncid, cf_longitude, vid_lon, lon);
for (size_t ii=0; ii<latlen; ii++) {
for (size_t ii=0; ii<latlen; ++ii) {
if (lat_array[ii] != lat[ii])
throw IOException("Error while writing grid - grid and lat/lon coordinates are inconsistent", AT);
}
for (size_t ii=0; ii<lonlen; ii++) {
for (size_t ii=0; ii<lonlen; ++ii) {
if (lon_array[ii] != lon[ii])
throw IOException("Error while writing grid - grid and lat/lon coordinates are inconsistent", AT);
}
......
/***********************************************************************************/
/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/* Copyright 2014 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/***********************************************************************************/
/* This file is part of MeteoIO.
MeteoIO is free software: you can redistribute it and/or modify
......@@ -20,14 +20,8 @@
#include <meteoio/IOInterface.h>
#include <meteoio/Config.h>
#include <meteoio/ResamplingAlgorithms2D.h>
#include <meteoio/meteostats/libinterpol1D.h>
#include <meteoio/plugins/libncpp.h>
#include <string>
#include <cmath>
#include <cstdio>
#include <algorithm>
namespace mio {
......
/***********************************************************************************/
/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/* Copyright 2014 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/***********************************************************************************/
/* This file is part of MeteoIO.
MeteoIO is free software: you can redistribute it and/or modify
......@@ -67,17 +67,17 @@ void get_attribute(const int& ncid, const std::string& varname, const int& varid
{
size_t attr_len;
int status = nc_inq_attlen (ncid, varid, attr_name.c_str(), &attr_len);
int status = nc_inq_attlen (ncid, varid, attr_name.c_str(), &attr_len);
if (status != NC_NOERR)
throw IOException("Could not retrieve attribute '" + attr_name + "'for var '" + varname + "': " + nc_strerror(status), AT);
char* value = new char[attr_len + 1]; // +1 for trailing null
char* value = new char[attr_len + 1]; // +1 for trailing null
status = nc_get_att_text(ncid, varid, attr_name.c_str(), value);
status = nc_get_att_text(ncid, varid, attr_name.c_str(), value);
if (status != NC_NOERR)
throw IOException("Could not read attribute '" + attr_name + "'for var '" + varname + "': " + nc_strerror(status), AT);
value[attr_len] = '\0';
value[attr_len] = '\0';
attr_value = string(value);
delete[] value;
......@@ -113,14 +113,13 @@ void get_variables(const int& ncid, const std::vector<std::string>& dimensions,
// Variable IDs in a NetCDF file are consecutive integers starting with 0
for (int ii=0; ii<nr_of_variables; ++ii) {
char name[NC_MAX_NAME+1];
status = nc_inq_varname(ncid, ii, name);
if (status != NC_NOERR) throw IOException(nc_strerror(status), AT);
const int stat = nc_inq_varname(ncid, ii, name);
if (stat != NC_NOERR) throw IOException(nc_strerror(stat), AT);
string varname(name);
const string varname(name);
const bool check = check_dimensions(ncid, varname, ii, dimensions);
if (check) variables.push_back(varname);
//cout << varname << " is conformant: " << (check ? "yes" : "no") << endl;
}
}
......@@ -136,13 +135,13 @@ bool check_dimensions(const int& ncid, const std::string& varname, const int& va
if ((int)names.size() != ndimsp) return false;
for (int ii=0; ii<ndimsp; ii++) {
for (int ii=0; ii<ndimsp; ++ii) {
char name[NC_MAX_NAME+1];
const int stat = nc_inq_dimname(ncid, dimids[ii], name);
if (stat != NC_NOERR) throw IOException(nc_strerror(stat), AT);
const string dimname = string(name);
const string dimname(name);
const bool exists = (dimname == names[ii]); //(find(names.begin(), names.end(), dimname) != names.end());
if (!exists) return false;
......@@ -152,7 +151,7 @@ bool check_dimensions(const int& ncid, const std::string& varname, const int& va
}
void get_dimension(const int& ncid, const std::string& varname, const int& varid,
std::vector<int>& dimid, std::vector<int>& dim_varid, std::vector<std::string>& dimname, std::vector<size_t>& dimlen)
std::vector<int>& dimid, std::vector<int>& dim_varid, std::vector<std::string>& dimname, std::vector<size_t>& dimlen)
{
dimid.clear(); dim_varid.clear(); dimname.clear(); dimlen.clear();
......@@ -162,7 +161,7 @@ void get_dimension(const int& ncid, const std::string& varname, const int& varid
if (status != NC_NOERR)
throw IOException("Could not retrieve dimensions for variable '" + varname + "': " + nc_strerror(status), AT);
for (int ii=0; ii<ndimsp; ii++) {
for (int ii=0; ii<ndimsp; ++ii) {
int dimvarid;
size_t length=0;
char name[NC_MAX_NAME+1];
......@@ -185,7 +184,7 @@ void get_dimension(const int& ncid, const std::string& varname, const int& varid
}
void read_data_2D(const int& ncid, const std::string& varname, const int& varid,
const size_t& record, const size_t& nr_of_records, const size_t& length, double*& data)
const size_t& record, const size_t& nr_of_records, const size_t& length, double*& data)
{
size_t start[] = {record, 0};
size_t count[] = {nr_of_records, length};
......@@ -211,7 +210,7 @@ void read_value(const int& ncid, const std::string& varname, const int& varid, c
}
void read_data(const int& ncid, const std::string& varname, const int& varid,
const size_t& pos, const size_t& latlen, const size_t& lonlen, double*& data)
const size_t& pos, const size_t& latlen, const size_t& lonlen, double*& data)
{
size_t start[] = {pos, 0, 0};
size_t count[] = {1, latlen, lonlen};
......@@ -265,7 +264,7 @@ size_t add_record(const int& ncid, const std::string& varname, const int& varid,
if (last_value == data) return (dimlen - 1); //The timestamp already exists
if (last_value > data) {
size_t pos = find_record(ncid, varname, dimid, data); // Search for a possible match
const size_t pos = find_record(ncid, varname, dimid, data); // Search for a possible match
if (pos != IOUtils::npos) {
return pos;
......
/***********************************************************************************/
/* Copyright 2009 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/* Copyright 2014 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
/***********************************************************************************/
/* This file is part of MeteoIO.
MeteoIO is free software: you can redistribute it and/or modify
......@@ -39,7 +39,7 @@ namespace ncpp {
void add_1D_variable(const int& ncid, const std::string& varname, const nc_type& xtype, const int& dimid, int& varid);
void add_2D_variable(const int& ncid, const std::string& varname, const nc_type& xtype, const int& dimid1, const int& dimid2, int& varid);
void add_3D_variable(const int& ncid, const std::string& varname, const nc_type& xtype, const int& dimid_record,
const int& dimid1, const int& dimid2, int& varid);
const int& dimid1, const int& dimid2, int& varid);
//Adding attributes
void add_attribute(const int& ncid, const int& varid, const std::string& attr_name, const std::string& attr_value);
......@@ -52,9 +52,9 @@ namespace ncpp {
//Reading data from NetCDF file
void read_data(const int& ncid, const std::string& varname, const int& varid,
const size_t& pos, const size_t& latlen, const size_t& lonlen, double*& data);
const size_t& pos, const size_t& latlen, const size_t& lonlen, double*& data);
void read_data_2D(const int& ncid, const std::string& varname, const int& varid,
const size_t& record, const size_t& count, const size_t& length, double*& data);
const size_t& record, const size_t& count, const size_t& length, double*& data);
void read_value(const int& ncid, const std::string& varname, const int& varid, double& data);
void read_value(const int& ncid, const std::string& varname, const int& varid, const size_t& pos, double& data);
void read_data(const int& ncid, const std::string& varname, const int& varid, double*& data);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment