MPTRAC
Functions
met_spec.c File Reference

Spectral analysis of meteorological data. More...

#include "mptrac.h"

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Detailed Description

Spectral analysis of meteorological data.

Definition in file met_spec.c.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file met_spec.c.

29 {
30
31 ctl_t ctl;
32
33 clim_t *clim;
34
35 met_t *met;
36
37 dd_t *dd;
38
39 FILE *out;
40
41 static double cutImag[EX], cutReal[EX], lx[EX], A[EX], phi[EX];
42
43 /* Allocate... */
44 ALLOC(clim, clim_t, 1);
45 ALLOC(met, met_t, 1);
46 ALLOC(dd, dd_t, 1);
47
48 /* Check arguments... */
49 if (argc < 4)
50 ERRMSG("Give parameters: <ctl> <spec.tab> <met0>");
51
52 /* Read control parameters... */
53 mptrac_read_ctl(argv[1], argc, argv, &ctl);
54 const double wavemax =
55 (int) scan_ctl(argv[1], argc, argv, "SPEC_WAVEMAX", -1, "7", NULL);
56
57 /* Read climatological data... */
58 mptrac_read_clim(&ctl, clim);
59
60 /* Read meteorological data... */
61 if (!mptrac_read_met(argv[3], &ctl, clim, met, dd))
62 ERRMSG("Cannot read meteo data!");
63
64 /* Create output file... */
65 LOG(1, "Write spectral data file: %s", argv[2]);
66 if (!(out = fopen(argv[2], "w")))
67 ERRMSG("Cannot create file!");
68
69 /* Write header... */
70 fprintf(out,
71 "# $1 = time [s]\n"
72 "# $2 = altitude [km]\n"
73 "# $3 = longitude [deg]\n" "# $4 = latitude [deg]\n");
74 for (int ix = 0; ix <= wavemax; ix++) {
75 fprintf(out, "# $%d = wavelength (PW%d) [km]\n", 5 + 3 * ix, ix);
76 fprintf(out, "# $%d = amplitude (PW%d) [K]\n", 6 + 3 * ix, ix);
77 fprintf(out, "# $%d = phase (PW%d) [deg]\n", 7 + 3 * ix, ix);
78 }
79
80 /* Loop over pressure levels... */
81 for (int ip = 0; ip < met->np; ip++) {
82
83 /* Write output... */
84 fprintf(out, "\n");
85
86 /* Loop over latitudes... */
87 for (int iy = 0; iy < met->ny; iy++) {
88
89 /* Copy data... */
90 for (int ix = 0; ix < met->nx; ix++) {
91 cutReal[ix] = met->t[ix][iy][ip];
92 cutImag[ix] = 0.0;
93 }
94
95 /* FFT... */
96 fft_help(cutReal, cutImag, met->nx);
97
98 /*
99 Get wavelength, amplitude, and phase:
100 A(x) = A[0] + A[1] * cos(2 pi x / lx[1] + phi[1]) + A[2] * cos...
101 */
102 for (int ix = 0; ix < met->nx; ix++) {
103 lx[ix] = DEG2DX(met->lon[met->nx - 1] - met->lon[0], met->lat[iy])
104 / ((ix < met->nx / 2) ? (double) ix : -(double) (met->nx - ix));
105 A[ix] = (ix == 0 ? 1.0 : 2.0) / (met->nx)
106 * sqrt(gsl_pow_2(cutReal[ix]) + gsl_pow_2(cutImag[ix]));
107 phi[ix] = RAD2DEG(atan2(cutImag[ix], cutReal[ix]));
108 }
109
110 /* Write data... */
111 fprintf(out, "%.2f %g %g %g", met->time, Z(met->p[ip]), 0.0,
112 met->lat[iy]);
113 for (int ix = 0; ix <= wavemax; ix++)
114 fprintf(out, " %g %g %g", lx[ix], A[ix], phi[ix]);
115 fprintf(out, "\n");
116 }
117 }
118
119 /* Close file... */
120 fclose(out);
121
122 /* Free... */
123 free(clim);
124 free(met);
125
126 return EXIT_SUCCESS;
127}
void fft_help(double *fcReal, double *fcImag, const int n)
Computes the Fast Fourier Transform (FFT) of a complex sequence.
Definition: mptrac.c:1876
double scan_ctl(const char *filename, int argc, char *argv[], const char *varname, const int arridx, const char *defvalue, char *value)
Scans a control file or command-line arguments for a specified variable.
Definition: mptrac.c:10899
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:5406
int mptrac_read_met(const char *filename, const ctl_t *ctl, const clim_t *clim, met_t *met, dd_t *dd)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
Definition: mptrac.c:6357
void mptrac_read_ctl(const char *filename, int argc, char *argv[], ctl_t *ctl)
Reads control parameters from a configuration file and populates the given structure.
Definition: mptrac.c:5466
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2043
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1880
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:288
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:416
#define RAD2DEG(rad)
Converts radians to degrees.
Definition: mptrac.h:1535
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1973
#define DEG2DX(dlon, lat)
Convert a longitude difference to a distance in the x-direction (east-west) at a specific latitude.
Definition: mptrac.h:529
Climatological data.
Definition: mptrac.h:3487
Control parameters.
Definition: mptrac.h:2264
Domain decomposition data structure.
Definition: mptrac.h:3720
Meteo data structure.
Definition: mptrac.h:3546
int nx
Number of longitudes.
Definition: mptrac.h:3552
int ny
Number of latitudes.
Definition: mptrac.h:3555
int np
Number of pressure levels.
Definition: mptrac.h:3558
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3660
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3564
double time
Time [s].
Definition: mptrac.h:3549
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3567
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3570
Here is the call graph for this function: