#ifdef HAVE_CONFIG_H # include "config.h" #endif #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #include #include "sww_file.hh" using boost::shared_array; using std::string; SWWFile::SWWFile(const string &file_name){ int ncstatus; if((ncstatus = nc_open(file_name.c_str(), NC_SHARE, &this->netcdf_id)) != NC_NOERR) throw nc_strerror(ncstatus); this->number_of_points = this->nc_inq_dimlen_by_name("number_of_points"); this->number_of_timesteps = this->nc_inq_dimlen_by_name("number_of_timesteps"); this->number_of_vertices = this->nc_inq_dimlen_by_name("number_of_vertices"); this->number_of_volumes = this->nc_inq_dimlen_by_name("number_of_volumes"); this->x = shared_array(new float[this->number_of_points]); this->nc_get_var_float_by_name("x", this->x.get()); this->y = shared_array(new float[this->number_of_points]); this->nc_get_var_float_by_name("y", this->y.get()); this->volumes = shared_array(new int[this->number_of_volumes * this->number_of_vertices]); this->nc_get_var_int_by_name("volumes", this->volumes.get()); this->compute_extents(); } SWWFile::~SWWFile(void){ nc_close(this->netcdf_id); } int SWWFile::nc_inq_varndims_by_name(const string &name) const{ int ncstatus; int dimid; int ndims; if(((ncstatus = nc_inq_varid(this->netcdf_id, name.c_str(), &dimid)) != NC_NOERR) || ((ncstatus = nc_inq_varndims(this->netcdf_id, dimid, &ndims)) != NC_NOERR)) throw nc_strerror(ncstatus); return ndims; } void SWWFile::compute_extents(void){ this->minX = this->x[0]; this->maxX = this->x[0]; this->minY = this->y[0]; this->maxY = this->y[0]; for(int i = 1 ; i < this->number_of_points ; ++i){ if(this->x[i] < this->minX) this->minX = this->x[i]; if(this->x[i] > this->maxX) this->maxX = this->x[i]; if(this->y[i] < this->minY) this->minY = this->y[i]; if(this->y[i] > this->maxY) this->maxY = this->y[i]; } } void SWWFile::nc_get_var_float_by_name(const string &name, float array[]) const{ int ncstatus; int varid; if(((ncstatus = nc_inq_varid(this->netcdf_id, name.c_str(), &varid)) != NC_NOERR) || ((ncstatus = nc_get_var_float(this->netcdf_id, varid, array)) != NC_NOERR)) throw nc_strerror(ncstatus); } void SWWFile::nc_get_var_int_by_name(const string &name, int array[]) const{ int ncstatus; int varid; if(((ncstatus = nc_inq_varid(this->netcdf_id, name.c_str(), &varid)) != NC_NOERR) || ((ncstatus = nc_get_var_int(this->netcdf_id, varid, array)) != NC_NOERR)) throw nc_strerror(ncstatus); } size_t SWWFile::nc_inq_dimlen_by_name(const string &name) const{ int ncstatus; int dimid; size_t len; if(((ncstatus = nc_inq_dimid(this->netcdf_id, name.c_str(), &dimid)) != NC_NOERR) || ((ncstatus = nc_inq_dimlen(this->netcdf_id, dimid, &len)) != NC_NOERR)) throw nc_strerror(ncstatus); return len; }