]> Pileus Git - ~andy/rsl/blob - radtec_to_radar.c
Initial import
[~andy/rsl] / radtec_to_radar.c
1 /*
2     NASA/TRMM, Code 910.1.
3     This is the TRMM Office Radar Software Library.
4     Copyright (C) 1996, 1997, 1998
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 <string.h>
25 #include "rsl.h"
26
27 #ifdef HAVE_LIBIMPLODE
28 #include "radtec.h"
29
30 extern int radar_verbose_flag;
31
32 static void fill_ray_header(Ray_header *h, Radtec_header *rh, Radtec_ray_header *rrh)
33 {
34   
35   /* Fill the limited ray header information. */
36   h->month = rh->month; /* Time for this ray; month (1-12). */
37   h->day   = rh->day;   /* Time for this ray; day (1-31).   */
38   h->year  = rh->year;  /* Time for this ray; year (eg. 1993). */
39   h->hour  = rrh->hour;  /* Date for this ray; hour (0-23). */
40   h->minute = rrh->min;/* Date for this ray; minute (0-59).*/
41   h->sec   = rrh->sec; /* Date for this ray; second + fraction of second. */
42   h->azimuth  = rrh->azim_angle;   /* Azimuth angle. (degrees). Must be positive
43                                          * 0=North, 90=east, -90/270=west.
44                                          * This angle is the mean azimuth for the whole ray.
45                                          * Eg. for NSIG the beginning and end azimuths are
46                                          *     averaged.
47                                          */
48   h->ray_num = rrh->ray_num;    /* Ray no. within elevation scan. */
49   h->elev    = rrh->elev_angle;       /* Elevation angle. (degrees). */
50   
51   h->gate_size  = rh->range_bin_size * RSL_SPEED_OF_LIGHT / 1e6;  /* Data gate size (meters)*/
52   h->lat = rh->site_latitude;          /* Latitude (degrees) */
53   h->lon = rh->site_longitude;          /* Longitude (degrees) */
54   h->alt = rh->site_elevation;          /* Altitude (m) */
55   h->nbins = rh->num_range_bins;             /* Number of array elements for 'Range'. */
56   h->beam_width = rh->azim_resolution;  /* Beamwidth in degrees. */
57   
58 #ifdef RADTEC_UNKNOWN
59   h->unam_rng = 0;  /* Unambiguous range. (KM). */
60   h->elev_num = 0;   /* Elevation no. within volume scan. */
61   h->range_bin1 = 0; /* Range to first gate.(meters) */
62   h-> vel_res = 0;   /* Doppler velocity resolution */
63   h->sweep_rate = 0; /* Sweep rate. Full sweeps/min. */
64   h->prf = 0;          /* Pulse repetition frequency, in Hz. */
65   h->azim_rate;  /* degrees/sec */
66   h->fix_angle;
67   h->pitch;      /* Pitch angle. */
68   h->roll;       /* Roll  angle. */
69   h->heading;    /* Heading. */
70   h->pitch_rate; /* (angle/sec) */
71   h->roll_rate;  /* (angle/sec) */
72   h->heading_rate; /* (angle/sec) */
73   h->rvc;          /* Radial velocity correction (m/sec) */
74   h->vel_east;     /* Platform velocity to the east  (m/sec) */
75   h->vel_north;    /* Platform velocity to the north (m/sec) */
76   h->vel_up;       /* Platform velocity toward up    (m/sec) */
77   h->pulse_count; /* Pulses used in a single dwell time. */
78   h->pulse_width; /* Pulse width (micro-sec). */
79   h->frequency;   /* Carrier freq. GHz. */
80   h->wavelength;  /* Wavelength. Meters. */
81   h->nyq_vel;     /* Nyquist velocity. m/s */
82 #endif
83
84   return;
85 }
86
87
88 Radar *RSL_radtec_to_radar(char *infile)
89 {
90   Radar  *radar;
91   Volume *volume;
92   Sweep  *sweep;
93   Ray    *ray;
94   float (*f)(Range x);       /* Data conversion function. f(x). */
95   Range (*invf)(float x);    /* Data conversion function. invf(x). */
96
97   Radtec_file *rfile;
98   float tmp;
99   int nsweeps, nrays, nbins;
100   int isweep, iray, ibin;
101   float sum_elev;
102
103   rfile = radtec_read_file(infile);
104
105   if (radar_verbose_flag) {
106         radtec_print_header(&rfile->h);
107   }
108
109   /* Have the entire radtec file; headers and data.
110    * Initialize the RSL radar and load it up.
111    *
112    * Handles both RHI and PPI scan types.
113    */
114
115   /* Only one field type per file. */
116   radar = RSL_new_radar(MAX_RADAR_VOLUMES);
117
118   if (rfile->h.scan_type == 1) { /* PPI */
119         nrays   = (int)(360.0 / rfile->h.azim_resolution + 0.5);
120         nsweeps = (int)(rfile->h.num_rays / nrays + 0.5);
121   } else { /* RHI */
122         nrays   = 1;
123         nsweeps = (int)(rfile->h.num_rays / nrays + 0.5);
124         /* Each sweep will contain one ray for RHI scans. */
125   }
126   nbins = rfile->h.num_range_bins;
127
128   if (radar_verbose_flag) {
129         fprintf(stderr,"Expecting %d sweeps.\n", nsweeps);
130         fprintf(stderr,"Expecting %d rays.\n", nrays);
131         fprintf(stderr,"Expecting %d bins.\n", nbins);
132   }
133
134   if (rfile->h.scan_mode == 0) { /* Log video == Reflectivity. */
135         f    = DZ_F;
136         invf = DZ_INVF;
137         volume = radar->v[DZ_INDEX] = RSL_new_volume(nsweeps);
138         volume->h.type_str = strdup("Reflectivity");
139   } else { /* Doppler */
140         f    = VR_F;
141         invf = VR_INVF;
142         volume = radar->v[VR_INDEX] = RSL_new_volume(nsweeps);
143         volume->h.type_str = strdup("Velocity");
144   }
145   volume->h.f = f;
146   volume->h.invf = invf;
147
148   /* Load the radar header information. */
149   radar->h.month = rfile->h.month;
150   radar->h.day   = rfile->h.day;
151   radar->h.year  = rfile->h.year;
152   radar->h.hour  = rfile->h.hour;
153   radar->h.minute = rfile->h.min;
154   radar->h.sec    = rfile->h.sec; /* Second plus fractional part. */
155   sprintf(radar->h.radar_type,"radtec");
156
157   radar->h.number = rfile->h.version; /* arbitrary number of this radar site */
158   sprintf(radar->h.name,"radtec");      /* Nexrad site name */
159   sprintf(radar->h.radar_name,"RADTEC"); /* Radar name. */
160   sprintf(radar->h.city,"Unknown"); /* nearest city to  radar site */
161   sprintf(radar->h.state,"??");     /* state of radar site */
162   sprintf(radar->h.country,"???");
163
164    /** Latitude deg, min, sec **/
165   radar->h.latd = (int)rfile->h.site_latitude;
166   tmp = (rfile->h.site_latitude - radar->h.latd) * 60.0;
167   radar->h.latm = (int)tmp;
168   radar->h.lats = (int)((tmp - radar->h.latm) * 60.0);
169    /** Longitude deg, min, sec **/
170   radar->h.lond = (int)rfile->h.site_longitude;
171   tmp = (rfile->h.site_longitude - radar->h.lond) * 60.0;
172   radar->h.lonm = (int)tmp;
173   radar->h.lons = (int)((tmp - radar->h.lonm) * 60.0);
174   radar->h.height = rfile->h.site_elevation; /* height of site in meters above sea level*/
175   radar->h.spulse = 0; /* length of short pulse (ns)*/
176   radar->h.lpulse = 0; /* length of long pulse (ns) */
177
178   /* Loop through the RSL number of sweeps and attach the data. */
179   for (isweep = 0; isweep < nsweeps; isweep++) {
180         sweep = volume->sweep[isweep] = RSL_new_sweep(nrays);
181         sweep->h.f = f;
182         sweep->h.invf = invf;
183         sum_elev = 0;
184         for (iray = 0; iray < nrays; iray++) {
185           ray = sweep->ray[iray] = RSL_new_ray(nbins);
186           ray->h.f = f;
187           ray->h.invf = invf;
188           fill_ray_header(&ray->h, &rfile->h, rfile->ray[iray].h);
189           RSL_fix_time(ray);
190           sum_elev += ray->h.elev;
191           /* Fill the data. */
192           for (ibin=0; ibin < nbins; ibin++) {
193                 /*              printf ("ray[%d].dbz[%d] = %f\n", iray, ibin, rfile->ray[iray].dbz[ibin]); */
194                 ray->range[ibin] = invf(rfile->ray[iray].dbz[ibin]);
195           }
196         }
197         sweep->h.elev = sum_elev / nrays;
198     sweep->h.beam_width = rfile->h.azim_resolution;  /* This is in the ray header too. */
199     sweep->h.vert_half_bw = sweep->h.beam_width/2;  /* Vertical beam width divided by 2 */
200     sweep->h.horz_half_bw = sweep->h.beam_width/2;  /* Horizontal beam width divided by 2 */
201   }
202
203   radtec_free_file(rfile);
204   rfile = NULL;
205
206   return radar;
207 }
208 #else
209 Radar *RSL_radtec_to_radar(char *infile)
210 {
211   fprintf(stderr, "RADTEC is not installed in this version of RSL.\n");
212   fprintf(stderr, "The library libimplode.a (or .so) was not found during\n");
213   fprintf(stderr, "the RSL configuration.\n");
214   return NULL;
215 }
216 #endif