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

void usage (void)
 Print command-line help. More...
 
int main (int argc, char *argv[])
 

Detailed Description

Spectral analysis of meteorological data.

Definition in file met_spec.c.

Function Documentation

◆ usage()

void usage ( void  )

Print command-line help.

Definition at line 150 of file met_spec.c.

151 {
152
153 printf("\nMPTRAC met_spec tool.\n\n");
154 printf("Conduct spectral analysis of meteorological temperature fields.\n");
155 printf("\n");
156 printf("Usage:\n");
157 printf(" met_spec <ctl> <spec.tab> <met0>\n");
158 printf("\n");
159 printf("Arguments:\n");
160 printf(" <ctl> Control file.\n");
161 printf(" <spec.tab> Output table.\n");
162 printf(" <met0> Meteorological input file.\n");
163 printf("\nFurther information:\n");
164 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
165}

◆ main()

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

Definition at line 39 of file met_spec.c.

41 {
42
43 ctl_t ctl;
44
45 clim_t *clim;
46
47 met_t *met;
48
49 dd_t *dd;
50
51 FILE *out;
52
53 static double cutImag[EX], cutReal[EX], lx[EX], A[EX], phi[EX];
54
55 /* Allocate... */
56 ALLOC(clim, clim_t, 1);
57 ALLOC(met, met_t, 1);
58 ALLOC(dd, dd_t, 1);
59
60 /* Print usage information... */
61 USAGE;
62
63 /* Check arguments... */
64 if (argc < 4)
65 ERRMSG("Missing or invalid command-line arguments.\n\n"
66 "Usage: met_spec <ctl> <spec.tab> <met0>\n\n"
67 "Use -h for full help.");
68
69 /* Read control parameters... */
70 mptrac_read_ctl(argv[1], argc, argv, &ctl);
71 const double wavemax =
72 (int) scan_ctl(argv[1], argc, argv, "SPEC_WAVEMAX", -1, "7", NULL);
73
74 /* Read climatological data... */
75 mptrac_read_clim(&ctl, clim);
76
77 /* Read meteorological data... */
78 if (!mptrac_read_met(argv[3], &ctl, clim, met, dd))
79 ERRMSG("Cannot read meteo data!");
80
81 /* Create output file... */
82 LOG(1, "Write spectral data file: %s", argv[2]);
83 if (!(out = fopen(argv[2], "w")))
84 ERRMSG("Cannot create file!");
85
86 /* Write header... */
87 fprintf(out,
88 "# $1 = time [s]\n"
89 "# $2 = altitude [km]\n"
90 "# $3 = longitude [deg]\n" "# $4 = latitude [deg]\n");
91 for (int ix = 0; ix <= wavemax; ix++) {
92 fprintf(out, "# $%d = wavelength (PW%d) [km]\n", 5 + 3 * ix, ix);
93 fprintf(out, "# $%d = amplitude (PW%d) [K]\n", 6 + 3 * ix, ix);
94 fprintf(out, "# $%d = phase (PW%d) [deg]\n", 7 + 3 * ix, ix);
95 }
96
97 /* Loop over pressure levels... */
98 for (int ip = 0; ip < met->np; ip++) {
99
100 /* Write output... */
101 fprintf(out, "\n");
102
103 /* Loop over latitudes... */
104 for (int iy = 0; iy < met->ny; iy++) {
105
106 /* Copy data... */
107 for (int ix = 0; ix < met->nx; ix++) {
108 cutReal[ix] = met->t[ix][iy][ip];
109 cutImag[ix] = 0.0;
110 }
111
112 /* FFT... */
113 fft_help(cutReal, cutImag, met->nx);
114
115 /*
116 Get wavelength, amplitude, and phase:
117 A(x) = A[0] + A[1] * cos(2 pi x / lx[1] + phi[1]) + A[2] * cos...
118 */
119 for (int ix = 0; ix < met->nx; ix++) {
120 lx[ix] = DEG2DX(met->lon[met->nx - 1] - met->lon[0], met->lat[iy])
121 / ((ix < met->nx / 2) ? (double) ix : -(double) (met->nx - ix));
122 A[ix] = (ix == 0 ? 1.0 : 2.0) / (met->nx)
123 * sqrt(gsl_pow_2(cutReal[ix]) + gsl_pow_2(cutImag[ix]));
124 phi[ix] = RAD2DEG(atan2(cutImag[ix], cutReal[ix]));
125 }
126
127 /* Write data... */
128 fprintf(out, "%.2f %g %g %g", met->time, Z(met->p[ip]), 0.0,
129 met->lat[iy]);
130 for (int ix = 0; ix <= wavemax; ix++)
131 fprintf(out, " %g %g %g", lx[ix], A[ix], phi[ix]);
132 fprintf(out, "\n");
133 }
134 }
135
136 /* Close file... */
137 fclose(out);
138
139 /* Free... */
140 free(clim);
141 free(met);
142 free(dd);
143
144 return EXIT_SUCCESS;
145}
void fft_help(double *fcReal, double *fcImag, const int n)
Computes the Fast Fourier Transform (FFT) of a complex sequence.
Definition: mptrac.c:1635
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:10745
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:5188
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:6151
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:5248
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2102
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:1909
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:1939
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:335
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:453
#define RAD2DEG(rad)
Converts radians to degrees.
Definition: mptrac.h:1572
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:2032
#define DEG2DX(dlon, lat)
Convert a longitude difference to a distance in the x-direction (east-west) at a specific latitude.
Definition: mptrac.h:566
Climatological data.
Definition: mptrac.h:3436
Control parameters.
Definition: mptrac.h:2190
Domain decomposition data structure.
Definition: mptrac.h:3669
Meteo data structure.
Definition: mptrac.h:3495
int nx
Number of longitudes.
Definition: mptrac.h:3501
int ny
Number of latitudes.
Definition: mptrac.h:3504
int np
Number of pressure levels.
Definition: mptrac.h:3507
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3609
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3513
double time
Time [s].
Definition: mptrac.h:3498
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3516
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3519
Here is the call graph for this function: