]> Pileus Git - ~andy/rsl/blob - sort_rays.c
Changes from Bart (2009-10-28)
[~andy/rsl] / sort_rays.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996, 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 /*-----------------------------------------------------------------*/
24 /*                                                                 */
25 /*            ray_sort_compare                                     */
26 /*            RSL_sort_rays_in_sweep                               */
27 /*            RSL_sort_rays_in_volume                              */
28 /*                                                                 */
29 /*  By: John Merritt                                               */
30 /*      Space Applications Corporation                             */
31 /*      August 26, 1994                                            */
32 /*
33  * Update and Revisions:
34  *
35  *      New Routines:
36  *        int     ray_sort_compare_by_time(Ray **r1,Ray **r2);
37  *        int     sweep_sort_compare(Sweep **s1, Sweep **s2)
38  *        Sweep  *RSL_sort_rays_by_time(Sweep *s);
39  *        Volume *RSL_sort_sweeps_in_volume(Volume *v)
40  *        Volume *RSL_sort_volume(Volume *v)
41  *        Radar  *RSL_sort_radar(Radar *r)
42  *
43  *      Modifications:
44  *         Routines that sort data structures now set the 
45  *         number of structures in the parent data structure.
46  *    
47  *         Routines set the enum sorted variable in the
48  *         appropriate data structure.
49  *
50  *      Dennis Flanigan,Jr.
51  *      3/23/95
52  */
53 /*-----------------------------------------------------------------*/
54
55 #include <stdlib.h>
56 #include "rsl.h"
57
58 static int ray_sort_compare(Ray **r1, Ray **r2)
59    {
60    /* Compare azim values.  Return -1, 0, 1 for <, =, > comparison. */
61    if (*r1 == NULL) return 1;
62    if (*r2 == NULL) return -1;
63
64    if ((*r1)->h.azimuth < (*r2)->h.azimuth) return -1;
65    if ((*r1)->h.azimuth > (*r2)->h.azimuth) return 1;
66    return 0;
67    }
68
69
70 static int ray_sort_compare_by_time(Ray **r1, Ray **r2)
71    {
72    /* Compare time values.  Return -1, 0, 1 for <, =, > comparison. */
73
74    if (*r1 == NULL) return 1;
75    if (*r2 == NULL) return -1;
76
77    /* Compare year value */
78    if ((*r1)->h.year < (*r2)->h.year) return -1;
79    if ((*r1)->h.year > (*r2)->h.year) return 1;
80
81    /* Compare month value */
82    if ((*r1)->h.month < (*r2)->h.month) return -1;
83    if ((*r1)->h.month > (*r2)->h.month) return 1;
84
85    /* Compare day value */
86    if ((*r1)->h.day < (*r2)->h.day) return -1;
87    if ((*r1)->h.day > (*r2)->h.day) return 1;
88
89    /* Compare hour value */
90    if ((*r1)->h.hour < (*r2)->h.hour) return -1;
91    if ((*r1)->h.hour > (*r2)->h.hour) return 1;
92
93    /* Compare minute value */
94    if ((*r1)->h.minute < (*r2)->h.minute) return -1;
95    if ((*r1)->h.minute > (*r2)->h.minute) return 1;
96    
97    /* Compare second value */
98    if ((*r1)->h.sec < (*r2)->h.sec) return -1;
99    if ((*r1)->h.sec > (*r2)->h.sec) return 1;
100
101    return 0;
102    }
103
104 static int sweep_sort_compare(Sweep **s1, Sweep **s2)
105    {
106    /* Compare elevation values.  Return -1, 0, 1 for <, =, > comparison. */
107    if (*s1 == NULL) return 1;
108    if (*s2 == NULL) return -1;
109
110    if ((*s1)->h.elev < (*s2)->h.elev) return -1;
111    if ((*s1)->h.elev > (*s2)->h.elev) return 1;
112    return 0;
113    }
114
115 Sweep *RSL_sort_rays_in_sweep(Sweep *s)
116    {
117    /* Sort rays by azimuth in passed sweep */
118    int a;
119    
120    if (s == NULL) return NULL;
121
122    qsort((void *)s->ray, s->h.nrays, sizeof(Ray *),
123                  (int (*)(const void *, const void *))ray_sort_compare);
124
125    /* Set nrays values to number of non-NULL indexes.
126     * After sorting this is highest useable index.
127         */
128    for(a=s->h.nrays-1; a>=0 ;a--) {
129          if(s->ray[a] != NULL) {
130            s->h.nrays = a+1;
131            break;
132          }
133    }
134
135    return s;
136    }
137
138 Sweep *RSL_sort_rays_by_time(Sweep *s)
139    {
140    /* Set rays in passed sweep by time */
141    int a;
142    
143    if (s == NULL) return NULL;
144
145    qsort((void *)s->ray, s->h.nrays, sizeof(Ray *), 
146                  (int (*)(const void *, const void *))ray_sort_compare_by_time);
147
148    /* Set nrays values to number of non-NULL indexes.
149     * After sorting this is highest useable index.
150         */
151    for(a=s->h.nrays-1; a>=0 ;a--) {
152          if(s->ray[a] != NULL) {
153            s->h.nrays = a+1;
154            break;
155          }
156    }
157
158    return s;
159    }
160
161
162 Volume *RSL_sort_rays_in_volume(Volume *v)
163    {
164    /* Sort rays in the sweeps pointed to by the volume .
165         * (Does not sort the sweeps pointers)
166         */
167    int i;
168    if (v == NULL) return NULL;
169  
170    for (i=0; i<v->h.nsweeps; i++)
171    v->sweep[i] = RSL_sort_rays_in_sweep(v->sweep[i]);
172
173    return v;
174    }
175
176 Volume *RSL_sort_sweeps_in_volume(Volume *v)
177    {
178    /* Sort sweeps pointers in passed volume data structure. 
179         * (Does not sort rays in sweeps.)
180         */
181    int a;
182    
183    if (v == NULL) return NULL;
184    
185    qsort((void *)v->sweep,v->h.nsweeps,sizeof(Sweep *),
186                  (int (*)(const void *, const void *))sweep_sort_compare);
187
188    /* Set nsweeps value to number of non-NULL indexes.
189     * After sorting, this is the highest usable index.
190         */
191    for(a=0;a<v->h.nsweeps;a++)
192           {
193           if(v->sweep[a] == NULL)
194                  {
195                  v->h.nsweeps = a;
196                  break;
197                  }
198           }
199    
200    return v;
201    }
202
203 Volume *RSL_sort_volume(Volume *v)
204    {
205    /* Sort sweeps and rays by angle in the Volume data structure
206         * passed.
207         */
208    
209    if(v == NULL) return NULL;
210
211    v = RSL_sort_sweeps_in_volume(v);
212    v = RSL_sort_rays_in_volume(v);
213
214    return v;
215    }
216
217
218 Radar *RSL_sort_radar(Radar *r)
219    {
220    /* Sort sweeps and rays in all Volumes of the passed Radar data 
221         * structure.
222         */
223    int a;
224    
225    if(r == NULL) return NULL;
226    
227    for(a=0; a<r->h.nvolumes;a++)
228           {
229           r->v[a] = RSL_sort_volume(r->v[a]);
230           }
231
232    return r;
233    }
234
235