28#include "raveobject_hashtable.h"
29#include "rave_debug.h"
30#include "rave_alloc.h"
54 if (img->
image != NULL) {
55 if (img->
image->heights != NULL) {
56 RAVE_FREE(img->
image->heights);
58 if (img->
image->array != NULL) {
59 RAVE_FREE(img->
image->array);
64 RAVE_FREE(img->
image);
72static int RaveFmiImage_constructor(RaveCoreObject* obj)
79 this->image = new_image(1);
80 this->attrs = RAVE_OBJECT_NEW(&RaveObjectHashTable_TYPE);
81 if (this->image == NULL || this->attrs == NULL) {
87 RaveFmiImageInternal_resetImage(
this);
88 RAVE_OBJECT_RELEASE(this->attrs);
95static void RaveFmiImage_destructor(RaveCoreObject* obj)
98 RaveFmiImageInternal_resetImage(src);
99 RAVE_OBJECT_RELEASE(src->
attrs);
108static int RaveFmiImage_copyconstructor(RaveCoreObject* obj, RaveCoreObject* srcobj)
112 this->offset = src->
offset;
113 this->gain = src->
gain;
114 this->nodata = src->
nodata;
116 this->image = new_image(1);
117 this->attrs = RAVE_OBJECT_CLONE(src->
attrs);
118 if (this->image == NULL || this->attrs == NULL) {
121 canonize_image(src->
image, this->image);
124 RaveFmiImageInternal_resetImage(
this);
125 RAVE_OBJECT_RELEASE(this->attrs);
136static int RaveFmiImageInternal_scanToFmiImage(PolarScan_t* scan,
const char* quantity,
RaveFmiImage_t* raveimg)
141 PolarScanParam_t* param = NULL;
144 RAVE_ASSERT((scan != NULL),
"scan == NULL");
145 RAVE_ASSERT((raveimg != NULL),
"image == NULL");
149 image->width=PolarScan_getNbins(scan);
150 image->height=PolarScan_getNrays(scan);
151 image->bin_depth=PolarScan_getRscale(scan);
152 image->elevation_angle=PolarScan_getElangle(scan) * 180.0 / M_PI;
153 image->max_value=255;
155 initialize_image(image);
157 if (quantity == NULL) {
158 param = PolarScan_getParameter(scan,
"DBZH");
160 param = PolarScan_getParameter(scan, quantity);
164 RAVE_WARNING1(
"Failed to extract parameter %s from scan", quantity);
168 gain = PolarScanParam_getGain(param);
169 offset = PolarScanParam_getOffset(param);
181 image->original_type = PolarScanParam_getDataType(param);
183 if (image->original_type != RaveDataType_CHAR && image->original_type != RaveDataType_UCHAR) {
191 for (j = 0; j < image->height; j++) {
192 for (i = 0; i < image->width; i++) {
193 double value = 0.0, bvalue = 0.0;;
194 if (image->original_type == RaveDataType_CHAR || image->original_type == RaveDataType_UCHAR) {
195 PolarScanParam_getValue(param, i, j, &value);
199 RaveValueType t = PolarScanParam_getValue(param, i, j, &value);
200 if (t == RaveValueType_UNDETECT) {
202 }
else if (t == RaveValueType_NODATA) {
205 bvalue = ((value*gain + offset) - (-32.0))/0.5;
208 put_pixel(image, i, j, 0, (Byte)(bvalue));
209 put_pixel_orig(image, i, j, 0, value);
215 RAVE_OBJECT_RELEASE(param);
225static int RaveFmiImageInternal_fieldToFmiImage(RaveField_t* field,
RaveFmiImage_t* raveimg)
229 double nodata=255.0, undetect=0.0, gain=1.0, offset=0.0;
230 RaveAttribute_t* attr = NULL;
233 RAVE_ASSERT((field != NULL),
"field == NULL");
234 RAVE_ASSERT((raveimg != NULL),
"raveimg == NULL");
238 image->width=RaveField_getXsize(field);
239 image->height=RaveField_getYsize(field);
241 initialize_image(image);
243 attr = RaveField_getAttribute(field,
"what/gain");
245 RaveAttribute_getDouble(attr, &gain);
247 RAVE_OBJECT_RELEASE(attr);
248 attr = RaveField_getAttribute(field,
"what/offset");
250 RaveAttribute_getDouble(attr, &offset);
252 RAVE_OBJECT_RELEASE(attr);
253 attr = RaveField_getAttribute(field,
"what/nodata");
255 RaveAttribute_getDouble(attr, &nodata);
257 RAVE_OBJECT_RELEASE(attr);
258 attr = RaveField_getAttribute(field,
"what/undetect");
260 RaveAttribute_getDouble(attr, &undetect);
262 RAVE_OBJECT_RELEASE(attr);
264 image->original_type = RaveField_getDataType(field);
276 if (image->original_type != RaveDataType_CHAR && image->original_type != RaveDataType_UCHAR) {
284 for (j = 0; j < image->height; j++) {
285 for (i = 0; i < image->width; i++) {
286 double value = 0.0, bvalue = 0.0;;
288 if (image->original_type == RaveDataType_CHAR || image->original_type == RaveDataType_UCHAR) {
289 RaveField_getValue(field, i, j, &value);
293 RaveField_getValue(field, i, j, &value);
294 if (value == undetect) {
296 }
else if (value == nodata) {
299 bvalue = ((value*gain + offset) - (-32.0))/0.5;
303 put_pixel(image, i, j, 0, (Byte)(bvalue));
304 put_pixel_orig(image, i, j, 0, value);
318static PolarScan_t* RaveFmiImageInternal_fmiImageToScan(
RaveFmiImage_t* self,
const char* quantity,
int datatype)
320 PolarScan_t* scan = NULL;
321 PolarScan_t* result = NULL;
322 PolarScanParam_t* param = NULL;
323 int ray = 0, bin = 0;
326 RAVE_ASSERT((self != NULL),
"self == NULL");
330 scan = RAVE_OBJECT_NEW(&PolarScan_TYPE);
331 param = RAVE_OBJECT_NEW(&PolarScanParam_TYPE);
332 if (scan == NULL || param == NULL) {
333 RAVE_CRITICAL0(
"Failed to allocate memory for polar scan");
337 if (datatype != 2 && (image->original_type == RaveDataType_CHAR || image->original_type == RaveDataType_UCHAR || datatype == 1)) {
338 PolarScanParam_setGain(param, self->
gain);
339 PolarScanParam_setOffset(param, self->
offset);
340 PolarScanParam_setNodata(param, self->
nodata);
341 PolarScanParam_setUndetect(param, self->
undetect);
344 PolarScanParam_setGain(param, 0.5);
345 PolarScanParam_setOffset(param, -32.0);
346 PolarScanParam_setNodata(param, 255.0);
347 PolarScanParam_setUndetect(param, 0.0);
356 if (quantity != NULL) {
357 PolarScanParam_setQuantity(param, quantity);
359 PolarScanParam_setQuantity(param,
"DBZH");
363 if (!PolarScanParam_createData(param, image->width, image->height, image->original_type)) {
364 RAVE_CRITICAL0(
"Failed to allocate memory for data");
368 if (!PolarScanParam_createData(param, image->width, image->height, RaveDataType_UCHAR)) {
369 RAVE_CRITICAL0(
"Failed to allocate memory for data");
374 for (ray = 0; ray < image->height; ray++) {
375 for (bin = 0; bin < image->width; bin++) {
376 if (datatype != 2 && (image->original_type == RaveDataType_CHAR || image->original_type == RaveDataType_UCHAR || datatype == 1)) {
377 PolarScanParam_setValue(param, bin, ray, (
double)get_pixel(image, bin, ray, 0));
379 double v = (double)get_pixel_orig(image, bin, ray, 0);
381 if (v == image->original_nodata) {
383 }
else if (v == image->original_undetect) {
386 v = v*image->original_gain + v*image->original_offset;
387 v = (v - (-32.0))/0.5;
390 PolarScanParam_setValue(param, bin, ray, v);
394 if (!PolarScan_addParameter(scan, param)) {
395 RAVE_CRITICAL0(
"Failed to add parameter to scan");
398 PolarScan_setRscale(scan, image->bin_depth);
399 PolarScan_setElangle(scan, image->elevation_angle * M_PI / 180.0);
400 PolarScan_setRstart(scan, 0.0);
402 result = RAVE_OBJECT_COPY(scan);
404 RAVE_OBJECT_RELEASE(scan);
405 RAVE_OBJECT_RELEASE(param);
415static RaveField_t* RaveFmiImageInternal_fmiImageToField(
FmiImage* image,
int datatype)
417 RaveField_t* field = NULL;
418 RaveField_t* result = NULL;
419 int ray = 0, bin = 0;
420 RAVE_ASSERT((image != NULL),
"image == NULL");
422 field = RAVE_OBJECT_NEW(&RaveField_TYPE);
424 RAVE_CRITICAL0(
"Failed to allocate memory for field");
429 if (!RaveField_createData(field, image->width, image->height, image->original_type)) {
430 RAVE_CRITICAL0(
"Failed to allocate memory for data");
434 if (!RaveField_createData(field, image->width, image->height, RaveDataType_UCHAR)) {
435 RAVE_CRITICAL0(
"Failed to allocate memory for data");
440 for (ray = 0; ray < image->height; ray++) {
441 for (bin = 0; bin < image->width; bin++) {
442 if (datatype != 2 && (image->original_type == RaveDataType_CHAR || image->original_type == RaveDataType_UCHAR || datatype == 1)) {
443 RaveField_setValue(field, bin, ray, (
double)get_pixel(image, bin, ray, 0));
445 RaveField_setValue(field, bin, ray, (
double)get_pixel_orig(image, bin, ray, 0));
450 result = RAVE_OBJECT_COPY(field);
452 RAVE_OBJECT_RELEASE(field);
462 RAVE_ASSERT((self != NULL),
"self == NULL");
463 if (width <= 0 || height <= 0) {
464 RAVE_ERROR0(
"You can not initialize a fmi image with width or height = 0");
467 if (images != NULL) {
468 RaveFmiImageInternal_resetImage(self);
469 self->
image = images;
470 self->
image->width = width;
471 self->
image->height = height;
472 self->
image->channels = 1;
473 initialize_image(self->
image);
482 RAVE_ASSERT((self != NULL),
"self == NULL");
483 if (self->
image != NULL) {
484 fill_image(self->
image, (Byte)v);
487 for (i = 0; i < self->
image->volume; i++) {
496 RAVE_ASSERT((self != NULL),
"self == NULL");
497 if (self->
image != NULL) {
501 for (i = 0; i < nv; i++) {
510 RAVE_ASSERT((self != NULL),
"self == NULL");
516 PolarScan_t* result = NULL;
517 PolarScan_t* scan = NULL;
518 RaveObjectList_t* attributes = NULL;
523 RAVE_ASSERT((self != NULL),
"self == NULL");
525 scan = RaveFmiImageInternal_fmiImageToScan(self, quantity, datatype);
527 RAVE_CRITICAL0(
"Failed to convert image to scan");
531 attributes = RaveObjectHashTable_values(self->
attrs);
532 if (attributes == NULL) {
533 RAVE_CRITICAL0(
"Failed to get attributes");
536 nrAttrs = RaveObjectList_size(attributes);
537 for (i = 0; i < nrAttrs; i++) {
538 RaveAttribute_t* attr = (RaveAttribute_t*)RaveObjectList_get(attributes, i);
539 PolarScan_addAttribute(scan, attr);
540 RAVE_OBJECT_RELEASE(attr);
543 result = RAVE_OBJECT_COPY(scan);
545 RAVE_OBJECT_RELEASE(attributes);
546 RAVE_OBJECT_RELEASE(scan);
552 RaveField_t* result = NULL;
553 RaveField_t* field = NULL;
554 RaveObjectList_t* attributes = NULL;
559 RAVE_ASSERT((self != NULL),
"self == NULL");
561 field = RaveFmiImageInternal_fmiImageToField(&self->
image[0], datatype);
563 RAVE_CRITICAL0(
"Failed to convert image to field");
567 attributes = RaveObjectHashTable_values(self->
attrs);
568 if (attributes == NULL) {
569 RAVE_CRITICAL0(
"Failed to get attributes");
573 nrAttrs = RaveObjectList_size(attributes);
574 for (i = 0; i < nrAttrs; i++) {
575 RaveAttribute_t* attr = (RaveAttribute_t*)RaveObjectList_get(attributes, i);
576 RaveField_addAttribute(field, attr);
577 RAVE_OBJECT_RELEASE(attr);
580 result = RAVE_OBJECT_COPY(field);
582 RAVE_OBJECT_RELEASE(attributes);
583 RAVE_OBJECT_RELEASE(field);
591 const char* name = NULL;
594 RAVE_ASSERT((self != NULL),
"self == NULL");
596 name = RaveAttribute_getName(attribute);
597 if (!RaveAttributeHelp_extractGroupAndName(name, &gname, &aname)) {
598 RAVE_ERROR1(
"Failed to extract group and name from %s", name);
602 if ((strcasecmp(
"how", gname)==0 ||
603 strcasecmp(
"what", gname)==0 ||
604 strcasecmp(
"where", gname)==0) &&
605 strchr(aname,
'/') == NULL) {
606 result = RaveObjectHashTable_put(self->
attrs, name, (RaveCoreObject*)attribute);
617 RAVE_ASSERT((self != NULL),
"self == NULL");
619 RAVE_ERROR0(
"Trying to get an attribute with NULL name");
622 return (RaveAttribute_t*)RaveObjectHashTable_get(self->
attrs, name);
627 RAVE_ASSERT((self != NULL),
"self == NULL");
628 return RaveObjectHashTable_keys(self->
attrs);
633 RAVE_ASSERT((self != NULL),
"self == NULL");
639 RAVE_ASSERT((self != NULL),
"self == NULL");
645 RAVE_ASSERT((self != NULL),
"self == NULL");
651 RAVE_ASSERT((self != NULL),
"self == NULL");
657 RAVE_ASSERT((self != NULL),
"self == NULL");
663 RAVE_ASSERT((self != NULL),
"self == NULL");
669 RAVE_ASSERT((self != NULL),
"self == NULL");
675 RAVE_ASSERT((self != NULL),
"self == NULL");
681 RAVE_ASSERT((self != NULL),
"self == NULL");
682 self->
image->original_gain = gain;
687 RAVE_ASSERT((self != NULL),
"self == NULL");
688 return self->
image->original_gain;
693 RAVE_ASSERT((self != NULL),
"self == NULL");
694 self->
image->original_offset = offset;
699 RAVE_ASSERT((self != NULL),
"self == NULL");
700 return self->
image->original_offset;
705 RAVE_ASSERT((self != NULL),
"self == NULL");
706 self->
image->original_nodata = v;
711 RAVE_ASSERT((self != NULL),
"self == NULL");
712 return self->
image->original_nodata;
717 RAVE_ASSERT((self != NULL),
"self == NULL");
718 self->
image->original_undetect = v;
723 RAVE_ASSERT((self != NULL),
"self == NULL");
724 return self->
image->original_undetect;
730 if (result != NULL) {
732 RAVE_OBJECT_RELEASE(result);
741 PolarScan_t* scan = NULL;
745 RAVE_ASSERT((volume != NULL),
"volume == NULL");
747 nrScans = PolarVolume_getNumberOfScans(volume);
749 if (scannr < 0 || scannr >= nrScans) {
750 RAVE_ERROR1(
"There is no scan at index %d for this volume.", scannr);
756 RAVE_CRITICAL0(
"Failed to create fmi image");
760 scan = PolarVolume_getScan(volume, scannr);
762 RAVE_ERROR1(
"Could not read scan at index %d.", scannr);
765 if (quantity == NULL || PolarScan_hasParameter(scan, quantity)) {
766 if (!RaveFmiImageInternal_scanToFmiImage(scan, quantity, image)) {
767 RAVE_ERROR0(
"Failed to convert scan to fmi image");
772 result = RAVE_OBJECT_COPY(image);
774 RAVE_OBJECT_RELEASE(scan);
775 RAVE_OBJECT_RELEASE(image);
784 RAVE_ASSERT((scan != NULL),
"scan == NULL");
788 RAVE_CRITICAL0(
"Failed to create fmi image");
792 if (quantity == NULL || PolarScan_hasParameter(scan, quantity)) {
793 if (!RaveFmiImageInternal_scanToFmiImage(scan, quantity, image)) {
794 RAVE_ERROR0(
"Failed to convert scan to fmi image");
799 result = RAVE_OBJECT_COPY(image);
801 RAVE_OBJECT_RELEASE(image);
810 RAVE_ASSERT((field != NULL),
"field == NULL");
814 RAVE_CRITICAL0(
"Failed to create fmi image");
818 if (!RaveFmiImageInternal_fieldToFmiImage(field, image)) {
819 RAVE_ERROR0(
"Failed to convert rave field to fmi image");
823 result = RAVE_OBJECT_COPY(image);
825 RAVE_OBJECT_RELEASE(image);
831 RAVE_ASSERT((
object != NULL),
"object == NULL");
832 if (RAVE_OBJECT_CHECK_TYPE(
object, &PolarVolume_TYPE)) {
834 }
else if (RAVE_OBJECT_CHECK_TYPE(
object, &PolarScan_TYPE)) {
836 }
else if (RAVE_OBJECT_CHECK_TYPE(
object, &RaveField_TYPE)) {
839 RAVE_ERROR1(
"RaveFmiImage_fromRave does not support %s", object->roh_type->name);
848 RaveFmiImage_constructor,
849 RaveFmiImage_destructor,
850 RaveFmiImage_copyconstructor
FmiImage * RaveFmiImage_getImage(RaveFmiImage_t *self)
RaveFmiImage_t * RaveFmiImage_new(int width, int height)
void RaveFmiImage_fill(RaveFmiImage_t *self, unsigned char v)
double RaveFmiImage_getOriginalUndetect(RaveFmiImage_t *self)
double RaveFmiImage_getOffset(RaveFmiImage_t *self)
int RaveFmiImage_addAttribute(RaveFmiImage_t *self, RaveAttribute_t *attribute)
RaveFmiImage_t * RaveFmiImage_fromRave(RaveCoreObject *object, const char *quantity)
RaveAttribute_t * RaveFmiImage_getAttribute(RaveFmiImage_t *self, const char *name)
void RaveFmiImage_setGain(RaveFmiImage_t *self, double gain)
void RaveFmiImage_setOriginalGain(RaveFmiImage_t *self, double gain)
void RaveFmiImage_setOriginalNodata(RaveFmiImage_t *self, double v)
RaveField_t * RaveFmiImage_toRaveField(RaveFmiImage_t *self, int datatype)
RaveCoreObjectType RaveFmiImage_TYPE
void RaveFmiImage_fillOriginal(RaveFmiImage_t *self, double v)
PolarScan_t * RaveFmiImage_toPolarScan(RaveFmiImage_t *self, const char *quantity, int datatype)
void RaveFmiImage_setOriginalUndetect(RaveFmiImage_t *self, double v)
void RaveFmiImage_setNodata(RaveFmiImage_t *self, double v)
double RaveFmiImage_getUndetect(RaveFmiImage_t *self)
double RaveFmiImage_getGain(RaveFmiImage_t *self)
RaveList_t * RaveFmiImage_getAttributeNames(RaveFmiImage_t *self)
double RaveFmiImage_getOriginalNodata(RaveFmiImage_t *self)
RaveFmiImage_t * RaveFmiImage_fromPolarVolume(PolarVolume_t *volume, int scannr, const char *quantity)
void RaveFmiImage_setOriginalOffset(RaveFmiImage_t *self, double offset)
int RaveFmiImage_initialize(RaveFmiImage_t *self, int width, int height)
double RaveFmiImage_getNodata(RaveFmiImage_t *self)
double RaveFmiImage_getOriginalGain(RaveFmiImage_t *self)
void RaveFmiImage_setUndetect(RaveFmiImage_t *self, double v)
RaveFmiImage_t * RaveFmiImage_fromRaveField(RaveField_t *field)
double RaveFmiImage_getOriginalOffset(RaveFmiImage_t *self)
RaveFmiImage_t * RaveFmiImage_fromPolarScan(PolarScan_t *scan, const char *quantity)
void RaveFmiImage_setOffset(RaveFmiImage_t *self, double offset)
struct _RaveFmiImage_t RaveFmiImage_t
RAVE_OBJECT_HEAD FmiImage * image
RaveObjectHashTable_t * attrs