MPTRAC
cape.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-2026 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27/* ------------------------------------------------------------
28 Functions...
29 ------------------------------------------------------------ */
30
32void usage(
33 void);
34
35/* ------------------------------------------------------------
36 Main...
37 ------------------------------------------------------------ */
38
39int main(
40 int argc,
41 char *argv[]) {
42
43 ctl_t ctl;
44
45 clim_t *clim;
46
47 met_t *met;
48
49 dd_t *dd;
50
51 char tstr[LEN];
52
53 float help[EX * EY];
54
55 int dims[10], ncid, varid;
56
57 size_t start[10], count[10];
58
59 /* Allocate... */
60 ALLOC(clim, clim_t, 1);
61 ALLOC(met, met_t, 1);
62 ALLOC(dd, dd_t, 1);
63
64 /* Print usage information... */
65 USAGE;
66
67 /* Check arguments... */
68 if (argc < 2)
69 ERRMSG("Missing or invalid command-line arguments.\n\n"
70 "Usage: cape <ctl> <met.nc>\n\n" "Use -h for full help.");
71
72 /* Read control parameters... */
73 mptrac_read_ctl(argv[1], argc, argv, &ctl);
74
75 /* Read climatological data... */
76 mptrac_read_clim(&ctl, clim);
77
78 /* Read meteorological data... */
79 if (!mptrac_read_met(argv[2], &ctl, clim, met, dd))
80 ERRMSG("Cannot open file!");
81
82 /* Open netCDF file... */
83 if (nc_open(argv[2], NC_WRITE, &ncid) != NC_NOERR)
84 ERRMSG("Cannot open file!");
85
86 /* Get dimensions... */
87 NC_INQ_DIM("time", &dims[0], 1, 1, 1);
88 NC_INQ_DIM("lat", &dims[1], met->ny, met->ny, 1);
89 NC_INQ_DIM("lon", &dims[2], met->nx - 1, met->nx - 1, 1);
90 NC(nc_inq_dimid(ncid, "time", &dims[0]));
91 NC(nc_inq_dimid(ncid, "lat", &dims[1]));
92 NC(nc_inq_dimid(ncid, "lon", &dims[2]));
93
94 /* Set define mode... */
95 NC(nc_redef(ncid));
96
97 /* Create variables... */
98 NC_DEF_VAR("CAPE_MPT", NC_FLOAT, 3, dims,
99 "convective available potential energy", "J kg**-1", 0, 0);
100 NC_DEF_VAR("CIN_MPT", NC_FLOAT, 3, dims,
101 "convective inhibition", "J kg**-1", 0, 0);
102 NC_DEF_VAR("PEL_MPT", NC_FLOAT, 3, dims,
103 "pressure at equilibrium level", "hPa", 0, 0);
104
105 /* Get current time... */
106 time_t t = time(NULL);
107 struct tm tm = *localtime(&t);
108 sprintf(tstr, "%d-%02d-%02d %02d:%02d:%02d",
109 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
110 tm.tm_hour, tm.tm_min, tm.tm_sec);
111
112 /* Set additional attributes... */
113 NC_PUT_ATT("CAPE_MPT", "creator_of_parameter", "MPTRAC");
114 NC_PUT_ATT("CIN_MPT", "creator_of_parameter", "MPTRAC");
115 NC_PUT_ATT("PEL_MPT", "creator_of_parameter", "MPTRAC");
116
117 NC_PUT_ATT("CAPE_MPT", "param_creation_time", tstr);
118 NC_PUT_ATT("CIN_MPT", "param_creation_time", tstr);
119 NC_PUT_ATT("PEL_MPT", "param_creation_time", tstr);
120
121 NC_PUT_ATT("CAPE_MPT", "param_modification_time", tstr);
122 NC_PUT_ATT("CIN_MPT", "param_modification_time", tstr);
123 NC_PUT_ATT("PEL_MPT", "param_modification_time", tstr);
124
125 NC_PUT_ATT("CAPE_MPT", "flag", "NONE");
126 NC_PUT_ATT("CIN_MPT", "flag", "NONE");
127 NC_PUT_ATT("PEL_MPT", "flag", "NONE");
128
129 float miss[1] = { NAN };
130 NC(nc_inq_varid(ncid, "CAPE_MPT", &varid));
131 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
132 NC(nc_inq_varid(ncid, "CIN_MPT", &varid));
133 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
134 NC(nc_inq_varid(ncid, "PEL_MPT", &varid));
135 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
136
137 /* End define mode... */
138 NC(nc_enddef(ncid));
139
140 /* Write data... */
141 for (int ix = 0; ix < met->nx - 1; ix++)
142 for (int iy = 0; iy < met->ny; iy++)
143 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->cape[ix][iy];
144 NC_PUT_FLOAT("CAPE_MPT", help, 0);
145
146 for (int ix = 0; ix < met->nx - 1; ix++)
147 for (int iy = 0; iy < met->ny; iy++)
148 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->cin[ix][iy];
149 NC_PUT_FLOAT("CIN_MPT", help, 0);
150
151 for (int ix = 0; ix < met->nx - 1; ix++)
152 for (int iy = 0; iy < met->ny; iy++)
153 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->pel[ix][iy];
154 NC_PUT_FLOAT("PEL_MPT", help, 0);
155
156 /* Close file... */
157 NC(nc_close(ncid));
158
159 /* Free... */
160 free(clim);
161 free(met);
162 free(dd);
163
164 return EXIT_SUCCESS;
165}
166
167/*****************************************************************************/
168
170void usage(
171 void) {
172
173 printf("\nMPTRAC cape tool.\n\n");
174 printf("Add CAPE data to a meteorological netCDF file.\n");
175 printf("\n");
176 printf("Usage:\n");
177 printf(" cape <ctl> <met.nc>\n");
178 printf("\n");
179 printf("Arguments:\n");
180 printf(" <ctl> Control file.\n");
181 printf(" <met.nc> Meteorological netCDF file to update.\n");
182 printf("\nFurther information:\n");
183 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
184}
int main(int argc, char *argv[])
Definition: cape.c:39
void usage(void)
Print command-line help.
Definition: cape.c:170
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
MPTRAC library declarations.
#define LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:345
#define NC(cmd)
Execute a NetCDF command and check for errors.
Definition: mptrac.h:1204
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2102
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:340
#define ARRAY_2D(ix, iy, ny)
Macro for computing the linear index of a 2D array element.
Definition: mptrac.h:476
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:1909
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:335
#define NC_PUT_ATT(varname, attname, text)
Add a text attribute to a NetCDF variable.
Definition: mptrac.h:1386
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:453
#define NC_PUT_FLOAT(varname, ptr, hyperslab)
Write a float array to a NetCDF file.
Definition: mptrac.h:1341
#define NC_DEF_VAR(varname, type, ndims, dims, long_name, units, level, quant)
Define a NetCDF variable with attributes.
Definition: mptrac.h:1233
#define NC_INQ_DIM(dimname, ptr, min, max, check)
Inquire the length of a dimension in a NetCDF file.
Definition: mptrac.h:1293
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
float cape[EX][EY]
Convective available potential energy [J/kg].
Definition: mptrac.h:3597
int nx
Number of longitudes.
Definition: mptrac.h:3501
int ny
Number of latitudes.
Definition: mptrac.h:3504
float pel[EX][EY]
Pressure at equilibrium level (EL) [hPa].
Definition: mptrac.h:3594
float cin[EX][EY]
Convective inhibition [J/kg].
Definition: mptrac.h:3600