MPTRAC
Functions
cape.c File Reference

Add CAPE data to netCDF file. More...

#include "mptrac.h"

Go to the source code of this file.

Functions

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

Detailed Description

Add CAPE data to netCDF file.

Definition in file cape.c.

Function Documentation

◆ main()

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

Definition at line 27 of file cape.c.

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