ROPO
Loading...
Searching...
No Matches
ropo_hdf.c
1#include <stdio.h>
2#include <string.h>
3#include <assert.h>
4#include <rave_io.h>
5#include <polarvolume.h>
6#include <polarscan.h>
7#include <fmi_image.h>
8#include <fmi_radar_image.h>
9
10#include "ropo_hdf.h"
11
12int
13is_hdf_file(char *filename)
14{
15 FILE *fp;
16 char header[5];
17 fp = fopen(filename,"r");
18 assert(fp!=NULL);
19
20 fread(header, sizeof(char), 4, fp);
21
22 fclose(fp);
23
24 header[4] = '\0';
25 return (strcmp(header+1, "HDF") == 0);
26
27}
28
29/* Read a scan from the Rave polarvolume and put it into an FmiImage.
30 */
31
32int
33read_scan(RaveCoreObject *rave_pvol, FmiImage *pscan, int index)
34{
35 RaveCoreObject *rave_scan;
36 double value;
37 int i,j;
38 rave_scan=(RaveCoreObject *)PolarVolume_getScan((PolarVolume_t *)rave_pvol, index);
39
40 pscan->width=PolarScan_getNbins((PolarScan_t *)rave_scan);
41 pscan->height=PolarScan_getNrays((PolarScan_t *)rave_scan);
42 pscan->bin_depth=PolarScan_getRscale((PolarScan_t *)rave_scan);
43 pscan->elevation_angle=PolarScan_getElangle((PolarScan_t *)rave_scan);
44 pscan->max_value=255;
45 pscan->channels=1;
46 initialize_image(pscan);
47
48
49 for (j=0;j<pscan->height;j++)
50 for (i=0;i<pscan->width;i++)
51 {
52 PolarScan_getValue((PolarScan_t *)rave_scan, i, j, &value);
53 put_pixel(pscan, i, j, 0, (Byte)(value+0.5));
54 }
55 return 1;
56}
57
58/* Convert PolarVolume_t to FmiImage.
59 */
60
61int
62rave2ropo(PolarVolume_t *rave_pvol, FmiImage **pvol)
63{
64 int sweep_count, i;
65 FmiImage * current_pvol = NULL;
66 sweep_count=PolarVolume_getNumberOfScans(rave_pvol);
67 PolarVolume_sortByElevations(rave_pvol, 1);
68
69 current_pvol = *pvol;
70 for (i=0;i<sweep_count;i++)
71 {
72 read_scan((RaveCoreObject *)rave_pvol,&current_pvol[1+i],i);
73 }
74
75 return 1;
76}
77
78/* Open a Odim radar file, read it using Rave, and convert it to Ropo
79 format.
80 */
81
82PolarVolume_t * read_h5_radar_data(char *pvol_file, FmiImage **target){
83 PolarVolume_t *result = (PolarVolume_t *)RaveIO_getObject(RaveIO_open(pvol_file, 0, NULL));
84 rave2ropo(result, target);
85 return result;
86}
87
88
89PolarScan_t * create_scan(FmiImage *input, int index){
90 PolarScan_t* result = NULL;
91 PolarScan_t* scan = NULL;
92 PolarScanParam_t* parameter = NULL;
93 int ray = 0;
94 int bin = 0;
95 FmiImage *current_input = &input[index + 1];
96 scan = RAVE_OBJECT_NEW(&PolarScan_TYPE);
97 if (scan == NULL) {
98 goto error;
99 }
100
101 parameter = RAVE_OBJECT_NEW(&PolarScanParam_TYPE);
102 if (parameter == NULL) {
103 goto error;
104 }
105 PolarScan_setElangle(scan, fmi_radar_sweep_angles[index]);
106 PolarScan_setRscale(scan, input->bin_depth);
107 PolarScan_setRstart(scan, 0.0);
108
109 if (!PolarScanParam_createData(parameter,
110 current_input->width,
111 current_input->height,
112 RaveDataType_UCHAR)) {
113 goto error;
114 }
115
116 PolarScan_addParameter(scan, parameter);
117
118 for (ray = 0; ray < current_input->height; ray++) {
119 for (bin = 0; bin < current_input->width; bin++) {
120 PolarScan_setValue(scan, bin, ray, get_pixel(current_input, bin, ray, 0));
121 }
122 }
123 result = RAVE_OBJECT_COPY(scan);
124error:
125 RAVE_OBJECT_RELEASE(parameter);
126 RAVE_OBJECT_RELEASE(scan);
127 return result;
128}
129
130int
131fill_scan(FmiImage *input, PolarVolume_t *output, int index)
132{
133 PolarScan_t *scan = PolarVolume_getScan(output, index);
134 int ray, bin;
135 FmiImage *current_input = &input[index + 1];
136
137 PolarScan_setElangle(scan, fmi_radar_sweep_angles[index]/180.0 * M_PI);
138 PolarScan_setRscale(scan, input->bin_depth);
139 PolarScan_setRstart(scan, 0.0);
140 for (ray = 0; ray < current_input->height; ray++)
141 {
142 for (bin = 0; bin < current_input->width; bin++)
143 {
144 PolarScan_setValue(scan, bin, ray, get_pixel(current_input, bin, ray, 0));
145 }
146 }
147 return 1;
148
149
150}
151
152int
153ropo2rave(FmiImage *input, PolarVolume_t *output)
154{
155 int i = 0;
156
157 PolarVolume_sortByElevations(output, 1);
158
159 for(i=0; i < input->sweep_count; i++) {
160 fill_scan(input, output, i);
161 }
162 return 1;
163
164}
165
166
167
168int
169write_h5_radar_data(FmiImage *pvol, char *pvol_file, PolarVolume_t *template)
170{
171 RaveIO_t * raveio = NULL;
172 raveio = RAVE_OBJECT_NEW(&RaveIO_TYPE);
173
174 assert(raveio != NULL);
175 ropo2rave(pvol, template);
176 RaveIO_setObject(raveio, (RaveCoreObject*)template);
177 RaveIO_save(raveio, pvol_file);
178 return 1;
179}
180