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

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

Detailed Description

Add CAPE data to netCDF file.

Definition in file cape.c.

Function Documentation

◆ usage()

void usage ( void  )

Print command-line help.

Definition at line 166 of file cape.c.

167 {
168
169 printf("\nMPTRAC cape tool.\n\n");
170 printf("Add CAPE data to a meteorological netCDF file.\n");
171 printf("\n");
172 printf("Usage:\n");
173 printf(" cape <ctl> <met.nc>\n");
174 printf("\n");
175 printf("Arguments:\n");
176 printf(" <ctl> Control file.\n");
177 printf(" <met.nc> Meteorological netCDF file to update.\n");
178 printf("\nFurther information:\n");
179 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
180}

◆ main()

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

Definition at line 39 of file cape.c.

41 {
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 /* Print usage information... */
60 USAGE;
61
62 /* Check arguments... */
63 if (argc < 2)
64 ERRMSG("Missing or invalid command-line arguments.\n\n"
65 "Usage: cape <ctl> <met.nc>\n\n" "Use -h for full help.");
66
67 /* Allocate... */
68 mptrac_alloc(NULL, NULL, &clim, &met, NULL, NULL, NULL, &dd);
69
70 /* Read control parameters... */
71 mptrac_read_ctl(argv[1], argc, argv, &ctl);
72
73 /* Read climatological data... */
74 mptrac_read_clim(&ctl, clim);
75
76 /* Read meteorological data... */
77 if (!mptrac_read_met(argv[2], &ctl, clim, met, dd))
78 ERRMSG("Cannot open file!");
79
80 /* Open netCDF file... */
81 if (nc_open(argv[2], NC_WRITE, &ncid) != NC_NOERR)
82 ERRMSG("Cannot open file!");
83
84 /* Get dimensions... */
85 NC_INQ_DIM("time", &dims[0], 1, 1, 1);
86 NC_INQ_DIM("lat", &dims[1], met->ny, met->ny, 1);
87 NC_INQ_DIM("lon", &dims[2], met->nx - 1, met->nx - 1, 1);
88 NC(nc_inq_dimid(ncid, "time", &dims[0]));
89 NC(nc_inq_dimid(ncid, "lat", &dims[1]));
90 NC(nc_inq_dimid(ncid, "lon", &dims[2]));
91
92 /* Set define mode... */
93 NC(nc_redef(ncid));
94
95 /* Create variables... */
96 NC_DEF_VAR("CAPE_MPT", NC_FLOAT, 3, dims,
97 "convective available potential energy", "J kg**-1", 0, 0);
98 NC_DEF_VAR("CIN_MPT", NC_FLOAT, 3, dims,
99 "convective inhibition", "J kg**-1", 0, 0);
100 NC_DEF_VAR("PEL_MPT", NC_FLOAT, 3, dims,
101 "pressure at equilibrium level", "hPa", 0, 0);
102
103 /* Get current time... */
104 time_t t = time(NULL);
105 struct tm tm = *localtime(&t);
106 sprintf(tstr, "%d-%02d-%02d %02d:%02d:%02d",
107 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
108 tm.tm_hour, tm.tm_min, tm.tm_sec);
109
110 /* Set additional attributes... */
111 NC_PUT_ATT("CAPE_MPT", "creator_of_parameter", "MPTRAC");
112 NC_PUT_ATT("CIN_MPT", "creator_of_parameter", "MPTRAC");
113 NC_PUT_ATT("PEL_MPT", "creator_of_parameter", "MPTRAC");
114
115 NC_PUT_ATT("CAPE_MPT", "param_creation_time", tstr);
116 NC_PUT_ATT("CIN_MPT", "param_creation_time", tstr);
117 NC_PUT_ATT("PEL_MPT", "param_creation_time", tstr);
118
119 NC_PUT_ATT("CAPE_MPT", "param_modification_time", tstr);
120 NC_PUT_ATT("CIN_MPT", "param_modification_time", tstr);
121 NC_PUT_ATT("PEL_MPT", "param_modification_time", tstr);
122
123 NC_PUT_ATT("CAPE_MPT", "flag", "NONE");
124 NC_PUT_ATT("CIN_MPT", "flag", "NONE");
125 NC_PUT_ATT("PEL_MPT", "flag", "NONE");
126
127 float miss[1] = { NAN };
128 NC(nc_inq_varid(ncid, "CAPE_MPT", &varid));
129 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
130 NC(nc_inq_varid(ncid, "CIN_MPT", &varid));
131 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
132 NC(nc_inq_varid(ncid, "PEL_MPT", &varid));
133 NC(nc_put_att_float(ncid, varid, "missing_value", NC_FLOAT, 1, miss));
134
135 /* End define mode... */
136 NC(nc_enddef(ncid));
137
138 /* Write data... */
139 for (int ix = 0; ix < met->nx - 1; ix++)
140 for (int iy = 0; iy < met->ny; iy++)
141 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->cape[ix][iy];
142 NC_PUT_FLOAT("CAPE_MPT", help, 0);
143
144 for (int ix = 0; ix < met->nx - 1; ix++)
145 for (int iy = 0; iy < met->ny; iy++)
146 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->cin[ix][iy];
147 NC_PUT_FLOAT("CIN_MPT", help, 0);
148
149 for (int ix = 0; ix < met->nx - 1; ix++)
150 for (int iy = 0; iy < met->ny; iy++)
151 help[ARRAY_2D(iy, ix, met->nx - 1)] = met->pel[ix][iy];
152 NC_PUT_FLOAT("PEL_MPT", help, 0);
153
154 /* Close file... */
155 NC(nc_close(ncid));
156
157 /* Free... */
158 mptrac_free(NULL, NULL, clim, met, NULL, NULL, NULL, dd);
159
160 return EXIT_SUCCESS;
161}
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 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 LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:558
#define NC(cmd)
Execute a NetCDF command and check for errors.
Definition: mptrac.h:1507
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2405
#define EY
Maximum number of latitudes for meteo data.
Definition: mptrac.h:553
#define ARRAY_2D(ix, iy, ny)
Macro for computing the linear index of a 2D array element.
Definition: mptrac.h:689
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:2212
#define EX
Maximum number of longitudes for meteo data.
Definition: mptrac.h:548
#define NC_PUT_ATT(varname, attname, text)
Add a text attribute to a NetCDF variable.
Definition: mptrac.h:1689
#define NC_PUT_FLOAT(varname, ptr, hyperslab)
Write a float array to a NetCDF file.
Definition: mptrac.h:1644
#define NC_DEF_VAR(varname, type, ndims, dims, long_name, units, level, quant)
Define a NetCDF variable with attributes.
Definition: mptrac.h:1536
#define NC_INQ_DIM(dimname, ptr, min, max, check)
Inquire the length of a dimension in a NetCDF file.
Definition: mptrac.h:1596
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
float cape[EX][EY]
Convective available potential energy [J/kg].
Definition: mptrac.h:3951
int nx
Number of longitudes.
Definition: mptrac.h:3855
int ny
Number of latitudes.
Definition: mptrac.h:3858
float pel[EX][EY]
Pressure at equilibrium level (EL) [hPa].
Definition: mptrac.h:3948
float cin[EX][EY]
Convective inhibition [J/kg].
Definition: mptrac.h:3954
Here is the call graph for this function: