MPTRAC
met_spec.c
Go to the documentation of this file.
1/*
2 This file is part of MPTRAC.
3
4 MPTRAC is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 MPTRAC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with MPTRAC. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright (C) 2013-2024 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27int main(
28 int argc,
29 char *argv[]) {
30
31 ctl_t ctl;
32
33 clim_t *clim;
34
35 met_t *met;
36
37 FILE *out;
38
39 static double cutImag[EX], cutReal[EX], lx[EX], A[EX], phi[EX];
40
41 /* Allocate... */
42 ALLOC(clim, clim_t, 1);
43 ALLOC(met, met_t, 1);
44
45 /* Check arguments... */
46 if (argc < 4)
47 ERRMSG("Give parameters: <ctl> <spec.tab> <met0>");
48
49 /* Read control parameters... */
50 read_ctl(argv[1], argc, argv, &ctl);
51 double wavemax =
52 (int) scan_ctl(argv[1], argc, argv, "SPEC_WAVEMAX", -1, "7", NULL);
53
54 /* Read climatological data... */
55 read_clim(&ctl, clim);
56
57 /* Read meteorological data... */
58 if (!read_met(argv[3], &ctl, clim, met))
59 ERRMSG("Cannot read meteo data!");
60
61 /* Create output file... */
62 LOG(1, "Write spectral data file: %s", argv[2]);
63 if (!(out = fopen(argv[2], "w")))
64 ERRMSG("Cannot create file!");
65
66 /* Write header... */
67 fprintf(out,
68 "# $1 = time [s]\n"
69 "# $2 = altitude [km]\n"
70 "# $3 = longitude [deg]\n" "# $4 = latitude [deg]\n");
71 for (int ix = 0; ix <= wavemax; ix++) {
72 fprintf(out, "# $%d = wavelength (PW%d) [km]\n", 5 + 3 * ix, ix);
73 fprintf(out, "# $%d = amplitude (PW%d) [K]\n", 6 + 3 * ix, ix);
74 fprintf(out, "# $%d = phase (PW%d) [deg]\n", 7 + 3 * ix, ix);
75 }
76
77 /* Loop over pressure levels... */
78 for (int ip = 0; ip < met->np; ip++) {
79
80 /* Write output... */
81 fprintf(out, "\n");
82
83 /* Loop over latitudes... */
84 for (int iy = 0; iy < met->ny; iy++) {
85
86 /* Copy data... */
87 for (int ix = 0; ix < met->nx; ix++) {
88 cutReal[ix] = met->t[ix][iy][ip];
89 cutImag[ix] = 0.0;
90 }
91
92 /* FFT... */
93 fft_help(cutReal, cutImag, met->nx);
94
95 /*
96 Get wavelength, amplitude, and phase:
97 A(x) = A[0] + A[1] * cos(2 pi x / lx[1] + phi[1]) + A[2] * cos...
98 */
99 for (int ix = 0; ix < met->nx; ix++) {
100 lx[ix] = DEG2DX(met->lon[met->nx - 1] - met->lon[0], met->lat[iy])
101 / ((ix < met->nx / 2) ? (double) ix : -(double) (met->nx - ix));
102 A[ix] = (ix == 0 ? 1.0 : 2.0) / (met->nx)
103 * sqrt(gsl_pow_2(cutReal[ix]) + gsl_pow_2(cutImag[ix]));
104 phi[ix]
105 = 180. / M_PI * atan2(cutImag[ix], cutReal[ix]);
106 }
107
108 /* Write data... */
109 fprintf(out, "%.2f %g %g %g", met->time, Z(met->p[ip]), 0.0,
110 met->lat[iy]);
111 for (int ix = 0; ix <= wavemax; ix++)
112 fprintf(out, " %g %g %g", lx[ix], A[ix], phi[ix]);
113 fprintf(out, "\n");
114 }
115 }
116
117 /* Close file... */
118 fclose(out);
119
120 /* Free... */
121 free(clim);
122 free(met);
123
124 return EXIT_SUCCESS;
125}
int main(int argc, char *argv[])
Definition: met_spec.c:27
void read_clim(ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:4441
void fft_help(double *fcReal, double *fcImag, int n)
Computes the Fast Fourier Transform (FFT) of a complex sequence.
Definition: mptrac.c:901
void 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:4789
int read_met(const char *filename, ctl_t *ctl, clim_t *clim, met_t *met)
Reads meteorological data from a file and populates the provided structures.
Definition: mptrac.c:5611
double scan_ctl(const char *filename, int argc, char *argv[], const char *varname, int arridx, const char *defvalue, char *value)
Scans a control file or command-line arguments for a specified variable.
Definition: mptrac.c:7943
MPTRAC library declarations.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1881
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1706
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:261
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:344
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1811
#define DEG2DX(dlon, lat)
Convert a longitude difference to a distance in the x-direction (east-west) at a specific latitude.
Definition: mptrac.h:435
Climatological data.
Definition: mptrac.h:3230
Control parameters.
Definition: mptrac.h:2135
Meteo data structure.
Definition: mptrac.h:3289
int nx
Number of longitudes.
Definition: mptrac.h:3295
int ny
Number of latitudes.
Definition: mptrac.h:3298
int np
Number of pressure levels.
Definition: mptrac.h:3301
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3385
double lon[EX]
Longitude [deg].
Definition: mptrac.h:3307
double time
Time [s].
Definition: mptrac.h:3292
double lat[EY]
Latitude [deg].
Definition: mptrac.h:3310
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3313