]> Pileus Git - ~andy/rsl/blob - fraction.c
RSL v1.44
[~andy/rsl] / fraction.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 /*  By: John Merritt                                                 */
25 /*      Space Applications Corporation                               */
26 /*      June 12, 1994                                                */
27 /**********************************************************************/
28
29
30 #include <stdio.h>
31 #include "rsl.h"
32
33 /**********************************************************************/
34 /*                                                                    */
35 /*                   RSL_fraction_of_ray                              */
36 /*                   RSL_fraction_of_sweep                            */
37 /*                   RSL_fraction_of_volume                           */
38 /*                                                                    */
39 /*   Compute fraction of dBz within a specified range.                */
40 /*   Loops are unrolled in each higher function because we are        */
41 /*   computing fractiion.  I thought about a returning a Ratio        */
42 /*   structure, but dismissed it.                                     */
43 /*                                                                    */
44 /**********************************************************************/
45 typedef struct {
46   int n;
47   int ntotal;
48 } Frac_ratio;
49
50
51 Frac_ratio RSL_ratio_of_ray(Ray *r, float lo, float hi, float range)
52 {
53   int i;
54   int ibin_range;  /* Maximum bin include, based on range. */
55   Frac_ratio fr;
56
57   fr.n = fr.ntotal = 0;
58
59   if (r == NULL) return fr;
60   fr.n = 0;
61   ibin_range = range /( (float)r->h.gate_size / 1000.0 );
62   if (ibin_range > r->h.nbins) ibin_range = r->h.nbins;
63   for (i=0; i<ibin_range; i++) {
64         if (lo <= r->h.f(r->range[i]) && r->h.f(r->range[i]) <= hi) fr.n++;
65         fr.ntotal++;
66   }
67   return fr;
68 }
69
70 float RSL_fraction_of_ray(Ray *r, float lo, float hi, float range)
71 {
72   Frac_ratio fr;
73
74   fr = RSL_ratio_of_ray(r, lo, hi, range);
75   return (float)fr.n / (float)fr.ntotal;
76 }
77
78
79 Frac_ratio RSL_ratio_of_sweep(Sweep *s, float lo, float hi, float range)
80 {
81   Frac_ratio total_ratio;
82   Frac_ratio ray_ratio;
83   int i;
84
85   total_ratio.n = total_ratio.ntotal = 0;
86   if (s == NULL) return total_ratio;
87   for (i = 0; i<s->h.nrays; i++) {
88         ray_ratio = RSL_ratio_of_ray(s->ray[i], lo, hi, range);
89         total_ratio.n      += ray_ratio.n;
90         total_ratio.ntotal += ray_ratio.ntotal;
91   }
92   return total_ratio;
93 }
94
95
96 float RSL_fraction_of_sweep(Sweep *s, float lo, float hi, float range)
97 {
98   Frac_ratio fr;
99
100   fr = RSL_ratio_of_sweep(s, lo, hi, range);
101   return (float)fr.n / (float)fr.ntotal;
102 }
103
104
105
106 Frac_ratio RSL_ratio_of_volume(Volume *v, float lo, float hi, float range)
107 {
108   Frac_ratio total_ratio;
109   Frac_ratio sweep_ratio;
110   int i;
111
112   total_ratio.n = total_ratio.ntotal = 0;
113   if (v == NULL) return total_ratio;
114   for (i = 0; i<v->h.nsweeps; i++) {
115         sweep_ratio = RSL_ratio_of_sweep(v->sweep[i], lo, hi, range);
116         total_ratio.n      += sweep_ratio.n;
117         total_ratio.ntotal += sweep_ratio.ntotal;
118   }
119   return total_ratio;
120 }
121
122
123 float RSL_fraction_of_volume(Volume *v, float lo, float hi, float range)
124 {
125   Frac_ratio fr;
126
127   fr = RSL_ratio_of_volume(v, lo, hi, range);
128   return (float)fr.n / (float)fr.ntotal;
129 }
130
131