source: anuga_work/development/anugavis/src/sww_file.cc @ 5502

Last change on this file since 5502 was 5502, checked in by jack, 14 years ago

More work on height quantities.

File size: 3.0 KB
Line 
1#ifdef HAVE_CONFIG_H
2#  include "config.h"
3#endif
4
5#include <string>
6#ifdef HAVE_SYS_TYPES_H
7#  include <sys/types.h>
8#endif
9#include <boost/shared_array.hpp>
10#include <netcdf.h>
11#include "sww_file.hh"
12
13using boost::shared_array;
14using std::string;
15
16SWWFile::SWWFile(const string &file_name){
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);
21  this->number_of_points = this->nc_inq_dimlen_by_name("number_of_points");
22  this->number_of_timesteps =
23    this->nc_inq_dimlen_by_name("number_of_timesteps");
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();
34}
35
36SWWFile::~SWWFile(void){
37  nc_close(this->netcdf_id);
38}
39
40
41int SWWFile::nc_inq_varndims_by_name(const string &name) const{
42  int ncstatus;
43  int dimid;
44  int ndims;
45  if(((ncstatus = nc_inq_dimid(this->netcdf_id,
46                               name.c_str(), &dimid)) != NC_NOERR) ||
47     ((ncstatus = nc_inq_varndims(this->netcdf_id,
48                                  dimid, &ndims)) != NC_NOERR))
49    throw nc_strerror(ncstatus);
50  return ndims;
51}
52
53void SWWFile::compute_extents(void){
54  this->minX = this->x[0];
55  this->maxX = this->x[0];
56  this->minY = this->y[0];
57  this->maxY = this->y[0];
58  for(int i = 1 ; i < this->number_of_points ; ++i){
59    if(this->x[i] < this->minX) this->minX = this->x[i];
60    if(this->x[i] > this->maxX) this->maxX = this->x[i];
61    if(this->y[i] < this->minY) this->minY = this->y[i];
62    if(this->y[i] > this->maxY) this->maxY = this->y[i];
63  }
64}
65
66void SWWFile::nc_get_var_float_by_name(const string &name, float array[]) const{
67  int ncstatus;
68  int varid;
69  if(((ncstatus = nc_inq_varid(this->netcdf_id,
70                               name.c_str(), &varid)) != NC_NOERR) ||
71     ((ncstatus = nc_get_var_float(this->netcdf_id,
72                                   varid, array)) != NC_NOERR))
73    throw nc_strerror(ncstatus);
74}
75
76void SWWFile::nc_get_var_int_by_name(const string &name, int array[]) const{
77  int ncstatus;
78  int varid;
79  if(((ncstatus = nc_inq_varid(this->netcdf_id,
80                               name.c_str(), &varid)) != NC_NOERR) ||
81     ((ncstatus = nc_get_var_int(this->netcdf_id,
82                                 varid, array)) != NC_NOERR))
83    throw nc_strerror(ncstatus);
84}
85
86size_t SWWFile::nc_inq_dimlen_by_name(const string &name) const{
87  int ncstatus;
88  int dimid;
89  size_t len;
90  if(((ncstatus = nc_inq_dimid(this->netcdf_id,
91                               name.c_str(), &dimid)) != NC_NOERR) ||
92     ((ncstatus = nc_inq_dimlen(this->netcdf_id,
93                                dimid, &len)) != NC_NOERR))
94    throw nc_strerror(ncstatus);
95  return len;
96}
Note: See TracBrowser for help on using the repository browser.