Canoe
Comprehensive Atmosphere N' Ocean Engine
correlatedk_absorber.cpp
Go to the documentation of this file.
1 // C/C++
2 #include <algorithm>
3 #include <cstring>
4 #include <iostream>
5 #include <sstream>
6 #include <stdexcept>
7 #include <string>
8 
9 // canoe
10 #include <air_parcel.hpp>
11 #include <configure.hpp>
12 #include <constants.hpp>
13 
14 // netcdf
15 #ifdef NETCDFOUTPUT
16 extern "C" {
17 #include <netcdf.h>
18 }
19 #endif
20 
21 // climath
22 #include <climath/interpolation.h>
23 
24 // opacity
25 #include "correlatedk_absorber.hpp"
26 
27 void CorrelatedKAbsorber::LoadCoefficient(std::string fname, size_t bid) {
28 #ifdef NETCDFOUTPUT
29  int fileid, dimid, varid, err;
30  len_[0] = 22; // number of pressure
31  len_[1] = 27; // number of temperature
32  // len_[2] = 26; // number of bands
33  len_[2] = 8; // number of guass points
34 
35  nc_open(fname.c_str(), NC_NETCDF4, &fileid);
36 
37  axis_.resize(len_[0] + len_[1] + len_[2]);
38 
39  nc_inq_varid(fileid, "p", &varid);
40  nc_get_var_double(fileid, varid, axis_.data());
41  nc_inq_varid(fileid, "t", &varid);
42  nc_get_var_double(fileid, varid, axis_.data() + len_[0]);
43  nc_inq_varid(fileid, "samples", &varid);
44  nc_get_var_double(fileid, varid, axis_.data() + len_[0] + len_[1]);
45 
46  kcoeff_.resize(len_[0] * len_[1] * len_[2]);
47  nc_inq_varid(fileid, GetName().c_str(), &varid);
48  size_t start[4] = {0, 0, bid, 0};
49  size_t count[4] = {len_[0], len_[1], 1, len_[2]};
50  nc_get_vara_double(fileid, varid, start, count, kcoeff_.data());
51  nc_close(fileid);
52 #endif
53 }
54 
56  AirParcel const& var) const {
57  // first axis is wavenumber, second is pressure, third is temperature anomaly
58  Real val, coord[3] = {log(var.q[IPR]), var.q[IDN], g1};
59  interpn(&val, coord, kcoeff_.data(), axis_.data(), len_, 3, 1);
60  Real dens = var.q[IPR] / (Constants::kBoltz * var.q[IDN]);
61  return exp(val) * dens; // ln(m*2/kmol) -> 1/m
62 }
Real *const q
chemistry data
Definition: air_parcel.hpp:42
std::vector< Real > axis_
void LoadCoefficient(std::string fname, size_t bid) override
Load absorption coefficient from file.
Real GetAttenuation(Real g1, Real g2, AirParcel const &var) const override
Get attenuation coefficient [1/m].
std::vector< Real > kcoeff_
std::string GetName() const
void interpn(double *val, double const *coor, double const *data, double const *axis, size_t const *len, int ndim, int nval)
Definition: interpn.c:12
double const kBoltz
Definition: constants.hpp:7