00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00020
00022
00031 #include "arts.h"
00032 #include "nc_io.h"
00033 #include "nc_io_types.h"
00034 #include "nc_io_instantiation.h"
00035 #include "file.h"
00036 #include "messages.h"
00037 #include "exceptions.h"
00038
00039
00041
00043
00045
00051 void
00052 filename_nc ( String& filename,
00053 const String& varname )
00054 {
00055 if ("" == filename)
00056 {
00057 extern const String out_basename;
00058 filename = out_basename + "." + varname + ".nc";
00059 }
00060 }
00061
00062
00063
00065
00072 void
00073 filename_nc_with_index (
00074 String& filename,
00075 const Index& file_index,
00076 const String& varname )
00077 {
00078 Index dummy = file_index;
00079 dummy += 1;
00080
00081 if ("" == filename)
00082 {
00083 extern const String out_basename;
00084 ostringstream os;
00085 os << out_basename << "." << varname << "." << file_index << ".nc";
00086 filename = os.str();
00087 }
00088 else
00089 {
00090 ostringstream os;
00091 os << filename << "." << file_index << ".nc";
00092 filename = os.str();
00093 }
00094 }
00095
00096
00097 template<typename T> void
00098 nc_read_from_file (const String& filename,
00099 T& type)
00100 {
00101 out2 << " Reading " << filename << '\n';
00102
00103 int ncid;
00104 if (nc_open (filename.c_str(), NC_NOWRITE, &ncid))
00105 {
00106 ostringstream os;
00107 os << "Error reading file: " << filename << endl;
00108 throw runtime_error (os.str());
00109 }
00110
00111 nc_read_from_file (ncid, type);
00112
00113 nc_close (ncid);
00114 }
00115
00116
00117 template<typename T> void
00118 nc_write_to_file (const String& filename,
00119 const T& type)
00120 {
00121 out2 << " Writing " << filename << '\n';
00122
00123 int ncid;
00124 if (nc_create (filename.c_str(), NC_CLOBBER | NC_FORMAT_NETCDF4, &ncid))
00125 {
00126 ostringstream os;
00127 os << "Error writing file: " << filename << endl;
00128 throw runtime_error (os.str());
00129 }
00130
00131 nc_write_to_file (ncid, type);
00132
00133 nc_close (ncid);
00134 }
00135
00136
00137 void nc_get_data_int (const int ncid, const String &name, int *data)
00138 {
00139 int retval, varid;
00140 if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
00141 ncerror (retval, "nc_inq_varid("+name+")");
00142 if ((retval = nc_get_var_int (ncid, varid, data)))
00143 ncerror (retval, "nc_get_var("+name+")");
00144 }
00145
00146
00147 void nc_get_data_long (const int ncid, const String &name, long *data)
00148 {
00149 int retval, varid;
00150 if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
00151 ncerror (retval, "nc_inq_varid("+name+")");
00152 if ((retval = nc_get_var_long (ncid, varid, data)))
00153 ncerror (retval, "nc_get_var("+name+")");
00154 }
00155
00156
00157 void nc_get_data_double (const int ncid, const String &name, Numeric *data)
00158 {
00159 int retval, varid;
00160 if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
00161 ncerror (retval, "nc_inq_varid("+name+")");
00162 if ((retval = nc_get_var_double (ncid, varid, data)))
00163 ncerror (retval, "nc_get_var("+name+")");
00164 }
00165
00166
00167 void nc_get_dataa_double (const int ncid, const String &name,
00168 size_t start, size_t count, Numeric *data)
00169 {
00170 int retval, varid;
00171 if ((retval = nc_inq_varid (ncid, name.c_str(), &varid)))
00172 ncerror (retval, "nc_inq_varid("+name+")");
00173 if ((retval = nc_get_vara_double (ncid, varid, &start, &count, data)))
00174 ncerror (retval, "nc_get_var("+name+")");
00175 }
00176
00177
00178 Index nc_get_dim (const int ncid, const String &name)
00179 {
00180 int retval, dimid;
00181 size_t ndim;
00182 if ((retval = nc_inq_dimid (ncid, name.c_str(), &dimid)))
00183 ncerror (retval, "nc_inq_ndims("+name+")");
00184 if ((retval = nc_inq_dimlen (ncid, dimid, &ndim)))
00185 ncerror (retval, "nc_inq_dimlen("+name+")");
00186
00187 return (Index)ndim;
00188 }
00189
00190
00191 void ncerror (const int e, const String s)
00192 {
00193 ostringstream os;
00194 os << "NetCDF error: " << s << ", " << e;
00195 throw runtime_error (os.str());
00196 }
00197