]> Pileus Git - ~andy/rsl/blob - africa.c
7348587ed0c4eee7b14350069baca71f34932650
[~andy/rsl] / africa.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1997
5             John H. Merritt
6             Space Applications Corporation
7             Vienna, Virginia
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU Library General Public
11     License as published by the Free Software Foundation; either
12     version 2 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     Library General Public License for more details.
18
19     You should have received a copy of the GNU Library General Public
20     License along with this library; if not, write to the Free
21     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "africa.h"
26
27 int africa_read_buffer(FILE *fp, Africa_buffer *buffer)
28 {
29   int n;
30
31   n = fread(buffer, 1, sizeof(Africa_buffer), fp);
32   return n;
33 }
34
35 float africa_bcd_convert(unsigned short bcd)
36 {
37   int val;
38
39   val = 
40       ((bcd >> 12) & 0xf) * 1000
41         + ((bcd >> 8)  & 0xf) * 100
42         + ((bcd >> 4)  & 0xf) * 10
43         + ( bcd        & 0xf);
44   return val/10.0;
45 }
46
47 Africa_sweep * africa_new_sweep(int nray)
48 {
49   Africa_sweep *sweep;
50
51   sweep = (Africa_sweep *)calloc(nray, sizeof(Africa_sweep));
52   if (! sweep) {
53         perror("africa_new_sweep:sweep");
54         return NULL;
55   }
56   sweep->nrays = nray;
57   sweep->ray = (Africa_ray **) calloc(nray, sizeof(Africa_ray *));
58   if (! sweep -> ray) {
59         perror("africa_new_sweep:ray");
60         return NULL;
61   }
62   return sweep;
63 }
64
65 Africa_ray *africa_new_ray(void)
66 {
67   Africa_ray *ray;
68   ray = (Africa_ray *)calloc(1, sizeof(Africa_ray));
69   if (! ray) {
70         perror("africa_new_ray:ray");
71         return NULL;
72   }
73   return ray;
74 }
75   
76 void africa_free_ray(Africa_ray *r)
77 {
78   if (! r) return;
79   free(r);
80 }
81
82 void africa_free_sweep(Africa_sweep *s)
83 {
84   int i;
85   if (! s) return;
86   if (! s->ray) return;
87   for (i=0; i<s->nrays; i++) {
88         africa_free_ray(s->ray[i]);
89   }
90   free(s->ray);
91   free(s);
92 }
93
94 Africa_sweep *africa_read_sweep(FILE *fp)
95 {
96   /* This contains the next ray of data, except
97    * when this is the first ray.  This is a
98    * read-ahead buffer.
99    */
100   static Africa_buffer *buf = NULL; /* The read ahead buffer, too. */
101   Africa_sweep *sweep = NULL;
102   int cur_elev, ielev, iray;
103   Africa_ray *ray = NULL;
104   int nray;
105   
106   if (! buf)    {
107         buf = (Africa_buffer *) calloc(1, sizeof(Africa_buffer));
108         if (! buf) {
109           perror("allocate buf in read_sweep");
110           return NULL;
111         }
112         /* (pre)Read a record */
113         africa_read_buffer(fp, buf); /* allocates space for buffer */
114
115   }
116   /* Allocate 500 (should be enough) ray pointers. */
117   
118   sweep = africa_new_sweep(1000); /* 500 times 2 field types ? */
119   /* Determine the elevation step we're on so we know when to
120    * return the sweep.  Basically, the last read will be the 
121    * data for the next sweep; the meaning of the above code.
122    */
123   cur_elev = ielev = buf->raycount/512;
124   nray = 0;
125   while(cur_elev == ielev) {
126         iray  = buf->raycount - cur_elev*512;
127         if (iray != 0) {
128           ray = sweep->ray[nray];
129           if (!ray) sweep->ray[nray] = ray = africa_new_ray();
130           memcpy(ray, buf, sizeof(Africa_buffer));
131           nray++;
132         }
133         if (africa_read_buffer(fp, buf) == 0) break;
134         cur_elev = buf->raycount/512;
135         /*      isite    = buf->xmit_power_site & 0x1f; */
136   }
137   if (nray == 0) return NULL;
138   sweep->nrays = nray;
139
140   return sweep;
141 }