103#include <gsl/gsl_math.h>
104#include <gsl/gsl_blas.h>
105#include <gsl/gsl_linalg.h>
106#include <gsl/gsl_randist.h>
107#include <gsl/gsl_rng.h>
108#include <gsl/gsl_statistics.h>
121#define ALLOC(ptr, type, n) \
122 if((ptr=malloc((size_t)(n)*sizeof(type)))==NULL) \
123 ERRMSG("Out of memory!");
126#define BRIGHT(rad, nu) \
127 (C2 * (nu) / gsl_log1p(C1 * POW3(nu) / (rad)))
130#define DEG2RAD(deg) \
131 ((deg) * (M_PI / 180.0))
134#define DIST(a, b) sqrt(DIST2(a, b))
138 ((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])+(a[2]-b[2])*(a[2]-b[2]))
141#define DOTP(a, b) (a[0]*b[0]+a[1]*b[1]+a[2]*b[2])
144#define FREAD(ptr, type, size, out) { \
145 if(fread(ptr, sizeof(type), size, out)!=size) \
146 ERRMSG("Error while reading!"); \
150#define FWRITE(ptr, type, size, out) { \
151 if(fwrite(ptr, sizeof(type), size, out)!=size) \
152 ERRMSG("Error while writing!"); \
164#define LIN(x0, y0, x1, y1, x) \
165 ((y0)+((y1)-(y0))/((x1)-(x0))*((x)-(x0)))
168#define LOGX(x0, y0, x1, y1, x) \
169 (((x)/(x0)>0 && (x1)/(x0)>0) \
170 ? ((y0)+((y1)-(y0))*log((x)/(x0))/log((x1)/(x0))) \
171 : LIN(x0, y0, x1, y1, x))
174#define LOGY(x0, y0, x1, y1, x) \
176 ? ((y0)*exp(log((y1)/(y0))/((x1)-(x0))*((x)-(x0)))) \
177 : LIN(x0, y0, x1, y1, x))
180#define NORM(a) sqrt(DOTP(a, a))
183#define PLANCK(T, nu) \
184 (C1 * POW3(nu) / gsl_expm1(C2 * (nu) / (T)))
187#define POW2(x) ((x)*(x))
190#define POW3(x) ((x)*(x)*(x))
193#define RAD2DEG(rad) \
194 ((rad) * (180.0 / M_PI))
197#define REFRAC(p, T) \
198 (7.753e-05 * (p) / (T))
201#define TIMER(name, mode) \
202 {timer(name, __FILE__, __func__, __LINE__, mode);}
205#define TOK(line, tok, format, var) { \
206 if(((tok)=strtok((line), " \t"))) { \
207 if(sscanf(tok, format, &(var))!=1) continue; \
208 } else ERRMSG("Error while reading!"); \
221#define LOG(level, ...) { \
224 if(level <= LOGLEV) { \
225 printf(__VA_ARGS__); \
232 printf("\nWarning (%s, %s, l%d): ", __FILE__, __func__, __LINE__); \
233 LOG(0, __VA_ARGS__); \
237#define ERRMSG(...) { \
238 printf("\nError (%s, %s, l%d): ", __FILE__, __func__, __LINE__); \
239 LOG(0, __VA_ARGS__); \
240 exit(EXIT_FAILURE); \
244#define PRINT(format, var) \
245 printf("Print (%s, %s, l%d): %s= "format"\n", \
246 __FILE__, __func__, __LINE__, #var, var);
254#define C1 1.19104259e-8
284#define KB 1.3806504e-23
294#define NA 6.02214199e23
393#define N ((2+NG+NW)*NP+NCL+NSF+5)
398#define NQ (7+NG+NW+NCL+NSF)
438#define RFMNPTS 10000000
443#define RFMLINE 100000
457#define IDXQ(ig) (2+ig)
460#define IDXK(iw) (2+ctl->ng+iw)
463#define IDXCLZ (2+ctl->ng+ctl->nw)
466#define IDXCLDZ (3+ctl->ng+ctl->nw)
469#define IDXCLK(icl) (4+ctl->ng+ctl->nw+icl)
472#define IDXSFZ (4+ctl->ng+ctl->nw+ctl->ncl)
475#define IDXSFP (5+ctl->ng+ctl->nw+ctl->ncl)
478#define IDXSFT (6+ctl->ng+ctl->nw+ctl->ncl)
481#define IDXSFEPS(isf) (7+ctl->ng+ctl->nw+ctl->ncl+isf)
625 double retq_zmin[
NG];
628 double retq_zmax[
NG];
631 double retk_zmin[
NW];
634 double retk_zmax[
NW];
874 const atm_t * atm_src,
881 const obs_t * obs_src,
887 const char *emitter);
966 double tau_path[
ND][
NG],
975 double tau_path[
ND][
NG],
1050 const char *dirname,
1051 const char *filename,
1063 const char *dirname,
1064 const char *filename,
1065 gsl_matrix * matrix);
1069 const char *dirname,
1070 const char *filename,
1076 const char *basename,
1084 const char *filename,
1091 const char *filename,
1105 const char *varname,
1107 const char *defvalue,
1131 const double remain,
1144 const char *dirname,
1145 const char *filename,
1151 const char *filename,
1157 const char *dirname,
1158 const char *filename,
1160 const gsl_matrix * matrix,
1163 const char *rowspace,
1164 const char *colspace,
1169 const char *dirname,
1170 const char *filename,
1176 const char *filename,
1189 const gsl_vector * x,
1195 const gsl_vector * x,
1201 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 read_tbl(const ctl_t *ctl, tbl_t *tbl)
Read look-up table data.
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.
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 formod_pencil(const ctl_t *ctl, const atm_t *atm, obs_t *obs, const int ir)
Compute radiative transfer for a pencil beam.
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 formod(const ctl_t *ctl, atm_t *atm, obs_t *obs)
Determine ray paths and compute radiative transfer.
void climatology(const ctl_t *ctl, atm_t *atm_mean)
Interpolate climatological data.
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 init_srcfunc(const ctl_t *ctl, tbl_t *tbl)
Initialize source function table.
void write_shape(const char *filename, const double *x, const double *y, const int n)
Write shape function.
#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.
double scan_ctl(int argc, char *argv[], const char *varname, int arridx, const char *defvalue, char *value)
Search control parameter file for variable entry.
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.
void kernel(ctl_t *ctl, atm_t *atm, obs_t *obs, gsl_matrix *k)
Compute Jacobians.
#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].
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).
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 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 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 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.