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

Last change on this file since 5493 was 5493, checked in by jack, 16 years ago

Add required boost m4 macro.

File size: 2.7 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
40void SWWFile::compute_extents(void){
41  this->minX = this->x[0];
42  this->maxX = this->x[0];
43  this->minY = this->y[0];
44  this->maxY = this->y[0];
45  for(int i = 1 ; i < this->number_of_points ; ++i){
46    if(this->x[i] < this->minX) this->minX = this->x[i];
47    if(this->x[i] > this->maxX) this->maxX = this->x[i];
48    if(this->y[i] < this->minY) this->minY = this->y[i];
49    if(this->y[i] > this->maxY) this->maxY = this->y[i];
50  }
51}
52
53void SWWFile::nc_get_var_float_by_name(const string &name, float array[]){
54  int ncstatus;
55  int varid;
56  if(((ncstatus = nc_inq_varid(this->netcdf_id,
57                               name.c_str(), &varid)) != NC_NOERR) ||
58     ((ncstatus = nc_get_var_float(this->netcdf_id,
59                                   varid, array)) != NC_NOERR))
60    throw nc_strerror(ncstatus);
61}
62
63void SWWFile::nc_get_var_int_by_name(const string &name, int array[]){
64  int ncstatus;
65  int varid;
66  if(((ncstatus = nc_inq_varid(this->netcdf_id,
67                               name.c_str(), &varid)) != NC_NOERR) ||
68     ((ncstatus = nc_get_var_int(this->netcdf_id,
69                                 varid, array)) != NC_NOERR))
70    throw nc_strerror(ncstatus);
71}
72
73size_t SWWFile::nc_inq_dimlen_by_name(const string &name){
74  int ncstatus;
75  int dimid;
76  size_t len;
77  if(((ncstatus = nc_inq_dimid(this->netcdf_id,
78                               name.c_str(), &dimid)) != NC_NOERR) ||
79     ((ncstatus = nc_inq_dimlen(this->netcdf_id,
80                                dimid, &len)) != NC_NOERR))
81    throw nc_strerror(ncstatus);
82  return len;
83}
Note: See TracBrowser for help on using the repository browser.