ARTS  2.0.49
nc_io.cc
Go to the documentation of this file.
1 /* Copyright (C) 2008 Oliver Lemke <olemke@core-dump.info>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA. */
17 
18 
20 // File description
22 
31 #include "arts.h"
32 
33 #ifdef ENABLE_NETCDF
34 
35 #include "nc_io.h"
36 #include "nc_io_types.h"
37 #include "file.h"
38 #include "messages.h"
39 #include "exceptions.h"
40 
41 
43 // Default file name
45 
47 
53 void
54 filename_nc ( String& filename,
55  const String& varname )
56 {
57  if ("" == filename)
58  {
59  extern const String out_basename;
60  filename = out_basename + "." + varname + ".nc";
61  }
62 }
63 
64 
65 
67 
74 void
75 filename_nc_with_index (
76  String& filename,
77  const Index& file_index,
78  const String& varname )
79 {
80  if ("" == filename)
81  {
82  extern const String out_basename;
83  ostringstream os;
84  os << out_basename << "." << varname << "." << file_index << ".nc";
85  filename = os.str();
86  }
87  else
88  {
89  ostringstream os;
90  os << filename << "." << file_index << ".nc";
91  filename = os.str();
92  }
93 }
94 
95 
96 template<typename T> void
97 nc_read_from_file (const String& filename,
98  T& type,
99  const Verbosity& verbosity)
100 {
102 
103  String efilename = expand_path(filename);
104 
105  out2 << " Reading " << efilename << '\n';
106 
107  int ncid;
108  if (nc_open (efilename.c_str(), NC_NOWRITE, &ncid))
109  {
110  ostringstream os;
111  os << "Error reading file: " << efilename << endl;
112  throw runtime_error (os.str());
113  }
114 
115  nc_read_from_file (ncid, type);
116 
117  nc_close (ncid);
118 }
119 
120 
121 template<typename T> void
122 nc_write_to_file (const String& filename,
123  const T& type,
124  const Verbosity& verbosity)
125 {
127 
128  String efilename = expand_path(filename);
129 
130  out2 << " Writing " << efilename << '\n';
131 
132  int ncid;
133  if (nc_create (efilename.c_str(), NC_CLOBBER, &ncid))
134  {
135  ostringstream os;
136  os << "Error writing file: " << efilename << endl;
137  throw runtime_error (os.str());
138  }
139 
140  nc_write_to_file (ncid, type);
141 
142  nc_close (ncid);
143 }
144 
145 
146 void nc_get_data_int (const int ncid, const String &name, int *data)
147 {
148  int retval, varid;
149  if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
150  ncerror (retval, "nc_inq_varid("+name+")");
151  if ((retval = nc_get_var_int (ncid, varid, data)))
152  ncerror (retval, "nc_get_var("+name+")");
153 }
154 
155 
156 void nc_get_data_long (const int ncid, const String &name, long *data)
157 {
158  int retval, varid;
159  if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
160  ncerror (retval, "nc_inq_varid("+name+")");
161  if ((retval = nc_get_var_long (ncid, varid, data)))
162  ncerror (retval, "nc_get_var("+name+")");
163 }
164 
165 
166 void nc_get_data_double (const int ncid, const String &name, Numeric *data)
167 {
168  int retval, varid;
169  if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
170  ncerror (retval, "nc_inq_varid("+name+")");
171  if ((retval = nc_get_var_double (ncid, varid, data)))
172  ncerror (retval, "nc_get_var("+name+")");
173 }
174 
175 
176 void nc_get_dataa_double (const int ncid, const String &name,
177  size_t start, size_t count, Numeric *data)
178 {
179  int retval, varid;
180  if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
181  ncerror (retval, "nc_inq_varid("+name+")");
182  if ((retval = nc_get_vara_double (ncid, varid, &start, &count, data)))
183  ncerror (retval, "nc_get_var("+name+")");
184 }
185 
186 
187 Index nc_get_dim (const int ncid, const String &name)
188 {
189  int retval, dimid;
190  size_t ndim;
191  if ((retval = nc_inq_dimid (ncid, name.c_str(), &dimid)))
192  ncerror (retval, "nc_inq_ndims("+name+")");
193  if ((retval = nc_inq_dimlen (ncid, dimid, &ndim)))
194  ncerror (retval, "nc_inq_dimlen("+name+")");
195 
196  return (Index)ndim;
197 }
198 
199 
200 void ncerror (const int e, const String s)
201 {
202  ostringstream os;
203  os << "NetCDF error: " << s << ", " << e;
204  throw runtime_error (os.str());
205 }
206 
207 
208 // We can't do the instantiation at the beginning of this file, because the
209 // implementation of nc_write_to_file and nc_read_from_file have to be known.
210 
211 #include "nc_io_instantiation.h"
212 
213 #endif /* ENABLE_NETCDF */
214 
exceptions.h
The declarations of all the exception classes.
nc_io.h
This file contains basic functions to handle NetCDF data files.
CREATE_OUT2
#define CREATE_OUT2
Definition: messages.h:207
nc_io_types.h
This file contains private function declarations and template instantiation to handle NetCDF data fil...
messages.h
Declarations having to do with the four output streams.
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:62
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Verbosity
Definition: messages.h:50
out_basename
String out_basename
The basename for the report file and for all other output files.
Definition: messages.cc:42
expand_path
String expand_path(const String &path)
Definition: file.cc:274
nc_io_instantiation.h
file.h
This file contains basic functions to handle ASCII files.
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
arts.h
The global header file for ARTS.