104#include <gsl/gsl_math.h>
105#include <gsl/gsl_blas.h>
106#include <gsl/gsl_linalg.h>
107#include <gsl/gsl_randist.h>
108#include <gsl/gsl_rng.h>
109#include <gsl/gsl_statistics.h>
122#define ALLOC(ptr, type, n) \
123 if((ptr=malloc((size_t)(n)*sizeof(type)))==NULL) \
124 ERRMSG("Out of memory!");
127#define BRIGHT(rad, nu) \
128 (C2 * (nu) / gsl_log1p(C1 * POW3(nu) / (rad)))
131#define DEG2RAD(deg) \
132 ((deg) * (M_PI / 180.0))
135#define DIST(a, b) sqrt(DIST2(a, b))
139 ((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])+(a[2]-b[2])*(a[2]-b[2]))
142#define DOTP(a, b) (a[0]*b[0]+a[1]*b[1]+a[2]*b[2])
145#define FREAD(ptr, type, size, out) { \
146 if(fread(ptr, sizeof(type), size, out)!=size) \
147 ERRMSG("Error while reading!"); \
151#define FWRITE(ptr, type, size, out) { \
152 if(fwrite(ptr, sizeof(type), size, out)!=size) \
153 ERRMSG("Error while writing!"); \
165#define LIN(x0, y0, x1, y1, x) \
166 ((y0)+((y1)-(y0))/((x1)-(x0))*((x)-(x0)))
169#define LOGX(x0, y0, x1, y1, x) \
170 (((x)/(x0)>0 && (x1)/(x0)>0) \
171 ? ((y0)+((y1)-(y0))*log((x)/(x0))/log((x1)/(x0))) \
172 : LIN(x0, y0, x1, y1, x))
175#define LOGY(x0, y0, x1, y1, x) \
177 ? ((y0)*exp(log((y1)/(y0))/((x1)-(x0))*((x)-(x0)))) \
178 : LIN(x0, y0, x1, y1, x))
181#define NORM(a) sqrt(DOTP(a, a))
184#define PLANCK(T, nu) \
185 (C1 * POW3(nu) / gsl_expm1(C2 * (nu) / (T)))
188#define POW2(x) ((x)*(x))
191#define POW3(x) ((x)*(x)*(x))
194#define RAD2DEG(rad) \
195 ((rad) * (180.0 / M_PI))
198#define REFRAC(p, T) \
199 (7.753e-05 * (p) / (T))
202#define TIMER(name, mode) \
203 {timer(name, __FILE__, __func__, __LINE__, mode);}
206#define TOK(line, tok, format, var) { \
207 if(((tok)=strtok((line), " \t"))) { \
208 if(sscanf(tok, format, &(var))!=1) continue; \
209 } else ERRMSG("Error while reading!"); \
222#define LOG(level, ...) { \
225 if(level <= LOGLEV) { \
226 printf(__VA_ARGS__); \
233 printf("\nWarning (%s, %s, l%d): ", __FILE__, __func__, __LINE__); \
234 LOG(0, __VA_ARGS__); \
238#define ERRMSG(...) { \
239 printf("\nError (%s, %s, l%d): ", __FILE__, __func__, __LINE__); \
240 LOG(0, __VA_ARGS__); \
241 exit(EXIT_FAILURE); \
245#define PRINT(format, var) \
246 printf("Print (%s, %s, l%d): %s= "format"\n", \
247 __FILE__, __func__, __LINE__, #var, var);
255#define C1 1.19104259e-8
285#define KB 1.3806504e-23
295#define NA 6.02214199e23
404#define N ((2+NG+NW)*NP+NCL+NSF+5)
409#define NQ (7+NG+NW+NCL+NSF)
449#define RFMNPTS 10000000
463#define IDXQ(ig) (2+ig)
466#define IDXK(iw) (2+ctl->ng+iw)
469#define IDXCLZ (2+ctl->ng+ctl->nw)
472#define IDXCLDZ (3+ctl->ng+ctl->nw)
475#define IDXCLK(icl) (4+ctl->ng+ctl->nw+icl)
478#define IDXSFZ (4+ctl->ng+ctl->nw+ctl->ncl)
481#define IDXSFP (5+ctl->ng+ctl->nw+ctl->ncl)
484#define IDXSFT (6+ctl->ng+ctl->nw+ctl->ncl)
487#define IDXSFEPS(isf) (7+ctl->ng+ctl->nw+ctl->ncl+isf)
652 double retq_zmin[
NG];
655 double retq_zmax[
NG];
658 double retk_zmin[
NW];
661 double retk_zmax[
NW];
901 const atm_t * atm_src,
908 const obs_t * obs_src,
914 const char *emitter);
995 double tau_path[
ND][
NG],
1004 double tau_path[
ND][
NG],
1005 double tau_seg[
ND]);
1080 const char *dirname,
1081 const char *filename,
1093 const char *dirname,
1094 const char *filename,
1095 gsl_matrix * matrix);
1099 const char *dirname,
1100 const char *filename,
1106 const char *basename,
1114 const char *filename,
1121 const char *filename,
1134 const char *varname,
1136 const char *defvalue,
1160 const double remain,
1173 const char *dirname,
1174 const char *filename,
1180 const char *filename,
1186 const char *dirname,
1187 const char *filename,
1189 const gsl_matrix * matrix,
1192 const char *rowspace,
1193 const char *colspace,
1198 const char *dirname,
1199 const char *filename,
1205 const char *filename,
1218 const gsl_vector * x,
1224 const gsl_vector * x,
1230 const gsl_vector * y,
#define LEN
Maximum length of ASCII data lines.
void read_matrix(const char *dirname, const char *filename, gsl_matrix *matrix)
Read matrix.
void timer(const char *name, const char *file, const char *func, int line, int mode)
Measure wall-clock time.
void read_rfm_spec(const char *filename, double *nu, double *rad, int *npts)
Read RFM spectrum.
void write_atm(const char *dirname, const char *filename, const ctl_t *ctl, const atm_t *atm)
Write atmospheric data.
int locate_reg(const double *xx, const int n, const double x)
Find array index for regular grid.
void intpol_tbl_ega(const ctl_t *ctl, const tbl_t *tbl, const los_t *los, const int ip, double tau_path[ND][NG], double tau_seg[ND])
Get transmittance from look-up tables (EGA method).
double read_obs_rfm(const char *basename, const double z, double *nu, double *f, int n)
Read observation data in RFM format.
void formod_rfm(const ctl_t *ctl, const atm_t *atm, obs_t *obs)
Apply RFM for radiative transfer calculations.
double ctmo2(const double nu, const double p, const double t)
Compute oxygen continuum (absorption coefficient).
void idx2name(const ctl_t *ctl, const int idx, char *quantity)
Determine name of state vector quantity for given index.
void read_ctl(int argc, char *argv[], ctl_t *ctl)
Read forward model control parameters.
void formod_continua(const ctl_t *ctl, const los_t *los, const int ip, double *beta)
Compute absorption coefficient of continua.
void raytrace(const ctl_t *ctl, const atm_t *atm, obs_t *obs, los_t *los, const int ir)
Do ray-tracing to determine LOS.
int locate_irr(const double *xx, const int n, const double x)
Find array index for irregular grid.
void time2jsec(const int year, const int mon, const int day, const int hour, const int min, const int sec, const double remain, double *jsec)
Convert date to seconds.
void x2atm(const ctl_t *ctl, const gsl_vector *x, atm_t *atm)
Decompose parameter vector or state vector.
void atm2x_help(const double value, const int value_iqa, const int value_ip, gsl_vector *x, int *iqa, int *ipa, size_t *n)
Add element to state vector.
double ctmh2o(const double nu, const double p, const double t, const double q, const double u)
Compute water vapor continuum (optical depth).
void intpol_tbl_cga(const ctl_t *ctl, const tbl_t *tbl, const los_t *los, const int ip, double tau_path[ND][NG], double tau_seg[ND])
Get transmittance from look-up tables (CGA method).
void write_atm_rfm(const char *filename, const ctl_t *ctl, const atm_t *atm)
Write atmospheric data in RFM format.
#define ND
Maximum number of radiance channels.
double intpol_tbl_u(const tbl_t *tbl, const int ig, const int id, const int ip, const int it, const double eps)
Interpolate column density from look-up tables.
double intpol_tbl_eps(const tbl_t *tbl, const int ig, const int id, const int ip, const int it, const double u)
Interpolate emissivity from look-up tables.
#define NSHAPE
Maximum number of shape function grid points.
void write_tbl(const ctl_t *ctl, const tbl_t *tbl)
Write look-up table data.
void intpol_atm(const ctl_t *ctl, const atm_t *atm, const double z, double *p, double *t, double *q, double *k)
Interpolate atmospheric data.
int find_emitter(const ctl_t *ctl, const char *emitter)
Find index of an emitter.
void tangent_point(const los_t *los, double *tpz, double *tplon, double *tplat)
Find tangent point of a given LOS.
#define TBLNU
Maximum number of column densities in emissivity tables.
void climatology(const ctl_t *ctl, atm_t *atm_mean)
Interpolate climatological data.
void formod_pencil(const ctl_t *ctl, const tbl_t *tbl, const atm_t *atm, obs_t *obs, const int ir)
Compute radiative transfer for a pencil beam.
double ctmco2(const double nu, const double p, const double t, const double u)
Compute carbon dioxide continuum (optical depth).
void read_atm(const char *dirname, const char *filename, const ctl_t *ctl, atm_t *atm)
Read atmospheric data.
void copy_obs(const ctl_t *ctl, obs_t *obs_dest, const obs_t *obs_src, const int init)
Copy and initialize observation data.
void read_obs(const char *dirname, const char *filename, const ctl_t *ctl, obs_t *obs)
Read observation data.
void write_obs(const char *dirname, const char *filename, const ctl_t *ctl, const obs_t *obs)
Write observation data.
#define TBLNT
Maximum number of temperatures in emissivity tables.
void x2atm_help(double *value, const gsl_vector *x, size_t *n)
Get element from state vector.
void cart2geo(const double *x, double *z, double *lon, double *lat)
Convert Cartesian coordinates to geolocation.
#define NP
Maximum number of atmospheric data points.
void formod_fov(const ctl_t *ctl, obs_t *obs)
Apply field of view convolution.
void kernel(const ctl_t *ctl, const tbl_t *tbl, atm_t *atm, obs_t *obs, gsl_matrix *k)
Compute Jacobians.
void init_srcfunc(const ctl_t *ctl, tbl_t *tbl)
Initialize source function table.
double scan_ctl(int argc, char *argv[], const char *varname, const int arridx, const char *defvalue, char *value)
Search control parameter file for variable entry.
void write_shape(const char *filename, const double *x, const double *y, const int n)
Write shape function.
void formod(const ctl_t *ctl, const tbl_t *tbl, atm_t *atm, obs_t *obs)
Determine ray paths and compute radiative transfer.
#define NG
Maximum number of emitters.
void formod_srcfunc(const ctl_t *ctl, const tbl_t *tbl, const double t, double *src)
Compute Planck source function.
void jsec2time(const double jsec, int *year, int *mon, int *day, int *hour, int *min, int *sec, double *remain)
Convert seconds to date.
#define TBLNS
Maximum number of source function temperature levels.
tbl_t * read_tbl(const ctl_t *ctl)
Read look-up table data.
double sza(double sec, double lon, double lat)
Calculate solar zenith angle.
void copy_atm(const ctl_t *ctl, atm_t *atm_dest, const atm_t *atm_src, const int init)
Copy and initialize atmospheric data.
size_t obs2y(const ctl_t *ctl, const obs_t *obs, gsl_vector *y, int *ida, int *ira)
Compose measurement vector.
void y2obs(const ctl_t *ctl, const gsl_vector *y, obs_t *obs)
Decompose measurement vector.
void hydrostatic(const ctl_t *ctl, atm_t *atm)
Set hydrostatic equilibrium.
void read_shape(const char *filename, double *x, double *y, int *n)
Read shape function.
double ctmn2(const double nu, const double p, const double t)
Compute nitrogen continuum (absorption coefficient).
size_t atm2x(const ctl_t *ctl, const atm_t *atm, gsl_vector *x, int *iqa, int *ipa)
Compose state vector or parameter vector.
#define TBLNP
Maximum number of pressure levels in emissivity tables.
void geo2cart(const double z, const double lon, const double lat, double *x)
Convert geolocation to Cartesian coordinates.
#define NSF
Maximum number of surface layer spectral grid points.
void write_matrix(const char *dirname, const char *filename, const ctl_t *ctl, const gsl_matrix *matrix, const atm_t *atm, const obs_t *obs, const char *rowspace, const char *colspace, const char *sort)
Write matrix.
#define NCL
Maximum number of cloud layer spectral grid points.
#define NLOS
Maximum number of LOS points.
#define NR
Maximum number of ray paths.
int locate_tbl(const float *xx, const int n, const double x)
Find array index in float array.
#define NW
Maximum number of spectral windows.
double sfz
Surface height [km].
double sfp
Surface pressure [hPa].
double clz
Cloud layer height [km].
int np
Number of data points.
double cldz
Cloud layer depth [km].
double sft
Surface temperature [K].
Forward model control parameters.
int write_matrix
Write matrix file (0=no, 1=yes).
int nw
Number of spectral windows.
double retp_zmin
Minimum altitude for pressure retrieval [km].
int ig_co2
Emitter index of CO2.
double hydz
Reference height for hydrostatic pressure profile (-999 to skip) [km].
int ret_sfz
Retrieve surface layer height (0=no, 1=yes).
int ctm_co2
Compute CO2 continuum (0=no, 1=yes).
double rett_zmax
Maximum altitude for temperature retrieval [km].
int ret_sfeps
Retrieve surface layer emissivity (0=no, 1=yes).
int ret_sft
Retrieve surface layer temperature (0=no, 1=yes).
int ret_clz
Retrieve cloud layer height (0=no, 1=yes).
int ctm_n2
Compute N2 continuum (0=no, 1=yes).
int ctm_h2o
Compute H2O continuum (0=no, 1=yes).
int formod
Forward model (0=CGA, 1=EGA, 2=RFM).
int ng
Number of emitters.
int refrac
Take into account refractivity (0=no, 1=yes).
int ig_o2
Emitter index of O2.
double rett_zmin
Minimum altitude for temperature retrieval [km].
double sfsza
Solar zenith angle at the surface [deg] (-999=auto).
int nd
Number of radiance channels.
int fov_n
Field-of-view number of data points.
int sftype
Surface treatment (0=none, 1=emissions, 2=downward, 3=solar).
int ret_sfp
Retrieve surface layer pressure (0=no, 1=yes).
int ncl
Number of cloud layer spectral grid points.
int ig_n2
Emitter index of N2.
int ctm_o2
Compute O2 continuum (0=no, 1=yes).
int ret_clk
Retrieve cloud layer extinction (0=no, 1=yes).
int nsf
Number of surface layer spectral grid points.
double rayds
Maximum step length for raytracing [km].
int ret_cldz
Retrieve cloud layer depth (0=no, 1=yes).
int ig_h2o
Emitter index of H2O.
int tblfmt
Look-up table file format (1=ASCII, 2=binary).
double raydz
Vertical step length for raytracing [km].
int write_bbt
Use brightness temperature instead of radiance (0=no, 1=yes).
double retp_zmax
Maximum altitude for pressure retrieval [km].
double sft
Surface temperature [K].
int np
Number of LOS points.
Observation geometry and radiance data.
int nr
Number of ray paths.
Emissivity look-up tables.