]> Pileus Git - ~andy/rsl/blob - africa.c
RSL v1.42
[~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 <string.h>
26 #include "africa.h"
27
28 int africa_read_buffer(FILE *fp, Africa_buffer *buffer)
29 {
30   int n;
31
32   n = fread(buffer, 1, sizeof(Africa_buffer), fp);
33   return n;
34 }
35
36 float africa_bcd_convert(unsigned short bcd)
37 {
38   int val;
39
40   val = 
41       ((bcd >> 12) & 0xf) * 1000
42         + ((bcd >> 8)  & 0xf) * 100
43         + ((bcd >> 4)  & 0xf) * 10
44         + ( bcd        & 0xf);
45   return val/10.0;
46 }
47
48 Africa_sweep * africa_new_sweep(int nray)
49 {
50   Africa_sweep *sweep;
51
52   sweep = (Africa_sweep *)calloc(nray, sizeof(Africa_sweep));
53   if (! sweep) {
54         perror("africa_new_sweep:sweep");
55         return NULL;
56   }
57   sweep->nrays = nray;
58   sweep->ray = (Africa_ray **) calloc(nray, sizeof(Africa_ray *));
59   if (! sweep -> ray) {
60         perror("africa_new_sweep:ray");
61         return NULL;
62   }
63   return sweep;
64 }
65
66 Africa_ray *africa_new_ray(void)
67 {
68   Africa_ray *ray;
69   ray = (Africa_ray *)calloc(1, sizeof(Africa_ray));
70   if (! ray) {
71         perror("africa_new_ray:ray");
72         return NULL;
73   }
74   return ray;
75 }
76   
77 void africa_free_ray(Africa_ray *r)
78 {
79   if (! r) return;
80   free(r);
81 }
82
83 void africa_free_sweep(Africa_sweep *s)
84 {
85   int i;
86   if (! s) return;
87   if (! s->ray) return;
88   for (i=0; i<s->nrays; i++) {
89         africa_free_ray(s->ray[i]);
90   }
91   free(s->ray);
92   free(s);
93 }
94
95 Africa_sweep *africa_read_sweep(FILE *fp)
96 {
97   /* This contains the next ray of data, except
98    * when this is the first ray.  This is a
99    * read-ahead buffer.
100    */
101   static Africa_buffer *buf = NULL; /* The read ahead buffer, too. */
102   Africa_sweep *sweep = NULL;
103   int cur_elev, ielev, iray;
104   Africa_ray *ray = NULL;
105   int nray;
106   
107   if (! buf)    {
108         buf = (Africa_buffer *) calloc(1, sizeof(Africa_buffer));
109         if (! buf) {
110           perror("allocate buf in read_sweep");
111           return NULL;
112         }
113         /* (pre)Read a record */
114         africa_read_buffer(fp, buf); /* allocates space for buffer */
115
116   }
117   /* Allocate 500 (should be enough) ray pointers. */
118   
119   sweep = africa_new_sweep(1000); /* 500 times 2 field types ? */
120   /* Determine the elevation step we're on so we know when to
121    * return the sweep.  Basically, the last read will be the 
122    * data for the next sweep; the meaning of the above code.
123    */
124   cur_elev = ielev = buf->raycount/512;
125   nray = 0;
126   while(cur_elev == ielev) {
127         iray  = buf->raycount - cur_elev*512;
128         if (iray != 0) {
129           ray = sweep->ray[nray];
130           if (!ray) sweep->ray[nray] = ray = africa_new_ray();
131           memcpy(ray, buf, sizeof(Africa_buffer));
132           nray++;
133         }
134         if (africa_read_buffer(fp, buf) == 0) break;
135         cur_elev = buf->raycount/512;
136         /*      isite    = buf->xmit_power_site & 0x1f; */
137   }
138   if (nray == 0) return NULL;
139   sweep->nrays = nray;
140
141   return sweep;
142 }