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 DIST(a, b) sqrt(DIST2(a, b))
130 ((a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1])+(a[2]-b[2])*(a[2]-b[2]))
133#define DOTP(a, b) (a[0]*b[0]+a[1]*b[1]+a[2]*b[2])
136#define FREAD(ptr, type, size, out) { \
137 if(fread(ptr, sizeof(type), size, out)!=size) \
138 ERRMSG("Error while reading!"); \
142#define FWRITE(ptr, type, size, out) { \
143 if(fwrite(ptr, sizeof(type), size, out)!=size) \
144 ERRMSG("Error while writing!"); \
156#define LIN(x0, y0, x1, y1, x) \
157 ((y0)+((y1)-(y0))/((x1)-(x0))*((x)-(x0)))
160#define LOGX(x0, y0, x1, y1, x) \
161 (((x)/(x0)>0 && (x1)/(x0)>0) \
162 ? ((y0)+((y1)-(y0))*log((x)/(x0))/log((x1)/(x0))) \
163 : LIN(x0, y0, x1, y1, x))
166#define LOGY(x0, y0, x1, y1, x) \
168 ? ((y0)*exp(log((y1)/(y0))/((x1)-(x0))*((x)-(x0)))) \
169 : LIN(x0, y0, x1, y1, x))
172#define NORM(a) sqrt(DOTP(a, a))
175#define POW2(x) ((x)*(x))
178#define POW3(x) ((x)*(x)*(x))
181#define TIMER(name, mode) \
182 {timer(name, __FILE__, __func__, __LINE__, mode);}
185#define TOK(line, tok, format, var) { \
186 if(((tok)=strtok((line), " \t"))) { \
187 if(sscanf(tok, format, &(var))!=1) continue; \
188 } else ERRMSG("Error while reading!"); \
201#define LOG(level, ...) { \
204 if(level <= LOGLEV) { \
205 printf(__VA_ARGS__); \
212 printf("\nWarning (%s, %s, l%d): ", __FILE__, __func__, __LINE__); \
213 LOG(0, __VA_ARGS__); \
217#define ERRMSG(...) { \
218 printf("\nError (%s, %s, l%d): ", __FILE__, __func__, __LINE__); \
219 LOG(0, __VA_ARGS__); \
220 exit(EXIT_FAILURE); \
224#define PRINT(format, var) \
225 printf("Print (%s, %s, l%d): %s= "format"\n", \
226 __FILE__, __func__, __LINE__, #var, var);
234#define C1 1.19104259e-8
264#define KB 1.3806504e-23
274#define NA 6.02214199e23
373#define N ((2+NG+NW)*NP+NCL+NSF+5)
378#define NQ (7+NG+NW+NCL+NSF)
418#define RFMNPTS 10000000
423#define RFMLINE 100000
437#define IDXQ(ig) (2+ig)
440#define IDXK(iw) (2+ctl->ng+iw)
443#define IDXCLZ (2+ctl->ng+ctl->nw)
446#define IDXCLDZ (3+ctl->ng+ctl->nw)
449#define IDXCLK(icl) (4+ctl->ng+ctl->nw+icl)
452#define IDXSFZ (4+ctl->ng+ctl->nw+ctl->ncl)
455#define IDXSFP (5+ctl->ng+ctl->nw+ctl->ncl)
458#define IDXSFT (6+ctl->ng+ctl->nw+ctl->ncl)
461#define IDXSFEPS(isf) (7+ctl->ng+ctl->nw+ctl->ncl+isf)
605 double retq_zmin[
NG];
608 double retq_zmax[
NG];
611 double retk_zmin[
NW];
614 double retk_zmax[
NW];
872 const char *emitter);
951 double tau_path[
ND][
NG],
960 double tau_path[
ND][
NG],
1040 const char *dirname,
1041 const char *filename,
1053 const char *dirname,
1054 const char *filename,
1055 gsl_matrix * matrix);
1059 const char *dirname,
1060 const char *filename,
1066 const char *basename,
1074 const char *filename,
1081 const char *filename,
1100 const char *varname,
1102 const char *defvalue,
1139 const char *dirname,
1140 const char *filename,
1146 const char *filename,
1152 const char *dirname,
1153 const char *filename,
1155 gsl_matrix * matrix,
1158 const char *rowspace,
1159 const char *colspace,
1164 const char *dirname,
1165 const char *filename,
1171 const char *filename,
#define LEN
Maximum length of ASCII data lines.
void write_shape(const char *filename, double *x, double *y, int n)
Write shape function.
double planck(double t, double nu)
Compute Planck function.
int locate_tbl(float *xx, int n, double x)
Find array index in float array.
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 x2atm(ctl_t *ctl, gsl_vector *x, atm_t *atm)
Decompose parameter vector or state vector.
int locate_irr(double *xx, int n, double x)
Find array index for irregular grid.
void read_atm(const char *dirname, const char *filename, ctl_t *ctl, atm_t *atm)
Read atmospheric data.
void init_srcfunc(ctl_t *ctl, tbl_t *tbl)
Initialize source function table.
void x2atm_help(double *value, gsl_vector *x, size_t *n)
Get element from state vector.
void read_ctl(int argc, char *argv[], ctl_t *ctl)
Read forward model control parameters.
void y2obs(ctl_t *ctl, gsl_vector *y, obs_t *obs)
Decompose measurement vector.
void raytrace(ctl_t *ctl, atm_t *atm, obs_t *obs, los_t *los, int ir)
Do ray-tracing to determine LOS.
void copy_obs(ctl_t *ctl, obs_t *obs_dest, obs_t *obs_src, int init)
Copy and initialize observation data.
void write_atm(const char *dirname, const char *filename, ctl_t *ctl, atm_t *atm)
Write atmospheric data.
double intpol_tbl_eps(tbl_t *tbl, int ig, int id, int ip, int it, double u)
Interpolate emissivity from look-up tables.
void read_tbl(ctl_t *ctl, tbl_t *tbl)
Read look-up table data.
void formod_fov(ctl_t *ctl, obs_t *obs)
Apply field of view convolution.
size_t obs2y(ctl_t *ctl, obs_t *obs, gsl_vector *y, int *ida, int *ira)
Compose measurement vector.
void time2jsec(int year, int mon, int day, int hour, int min, int sec, double remain, double *jsec)
Convert date to seconds.
void formod_pencil(ctl_t *ctl, atm_t *atm, obs_t *obs, int ir)
Compute radiative transfer for a pencil beam.
void tangent_point(los_t *los, double *tpz, double *tplon, double *tplat)
Find tangent point of a given LOS.
void write_obs(const char *dirname, const char *filename, ctl_t *ctl, obs_t *obs)
Write observation data.
void write_atm_rfm(const char *filename, ctl_t *ctl, atm_t *atm)
Write atmospheric data in RFM format.
#define ND
Maximum number of radiance channels.
void write_matrix(const char *dirname, const char *filename, ctl_t *ctl, gsl_matrix *matrix, atm_t *atm, obs_t *obs, const char *rowspace, const char *colspace, const char *sort)
Write matrix.
double refractivity(double p, double t)
Compute refractivity (return value is n - 1).
void hydrostatic(ctl_t *ctl, atm_t *atm)
Set hydrostatic equilibrium.
int locate_reg(double *xx, int n, double x)
Find array index for regular grid.
void intpol_atm(ctl_t *ctl, atm_t *atm, double z, double *p, double *t, double *q, double *k)
Interpolate atmospheric data.
#define TBLNU
Maximum number of column densities in emissivity tables.
double ctmo2(double nu, double p, double t)
Compute oxygen continuum (absorption coefficient).
void idx2name(ctl_t *ctl, int idx, char *quantity)
Determine name of state vector quantity for given index.
void intpol_tbl_ega(ctl_t *ctl, tbl_t *tbl, los_t *los, int ip, double tau_path[ND][NG], double tau_seg[ND])
Get transmittance from look-up tables (EGA method).
void formod_srcfunc(ctl_t *ctl, tbl_t *tbl, double t, double *src)
Compute Planck source function.
void jsec2time(double jsec, int *year, int *mon, int *day, int *hour, int *min, int *sec, double *remain)
Convert seconds to date.
double brightness(double rad, double nu)
Compute brightness temperature.
#define TBLNT
Maximum number of temperatures in emissivity tables.
#define NP
Maximum number of atmospheric data points.
void intpol_tbl_cga(ctl_t *ctl, tbl_t *tbl, los_t *los, int ip, double tau_path[ND][NG], double tau_seg[ND])
Get transmittance from look-up tables (CGA method).
void read_obs(const char *dirname, const char *filename, ctl_t *ctl, obs_t *obs)
Read observation data.
#define NG
Maximum number of emitters.
double ctmn2(double nu, double p, double t)
Compute nitrogen continuum (absorption coefficient).
int find_emitter(ctl_t *ctl, const char *emitter)
Find index of an emitter.
#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 read_shape(const char *filename, double *x, double *y, int *n)
Read shape function.
void geo2cart(double z, double lon, double lat, double *x)
Convert geolocation to Cartesian coordinates.
void climatology(ctl_t *ctl, atm_t *atm_mean)
Interpolate climatological data.
void formod(ctl_t *ctl, atm_t *atm, obs_t *obs)
Determine ray paths and compute radiative transfer.
double ctmh2o(double nu, double p, double t, double q, double u)
Compute water vapor continuum (optical depth).
#define TBLNP
Maximum number of pressure levels in emissivity tables.
#define NSF
Maximum number of surface layer spectral grid points.
double read_obs_rfm(const char *basename, double z, double *nu, double *f, int n)
Read observation data in RFM format.
#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.
void formod_continua(ctl_t *ctl, los_t *los, int ip, double *beta)
Compute absorption coefficient of continua.
void atm2x_help(double value, int value_iqa, int value_ip, gsl_vector *x, int *iqa, int *ipa, size_t *n)
Add element to state vector.
double intpol_tbl_u(tbl_t *tbl, int ig, int id, int ip, int it, double eps)
Interpolate column density from look-up tables.
void write_tbl(ctl_t *ctl, tbl_t *tbl)
Write look-up table data.
void copy_atm(ctl_t *ctl, atm_t *atm_dest, atm_t *atm_src, int init)
Copy and initialize atmospheric data.
#define NW
Maximum number of spectral windows.
size_t atm2x(ctl_t *ctl, atm_t *atm, gsl_vector *x, int *iqa, int *ipa)
Compose state vector or parameter vector.
void formod_rfm(ctl_t *ctl, atm_t *atm, obs_t *obs)
Apply RFM for radiative transfer calculations.
void cart2geo(double *x, double *z, double *lon, double *lat)
Convert Cartesian coordinates to geolocation.
double ctmco2(double nu, double p, double t, double u)
Compute carbon dioxide continuum (optical depth).
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.