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] = RAD2DEG(atan2(cutImag[ix], cutReal[ix]));
105 }
106
107 /* Write data... */
108 fprintf(out, "%.2f %g %g %g", met->time, Z(met->p[ip]), 0.0,
109 met->lat[iy]);
110 for (int ix = 0; ix <= wavemax; ix++)
111 fprintf(out, " %g %g %g", lx[ix], A[ix], phi[ix]);
112 fprintf(out, "\n");
113 }
114 }
115
116 /* Close file... */
117 fclose(out);
118
119 /* Free... */
120 free(clim);
121 free(met);
122
123 return EXIT_SUCCESS;
124}
int main(int argc, char *argv[])
Definition: met_spec.c:27
void fft_help(double *fcReal, double *fcImag, const int n)
Computes the Fast Fourier Transform (FFT) of a complex sequence.
Definition: mptrac.c:946
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:8098
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:4805
int read_met(const char *filename, ctl_t *ctl, clim_t *clim, met_t *met)
Reads meteorological data from a file, supporting multiple formats and MPI broadcasting.
Definition: mptrac.c:5655
void read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:4473
MPTRAC library declarations.
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1901
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1726
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:262
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:347
#define RAD2DEG(rad)
Converts radians to degrees.
Definition: mptrac.h:1381
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:1831
#define DEG2DX(dlon, lat)
Convert a longitude difference to a distance in the x-direction (east-west) at a specific latitude.
Definition: mptrac.h:438
Climatological data.
Definition: mptrac.h:3282
Control parameters.
Definition: mptrac.h:2155
Meteo data structure.
Definition: mptrac.h:3341
int nx
Number of longitudes.
Definition: mptrac.h:3347
int ny
Number of latitudes.
Definition: mptrac.h:3350
int np
Number of pressure levels.
Definition: mptrac.h:3353
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3437
double lon[EX]
Longitude [deg].
Definition: mptrac.h:3359
double time
Time [s].
Definition: mptrac.h:3344
double lat[EY]
Latitude [deg].
Definition: mptrac.h:3362
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3365