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 146 of file met_spec.c.

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

◆ 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 mptrac_alloc(NULL, NULL, &clim, &met, NULL, NULL, NULL, &dd);
57
58 /* Print usage information... */
59 USAGE;
60
61 /* Check arguments... */
62 if (argc < 4)
63 ERRMSG("Missing or invalid command-line arguments.\n\n"
64 "Usage: met_spec <ctl> <spec.tab> <met0>\n\n"
65 "Use -h for full help.");
66
67 /* Read control parameters... */
68 mptrac_read_ctl(argv[1], argc, argv, &ctl);
69 const double wavemax =
70 (int) scan_ctl(argv[1], argc, argv, "SPEC_WAVEMAX", -1, "7", NULL);
71
72 /* Read climatological data... */
73 mptrac_read_clim(&ctl, clim);
74
75 /* Read meteorological data... */
76 if (!mptrac_read_met(argv[3], &ctl, clim, met, dd))
77 ERRMSG("Cannot read meteo data!");
78
79 /* Create output file... */
80 LOG(1, "Write spectral data file: %s", argv[2]);
81 if (!(out = fopen(argv[2], "w")))
82 ERRMSG("Cannot create file!");
83
84 /* Write header... */
85 fprintf(out,
86 "# $1 = time [s]\n"
87 "# $2 = altitude [km]\n"
88 "# $3 = longitude [deg]\n" "# $4 = latitude [deg]\n");
89 for (int ix = 0; ix <= wavemax; ix++) {
90 fprintf(out, "# $%d = wavelength (PW%d) [km]\n", 5 + 3 * ix, ix);
91 fprintf(out, "# $%d = amplitude (PW%d) [K]\n", 6 + 3 * ix, ix);
92 fprintf(out, "# $%d = phase (PW%d) [deg]\n", 7 + 3 * ix, ix);
93 }
94
95 /* Loop over pressure levels... */
96 for (int ip = 0; ip < met->np; ip++) {
97
98 /* Write output... */
99 fprintf(out, "\n");
100
101 /* Loop over latitudes... */
102 for (int iy = 0; iy < met->ny; iy++) {
103
104 /* Copy data... */
105 for (int ix = 0; ix < met->nx; ix++) {
106 cutReal[ix] = met->t[ix][iy][ip];
107 cutImag[ix] = 0.0;
108 }
109
110 /* FFT... */
111 fft_help(cutReal, cutImag, met->nx);
112
113 /*
114 Get wavelength, amplitude, and phase:
115 A(x) = A[0] + A[1] * cos(2 pi x / lx[1] + phi[1]) + A[2] * cos...
116 */
117 for (int ix = 0; ix < met->nx; ix++) {
118 lx[ix] = DEG2DX(met->lon[met->nx - 1] - met->lon[0], met->lat[iy])
119 / ((ix < met->nx / 2) ? (double) ix : -(double) (met->nx - ix));
120 A[ix] = (ix == 0 ? 1.0 : 2.0) / (met->nx)
121 * sqrt(SQR(cutReal[ix]) + SQR(cutImag[ix]));
122 phi[ix] = RAD2DEG(atan2(cutImag[ix], cutReal[ix]));
123 }
124
125 /* Write data... */
126 fprintf(out, "%.2f %g %g %g", met->time, Z(met->p[ip]), 0.0,
127 met->lat[iy]);
128 for (int ix = 0; ix <= wavemax; ix++)
129 fprintf(out, " %g %g %g", lx[ix], A[ix], phi[ix]);
130 fprintf(out, "\n");
131 }
132 }
133
134 /* Close file... */
135 fclose(out);
136
137 /* Free... */
138 mptrac_free(NULL, NULL, clim, met, NULL, NULL, NULL, dd);
139
140 return EXIT_SUCCESS;
141}
void mptrac_free(ctl_t *ctl, cache_t *cache, clim_t *clim, met_t *met0, met_t *met1, atm_t *atm, depo_t *depo, dd_t *dd)
Frees memory resources allocated for MPTRAC.
Definition: mptrac.c:6397
void fft_help(double *fcReal, double *fcImag, const int n)
Computes the Fast Fourier Transform (FFT) of a complex sequence.
Definition: mptrac.c:2562
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:12462
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:6683
void mptrac_alloc(ctl_t **ctl, cache_t **cache, clim_t **clim, met_t **met0, met_t **met1, atm_t **atm, depo_t **depo, dd_t **dd)
Allocates and initializes memory resources for MPTRAC.
Definition: mptrac.c:6314
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:7767
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:6743
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2405
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:2212
#define Z(p)
Convert pressure to altitude.
Definition: mptrac.h:2242
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:548
#define SQR(x)
Compute the square of a value.
Definition: mptrac.h:2036
#define RAD2DEG(rad)
Converts radians to degrees.
Definition: mptrac.h:1875
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:2335
#define DEG2DX(dlon, lat)
Convert a longitude difference to a distance in the x-direction (east-west) at a specific latitude.
Definition: mptrac.h:818
Climatological data.
Definition: mptrac.h:3787
Control parameters.
Definition: mptrac.h:2493
Domain decomposition data structure.
Definition: mptrac.h:4023
Meteo data structure.
Definition: mptrac.h:3846
int nx
Number of longitudes.
Definition: mptrac.h:3855
int ny
Number of latitudes.
Definition: mptrac.h:3858
int np
Number of pressure levels.
Definition: mptrac.h:3861
float t[EX][EY][EP]
Temperature [K].
Definition: mptrac.h:3963
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3867
double time
Time [s].
Definition: mptrac.h:3849
double lat[EY]
Latitudes [deg].
Definition: mptrac.h:3870
double p[EP]
Pressure levels [hPa].
Definition: mptrac.h:3873
Here is the call graph for this function: