-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexradII.c
276 lines (264 loc) · 6.3 KB
/
nexradII.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <bzlib.h>
#include "ulog.h"
#include "mkdirs_open.h"
struct packet {
short junk1[6];
unsigned short size;
unsigned char id, type;
unsigned short seq, gen_date;
unsigned int gen_time;
unsigned short num_seg, seg;
unsigned int coll_time;
unsigned short coll_date, range, angle, radial, rad_status, elev_angle;
unsigned short elev_num;
short first_refl, first_dopp;
unsigned short refl_size, dopp_size;
unsigned short num_refl_gate, num_dopp_gate, sector;
float gain;
unsigned short refl_ptr, vel_ptr, spec_ptr, dopp_res, pattern;
short junk2[4];
unsigned short refl_ptr_rda, vel_ptr_rda, spec_ptr_rda, nyquist, atten;
short thresh;
short junk3[17];
unsigned char data[2304];
float dbz[460];
};
static char *compression_type = "BZIP2";
/*
nexradII outfile
*/
static void
usage(
char *av0 /* id string */
)
{
(void)fprintf(stderr,
"Usage: %s [options] [filename]\t\nOptions:\n", av0);
(void)fprintf(stderr,
"\t-v Verbose, tell me about each product\n");
(void)fprintf(stderr,
"\t-l logfile Default logs to syslogd\n");
(void)fprintf(stderr,
"\t-C type Compression BZIP2)\n");
(void)fprintf(stderr,
"\t-f Filter out type 2 radials with status 28\n");
exit(1);
}
/*
* called upon receipt of signals
*/
static void
signal_handler(int sig)
{
#ifdef SVR3SIGNALS
/*
* Some systems reset handler to SIG_DFL upon entry to handler.
* In that case, we reregister our handler.
*/
(void) signal(sig, signal_handler);
#endif
switch(sig) {
case SIGHUP :
udebug("SIGHUP") ;
return ;
case SIGINT :
unotice("Interrupt") ;
exit(0) ;
case SIGTERM :
udebug("SIGTERM") ;
exit(0) ;
case SIGUSR1 :
udebug("SIGUSR1") ;
return ;
case SIGUSR2 :
if (toggleulogpri(LOG_INFO))
unotice("Going verbose") ;
else
unotice("Going silent") ;
return ;
case SIGPIPE :
unotice("SIGPIPE") ;
exit(0) ;
}
udebug("signal_handler: unhandled signal: %d", sig) ;
}
static void
set_sigactions()
{
#ifndef NO_POSIXSIGNALS
struct sigaction sigact ;
sigact.sa_handler = signal_handler;
sigemptyset(&sigact.sa_mask) ;
sigact.sa_flags = 0 ;
#ifdef SA_RESTART /* SVR4, 4.3+ BSD */
/* usually, restart system calls */
sigact.sa_flags |= SA_RESTART ;
#endif
(void) sigaction(SIGHUP, &sigact, NULL) ;
(void) sigaction(SIGINT, &sigact, NULL) ;
(void) sigaction(SIGTERM, &sigact, NULL) ;
(void) sigaction(SIGUSR1, &sigact, NULL) ;
(void) sigaction(SIGUSR2, &sigact, NULL) ;
(void) sigaction(SIGPIPE, &sigact, NULL) ;
#else
(void) signal(SIGHUP, signal_handler) ;
(void) signal(SIGINT, signal_handler) ;
(void) signal(SIGTERM, signal_handler) ;
(void) signal(SIGUSR1, signal_handler) ;
(void) signal(SIGUSR2, signal_handler) ;
(void) signal(SIGPIPE, signal_handler) ;
#endif
}
int main(int argc, char *argv[], char *envp[])
{
char *block = (char *)malloc(8192), *oblock = (char *)malloc(262144);
unsigned isize = 8192, osize=262144, olength;
int length, go;
int fd;
int logmask = (LOG_MASK(LOG_ERR)) | (LOG_MASK(LOG_NOTICE));
int compress = 0;
int pipedes[2], logfd, pid;
char *logfname = "";
extern int optind;
extern int opterr;
extern char *optarg;
int ch;
int bzip2 = 1;
int filter = 0;
opterr = 1;
while ((ch = getopt(argc, argv, "fvxl:C:")) != EOF)
switch (ch) {
case 'v':
logmask |= LOG_MASK(LOG_INFO);
break;
case 'x':
logmask |= LOG_MASK(LOG_DEBUG);
break;
case 'l':
if(optarg[0] == '-' && optarg[1] != 0)
{
fprintf(stderr, "logfile \"%s\" ??\n",
optarg);
usage(argv[0]);
}
/* else */
logfname = optarg;
break;
case 'C':
if (strcmp(optarg, "BZIP2")) {
fprintf(stderr, "Compression type must be BZIP2\n");
exit(1);
}
else {
compression_type = optarg;
bzip2 = (strcmp(compression_type, "BZIP2")==0);
}
break;
case 'f':
filter = 1;
break;
case '?':
usage(argv[0]);
break;
}
setulogmask(logmask);
/*
* initialize logger
*/
if(logfname == NULL || !(*logfname == '-' && logfname[1] == 0))
(void) fclose(stderr);
logfd = openulog(ubasename(argv[0]),
(LOG_CONS|LOG_PID), LOG_LDM, logfname);
if (argc - optind <= 0 ) {
fd = 1;
}
else {
if ((fd=mkdirs_open(argv[optind],O_WRONLY | O_CREAT, 0664)) == -1) {
uerror("Cannot open %s", argv[1]);
return 1;
}
lseek(fd, 0, SEEK_END);
}
/*
* set up signal handlers
*/
set_sigactions();
go = 1;
while (go) {
int i;
if ((i=getbuf(0, &length, 4)) != 4) {
if (i > 0) uerror("Short block length");
return;
}
if (strncmp(&length, "ARCH", 4)==0 || strncmp(&length, "AR2V", 4)==0) {
memcpy(block, &length, 4);
if (getbuf(0, block+4, 20) != 20) {
uerror("Missing header");
return;
}
lseek(fd, 0, SEEK_SET);
write(fd, block, 24);
continue;
}
length = ntohl(length);
if(length < 0) {
uinfo("EOF");
length = -length;
go = 0;
}
if (length > isize) {
isize = length;
uinfo ("Expanding input buffer to %d", isize);
if ((block = (char *)realloc(block, isize)) == NULL) {
uerror("Cannot allocate input buffer");
return;
}
}
if (getbuf(0, block, length) != length) {
uerror("Short block read");
return;
}
if (length > 10) {
int error;
tryagain:
olength = osize;
if (bzip2 == 1)
#ifdef BZ_CONFIG_ERROR
error = BZ2_bzBuffToBuffDecompress(oblock, &olength,
#else
error = bzBuffToBuffDecompress(oblock, &olength,
#endif
block, length, 0, 0);
if (error) {
if (error == BZ_OUTBUFF_FULL) {
osize += 262144;
uinfo ("Expanding output buffer to %d", osize);
if ((oblock=(char*) realloc(oblock, osize)) == NULL) {
uerror("Cannot allocate output buffer");
}
goto tryagain;
}
uerror("decompress error - %d\n", error);
exit(1);
}
if (filter) {
int i;
for (i=0; i < olength; i += 2432) {
struct packet *packet=(struct packet *) (oblock+i);
if (packet->type != 2 || packet->rad_status != 28)
write(fd, oblock+i, 2432);
}
}
else write(fd, oblock, olength);
}
}
close(fd);
return;
}