[5490] | 1 | #ifdef HAVE_CONFIG_H |
---|
| 2 | # include "config.h" |
---|
| 3 | #endif |
---|
| 4 | |
---|
[5487] | 5 | #include <string> |
---|
[5490] | 6 | #ifdef HAVE_SYS_TYPES_H |
---|
| 7 | # include <sys/types.h> |
---|
| 8 | #endif |
---|
| 9 | #include <boost/shared_array.hpp> |
---|
[5487] | 10 | #include <netcdf.h> |
---|
[5488] | 11 | #include "sww_file.hh" |
---|
[5487] | 12 | |
---|
[5490] | 13 | using boost::shared_array; |
---|
[5487] | 14 | using std::string; |
---|
| 15 | |
---|
[5488] | 16 | SWWFile::SWWFile(const string &file_name){ |
---|
[5487] | 17 | int ncstatus; |
---|
| 18 | if((ncstatus = nc_open(file_name.c_str(), |
---|
| 19 | NC_SHARE, &this->netcdf_id)) != NC_NOERR) |
---|
| 20 | throw nc_strerror(ncstatus); |
---|
[5490] | 21 | this->number_of_points = this->nc_inq_dimlen_by_name("number_of_points"); |
---|
[5493] | 22 | this->number_of_timesteps = |
---|
| 23 | this->nc_inq_dimlen_by_name("number_of_timesteps"); |
---|
[5490] | 24 | this->number_of_vertices = this->nc_inq_dimlen_by_name("number_of_vertices"); |
---|
| 25 | this->number_of_volumes = this->nc_inq_dimlen_by_name("number_of_volumes"); |
---|
| 26 | this->x = shared_array<float>(new float[this->number_of_points]); |
---|
| 27 | this->nc_get_var_float_by_name("x", this->x.get()); |
---|
| 28 | this->y = shared_array<float>(new float[this->number_of_points]); |
---|
| 29 | this->nc_get_var_float_by_name("y", this->y.get()); |
---|
| 30 | this->volumes = shared_array<int>(new int[this->number_of_volumes * |
---|
| 31 | this->number_of_vertices]); |
---|
| 32 | this->nc_get_var_int_by_name("volumes", this->volumes.get()); |
---|
| 33 | this->compute_extents(); |
---|
[5487] | 34 | } |
---|
| 35 | |
---|
[5488] | 36 | SWWFile::~SWWFile(void){ |
---|
[5487] | 37 | nc_close(this->netcdf_id); |
---|
| 38 | } |
---|
[5490] | 39 | |
---|
[5502] | 40 | int SWWFile::nc_inq_varndims_by_name(const string &name) const{ |
---|
| 41 | int ncstatus; |
---|
| 42 | int dimid; |
---|
| 43 | int ndims; |
---|
[5598] | 44 | if(((ncstatus = nc_inq_varid(this->netcdf_id, |
---|
[5502] | 45 | name.c_str(), &dimid)) != NC_NOERR) || |
---|
| 46 | ((ncstatus = nc_inq_varndims(this->netcdf_id, |
---|
| 47 | dimid, &ndims)) != NC_NOERR)) |
---|
| 48 | throw nc_strerror(ncstatus); |
---|
| 49 | return ndims; |
---|
| 50 | } |
---|
| 51 | |
---|
[5490] | 52 | void SWWFile::compute_extents(void){ |
---|
| 53 | this->minX = this->x[0]; |
---|
| 54 | this->maxX = this->x[0]; |
---|
| 55 | this->minY = this->y[0]; |
---|
| 56 | this->maxY = this->y[0]; |
---|
| 57 | for(int i = 1 ; i < this->number_of_points ; ++i){ |
---|
| 58 | if(this->x[i] < this->minX) this->minX = this->x[i]; |
---|
| 59 | if(this->x[i] > this->maxX) this->maxX = this->x[i]; |
---|
| 60 | if(this->y[i] < this->minY) this->minY = this->y[i]; |
---|
| 61 | if(this->y[i] > this->maxY) this->maxY = this->y[i]; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
[5502] | 65 | void SWWFile::nc_get_var_float_by_name(const string &name, float array[]) const{ |
---|
[5490] | 66 | int ncstatus; |
---|
| 67 | int varid; |
---|
| 68 | if(((ncstatus = nc_inq_varid(this->netcdf_id, |
---|
| 69 | name.c_str(), &varid)) != NC_NOERR) || |
---|
| 70 | ((ncstatus = nc_get_var_float(this->netcdf_id, |
---|
| 71 | varid, array)) != NC_NOERR)) |
---|
| 72 | throw nc_strerror(ncstatus); |
---|
| 73 | } |
---|
| 74 | |
---|
[5502] | 75 | void SWWFile::nc_get_var_int_by_name(const string &name, int array[]) const{ |
---|
[5490] | 76 | int ncstatus; |
---|
| 77 | int varid; |
---|
| 78 | if(((ncstatus = nc_inq_varid(this->netcdf_id, |
---|
| 79 | name.c_str(), &varid)) != NC_NOERR) || |
---|
| 80 | ((ncstatus = nc_get_var_int(this->netcdf_id, |
---|
| 81 | varid, array)) != NC_NOERR)) |
---|
| 82 | throw nc_strerror(ncstatus); |
---|
| 83 | } |
---|
| 84 | |
---|
[5502] | 85 | size_t SWWFile::nc_inq_dimlen_by_name(const string &name) const{ |
---|
[5490] | 86 | int ncstatus; |
---|
| 87 | int dimid; |
---|
| 88 | size_t len; |
---|
| 89 | if(((ncstatus = nc_inq_dimid(this->netcdf_id, |
---|
| 90 | name.c_str(), &dimid)) != NC_NOERR) || |
---|
| 91 | ((ncstatus = nc_inq_dimlen(this->netcdf_id, |
---|
| 92 | dimid, &len)) != NC_NOERR)) |
---|
| 93 | throw nc_strerror(ncstatus); |
---|
| 94 | return len; |
---|
| 95 | } |
---|