-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathafrica.c
142 lines (125 loc) · 3.48 KB
/
africa.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
/*
NASA/TRMM, Code 910.1.
This is the TRMM Office Radar Software Library.
Copyright (C) 1997
John H. Merritt
Space Applications Corporation
Vienna, Virginia
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "africa.h"
int africa_read_buffer(FILE *fp, Africa_buffer *buffer)
{
int n;
n = fread(buffer, 1, sizeof(Africa_buffer), fp);
return n;
}
float africa_bcd_convert(unsigned short bcd)
{
int val;
val =
((bcd >> 12) & 0xf) * 1000
+ ((bcd >> 8) & 0xf) * 100
+ ((bcd >> 4) & 0xf) * 10
+ ( bcd & 0xf);
return val/10.0;
}
Africa_sweep * africa_new_sweep(int nray)
{
Africa_sweep *sweep;
sweep = (Africa_sweep *)calloc(nray, sizeof(Africa_sweep));
if (! sweep) {
perror("africa_new_sweep:sweep");
return NULL;
}
sweep->nrays = nray;
sweep->ray = (Africa_ray **) calloc(nray, sizeof(Africa_ray *));
if (! sweep -> ray) {
perror("africa_new_sweep:ray");
return NULL;
}
return sweep;
}
Africa_ray *africa_new_ray(void)
{
Africa_ray *ray;
ray = (Africa_ray *)calloc(1, sizeof(Africa_ray));
if (! ray) {
perror("africa_new_ray:ray");
return NULL;
}
return ray;
}
void africa_free_ray(Africa_ray *r)
{
if (! r) return;
free(r);
}
void africa_free_sweep(Africa_sweep *s)
{
int i;
if (! s) return;
if (! s->ray) return;
for (i=0; i<s->nrays; i++) {
africa_free_ray(s->ray[i]);
}
free(s->ray);
free(s);
}
Africa_sweep *africa_read_sweep(FILE *fp)
{
/* This contains the next ray of data, except
* when this is the first ray. This is a
* read-ahead buffer.
*/
static Africa_buffer *buf = NULL; /* The read ahead buffer, too. */
Africa_sweep *sweep = NULL;
int cur_elev, ielev, iray;
Africa_ray *ray = NULL;
int nray;
if (! buf) {
buf = (Africa_buffer *) calloc(1, sizeof(Africa_buffer));
if (! buf) {
perror("allocate buf in read_sweep");
return NULL;
}
/* (pre)Read a record */
africa_read_buffer(fp, buf); /* allocates space for buffer */
}
/* Allocate 500 (should be enough) ray pointers. */
sweep = africa_new_sweep(1000); /* 500 times 2 field types ? */
/* Determine the elevation step we're on so we know when to
* return the sweep. Basically, the last read will be the
* data for the next sweep; the meaning of the above code.
*/
cur_elev = ielev = buf->raycount/512;
nray = 0;
while(cur_elev == ielev) {
iray = buf->raycount - cur_elev*512;
if (iray != 0) {
ray = sweep->ray[nray];
if (!ray) sweep->ray[nray] = ray = africa_new_ray();
memcpy(ray, buf, sizeof(Africa_buffer));
nray++;
}
if (africa_read_buffer(fp, buf) == 0) break;
cur_elev = buf->raycount/512;
/* isite = buf->xmit_power_site & 0x1f; */
}
if (nray == 0) return NULL;
sweep->nrays = nray;
return sweep;
}