MPTRAC
atm2grid.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 atm_t *atm;
46
47 met_t *met0 = NULL, *met1 = NULL;
48
49 /* Allocate... */
50 ALLOC(atm, atm_t, 1);
51
52 /* Print usage information... */
53 USAGE;
54
55 /* Check arguments... */
56 if (argc < 3)
57 ERRMSG("Missing or invalid command-line arguments.\n\n"
58 "Usage: atm2grid <ctl> <atm_in> [KEY VALUE ...]\n\n"
59 "Use -h for full help.");
60
61 /* Read control parameters... */
62 mptrac_read_ctl(argv[1], argc, argv, &ctl);
63
64 /* Check grid basename... */
65 if (ctl.grid_basename[0] == '-')
66 ERRMSG("You need to specify GRID_BASENAME!");
67
68 /* Read atmospheric data... */
69 if (!mptrac_read_atm(argv[2], &ctl, atm))
70 ERRMSG("Cannot open file!");
71
72 /* Get time from filename... */
73 int year, mon, day, hour, min, sec;
74 double r, t = time_from_filename(argv[2], ctl.atm_type < 2 ? 20 : 19);
75 jsec2time(t, &year, &mon, &day, &hour, &min, &sec, &r);
76
77 /* Set output filename... */
78 char filename[3 * LEN];
79 sprintf(filename, "%s_%04d_%02d_%02d_%02d_%02d.%s",
80 ctl.grid_basename, year, mon, day, hour, min,
81 ctl.grid_type == 0 ? "tab" : "nc");
82
83 /* Write grid data... */
84 write_grid(filename, &ctl, met0, met1, atm, t);
85
86 /* Free... */
87 free(atm);
88
89 return EXIT_SUCCESS;
90}
91
92/*****************************************************************************/
93
95void usage(
96 void) {
97
98 printf("\nMPTRAC atm2grid tool.\n\n");
99 printf
100 ("Convert an atmospheric particle data file to gridded output data.\n");
101 printf("\n");
102 printf("Usage:\n");
103 printf(" atm2grid <ctl> <atm_in> [KEY VALUE ...]\n");
104 printf("\n");
105 printf("Arguments:\n");
106 printf(" <ctl> Control file.\n");
107 printf(" <atm_in> Atmospheric input file.\n");
108 printf(" [KEY VALUE] Optional control parameters.\n");
109 printf("\n");
110 printf("Notes:\n");
111 printf(" GRID_BASENAME must be set in the control parameters.\n");
112 printf("\nFurther information:\n");
113 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
114}
int main(int argc, char *argv[])
Definition: atm2grid.c:39
void usage(void)
Print command-line help.
Definition: atm2grid.c:95
double time_from_filename(const char *filename, const int offset)
Extracts and converts a timestamp from a filename to Julian seconds.
Definition: mptrac.c:10917
int mptrac_read_atm(const char *filename, const ctl_t *ctl, atm_t *atm)
Reads air parcel data from a specified file into the given atmospheric structure.
Definition: mptrac.c:5114
void jsec2time(const double jsec, int *year, int *mon, int *day, int *hour, int *min, int *sec, double *remain)
Converts Julian seconds to calendar date and time components.
Definition: mptrac.c:2294
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:5245
void write_grid(const char *filename, const ctl_t *ctl, met_t *met0, met_t *met1, const atm_t *atm, const double t)
Writes grid data to a file in ASCII or netCDF format.
Definition: mptrac.c:11739
MPTRAC library declarations.
#define LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:345
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2102
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:1909
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:453
Air parcel data.
Definition: mptrac.h:3241
Control parameters.
Definition: mptrac.h:2190
int atm_type
Type of atmospheric data files (0=ASCII, 1=binary, 2=netCDF, 3=CLaMS_traj, 4=CLaMS_pos).
Definition: mptrac.h:3007
int grid_type
Type of grid data files (0=ASCII, 1=netCDF).
Definition: mptrac.h:3129
char grid_basename[LEN]
Basename of grid data files.
Definition: mptrac.h:3078
Meteo data structure.
Definition: mptrac.h:3495