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-2023 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 char tstr[LEN];
38
39 float help[EX * EY];
40
41 int dims[10], ncid, varid;
42
43 size_t start[10], count[10];
44
45 /* Allocate... */
46 ALLOC(clim, clim_t, 1);
47 ALLOC(met, met_t, 1);
48
49 /* Check arguments... */
50 if (argc < 2)
51 ERRMSG("Give parameters: <ctl> <met.nc>");
52
53 /* Read control parameters... */
54 read_ctl(argv[1], argc, argv, &ctl);
55
56 /* Read climatological data... */
57 read_clim(&ctl, clim);
58
59 /* Read meteorological data... */
60 if (!read_met(argv[2], &ctl, clim, met))
61 ERRMSG("Cannot open file!");
62
63 /* Open netCDF file... */
64 if (nc_open(argv[2], NC_WRITE, &ncid) != NC_NOERR)
65 ERRMSG("Cannot open file!");
66
67 /* Get dimensions... */
68 NC_INQ_DIM("time", &dims[0], 1, 1);
69 NC_INQ_DIM("lat", &dims[1], met->ny, met->ny);
70 NC_INQ_DIM("lon", &dims[2], met->nx - 1, met->nx - 1);
71 NC(nc_inq_dimid(ncid, "time", &dims[0]));
72 NC(nc_inq_dimid(ncid, "lat", &dims[1]));
73 NC(nc_inq_dimid(ncid, "lon", &dims[2]));
74
75 /* Set define mode... */
76 NC(nc_redef(ncid));
77
78 /* Create variables... */
79 NC_DEF_VAR("CAPE_MPT", NC_FLOAT, 3, dims,
80 "convective available potential energy", "J kg**-1", 0, 0);
81 NC_DEF_VAR("CIN_MPT", NC_FLOAT, 3, dims,
82 "convective inhibition", "J kg**-1", 0, 0);
83 NC_DEF_VAR("PEL_MPT", NC_FLOAT, 3, dims,
84 "pressure at equilibrium level", "hPa", 0, 0);
85
86 /* Get current time... */
87 time_t t = time(NULL);
88 struct tm tm = *localtime(&t);
89 sprintf(tstr, "%d-%02d-%02d %02d:%02d:%02d",
90 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
91 tm.tm_hour, tm.tm_min, tm.tm_sec);
92
93 /* Set additional attributes... */
94 NC_PUT_ATT("CAPE_MPT", "creator_of_parameter", "MPTRAC");
95 NC_PUT_ATT("CIN_MPT", "creator_of_parameter", "MPTRAC");
96 NC_PUT_ATT("PEL_MPT", "creator_of_parameter", "MPTRAC");
97
98 NC_PUT_ATT("CAPE_MPT", "param_creation_time", tstr);
99 NC_PUT_ATT("CIN_MPT", "param_creation_time", tstr);
100 NC_PUT_ATT("PEL_MPT", "param_creation_time", tstr);
101
102 NC_PUT_ATT("CAPE_MPT", "param_modification_time", tstr);
103 NC_PUT_ATT("CIN_MPT", "param_modification_time", tstr);
104 NC_PUT_ATT("PEL_MPT", "param_modification_time", tstr);
105
106 NC_PUT_ATT("CAPE_MPT", "flag", "NONE");
107 NC_PUT_ATT("CIN_MPT", "flag", "NONE");
108 NC_PUT_ATT("PEL_MPT", "flag", "NONE");
109
110 float miss[1] = { NAN };
111 NC(nc_inq_varid(ncid, "CAPE_MPT", &varid));
112 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
113 NC(nc_inq_varid(ncid, "CIN_MPT", &varid));
114 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
115 NC(nc_inq_varid(ncid, "PEL_MPT", &varid));
116 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
117
118 /* End define mode... */
119 NC(nc_enddef(ncid));
120
121 /* Write data... */
122 for (int ix = 0; ix < met->nx - 1; ix++)
123 for (int iy = 0; iy < met->ny; iy++)
124 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->cape[ix][iy];
125 NC_PUT_FLOAT("CAPE_MPT", help, 0);
126
127 for (int ix = 0; ix < met->nx - 1; ix++)
128 for (int iy = 0; iy < met->ny; iy++)
129 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->cin[ix][iy];
130 NC_PUT_FLOAT("CIN_MPT", help, 0);
131
132 for (int ix = 0; ix < met->nx - 1; ix++)
133 for (int iy = 0; iy < met->ny; iy++)
134 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->pel[ix][iy];
135 NC_PUT_FLOAT("PEL_MPT", help, 0);
136
137 /* Close file... */
138 NC(nc_close(ncid));
139
140 /* Free... */
141 free(clim);
142 free(met);
143
144 return EXIT_SUCCESS;
145}
int main(int argc, char *argv[])
Definition: cape.c:27
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 LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:236
#define NC(cmd)
Execute a NetCDF command and check for errors.
Definition: mptrac.h:1001
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1901
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:269
#define ARRAY_2D(ix, iy, ny)
Macro for computing the linear index of a 2D array element.
Definition: mptrac.h:370
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:262
#define NC_PUT_ATT(varname, attname, text)
Add a text attribute to a NetCDF variable.
Definition: mptrac.h:1180
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:347
#define NC_INQ_DIM(dimname, ptr, min, max)
Inquire the length of a dimension in a NetCDF file.
Definition: mptrac.h:1088
#define NC_PUT_FLOAT(varname, ptr, hyperslab)
Write a float array to a NetCDF file.
Definition: mptrac.h:1135
#define NC_DEF_VAR(varname, type, ndims, dims, long_name, units, level, quant)
Define a NetCDF variable with attributes.
Definition: mptrac.h:1030
Climatological data.
Definition: mptrac.h:3282
Control parameters.
Definition: mptrac.h:2155
Meteo data structure.
Definition: mptrac.h:3341
float cape[EX][EY]
Convective available potential energy [J/kg].
Definition: mptrac.h:3425
int nx
Number of longitudes.
Definition: mptrac.h:3347
int ny
Number of latitudes.
Definition: mptrac.h:3350
float pel[EX][EY]
Pressure at equilibrium level [hPa].
Definition: mptrac.h:3422
float cin[EX][EY]
Convective inhibition [J/kg].
Definition: mptrac.h:3428