28#include "rave_debug.h"
29#include "rave_alloc.h"
44static int RaveFmiVolume_constructor(RaveCoreObject* obj)
59 if (img->
image != NULL) {
61 if (img->
image[i].heights != NULL) {
62 RAVE_FREE(img->
image[i].heights);
64 if (img->
image[i].array != NULL) {
65 RAVE_FREE(img->
image[i].array);
71 RAVE_FREE(img->
image);
80static void RaveFmiVolume_destructor(RaveCoreObject* obj)
83 RaveFmiVolumeInternal_resetImage(src);
91static int RaveFmiVolumeInternal_getSweepCount(PolarVolume_t* volume,
const char* quantity)
96 RAVE_ASSERT((volume != NULL),
"volume == NULL");
98 nrScans = PolarVolume_getNumberOfScans(volume);
103 for (i = 0; i < nrScans; i++) {
104 PolarScan_t* scan = PolarVolume_getScan(volume, i);
106 if (quantity == NULL || PolarScan_hasParameter(scan, quantity)) {
110 RAVE_OBJECT_RELEASE(scan);
123static int RaveFmiVolumeInternal_scanToFmiImage(PolarScan_t* scan,
const char* quantity,
FmiImage* image)
127 PolarScanParam_t* param = NULL;
129 RAVE_ASSERT((scan != NULL),
"scan == NULL");
130 RAVE_ASSERT((image != NULL),
"image == NULL");
132 image->width=PolarScan_getNbins(scan);
133 image->height=PolarScan_getNrays(scan);
134 image->bin_depth=PolarScan_getRscale(scan);
135 image->elevation_angle=PolarScan_getElangle(scan) * 180.0 / M_PI;
136 image->max_value=255;
139 initialize_image(image);
141 if (quantity == NULL) {
142 param = PolarScan_getParameter(scan,
"DBZH");
144 param = PolarScan_getParameter(scan, quantity);
148 RAVE_WARNING1(
"Failed to extract parameter %s from scan", quantity);
151 if (PolarScanParam_getDataType(param) != RaveDataType_CHAR &&
152 PolarScanParam_getDataType(param) != RaveDataType_UCHAR) {
153 RAVE_WARNING0(
"FmiImages can only support 8-bit data");
157 for (j = 0; j < image->height; j++) {
158 for (i = 0; i < image->width; i++) {
160 PolarScanParam_getValue(param, i, j, &value);
161 put_pixel(image, i, j, 0, (Byte)(value));
167 RAVE_OBJECT_RELEASE(param);
177static int RaveFmiVolumeInternal_fieldToFmiImage(RaveField_t* field,
FmiImage* image)
182 RAVE_ASSERT((field != NULL),
"field == NULL");
183 RAVE_ASSERT((image != NULL),
"image == NULL");
185 image->width=RaveField_getXsize(field);
186 image->height=RaveField_getYsize(field);
189 initialize_image(image);
191 if (RaveField_getDataType(field) != RaveDataType_CHAR &&
192 RaveField_getDataType(field) != RaveDataType_UCHAR) {
193 RAVE_WARNING0(
"FmiImages can only support 8-bit data");
197 for (j = 0; j < image->height; j++) {
198 for (i = 0; i < image->width; i++) {
200 RaveField_getValue(field, i, j, &value);
201 put_pixel(image, i, j, 0, (Byte)(value));
216static PolarScan_t* RaveFmiVolumeInternal_fmiImageToScan(
FmiImage* image,
const char* quantity)
218 PolarScan_t* scan = NULL;
219 PolarScan_t* result = NULL;
220 PolarScanParam_t* param = NULL;
221 int ray = 0, bin = 0;
222 RAVE_ASSERT((image != NULL),
"image == NULL");
224 scan = RAVE_OBJECT_NEW(&PolarScan_TYPE);
225 param = RAVE_OBJECT_NEW(&PolarScanParam_TYPE);
226 if (scan == NULL || param == NULL) {
227 RAVE_CRITICAL0(
"Failed to allocate memory for polar scan");
231 PolarScanParam_setGain(param, 1.0);
232 PolarScanParam_setOffset(param, 0.0);
233 PolarScanParam_setNodata(param, 255.0);
234 PolarScanParam_setUndetect(param, 0.0);
235 if (quantity != NULL) {
236 PolarScanParam_setQuantity(param, quantity);
238 PolarScanParam_setQuantity(param,
"DBZH");
240 if (!PolarScanParam_createData(param, image->width, image->height, RaveDataType_UCHAR)) {
241 RAVE_CRITICAL0(
"Failed to allocate memory for data");
245 for (ray = 0; ray < image->height; ray++) {
246 for (bin = 0; bin < image->width; bin++) {
247 PolarScanParam_setValue(param, bin, ray, (
double)get_pixel(image, bin, ray, 0));
250 if (!PolarScan_addParameter(scan, param)) {
251 RAVE_CRITICAL0(
"Failed to add parameter to scan");
254 PolarScan_setRscale(scan, image->bin_depth);
255 PolarScan_setElangle(scan, image->elevation_angle * M_PI / 180.0);
256 PolarScan_setRstart(scan, 0.0);
258 result = RAVE_OBJECT_COPY(scan);
260 RAVE_OBJECT_RELEASE(scan);
261 RAVE_OBJECT_RELEASE(param);
271 RAVE_ASSERT((self != NULL),
"self == NULL");
272 if (sweepCount <= 0) {
273 RAVE_ERROR0(
"You can not initialize a fmi image with sweepCount <= 0");
275 FmiImage* images = new_image(sweepCount);
276 if (images != NULL) {
277 RaveFmiVolumeInternal_resetImage(self);
278 self->
image = images;
288 RAVE_ASSERT((self != NULL),
"self == NULL");
294 RAVE_ASSERT((self != NULL),
"self == NULL");
300 RAVE_ASSERT((self != NULL),
"self == NULL");
301 if (sweep >= 0 && sweep < self->sweepCount) {
302 return &self->
image[sweep];
309 PolarVolume_t* result = NULL;
310 PolarVolume_t* volume = NULL;
311 PolarScan_t* scan = NULL;
315 RAVE_ASSERT((self != NULL),
"self == NULL");
317 volume = RAVE_OBJECT_NEW(&PolarVolume_TYPE);
318 if (volume == NULL) {
319 RAVE_CRITICAL0(
"Failed to create volume");
324 scan = RaveFmiVolumeInternal_fmiImageToScan(&self->
image[i], quantity);
326 RAVE_CRITICAL0(
"Failed to convert image to scan");
329 if (!PolarVolume_addScan(volume, scan)) {
330 RAVE_CRITICAL0(
"Failed to add scan to volume");
333 RAVE_OBJECT_RELEASE(scan);
336 result = RAVE_OBJECT_COPY(volume);
338 RAVE_OBJECT_RELEASE(scan);
339 RAVE_OBJECT_RELEASE(volume);
346 if (result != NULL) {
348 RAVE_OBJECT_RELEASE(result);
363 RAVE_ASSERT((volume != NULL),
"volume == NULL");
365 PolarVolume_sortByElevations(volume, 1);
366 nrScans = PolarVolume_getNumberOfScans(volume);
367 sweepCount = RaveFmiVolumeInternal_getSweepCount(volume, quantity);
369 if (sweepCount <= 0) {
370 RAVE_WARNING0(
"Volume does not contain any wanted parameters");
376 RAVE_CRITICAL0(
"Failed to create fmi image");
380 for (i = 0; i < nrScans; i++) {
381 PolarScan_t* scan = PolarVolume_getScan(volume, i);
382 if (quantity == NULL || PolarScan_hasParameter(scan, quantity)) {
383 if (!RaveFmiVolumeInternal_scanToFmiImage(scan, quantity, &image->
image[sweepIndex])) {
384 RAVE_ERROR0(
"Failed to convert scan to fmi image");
385 RAVE_OBJECT_RELEASE(scan);
390 RAVE_OBJECT_RELEASE(scan);
393 result = RAVE_OBJECT_COPY(image);
395 RAVE_OBJECT_RELEASE(image);
404 RAVE_ASSERT((scan != NULL),
"scan == NULL");
408 RAVE_CRITICAL0(
"Failed to create fmi image");
412 if (quantity == NULL || PolarScan_hasParameter(scan, quantity)) {
413 if (!RaveFmiVolumeInternal_scanToFmiImage(scan, quantity, &image->
image[0])) {
414 RAVE_ERROR0(
"Failed to convert scan to fmi image");
419 result = RAVE_OBJECT_COPY(image);
421 RAVE_OBJECT_RELEASE(image);
430 RAVE_ASSERT((field != NULL),
"field == NULL");
434 RAVE_CRITICAL0(
"Failed to create fmi image");
438 if (!RaveFmiVolumeInternal_fieldToFmiImage(field, &image->
image[0])) {
439 RAVE_ERROR0(
"Failed to convert rave field to fmi image");
443 result = RAVE_OBJECT_COPY(image);
445 RAVE_OBJECT_RELEASE(image);
451 RAVE_ASSERT((
object != NULL),
"object == NULL");
452 if (RAVE_OBJECT_CHECK_TYPE(
object, &PolarVolume_TYPE)) {
454 }
else if (RAVE_OBJECT_CHECK_TYPE(
object, &PolarScan_TYPE)) {
456 }
else if (RAVE_OBJECT_CHECK_TYPE(
object, &RaveField_TYPE)) {
459 RAVE_ERROR1(
"RaveFmiVolume_fromRave does not support %s", object->roh_type->name);
468 RaveFmiVolume_constructor,
469 RaveFmiVolume_destructor,
FmiImage * RaveFmiVolume_getImage(RaveFmiVolume_t *self)
int RaveFmiVolume_getSweepCount(RaveFmiVolume_t *self)
RaveFmiVolume_t * RaveFmiVolume_fromRaveField(RaveField_t *field)
RaveFmiVolume_t * RaveFmiVolume_fromRave(RaveCoreObject *object, const char *quantity)
RaveCoreObjectType RaveFmiVolume_TYPE
int RaveFmiVolume_initialize(RaveFmiVolume_t *self, int sweepCount)
RaveFmiVolume_t * RaveFmiVolume_fromPolarScan(PolarScan_t *scan, const char *quantity)
RaveFmiVolume_t * RaveFmiVolume_new(int sweepCount)
RaveFmiVolume_t * RaveFmiVolume_fromPolarVolume(PolarVolume_t *volume, const char *quantity)
PolarVolume_t * RaveFmiVolume_toRave(RaveFmiVolume_t *self, const char *quantity)
FmiImage * RaveFmiVolume_getSweep(RaveFmiVolume_t *self, int sweep)
struct _RaveFmiVolume_t RaveFmiVolume_t
RAVE_OBJECT_HEAD FmiImage * image