]> Pileus Git - aweather/blob - src/level2.c
init function, level-2 parseing template
[aweather] / src / level2.c
1 /* Prototype stuff for parsing Level-II data */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <error.h>
7 #include <arpa/inet.h>
8
9 struct packet {
10         short          junk1[6];
11         unsigned short size;
12         unsigned char  id, type;
13         unsigned short seq, gen_date;
14         unsigned int   gen_time;
15         unsigned short num_seg, seg;
16         unsigned int   coll_time;
17         unsigned short coll_date, range, angle, radial, rad_status, elev_angle;
18         unsigned short elev_num;
19         short          first_refl, first_dopp;
20         unsigned short refl_size, dopp_size;
21         unsigned short num_refl_gate, num_dopp_gate, sector;
22         float          gain;
23         unsigned short refl_ptr, vel_ptr, spec_ptr, dopp_res, pattern;
24         short          junk2[4];
25         unsigned short refl_ptr_rda, vel_ptr_rda, spec_ptr_rda, nyquist, atten;
26         short          thresh;
27         short          junk3[17];
28         unsigned char  data[2304];
29         float          dbz[460];
30 };
31
32 typedef struct {
33         /* 24 bytes */
34         char version[4];
35         char unknown0[16];
36         char station[4];
37 } __attribute__ ((packed)) header_t;
38
39 int main(int argc, char **argv)
40 {
41         if (argc < 2) {
42                 printf("usage: %s <level2-data>\n", argv[0]);
43                 return 0;
44         }
45
46         /* Read header */
47         FILE *fd = fopen(argv[1], "r");
48         if (fd == NULL)
49                 error(1, errno, "Error opening files `%s'", argv[1]);
50         header_t header;
51         if (1 != fread(&header, sizeof(header_t), 1, fd))
52                 error(1, errno, "Error reading header");
53
54         /* Cut up the bzips */
55         int file_num = 0;
56         char filename[16];
57         char *buf = NULL;
58         FILE *outfile = NULL;
59         unsigned int size = 0;
60
61         while ((int)size >= 0) {
62                 if (1 != fread(&size, 4, 1, fd))
63                         break; //error(1, errno, "Error reading size, pos=%x", (unsigned int)ftell(fd));
64                 size = ntohl(size);
65
66                 /* Read data */
67                 ;
68                 if (NULL == (buf = (char *)realloc(buf, size)))
69                         error(1, errno, "cannot allocate `%d' bytes for buffer", size);
70                 if (size != fread(buf, 1, size, fd))
71                         error(1, errno, "error reading from input file");
72
73                 /* Decmopress data */
74                 //char *block = (char *)malloc(8192), *oblock = (char *)malloc(262144);
75                 //error = BZ2_bzBuffToBuffDecompress(oblock, &olength, block, length, 0, 0);
76
77                 /* Write data */
78                 snprintf(filename, 16, "%d.bz2", file_num);
79                 outfile = fopen(filename, "w+");
80                 fwrite(buf, 1, size, outfile);
81                 fclose(outfile);
82
83                 //fprintf(stderr, "wrote `%d' bytes to file `%s'\n", size, filename);
84
85                 /* iterate */
86                 file_num++;
87         }
88
89         return 0;
90 }