00001 #include "drms.h"
00002
00003
00004
00005
00006
00007 DRMS_Array_t *drms_array_create(DRMS_Type_t type, int naxis,
00008 int *axis, void *data, int *status)
00009 {
00010 int i, stat=0;
00011 DRMS_Array_t *arr;
00012
00013 arr = malloc(sizeof(DRMS_Array_t));
00014 XASSERT(arr);
00015 memset(arr,0,sizeof(DRMS_Array_t));
00016
00017
00018
00019 arr->bscale = 1.0;
00020 arr->bzero = 0.0;
00021
00022 if (naxis <= DRMS_MAXRANK)
00023 {
00024 arr->naxis = naxis;
00025 }
00026 else
00027 {
00028 stat = DRMS_ERROR_INVALIDRANK;
00029 goto bailout;
00030 }
00031 if (type != DRMS_TYPE_RAW)
00032
00033 arr->type = type;
00034 else
00035 {
00036 stat = DRMS_ERROR_INVALIDTYPE;
00037 goto bailout;
00038 }
00039 memcpy(arr->axis,axis,naxis*sizeof(int));
00040 if (data)
00041 arr->data = data;
00042 else
00043 {
00044 arr->data = malloc(drms_array_size(arr));
00045 XASSERT(arr->data);
00046 }
00047
00048
00049 arr->dope[0] = drms_sizeof(arr->type);
00050 for (i=1; i<arr->naxis; i++)
00051 arr->dope[i] = arr->dope[i-1]*arr->axis[i-1];
00052
00053 if (status)
00054 *status = stat;
00055 return arr;
00056
00057 bailout:
00058 free(arr);
00059 if (status)
00060 *status = stat;
00061 return NULL;
00062 }
00063
00064
00065 void drms_free_array(DRMS_Array_t *arr)
00066 {
00067 if (arr)
00068 {
00069 if (arr->data)
00070 free(arr->data);
00071 free(arr);
00072 }
00073 }
00074
00075
00076 void drms_array_convert_inplace(DRMS_Type_t newtype, double bzero,
00077 double bscale, DRMS_Array_t *src)
00078 {
00079 DRMS_Array_t *tmp;
00080
00081 if (newtype != src->type || fabs(bzero)!=0.0 || bscale!=1.0)
00082 {
00083 tmp = drms_array_convert(newtype, bzero, bscale, src);
00084 src->type = newtype;
00085 free(src->data);
00086 src->data = tmp->data;
00087 free(tmp);
00088 }
00089 }
00090
00091
00092
00093 DRMS_Array_t *drms_array_convert(DRMS_Type_t dsttype, double bzero,
00094 double bscale, DRMS_Array_t *src)
00095 {
00096 arraylen_t n;
00097 DRMS_Array_t *arr;
00098
00099 arr = drms_array_create(dsttype, src->naxis, src->axis, NULL, NULL);
00100 n = drms_array_count(src);
00101 drms_array_rawconvert(n, dsttype, bzero, bscale, arr->data,
00102 src->type, src->data);
00103 return arr;
00104 }
00105
00106
00107
00108
00109 DRMS_Array_t *drms_array_slice(int *start, int *end, DRMS_Array_t *src)
00110 {
00111 int i;
00112 DRMS_Array_t *arr;
00113 int axis[DRMS_MAXRANK];
00114
00115
00116 for (i=0; i<src->naxis; i++)
00117 {
00118 if (start[i]>end[i])
00119 {
00120 fprintf(stderr,"ERROR in drms_array_slice: end[%d]=%d > start[%d]=%d\n",
00121 i,end[i],i,start[i]);
00122 return NULL;
00123 }
00124 axis[i] = end[i]-start[i]+1;
00125 }
00126 arr = drms_array_create(src->type, src->naxis, axis, NULL, NULL);
00127
00128 ndim_pack(drms_sizeof(src->type), src->naxis, src->axis, start, end,
00129 src->data, arr->data);
00130 return arr;
00131 }
00132
00133
00134
00135
00136
00137 DRMS_Array_t *drms_array_permute(DRMS_Array_t *src, int *perm, int *status)
00138 {
00139 int i, stat;
00140 DRMS_Array_t *dst;
00141
00142 dst = drms_array_create(src->type, src->naxis, src->axis, NULL, &stat);
00143 if (stat)
00144 goto bailout;
00145 stat = ndim_permute(drms_sizeof(src->type), src->naxis, src->axis, perm,
00146 src->data, dst->data);
00147 if (stat)
00148 goto bailout;
00149 else
00150 for (i=0; i<dst->naxis; i++)
00151 dst->axis[i] = src->axis[perm[i]];
00152 return dst;
00153
00154 bailout:
00155 if (status)
00156 *status = stat;
00157 return NULL;
00158 }
00159
00160
00161 int drms_array_rawconvert(arraylen_t n, DRMS_Type_t dsttype, double bzero,
00162 double bscale, void *dst, DRMS_Type_t srctype,
00163 void *src)
00164 {
00165 int stat=-1;
00166
00167 switch(dsttype)
00168 {
00169 case DRMS_TYPE_CHAR:
00170 stat = drms_array2char(n, srctype, bzero, bscale, src, (char *) dst);
00171 break;
00172 case DRMS_TYPE_SHORT:
00173 stat = drms_array2short(n, srctype, bzero, bscale, src, (short *) dst);
00174 break;
00175 case DRMS_TYPE_INT:
00176 stat = drms_array2int(n, srctype, bzero, bscale, src, (int *) dst);
00177 break;
00178 case DRMS_TYPE_LONGLONG:
00179 stat = drms_array2longlong(n, srctype, bzero, bscale, src, (long long *) dst);
00180 break;
00181 case DRMS_TYPE_FLOAT:
00182 stat = drms_array2float(n, srctype, bzero, bscale, src, (float *) dst);
00183 break;
00184 case DRMS_TYPE_DOUBLE:
00185 stat = drms_array2double(n, srctype, bzero, bscale, src, (double *) dst);
00186 break;
00187 case DRMS_TYPE_TIME:
00188 stat = drms_array2time(n, srctype, bzero, bscale, src, (double *) dst);
00189 break;
00190 case DRMS_TYPE_STRING:
00191 stat = drms_array2string(n, srctype, bzero, bscale, src, (char **) dst);
00192 break;
00193 default:
00194 fprintf(stderr, "ERROR: Unhandled DRMS type %d\n",(int)dsttype);
00195 }
00196 return stat;
00197 }
00198
00199
00200
00201 void drms_array2missing(DRMS_Array_t *arr)
00202 {
00203 arraylen_t i;
00204 arraylen_t n;
00205
00206 n = drms_array_count(arr);
00207 switch(arr->type)
00208 {
00209 case DRMS_TYPE_CHAR:
00210 {
00211 char *ssrc = (char *) (arr->data);
00212 for (i=0; i<n; i++, ssrc++)
00213 *ssrc = DRMS_MISSING_CHAR;
00214 }
00215 break;
00216 case DRMS_TYPE_SHORT:
00217 {
00218 short *ssrc = (short *) (arr->data);
00219 for (i=0; i<n; i++, ssrc++)
00220 *ssrc = DRMS_MISSING_SHORT;
00221 }
00222 break;
00223 case DRMS_TYPE_INT:
00224 {
00225 int *ssrc = (int *) (arr->data);
00226 for (i=0; i<n; i++, ssrc++)
00227 *ssrc = DRMS_MISSING_INT;
00228 }
00229 break;
00230 case DRMS_TYPE_LONGLONG:
00231 {
00232 long long *ssrc = (long long *) (arr->data);
00233 for (i=0; i<n; i++, ssrc++)
00234 *ssrc = DRMS_MISSING_LONGLONG;
00235 }
00236 break;
00237 case DRMS_TYPE_FLOAT:
00238 {
00239 float *ssrc = (float *) (arr->data);
00240 for (i=0; i<n; i++, ssrc++)
00241 *ssrc = DRMS_MISSING_FLOAT;
00242 }
00243 break;
00244 case DRMS_TYPE_DOUBLE:
00245 {
00246 double *ssrc = (double *) (arr->data);
00247 for (i=0; i<n; i++, ssrc++)
00248 *ssrc = DRMS_MISSING_DOUBLE;
00249 }
00250 break;
00251 case DRMS_TYPE_TIME:
00252 {
00253 double *ssrc = (double *) (arr->data);
00254 for (i=0; i<n; i++, ssrc++)
00255 *ssrc = DRMS_MISSING_TIME;
00256 }
00257 break;
00258 case DRMS_TYPE_STRING:
00259 {
00260 char **ssrc = (char **) (arr->data);
00261 for (i=0; i<n; i++)
00262 ssrc[i] = NULL;
00263
00264 }
00265 break;
00266 default:
00267 break;
00268 }
00269 }
00270
00271
00272
00273
00274
00275 void drms_array_print(DRMS_Array_t *arr, const char *colsep, const char *rowsep)
00276 {
00277 drms_array_fprint(stdout, arr, colsep, rowsep);
00278 }
00279
00280
00281
00282 void drms_array_fprint(FILE *keyfile, DRMS_Array_t *arr, const char *colsep,
00283 const char *rowsep)
00284 {
00285 int j, k, sz, index[DRMS_MAXRANK];
00286 unsigned char *p;
00287 arraylen_t count;
00288 arraylen_t i;
00289
00290 sz = drms_sizeof(arr->type);
00291 p = arr->data;
00292 switch(arr->naxis)
00293 {
00294 case 1:
00295 arr->axis[1]=1;
00296 case 2:
00297 for (i=0; i<arr->axis[1]; i++)
00298 {
00299 drms_fprintfval_raw(keyfile, arr->type, p);
00300 p += sz;
00301 for (j=1; j<arr->axis[0]; j++, p += sz)
00302 {
00303 fprintf(keyfile,"%s",colsep);
00304 drms_fprintfval_raw(keyfile, arr->type, p);
00305 }
00306 fprintf(keyfile, "%s",rowsep);
00307 }
00308 break;
00309 default:
00310 count = drms_array_count(arr);
00311 #ifdef DEBUG
00312 printf("count = %lld, sz = %d\n",count,sz);
00313 #endif
00314 memset(index,0,arr->naxis*sizeof(int));
00315
00316 i = 0;
00317 while ( i<count )
00318 {
00319 fprintf(keyfile, "array");
00320 for (j=arr->naxis-1; j>1; j--)
00321 fprintf(keyfile, "[%d]",index[j]);
00322 fprintf(keyfile, "[*][*] = \n");
00323 for (j=2; j<arr->naxis-1; j++)
00324 {
00325 if (index[j]==arr->axis[j]-1)
00326 {
00327 index[j] = 0;
00328 index[j+1] += 1;
00329 }
00330 else
00331 index[j] += 1;
00332 }
00333 for (k=0; k<arr->axis[1]; k++)
00334 {
00335 drms_fprintfval_raw(keyfile, arr->type, p);
00336 p += sz;
00337 i++;
00338 for (j=1; j<arr->axis[0]; j++, i++)
00339 {
00340 fprintf(keyfile, "%s",colsep);
00341 drms_fprintfval_raw(keyfile, arr->type, p);
00342 p += sz;
00343 }
00344 fprintf(keyfile,"%s",rowsep);
00345 }
00346 }
00347 }
00348 }
00349
00350
00351
00352
00353
00354 int drms_array2char(arraylen_t n, DRMS_Type_t src_type, double bzero, double bscale,
00355 void *src, char *dst)
00356 {
00357 int stat;
00358 arraylen_t i;
00359 double value;
00360 double rangechk;
00361
00362 if (bzero == 0.0 && bscale==1.0)
00363 {
00364 switch(src_type)
00365 {
00366 case DRMS_TYPE_CHAR:
00367 stat = DRMS_SUCCESS;
00368 memcpy(dst,src,n);
00369 break;
00370 case DRMS_TYPE_SHORT:
00371 {
00372 short *ssrc = (short *) src;
00373 stat = DRMS_SUCCESS;
00374 for (i=0; i<n; i++, ssrc++, dst++)
00375 {
00376 if (*ssrc == DRMS_MISSING_SHORT)
00377 *dst = DRMS_MISSING_CHAR;
00378 else if (!(*ssrc < SCHAR_MIN || *ssrc > SCHAR_MAX))
00379 *dst = (char) *ssrc;
00380 else
00381 {
00382 stat = DRMS_RANGE;
00383 *dst = DRMS_MISSING_CHAR;
00384 }
00385 }
00386 }
00387 break;
00388 case DRMS_TYPE_INT:
00389 {
00390 int *ssrc = (int *) src;
00391 stat = DRMS_SUCCESS;
00392 for (i=0; i<n; i++, ssrc++, dst++)
00393 {
00394 if (*ssrc == DRMS_MISSING_INT)
00395 *dst = DRMS_MISSING_CHAR;
00396 else if (!(*ssrc < SCHAR_MIN || *ssrc > SCHAR_MAX))
00397 *dst = (char) *ssrc;
00398 else
00399 {
00400 stat = DRMS_RANGE;
00401 *dst = DRMS_MISSING_CHAR;
00402 }
00403 }
00404 }
00405 break;
00406 case DRMS_TYPE_LONGLONG:
00407 {
00408 long long *ssrc = (long long *) src;
00409 stat = DRMS_SUCCESS;
00410 for (i=0; i<n; i++, ssrc++, dst++)
00411 {
00412 if (*ssrc == DRMS_MISSING_LONGLONG)
00413 *dst = DRMS_MISSING_CHAR;
00414 else if (!(*ssrc < SCHAR_MIN || *ssrc > SCHAR_MAX))
00415 *dst = (char) *ssrc;
00416 else
00417 {
00418 stat = DRMS_RANGE;
00419 *dst = DRMS_MISSING_CHAR;
00420 }
00421 }
00422 }
00423 break;
00424 case DRMS_TYPE_FLOAT:
00425 {
00426 float *ssrc = (float *) src;
00427 stat = DRMS_SUCCESS;
00428 for (i=0; i<n; i++, ssrc++, dst++)
00429 {
00430 if (isnan(*ssrc))
00431 *dst = DRMS_MISSING_CHAR;
00432 else
00433 {
00434 rangechk = round(*ssrc);
00435 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00436 *dst = (char)rangechk;
00437 else
00438 {
00439 stat = DRMS_RANGE;
00440 *dst = DRMS_MISSING_CHAR;
00441 }
00442 }
00443 }
00444 }
00445 break;
00446 case DRMS_TYPE_TIME:
00447 {
00448 double *ssrc = (double *) src;
00449 stat = DRMS_SUCCESS;
00450 for (i=0; i<n; i++, ssrc++, dst++)
00451 {
00452 if (*ssrc == DRMS_MISSING_TIME)
00453 *dst = DRMS_MISSING_CHAR;
00454 else
00455 {
00456 rangechk = round(*ssrc);
00457 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00458 *dst = (char)rangechk;
00459 else
00460 {
00461 stat = DRMS_RANGE;
00462 *dst = DRMS_MISSING_CHAR;
00463 }
00464 }
00465 }
00466 }
00467 case DRMS_TYPE_DOUBLE:
00468 {
00469 double *ssrc = (double *) src;
00470 stat = DRMS_SUCCESS;
00471 for (i=0; i<n; i++, ssrc++, dst++)
00472 {
00473 if (isnan(*ssrc))
00474 *dst = DRMS_MISSING_CHAR;
00475 else
00476 {
00477 rangechk = round(*ssrc);
00478 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00479 *dst = (char)rangechk;
00480 else
00481 {
00482 stat = DRMS_RANGE;
00483 *dst = DRMS_MISSING_CHAR;
00484 }
00485 }
00486 }
00487 }
00488 break;
00489 case DRMS_TYPE_STRING:
00490 {
00491 double val;
00492 char *endptr;
00493 char **ssrc = (char **) src;
00494 stat = DRMS_SUCCESS;
00495 for (i=0; i<n; i++, ssrc++, dst++)
00496 {
00497 val = strtod(*ssrc,&endptr);
00498 if (val==0 && endptr==*ssrc )
00499 {
00500 stat = DRMS_BADSTRING;
00501 *dst = DRMS_MISSING_CHAR;
00502 }
00503 else
00504 {
00505 rangechk = round(val);
00506 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00507 *dst = (char)rangechk;
00508 else
00509 {
00510 stat = DRMS_RANGE;
00511 *dst = DRMS_MISSING_CHAR;
00512 }
00513 }
00514 }
00515 }
00516 break;
00517 default:
00518 stat = DRMS_ERROR_INVALIDTYPE;
00519 }
00520
00521 }
00522 else
00523 {
00524 switch(src_type)
00525 {
00526 case DRMS_TYPE_CHAR:
00527 {
00528 char *ssrc = (char *) src;
00529 stat = DRMS_SUCCESS;
00530 for (i=0; i<n; i++, ssrc++, dst++)
00531 {
00532 if (*ssrc==DRMS_MISSING_CHAR)
00533 *dst = DRMS_MISSING_CHAR;
00534 else
00535 {
00536 value = bscale* *ssrc + bzero;
00537 rangechk = round(value);
00538 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00539 *dst = (char)rangechk;
00540 else
00541 {
00542 stat = DRMS_RANGE;
00543 *dst = DRMS_MISSING_CHAR;
00544 }
00545 }
00546 }
00547 }
00548 break;
00549 case DRMS_TYPE_SHORT:
00550 {
00551 short *ssrc = (short *) src;
00552 stat = DRMS_SUCCESS;
00553 for (i=0; i<n; i++, ssrc++, dst++)
00554 {
00555 if (*ssrc==DRMS_MISSING_SHORT)
00556 *dst = DRMS_MISSING_CHAR;
00557 else
00558 {
00559 value = bscale* *ssrc + bzero;
00560 rangechk = round(value);
00561 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00562 *dst = (char)rangechk;
00563 else
00564 {
00565 stat = DRMS_RANGE;
00566 *dst = DRMS_MISSING_CHAR;
00567 }
00568 }
00569 }
00570 }
00571 break;
00572 case DRMS_TYPE_INT:
00573 {
00574 int *ssrc = (int *) src;
00575 stat = DRMS_SUCCESS;
00576 for (i=0; i<n; i++, ssrc++, dst++)
00577 {
00578 if (*ssrc==DRMS_MISSING_INT)
00579 *dst = DRMS_MISSING_CHAR;
00580 else
00581 {
00582 value = bscale* *ssrc + bzero;
00583
00584 rangechk = round(value);
00585 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00586 *dst = (char)rangechk;
00587 else
00588 {
00589 stat = DRMS_RANGE;
00590 *dst = DRMS_MISSING_CHAR;
00591 }
00592 }
00593 }
00594 }
00595 break;
00596 case DRMS_TYPE_LONGLONG:
00597 {
00598 long long *ssrc = (long long *) src;
00599 stat = DRMS_SUCCESS;
00600 for (i=0; i<n; i++, ssrc++, dst++)
00601 {
00602 if (*ssrc==DRMS_MISSING_LONGLONG)
00603 *dst = DRMS_MISSING_CHAR;
00604 else
00605 {
00606 value = bscale* *ssrc + bzero;
00607
00608 rangechk = round(value);
00609 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00610 *dst = (char)rangechk;
00611 else
00612 {
00613 stat = DRMS_RANGE;
00614 *dst = DRMS_MISSING_CHAR;
00615 }
00616 }
00617 }
00618 }
00619 break;
00620 case DRMS_TYPE_FLOAT:
00621 {
00622 float *ssrc = (float *) src;
00623 stat = DRMS_SUCCESS;
00624 for (i=0; i<n; i++, ssrc++, dst++)
00625 {
00626 if (isnan(*ssrc))
00627 *dst = DRMS_MISSING_CHAR;
00628 else
00629 {
00630 value = bscale* *ssrc + bzero;
00631
00632 rangechk = round(value);
00633 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00634 *dst = (char)rangechk;
00635 else
00636 {
00637 stat = DRMS_RANGE;
00638 *dst = DRMS_MISSING_CHAR;
00639 }
00640 }
00641 }
00642 }
00643 break;
00644 case DRMS_TYPE_TIME:
00645 {
00646 double *ssrc = (double *) src;
00647 stat = DRMS_SUCCESS;
00648 for (i=0; i<n; i++, ssrc++, dst++)
00649 {
00650 if (*ssrc==DRMS_MISSING_TIME)
00651 *dst = DRMS_MISSING_CHAR;
00652 else
00653 {
00654 value = bscale* *ssrc + bzero;
00655
00656 rangechk = round(value);
00657 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00658 *dst = (char)rangechk;
00659 else
00660 {
00661 stat = DRMS_RANGE;
00662 *dst = DRMS_MISSING_CHAR;
00663 }
00664 }
00665 }
00666 }
00667 case DRMS_TYPE_DOUBLE:
00668 {
00669 double *ssrc = (double *) src;
00670 stat = DRMS_SUCCESS;
00671 for (i=0; i<n; i++, ssrc++, dst++)
00672 {
00673 if (isnan(*ssrc))
00674 *dst = DRMS_MISSING_CHAR;
00675 else
00676 {
00677 value = bscale* *ssrc + bzero;
00678 rangechk = round(value);
00679 if (!(rangechk < SCHAR_MIN || rangechk > SCHAR_MAX))
00680 *dst = (char)rangechk;
00681 else
00682 {
00683 stat = DRMS_RANGE;
00684 *dst = DRMS_MISSING_CHAR;
00685 }
00686 }
00687 }
00688 }
00689 break;
00690 case DRMS_TYPE_STRING:
00691 {
00692 char *endptr;
00693 char **ssrc = (char **) src;
00694 stat = DRMS_SUCCESS;
00695 for (i=0; i<n; i++, ssrc++, dst++)
00696 {
00697 value = strtod(*ssrc,&endptr);
00698 if (value==0.0 && endptr==*ssrc )
00699 {
00700 stat = DRMS_BADSTRING;
00701 *dst = DRMS_MISSING_CHAR;
00702 }
00703 else
00704 {
00705 value = bscale * value + bzero;
00706 rangechk = round(value);
00707
00708 if ( rangechk < SCHAR_MIN || rangechk > SCHAR_MAX)
00709 {
00710 stat = DRMS_RANGE;
00711 *dst = DRMS_MISSING_CHAR;
00712 }
00713 else
00714 *dst = (char)rangechk;
00715 }
00716 }
00717 }
00718 break;
00719 default:
00720 stat = DRMS_ERROR_INVALIDTYPE;
00721 }
00722 }
00723 return stat;
00724 }
00725
00726
00727
00728 int drms_array2short(arraylen_t n, DRMS_Type_t src_type, double bzero, double bscale,
00729 void *src, short *dst)
00730 {
00731 double value;
00732 int stat;
00733 arraylen_t i;
00734 double rangechk;
00735
00736 if (fabs(bzero)==0.0 && bscale==1.0)
00737 {
00738 switch(src_type)
00739 {
00740 case DRMS_TYPE_CHAR:
00741 {
00742 char *ssrc = (char *) src;
00743 stat = DRMS_SUCCESS;
00744 for (i=0; i<n; i++, ssrc++, dst++)
00745 {
00746 if (*ssrc == DRMS_MISSING_CHAR)
00747 *dst = DRMS_MISSING_SHORT;
00748 else
00749 *dst = (short) *ssrc;
00750 }
00751 }
00752 break;
00753 case DRMS_TYPE_SHORT:
00754 stat = DRMS_SUCCESS;
00755 memcpy(dst,src,n*sizeof(short));
00756 break;
00757 case DRMS_TYPE_INT:
00758 {
00759 int *ssrc = (int *) src;
00760 stat = DRMS_SUCCESS;
00761 for (i=0; i<n; i++, ssrc++, dst++)
00762 {
00763 if (*ssrc == DRMS_MISSING_INT)
00764 *dst = DRMS_MISSING_SHORT;
00765 else if (!(*ssrc < SHRT_MIN || *ssrc > SHRT_MAX))
00766 *dst = (short) *ssrc;
00767 else
00768 {
00769 stat = DRMS_RANGE;
00770 *dst = DRMS_MISSING_SHORT;
00771 }
00772 }
00773 }
00774 break;
00775 case DRMS_TYPE_LONGLONG:
00776 {
00777 long long *ssrc = (long long *) src;
00778 stat = DRMS_SUCCESS;
00779 for (i=0; i<n; i++, ssrc++, dst++)
00780 {
00781 if (*ssrc == DRMS_MISSING_LONGLONG)
00782 *dst = DRMS_MISSING_SHORT;
00783 else if (!(*ssrc < SHRT_MIN || *ssrc > SHRT_MAX))
00784 *dst = (short) *ssrc;
00785 else
00786 {
00787 stat = DRMS_RANGE;
00788 *dst = DRMS_MISSING_SHORT;
00789 }
00790 }
00791 }
00792 break;
00793 case DRMS_TYPE_FLOAT:
00794 {
00795 float *ssrc = (float *) src;
00796 stat = DRMS_SUCCESS;
00797 for (i=0; i<n; i++, ssrc++, dst++)
00798 {
00799 if (isnan(*ssrc))
00800 *dst = DRMS_MISSING_SHORT;
00801 else
00802 {
00803 rangechk = round(*ssrc);
00804 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00805 *dst = (short)rangechk;
00806 else
00807 {
00808 stat = DRMS_RANGE;
00809 *dst = DRMS_MISSING_SHORT;
00810 }
00811 }
00812 }
00813 }
00814 break;
00815 case DRMS_TYPE_TIME:
00816 {
00817 double *ssrc = (double *) src;
00818 stat = DRMS_SUCCESS;
00819 for (i=0; i<n; i++, ssrc++, dst++)
00820 {
00821 if (*ssrc == DRMS_MISSING_TIME)
00822 *dst = DRMS_MISSING_SHORT;
00823 else
00824 {
00825 rangechk = round(*ssrc);
00826 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00827 *dst = (short)rangechk;
00828 else
00829 {
00830 stat = DRMS_RANGE;
00831 *dst = DRMS_MISSING_SHORT;
00832 }
00833 }
00834 }
00835 }
00836 break;
00837 case DRMS_TYPE_DOUBLE:
00838 {
00839 double *ssrc = (double *) src;
00840 stat = DRMS_SUCCESS;
00841 for (i=0; i<n; i++, ssrc++, dst++)
00842 {
00843 if (isnan(*ssrc))
00844 *dst = DRMS_MISSING_SHORT;
00845 else
00846 {
00847 rangechk = round(*ssrc);
00848 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00849 *dst = (short)rangechk;
00850 else
00851 {
00852 stat = DRMS_RANGE;
00853 *dst = DRMS_MISSING_SHORT;
00854 }
00855 }
00856 }
00857 }
00858 break;
00859 case DRMS_TYPE_STRING:
00860 {
00861 double val;
00862 char *endptr;
00863 char **ssrc = (char **) src;
00864 stat = DRMS_SUCCESS;
00865 for (i=0; i<n; i++, ssrc++, dst++)
00866 {
00867 val = strtod(*ssrc,&endptr);
00868 if (val==0 && endptr==*ssrc )
00869 {
00870 stat = DRMS_BADSTRING;
00871 *dst = DRMS_MISSING_SHORT;
00872 }
00873 else
00874 {
00875 rangechk = round(val);
00876 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00877 *dst = (short)rangechk;
00878 else
00879 {
00880 stat = DRMS_RANGE;
00881 *dst = DRMS_MISSING_SHORT;
00882 }
00883 }
00884 }
00885 }
00886 break;
00887 default:
00888 stat = DRMS_ERROR_INVALIDTYPE;
00889 }
00890 }
00891 else
00892 {
00893 switch(src_type)
00894 {
00895 case DRMS_TYPE_CHAR:
00896 {
00897 char *ssrc = (char *) src;
00898 stat = DRMS_SUCCESS;
00899 for (i=0; i<n; i++, ssrc++, dst++)
00900 {
00901 if (*ssrc==DRMS_MISSING_CHAR)
00902 *dst = DRMS_MISSING_SHORT;
00903 else
00904 {
00905 value = bscale * *ssrc + bzero;
00906 rangechk = round(value);
00907 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00908 *dst = (short)round(rangechk);
00909 else
00910 {
00911 stat = DRMS_RANGE;
00912 *dst = DRMS_MISSING_SHORT;
00913 }
00914 }
00915 }
00916 }
00917 break;
00918 case DRMS_TYPE_SHORT:
00919 {
00920 short *ssrc = (short *) src;
00921 stat = DRMS_SUCCESS;
00922 for (i=0; i<n; i++, ssrc++, dst++)
00923 {
00924 if (*ssrc==DRMS_MISSING_SHORT)
00925 *dst = DRMS_MISSING_SHORT;
00926 else
00927 {
00928 value = bscale * *ssrc + bzero;
00929 rangechk = round(value);
00930 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00931 *dst = (short)round(rangechk);
00932 else
00933 {
00934 stat = DRMS_RANGE;
00935 *dst = DRMS_MISSING_SHORT;
00936 }
00937 }
00938 }
00939 }
00940 break;
00941 case DRMS_TYPE_INT:
00942 {
00943 int *ssrc = (int *) src;
00944 stat = DRMS_SUCCESS;
00945 for (i=0; i<n; i++, ssrc++, dst++)
00946 {
00947 if (*ssrc==DRMS_MISSING_INT)
00948 *dst = DRMS_MISSING_SHORT;
00949 else
00950 {
00951 value = bscale * *ssrc + bzero;
00952 rangechk = round(value);
00953 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00954 *dst = (short)round(rangechk);
00955 else
00956 {
00957 stat = DRMS_RANGE;
00958 *dst = DRMS_MISSING_SHORT;
00959 }
00960 }
00961 }
00962 }
00963 break;
00964 case DRMS_TYPE_LONGLONG:
00965 {
00966 long long *ssrc = (long long *) src;
00967 stat = DRMS_SUCCESS;
00968 for (i=0; i<n; i++, ssrc++, dst++)
00969 {
00970 if (*ssrc==DRMS_MISSING_LONGLONG)
00971 *dst = DRMS_MISSING_SHORT;
00972 else
00973 {
00974 value = bscale * *ssrc + bzero;
00975 rangechk = round(value);
00976 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
00977 *dst = (short)round(rangechk);
00978 else
00979 {
00980 stat = DRMS_RANGE;
00981 *dst = DRMS_MISSING_SHORT;
00982 }
00983 }
00984 }
00985 }
00986 break;
00987 case DRMS_TYPE_FLOAT:
00988 {
00989 float *ssrc = (float *) src;
00990 stat = DRMS_SUCCESS;
00991 for (i=0; i<n; i++, ssrc++, dst++)
00992 {
00993 if (isnan(*ssrc))
00994 *dst = DRMS_MISSING_SHORT;
00995 else
00996 {
00997 value = bscale * *ssrc + bzero;
00998 rangechk = round(value);
00999 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
01000 *dst = (short)round(rangechk);
01001 else
01002 {
01003 stat = DRMS_RANGE;
01004 *dst = DRMS_MISSING_SHORT;
01005 }
01006 }
01007 }
01008 }
01009 break;
01010 case DRMS_TYPE_TIME:
01011 {
01012 double *ssrc = (double *) src;
01013 stat = DRMS_SUCCESS;
01014 for (i=0; i<n; i++, ssrc++, dst++)
01015 {
01016 if (*ssrc==DRMS_MISSING_TIME)
01017 *dst = DRMS_MISSING_SHORT;
01018 else
01019 {
01020 value = bscale * *ssrc + bzero;
01021 rangechk = round(value);
01022 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
01023 *dst = (short)round(rangechk);
01024 else
01025 {
01026 stat = DRMS_RANGE;
01027 *dst = DRMS_MISSING_SHORT;
01028 }
01029 }
01030 }
01031 }
01032 case DRMS_TYPE_DOUBLE:
01033 {
01034 double *ssrc = (double *) src;
01035 stat = DRMS_SUCCESS;
01036 for (i=0; i<n; i++, ssrc++, dst++)
01037 {
01038 if (isnan(*ssrc))
01039 *dst = DRMS_MISSING_SHORT;
01040 else
01041 {
01042 value = bscale * *ssrc + bzero;
01043 rangechk = round(value);
01044 if (!(rangechk < SHRT_MIN || rangechk > SHRT_MAX))
01045 *dst = (short)round(rangechk);
01046 else
01047 {
01048 stat = DRMS_RANGE;
01049 *dst = DRMS_MISSING_SHORT;
01050 }
01051 }
01052 }
01053 }
01054 break;
01055 case DRMS_TYPE_STRING:
01056 {
01057 char *endptr;
01058 char **ssrc = (char **) src;
01059 stat = DRMS_SUCCESS;
01060 for (i=0; i<n; i++, ssrc++, dst++)
01061 {
01062 value = strtod(*ssrc,&endptr);
01063 if (value==0.0 && endptr==*ssrc )
01064 {
01065 stat = DRMS_BADSTRING;
01066 *dst = DRMS_MISSING_SHORT;
01067 }
01068 else
01069 {
01070 value = bscale*value + bzero;
01071 rangechk = round(value);
01072
01073 if (rangechk < SHRT_MIN || rangechk > SHRT_MAX)
01074 {
01075 stat = DRMS_RANGE;
01076 *dst = DRMS_MISSING_SHORT;
01077 }
01078 else
01079 *dst = (short)rangechk;
01080 }
01081 }
01082 }
01083 break;
01084 default:
01085 stat = DRMS_ERROR_INVALIDTYPE;
01086 }
01087 }
01088 return stat;
01089 }
01090
01091
01092
01093 int drms_array2int(arraylen_t n, DRMS_Type_t src_type, double bzero, double bscale,
01094 void *src, int *dst)
01095 {
01096 double value;
01097 int stat;
01098 arraylen_t i;
01099 double rangechk;
01100
01101 if (fabs(bzero)==0.0 && bscale==1.0)
01102 {
01103 switch(src_type)
01104 {
01105 case DRMS_TYPE_CHAR:
01106 {
01107 char *ssrc = (char *) src;
01108 stat = DRMS_SUCCESS;
01109 for (i=0; i<n; i++, ssrc++, dst++)
01110 if (*ssrc == DRMS_MISSING_CHAR)
01111 *dst = DRMS_MISSING_INT;
01112 else
01113 *dst = (int) *ssrc;
01114 }
01115 break;
01116 case DRMS_TYPE_SHORT:
01117 {
01118 short *ssrc = (short *) src;
01119 stat = DRMS_SUCCESS;
01120 for (i=0; i<n; i++, ssrc++, dst++)
01121 if (*ssrc == DRMS_MISSING_SHORT)
01122 *dst = DRMS_MISSING_INT;
01123 else
01124 *dst = (int) *ssrc;
01125 }
01126 break;
01127 case DRMS_TYPE_INT:
01128 stat = DRMS_SUCCESS;
01129 memcpy(dst,src,n*sizeof(int));
01130 break;
01131 case DRMS_TYPE_LONGLONG:
01132 {
01133 long long *ssrc = (long long *) src;
01134 stat = DRMS_SUCCESS;
01135 for (i=0; i<n; i++, ssrc++, dst++)
01136 {
01137 if (*ssrc == DRMS_MISSING_LONGLONG)
01138 *dst = DRMS_MISSING_INT;
01139 else if (!(*ssrc < INT_MIN || *ssrc > INT_MAX))
01140 *dst = (int) *ssrc;
01141 else
01142 {
01143 stat = DRMS_RANGE;
01144 *dst = DRMS_MISSING_INT;
01145 }
01146 }
01147 }
01148 break;
01149 case DRMS_TYPE_FLOAT:
01150 {
01151 float *ssrc = (float *) src;
01152 stat = DRMS_SUCCESS;
01153 for (i=0; i<n; i++, ssrc++, dst++)
01154 {
01155 if (isnan(*ssrc))
01156 *dst = DRMS_MISSING_INT;
01157 else
01158 {
01159 rangechk = round(*ssrc);
01160 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01161 *dst = (int)rangechk;
01162 else
01163 {
01164 stat = DRMS_RANGE;
01165 *dst = DRMS_MISSING_INT;
01166 }
01167 }
01168 }
01169 }
01170 break;
01171 case DRMS_TYPE_TIME:
01172 {
01173 double *ssrc = (double *) src;
01174 stat = DRMS_SUCCESS;
01175 for (i=0; i<n; i++, ssrc++, dst++)
01176 {
01177 if (*ssrc == DRMS_MISSING_TIME)
01178 *dst = DRMS_MISSING_INT;
01179 else
01180 {
01181 rangechk = round(*ssrc);
01182 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01183 *dst = (int)rangechk;
01184 else
01185 {
01186 stat = DRMS_RANGE;
01187 *dst = DRMS_MISSING_INT;
01188 }
01189 }
01190 }
01191 }
01192 break;
01193 case DRMS_TYPE_DOUBLE:
01194 {
01195 double *ssrc = (double *) src;
01196 stat = DRMS_SUCCESS;
01197 for (i=0; i<n; i++, ssrc++, dst++)
01198 {
01199 if (isnan(*ssrc))
01200 *dst = DRMS_MISSING_INT;
01201 else
01202 {
01203 rangechk = round(*ssrc);
01204 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01205 *dst = (int)rangechk;
01206 else
01207 {
01208 stat = DRMS_RANGE;
01209 *dst = DRMS_MISSING_INT;
01210 }
01211 }
01212 }
01213 }
01214 break;
01215 case DRMS_TYPE_STRING:
01216 {
01217 double val;
01218 char *endptr;
01219 char **ssrc = (char **) src;
01220 stat = DRMS_SUCCESS;
01221 for (i=0; i<n; i++, ssrc++, dst++)
01222 {
01223 val = strtod(*ssrc,&endptr);
01224 if (val==0 && endptr==*ssrc )
01225 {
01226 stat = DRMS_BADSTRING;
01227 *dst = DRMS_MISSING_INT;
01228 }
01229 else
01230 {
01231 rangechk = round(val);
01232 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01233 *dst = (int)rangechk;
01234 else
01235 {
01236 stat = DRMS_RANGE;
01237 *dst = DRMS_MISSING_INT;
01238 }
01239 }
01240 }
01241 }
01242 break;
01243 default:
01244 stat = DRMS_ERROR_INVALIDTYPE;
01245 }
01246 }
01247 else
01248 {
01249 switch(src_type)
01250 {
01251 case DRMS_TYPE_CHAR:
01252 {
01253 char *ssrc = (char *) src;
01254 stat = DRMS_SUCCESS;
01255 for (i=0; i<n; i++, ssrc++, dst++)
01256 {
01257 if (*ssrc==DRMS_MISSING_CHAR)
01258 *dst = DRMS_MISSING_INT;
01259 else
01260 {
01261 value = bscale * *ssrc + bzero;
01262 rangechk = round(value);
01263 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01264 *dst = (int)rangechk;
01265 else
01266 {
01267 stat = DRMS_RANGE;
01268 *dst = DRMS_MISSING_INT;
01269 }
01270 }
01271 }
01272 }
01273 break;
01274 case DRMS_TYPE_SHORT:
01275 {
01276 short *ssrc = (short *) src;
01277 stat = DRMS_SUCCESS;
01278 for (i=0; i<n; i++, ssrc++, dst++)
01279 {
01280 if (*ssrc==DRMS_MISSING_SHORT)
01281 *dst = DRMS_MISSING_INT;
01282 else
01283 {
01284 value = bscale * *ssrc + bzero;
01285 rangechk = round(value);
01286 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01287 *dst = (int)rangechk;
01288 else
01289 {
01290 stat = DRMS_RANGE;
01291 *dst = DRMS_MISSING_INT;
01292 }
01293 }
01294 }
01295 }
01296 break;
01297 case DRMS_TYPE_INT:
01298 {
01299 int *ssrc = (int *) src;
01300 stat = DRMS_SUCCESS;
01301 for (i=0; i<n; i++, ssrc++, dst++)
01302 {
01303 if (*ssrc==DRMS_MISSING_INT)
01304 *dst = DRMS_MISSING_INT;
01305 else
01306 {
01307 value = bscale * *ssrc + bzero;
01308 rangechk = round(value);
01309 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01310 *dst = (int)rangechk;
01311 else
01312 {
01313 stat = DRMS_RANGE;
01314 *dst = DRMS_MISSING_INT;
01315 }
01316 }
01317 }
01318 }
01319 break;
01320 case DRMS_TYPE_LONGLONG:
01321 {
01322 long long *ssrc = (long long *) src;
01323 stat = DRMS_SUCCESS;
01324 for (i=0; i<n; i++, ssrc++, dst++)
01325 {
01326 if (*ssrc==DRMS_MISSING_LONGLONG)
01327 *dst = DRMS_MISSING_INT;
01328 else
01329 {
01330 value = bscale * *ssrc + bzero;
01331 rangechk = round(value);
01332 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01333 *dst = (int)rangechk;
01334 else
01335 {
01336 stat = DRMS_RANGE;
01337 *dst = DRMS_MISSING_INT;
01338 }
01339 }
01340 }
01341 }
01342 break;
01343 case DRMS_TYPE_FLOAT:
01344 {
01345 float *ssrc = (float *) src;
01346 stat = DRMS_SUCCESS;
01347 for (i=0; i<n; i++, ssrc++, dst++)
01348 {
01349 if (isnan(*ssrc))
01350 {
01351 *dst = DRMS_MISSING_INT;
01352 }
01353 else
01354 {
01355 value = bscale * *ssrc + bzero;
01356 rangechk = round(value);
01357 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01358 *dst = (int)rangechk;
01359 else
01360 {
01361 stat = DRMS_RANGE;
01362 *dst = DRMS_MISSING_INT;
01363 }
01364 }
01365 }
01366 }
01367 break;
01368 case DRMS_TYPE_TIME:
01369 {
01370 double *ssrc = (double *) src;
01371 stat = DRMS_SUCCESS;
01372 for (i=0; i<n; i++, ssrc++, dst++)
01373 {
01374 if (*ssrc==DRMS_MISSING_TIME)
01375 *dst = DRMS_MISSING_INT;
01376 else
01377 {
01378 value = bscale * *ssrc + bzero;
01379 rangechk = round(value);
01380 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01381 *dst = (int)rangechk;
01382 else
01383 {
01384 stat = DRMS_RANGE;
01385 *dst = DRMS_MISSING_INT;
01386 }
01387 }
01388 }
01389 }
01390 case DRMS_TYPE_DOUBLE:
01391 {
01392 double *ssrc = (double *) src;
01393 stat = DRMS_SUCCESS;
01394 for (i=0; i<n; i++, ssrc++, dst++)
01395 {
01396 if (isnan(*ssrc))
01397 *dst = DRMS_MISSING_INT;
01398 else
01399 {
01400 value = bscale * *ssrc + bzero;
01401 rangechk = round(value);
01402 if (!(rangechk < INT_MIN || rangechk > INT_MAX))
01403 *dst = (int)rangechk;
01404 else
01405 {
01406 stat = DRMS_RANGE;
01407 *dst = DRMS_MISSING_INT;
01408 }
01409 }
01410 }
01411 }
01412 break;
01413 case DRMS_TYPE_STRING:
01414 {
01415 char *endptr;
01416 char **ssrc = (char **) src;
01417 stat = DRMS_SUCCESS;
01418 for (i=0; i<n; i++, ssrc++, dst++)
01419 {
01420 value = strtod(*ssrc,&endptr);
01421 if (value==0.0 && endptr==*ssrc )
01422 {
01423 stat = DRMS_BADSTRING;
01424 *dst = DRMS_MISSING_INT;
01425 }
01426 else
01427 {
01428 value = bscale*value + bzero;
01429 rangechk = round(value);
01430
01431 if ( rangechk < INT_MIN || rangechk > INT_MAX)
01432 {
01433 stat = DRMS_RANGE;
01434 *dst = DRMS_MISSING_INT;
01435 }
01436 else
01437 *dst = (int)rangechk;
01438 }
01439 }
01440 }
01441 break;
01442 default:
01443 stat = DRMS_ERROR_INVALIDTYPE;
01444 }
01445 }
01446 return stat;
01447 }
01448
01449 int drms_array2longlong(arraylen_t n, DRMS_Type_t src_type, double bzero,
01450 double bscale, void *src, long long *dst)
01451 {
01452 double value;
01453 int stat;
01454 arraylen_t i;
01455 double rangechk;
01456
01457 if (fabs(bzero)==0.0 && bscale==1.0)
01458 {
01459 switch(src_type)
01460 {
01461 case DRMS_TYPE_CHAR:
01462 {
01463 char *ssrc = (char *) src;
01464 stat = DRMS_SUCCESS;
01465 for (i=0; i<n; i++, ssrc++, dst++)
01466 if (*ssrc == DRMS_MISSING_CHAR)
01467 *dst = DRMS_MISSING_LONGLONG;
01468 else
01469 *dst = (long long) *ssrc;
01470 }
01471 break;
01472 case DRMS_TYPE_SHORT:
01473 {
01474 short *ssrc = (short *) src;
01475 stat = DRMS_SUCCESS;
01476 for (i=0; i<n; i++, ssrc++, dst++)
01477 if (*ssrc == DRMS_MISSING_SHORT)
01478 *dst = DRMS_MISSING_LONGLONG;
01479 else
01480 *dst = (long long) *ssrc;
01481 }
01482 break;
01483 case DRMS_TYPE_INT:
01484 {
01485 int *ssrc = (int *) src;
01486 stat = DRMS_SUCCESS;
01487 for (i=0; i<n; i++, ssrc++, dst++)
01488 if (*ssrc == DRMS_MISSING_INT)
01489 *dst = DRMS_MISSING_LONGLONG;
01490 else
01491 *dst = (long long) *ssrc;
01492 }
01493 break;
01494 case DRMS_TYPE_LONGLONG:
01495 stat = DRMS_SUCCESS;
01496 memcpy(dst,src,n*sizeof(long long));
01497 break;
01498 case DRMS_TYPE_FLOAT:
01499 {
01500 float *ssrc = (float *) src;
01501 stat = DRMS_SUCCESS;
01502 for (i=0; i<n; i++, ssrc++, dst++)
01503 {
01504 if (isnan(*ssrc))
01505 *dst = DRMS_MISSING_LONGLONG;
01506 else
01507 {
01508 rangechk = round(*ssrc);
01509 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01510 *dst = (long long)rangechk;
01511 else
01512 {
01513 stat = DRMS_RANGE;
01514 *dst = DRMS_MISSING_LONGLONG;
01515 }
01516 }
01517 }
01518 }
01519 break;
01520 case DRMS_TYPE_TIME:
01521 {
01522 double *ssrc = (double *) src;
01523 stat = DRMS_SUCCESS;
01524 for (i=0; i<n; i++, ssrc++, dst++)
01525 {
01526 if (*ssrc == DRMS_MISSING_TIME)
01527 *dst = DRMS_MISSING_LONGLONG;
01528 else
01529 {
01530 rangechk = round(*ssrc);
01531 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01532 *dst = (long long)rangechk;
01533 else
01534 {
01535 stat = DRMS_RANGE;
01536 *dst = DRMS_MISSING_LONGLONG;
01537 }
01538
01539 }
01540 }
01541 }
01542 case DRMS_TYPE_DOUBLE:
01543 {
01544 double *ssrc = (double *) src;
01545 stat = DRMS_SUCCESS;
01546 for (i=0; i<n; i++, ssrc++, dst++)
01547 {
01548 if (isnan(*ssrc))
01549 *dst = DRMS_MISSING_LONGLONG;
01550 else
01551 {
01552 rangechk = round(*ssrc);
01553 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01554 *dst = (long long)rangechk;
01555 else
01556 {
01557 stat = DRMS_RANGE;
01558 *dst = DRMS_MISSING_LONGLONG;
01559 }
01560 }
01561 }
01562 }
01563 break;
01564 case DRMS_TYPE_STRING:
01565 {
01566 double val;
01567 char *endptr;
01568 char **ssrc = (char **) src;
01569 stat = DRMS_SUCCESS;
01570 for (i=0; i<n; i++, ssrc++, dst++)
01571 {
01572 val = strtod(*ssrc,&endptr);
01573 if (val==0 && endptr==*ssrc )
01574 {
01575 stat = DRMS_BADSTRING;
01576 *dst = DRMS_MISSING_LONGLONG;
01577 }
01578 else
01579 {
01580 rangechk = round(val);
01581 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01582 *dst = (long long)rangechk;
01583 else
01584 {
01585 stat = DRMS_RANGE;
01586 *dst = DRMS_MISSING_LONGLONG;
01587 }
01588 }
01589 }
01590 }
01591 break;
01592 default:
01593 stat = DRMS_ERROR_INVALIDTYPE;
01594 }
01595 }
01596 else
01597 {
01598 switch(src_type)
01599 {
01600 case DRMS_TYPE_CHAR:
01601 {
01602 char *ssrc = (char *) src;
01603 stat = DRMS_SUCCESS;
01604 for (i=0; i<n; i++, ssrc++, dst++)
01605 {
01606 if (*ssrc==DRMS_MISSING_CHAR)
01607 *dst = DRMS_MISSING_LONGLONG;
01608 else
01609 {
01610 value = bscale * *ssrc + bzero;
01611 rangechk = round(value);
01612 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01613 *dst = (long long)rangechk;
01614 else
01615 {
01616 stat = DRMS_RANGE;
01617 *dst = DRMS_MISSING_LONGLONG;
01618 }
01619 }
01620 }
01621 }
01622 break;
01623 case DRMS_TYPE_SHORT:
01624 {
01625 short *ssrc = (short *) src;
01626 stat = DRMS_SUCCESS;
01627 for (i=0; i<n; i++, ssrc++, dst++)
01628 {
01629 if (*ssrc==DRMS_MISSING_SHORT)
01630 *dst = DRMS_MISSING_LONGLONG;
01631 else
01632 {
01633 value = bscale * *ssrc + bzero;
01634 rangechk = round(value);
01635 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01636 *dst = (long long)rangechk;
01637 else
01638 {
01639 stat = DRMS_RANGE;
01640 *dst = DRMS_MISSING_LONGLONG;
01641 }
01642 }
01643 }
01644 }
01645 break;
01646 case DRMS_TYPE_INT:
01647 {
01648 int *ssrc = (int *) src;
01649 stat = DRMS_SUCCESS;
01650 for (i=0; i<n; i++, ssrc++, dst++)
01651 {
01652 if (*ssrc==DRMS_MISSING_INT)
01653 *dst = DRMS_MISSING_LONGLONG;
01654 else
01655 {
01656 value = bscale * *ssrc + bzero;
01657 rangechk = round(value);
01658 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01659 *dst = (long long)rangechk;
01660 else
01661 {
01662 stat = DRMS_RANGE;
01663 *dst = DRMS_MISSING_LONGLONG;
01664 }
01665 }
01666 }
01667 }
01668 break;
01669 case DRMS_TYPE_LONGLONG:
01670 {
01671 long long *ssrc = (long long *) src;
01672 stat = DRMS_SUCCESS;
01673 for (i=0; i<n; i++, ssrc++, dst++)
01674 {
01675 if (*ssrc==DRMS_MISSING_LONGLONG)
01676 *dst = DRMS_MISSING_LONGLONG;
01677 else
01678 {
01679 value = bscale * *ssrc + bzero;
01680 rangechk = round(value);
01681 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01682 *dst = (long long)rangechk;
01683 else
01684 {
01685 stat = DRMS_RANGE;
01686 *dst = DRMS_MISSING_LONGLONG;
01687 }
01688 }
01689 }
01690 }
01691 break;
01692 case DRMS_TYPE_FLOAT:
01693 {
01694 float *ssrc = (float *) src;
01695 stat = DRMS_SUCCESS;
01696 for (i=0; i<n; i++, ssrc++, dst++)
01697 {
01698 if (isnan(*ssrc))
01699 *dst = DRMS_MISSING_LONGLONG;
01700 else
01701 {
01702 value = bscale * *ssrc + bzero;
01703 rangechk = round(value);
01704 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01705 *dst = (long long)rangechk;
01706 else
01707 {
01708 stat = DRMS_RANGE;
01709 *dst = DRMS_MISSING_LONGLONG;
01710 }
01711 }
01712 }
01713 }
01714 break;
01715 case DRMS_TYPE_TIME:
01716 {
01717 double *ssrc = (double *) src;
01718 stat = DRMS_SUCCESS;
01719 for (i=0; i<n; i++, ssrc++, dst++)
01720 {
01721 if (*ssrc==DRMS_MISSING_TIME)
01722 *dst = DRMS_MISSING_LONGLONG;
01723 else
01724 {
01725 value = bscale * *ssrc + bzero;
01726 rangechk = round(value);
01727 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01728 *dst = (long long)rangechk;
01729 else
01730 {
01731 stat = DRMS_RANGE;
01732 *dst = DRMS_MISSING_LONGLONG;
01733 }
01734 }
01735 }
01736 }
01737 break;
01738 case DRMS_TYPE_DOUBLE:
01739 {
01740 double *ssrc = (double *) src;
01741 stat = DRMS_SUCCESS;
01742 for (i=0; i<n; i++, ssrc++, dst++)
01743 {
01744 if (isnan(*ssrc))
01745 *dst = DRMS_MISSING_LONGLONG;
01746 else
01747 {
01748 value = bscale * *ssrc + bzero;
01749 rangechk = round(value);
01750 if (!(rangechk < LLONG_MIN || rangechk > LLONG_MAX))
01751 *dst = (long long)rangechk;
01752 else
01753 {
01754 stat = DRMS_RANGE;
01755 *dst = DRMS_MISSING_LONGLONG;
01756 }
01757 }
01758 }
01759 }
01760 break;
01761 case DRMS_TYPE_STRING:
01762 {
01763 char *endptr;
01764 char **ssrc = (char **) src;
01765 stat = DRMS_SUCCESS;
01766 for (i=0; i<n; i++, ssrc++, dst++)
01767 {
01768 value = strtod(*ssrc,&endptr);
01769 if (value==0.0 && endptr==*ssrc )
01770 {
01771 stat = DRMS_BADSTRING;
01772 *dst = DRMS_MISSING_LONGLONG;
01773 }
01774 else
01775 {
01776 value = bscale*value + bzero;
01777 rangechk = round(value);
01778
01779 if ( rangechk < LLONG_MIN || rangechk > LLONG_MAX)
01780 {
01781 stat = DRMS_RANGE;
01782 *dst = DRMS_MISSING_LONGLONG;
01783 }
01784 else
01785 *dst = (long long)rangechk;
01786 }
01787 }
01788 }
01789 break;
01790 default:
01791 stat = DRMS_ERROR_INVALIDTYPE;
01792 }
01793 }
01794
01795 return stat;
01796 }
01797
01798
01799
01800 int drms_array2float(arraylen_t n, DRMS_Type_t src_type, double bzero, double bscale,
01801 void *src, float *dst)
01802 {
01803 double value;
01804 int stat;
01805 arraylen_t i;
01806
01807 if ( fabs(bzero)==0.0 && bscale==1.0 )
01808 {
01809 switch(src_type)
01810 {
01811 case DRMS_TYPE_CHAR:
01812 {
01813 char *ssrc = (char *) src;
01814 stat = DRMS_SUCCESS;
01815 for (i=0; i<n; i++, ssrc++, dst++)
01816 if (*ssrc == DRMS_MISSING_CHAR)
01817 *dst = DRMS_MISSING_FLOAT;
01818 else
01819 *dst = (float) *ssrc;
01820 }
01821 break;
01822 case DRMS_TYPE_SHORT:
01823 {
01824 short *ssrc = (short *) src;
01825 stat = DRMS_SUCCESS;
01826 for (i=0; i<n; i++, ssrc++, dst++)
01827 if (*ssrc == DRMS_MISSING_SHORT)
01828 *dst = DRMS_MISSING_FLOAT;
01829 else
01830 *dst = (float) *ssrc;
01831 }
01832 break;
01833 case DRMS_TYPE_INT:
01834 {
01835 int *ssrc = (int *) src;
01836 stat = DRMS_SUCCESS;
01837 for (i=0; i<n; i++, ssrc++, dst++)
01838 if (*ssrc == DRMS_MISSING_INT)
01839 *dst = DRMS_MISSING_FLOAT;
01840 else
01841 *dst = (float) *ssrc;
01842 }
01843 break;
01844 case DRMS_TYPE_LONGLONG:
01845 {
01846 long long *ssrc = (long long *) src;
01847 stat = DRMS_SUCCESS;
01848 for (i=0; i<n; i++, ssrc++, dst++)
01849 if (*ssrc == DRMS_MISSING_LONGLONG)
01850 *dst = DRMS_MISSING_FLOAT;
01851 else
01852 *dst = (float) *ssrc;
01853 }
01854 break;
01855 case DRMS_TYPE_FLOAT:
01856 stat = DRMS_SUCCESS;
01857 memcpy(dst,src,n);
01858 break;
01859 case DRMS_TYPE_TIME:
01860 {
01861 double *ssrc = (double *) src;
01862 stat = DRMS_SUCCESS;
01863 for (i=0; i<n; i++, ssrc++, dst++)
01864 {
01865 if (*ssrc == DRMS_MISSING_TIME)
01866 *dst = DRMS_MISSING_FLOAT;
01867 else if (!(*ssrc < -FLT_MAX || *ssrc > FLT_MAX))
01868 *dst = (float) *ssrc;
01869 else
01870 {
01871 stat = DRMS_RANGE;
01872 *dst = DRMS_MISSING_FLOAT;
01873 }
01874 }
01875 }
01876 case DRMS_TYPE_DOUBLE:
01877 {
01878 double *ssrc = (double *) src;
01879 stat = DRMS_SUCCESS;
01880 for (i=0; i<n; i++, ssrc++, dst++)
01881 {
01882 if (isnan(*ssrc))
01883 *dst = DRMS_MISSING_FLOAT;
01884 else if (!(*ssrc < -FLT_MAX || *ssrc > FLT_MAX))
01885 *dst = (float) *ssrc;
01886 else
01887 {
01888 stat = DRMS_RANGE;
01889 *dst = DRMS_MISSING_FLOAT;
01890 }
01891 }
01892 }
01893 break;
01894 case DRMS_TYPE_STRING:
01895 {
01896 float val;
01897 char *endptr;
01898 char **ssrc = (char **) src;
01899 stat = DRMS_SUCCESS;
01900 for (i=0; i<n; i++, ssrc++, dst++)
01901 {
01902 val = strtof(*ssrc, &endptr);
01903 if (val==0 && endptr==*ssrc )
01904 {
01905 stat = DRMS_BADSTRING;
01906 *dst = DRMS_MISSING_FLOAT;
01907 }
01908 else if ( (val == HUGE_VALF || val == -HUGE_VALF) && errno==ERANGE)
01909 {
01910 stat = DRMS_RANGE;
01911 *dst = DRMS_MISSING_FLOAT;
01912 }
01913 else
01914 *dst = val;
01915 }
01916 }
01917 break;
01918 default:
01919 stat = DRMS_ERROR_INVALIDTYPE;
01920 }
01921 }
01922 else
01923 {
01924 switch(src_type)
01925 {
01926 case DRMS_TYPE_CHAR:
01927 {
01928 char *ssrc = (char *) src;
01929 stat = DRMS_SUCCESS;
01930 for (i=0; i<n; i++, ssrc++, dst++)
01931 {
01932 if (*ssrc==DRMS_MISSING_CHAR)
01933 *dst = DRMS_MISSING_FLOAT;
01934 else
01935 {
01936 value = bscale * *ssrc + bzero;
01937 if (!(value < -FLT_MAX || value > FLT_MAX))
01938 *dst = (float) value;
01939 else
01940 {
01941 stat = DRMS_RANGE;
01942 *dst = DRMS_MISSING_FLOAT;
01943 }
01944 }
01945 }
01946 }
01947 break;
01948 case DRMS_TYPE_SHORT:
01949 {
01950 short *ssrc = (short *) src;
01951 stat = DRMS_SUCCESS;
01952 for (i=0; i<n; i++, ssrc++, dst++)
01953 {
01954 if (*ssrc==DRMS_MISSING_SHORT)
01955 *dst = DRMS_MISSING_FLOAT;
01956 else
01957 {
01958 value = bscale * *ssrc + bzero;
01959 if (!(value < -FLT_MAX || value > FLT_MAX))
01960 *dst = (float) value;
01961 else
01962 {
01963 stat = DRMS_RANGE;
01964 *dst = DRMS_MISSING_FLOAT;
01965 }
01966 }
01967 }
01968 }
01969 break;
01970 case DRMS_TYPE_INT:
01971 {
01972 int *ssrc = (int *) src;
01973 stat = DRMS_SUCCESS;
01974 for (i=0; i<n; i++, ssrc++, dst++)
01975 {
01976 if (*ssrc==DRMS_MISSING_INT)
01977 *dst = DRMS_MISSING_FLOAT;
01978 else
01979 {
01980 value = bscale * *ssrc + bzero;
01981 if (!(value < -FLT_MAX || value > FLT_MAX))
01982 *dst = (float) value;
01983 else
01984 {
01985 stat = DRMS_RANGE;
01986 *dst = DRMS_MISSING_FLOAT;
01987 }
01988 }
01989 }
01990 }
01991 break;
01992 case DRMS_TYPE_LONGLONG:
01993 {
01994 long long *ssrc = (long long *) src;
01995 stat = DRMS_SUCCESS;
01996 for (i=0; i<n; i++, ssrc++, dst++)
01997 {
01998 if (*ssrc==DRMS_MISSING_LONGLONG)
01999 *dst = DRMS_MISSING_FLOAT;
02000 else
02001 {
02002 value = bscale * *ssrc + bzero;
02003 if (!(value < -FLT_MAX || value > FLT_MAX))
02004 *dst = (float) value;
02005 else
02006 {
02007 stat = DRMS_RANGE;
02008 *dst = DRMS_MISSING_FLOAT;
02009 }
02010 }
02011 }
02012 }
02013 break;
02014 case DRMS_TYPE_FLOAT:
02015 {
02016 float *ssrc = (float *) src;
02017 stat = DRMS_SUCCESS;
02018 for (i=0; i<n; i++, ssrc++, dst++)
02019 {
02020 if (isnan(*ssrc))
02021 *dst = DRMS_MISSING_FLOAT;
02022 else
02023 {
02024 value = bscale * *ssrc + bzero;
02025 if (!(value < -FLT_MAX || value > FLT_MAX))
02026 *dst = (float) value;
02027 else
02028 {
02029 stat = DRMS_RANGE;
02030 *dst = DRMS_MISSING_FLOAT;
02031 }
02032 }
02033 }
02034 }
02035 break;
02036 case DRMS_TYPE_TIME:
02037 {
02038 double *ssrc = (double *) src;
02039 stat = DRMS_SUCCESS;
02040 for (i=0; i<n; i++, ssrc++, dst++)
02041 {
02042 if (*ssrc==DRMS_MISSING_TIME)
02043 *dst = DRMS_MISSING_FLOAT;
02044 else
02045 {
02046 value = bscale * *ssrc + bzero;
02047 if (!(value < -FLT_MAX || value > FLT_MAX))
02048 *dst = (float) value;
02049 else
02050 {
02051 stat = DRMS_RANGE;
02052 *dst = DRMS_MISSING_FLOAT;
02053 }
02054 }
02055 }
02056 }
02057 case DRMS_TYPE_DOUBLE:
02058 {
02059 double *ssrc = (double *) src;
02060 stat = DRMS_SUCCESS;
02061 for (i=0; i<n; i++, ssrc++, dst++)
02062 {
02063 if (isnan(*ssrc))
02064 *dst = DRMS_MISSING_FLOAT;
02065 else
02066 {
02067 value = bscale * *ssrc + bzero;
02068 if (!(value < -FLT_MAX || value > FLT_MAX))
02069 *dst = (float) value;
02070 else
02071 {
02072 stat = DRMS_RANGE;
02073 *dst = DRMS_MISSING_FLOAT;
02074 }
02075 }
02076 }
02077 }
02078 break;
02079 case DRMS_TYPE_STRING:
02080 {
02081 char *endptr;
02082 char **ssrc = (char **) src;
02083 stat = DRMS_SUCCESS;
02084 for (i=0; i<n; i++, ssrc++, dst++)
02085 {
02086 value = strtod(*ssrc,&endptr);
02087 if (value==0.0 && endptr==*ssrc )
02088 {
02089 stat = DRMS_BADSTRING;
02090 *dst = DRMS_MISSING_FLOAT;
02091 }
02092 else if ((value == HUGE_VALF || value == -HUGE_VALF) && errno==ERANGE)
02093 {
02094 stat = DRMS_RANGE;
02095 *dst = DRMS_MISSING_FLOAT;
02096 }
02097 else
02098 {
02099 value = bscale*value + bzero;
02100 if ( value < -FLT_MAX || value > FLT_MAX)
02101 {
02102 stat = DRMS_RANGE;
02103 *dst = DRMS_MISSING_FLOAT;
02104 }
02105 else
02106 *dst = (float) value;
02107 }
02108 }
02109 }
02110 break;
02111 default:
02112 stat = DRMS_ERROR_INVALIDTYPE;
02113 }
02114 }
02115 return stat;
02116 }
02117
02118
02119 int drms_array2double(arraylen_t n, DRMS_Type_t src_type, double bzero, double bscale,
02120 void *src, double *dst)
02121 {
02122 long double value;
02123 int stat;
02124 arraylen_t i;
02125
02126 if (fabs(bzero)==0.0 && bscale==1.0)
02127 {
02128 switch(src_type)
02129 {
02130 case DRMS_TYPE_CHAR:
02131 {
02132 char *ssrc = (char *) src;
02133 stat = DRMS_SUCCESS;
02134 for (i=0; i<n; i++, ssrc++, dst++)
02135 if (*ssrc == DRMS_MISSING_CHAR)
02136 *dst = DRMS_MISSING_DOUBLE;
02137 else
02138 *dst = (double) *ssrc;
02139 }
02140 break;
02141 case DRMS_TYPE_SHORT:
02142 {
02143 short *ssrc = (short *) src;
02144 stat = DRMS_SUCCESS;
02145 for (i=0; i<n; i++, ssrc++, dst++)
02146 if (*ssrc == DRMS_MISSING_SHORT)
02147 *dst = DRMS_MISSING_DOUBLE;
02148 else
02149 *dst = (double) *ssrc;
02150 }
02151 break;
02152 case DRMS_TYPE_INT:
02153 {
02154 int *ssrc = (int *) src;
02155 stat = DRMS_SUCCESS;
02156 for (i=0; i<n; i++, ssrc++, dst++)
02157 if (*ssrc == DRMS_MISSING_INT)
02158 *dst = DRMS_MISSING_DOUBLE;
02159 else
02160 *dst = (double) *ssrc;
02161 }
02162 break;
02163 case DRMS_TYPE_LONGLONG:
02164 {
02165 long long *ssrc = (long long *) src;
02166 stat = DRMS_SUCCESS;
02167 for (i=0; i<n; i++, ssrc++, dst++)
02168 if (*ssrc == DRMS_MISSING_LONGLONG)
02169 *dst = DRMS_MISSING_DOUBLE;
02170 else
02171 *dst = (double) *ssrc;
02172 }
02173 break;
02174 case DRMS_TYPE_FLOAT:
02175 {
02176 float *ssrc = (float *) src;
02177 stat = DRMS_SUCCESS;
02178 for (i=0; i<n; i++, ssrc++, dst++)
02179 if (isnan(*ssrc))
02180 *dst = DRMS_MISSING_DOUBLE;
02181 else
02182 *dst = (double) *ssrc;
02183 }
02184 break;
02185 case DRMS_TYPE_TIME:
02186 {
02187 double *ssrc = (double *) src;
02188 stat = DRMS_SUCCESS;
02189 for (i=0; i<n; i++, ssrc++, dst++)
02190 if (*ssrc == DRMS_MISSING_TIME)
02191 *dst = DRMS_MISSING_DOUBLE;
02192 else
02193 *dst = (double) *ssrc;
02194 }
02195 break;
02196 case DRMS_TYPE_DOUBLE:
02197 stat = DRMS_SUCCESS;
02198 memcpy(dst,src,n*sizeof(double));
02199 break;
02200 case DRMS_TYPE_STRING:
02201 {
02202 double val;
02203 char *endptr;
02204 char **ssrc = (char **) src;
02205 stat = DRMS_SUCCESS;
02206 for (i=0; i<n; i++, ssrc++, dst++)
02207 {
02208 val = strtod(*ssrc, &endptr);
02209 if (val==0 && endptr==*ssrc )
02210 {
02211 stat = DRMS_BADSTRING;
02212 *dst = DRMS_MISSING_DOUBLE;
02213 }
02214 else if ( (val == HUGE_VAL || val == -HUGE_VAL) && errno==ERANGE)
02215 {
02216 stat = DRMS_RANGE;
02217 *dst = DRMS_MISSING_DOUBLE;
02218 }
02219 else
02220 *dst = val;
02221 }
02222 }
02223 break;
02224 default:
02225 stat = DRMS_ERROR_INVALIDTYPE;
02226 }
02227 }
02228 else
02229 {
02230 switch(src_type)
02231 {
02232 case DRMS_TYPE_CHAR:
02233 {
02234 char *ssrc = (char *) src;
02235 stat = DRMS_SUCCESS;
02236 for (i=0; i<n; i++, ssrc++, dst++)
02237 {
02238 if (*ssrc==DRMS_MISSING_CHAR)
02239 *dst = DRMS_MISSING_DOUBLE;
02240 else
02241 {
02242 value = bscale * *ssrc + bzero;
02243 if (!(value < -DBL_MAX || value > DBL_MAX))
02244 *dst = (double) value;
02245 else
02246 {
02247 stat = DRMS_RANGE;
02248 *dst = DRMS_MISSING_DOUBLE;
02249 }
02250 }
02251 }
02252 }
02253 break;
02254 case DRMS_TYPE_SHORT:
02255 {
02256 short *ssrc = (short *) src;
02257 stat = DRMS_SUCCESS;
02258 for (i=0; i<n; i++, ssrc++, dst++)
02259 {
02260 if (*ssrc==DRMS_MISSING_SHORT)
02261 *dst = DRMS_MISSING_DOUBLE;
02262 else
02263 {
02264 value = bscale * *ssrc + bzero;
02265 if (!(value < -DBL_MAX || value > DBL_MAX))
02266 *dst = (double) value;
02267 else
02268 {
02269 stat = DRMS_RANGE;
02270 *dst = DRMS_MISSING_DOUBLE;
02271 }
02272 }
02273 }
02274 }
02275 break;
02276 case DRMS_TYPE_INT:
02277 {
02278 int *ssrc = (int *) src;
02279 stat = DRMS_SUCCESS;
02280 for (i=0; i<n; i++, ssrc++, dst++)
02281 {
02282 if (*ssrc==DRMS_MISSING_INT)
02283 *dst = DRMS_MISSING_DOUBLE;
02284 else
02285 {
02286 value = bscale * *ssrc + bzero;
02287 if (!(value < -DBL_MAX || value > DBL_MAX))
02288 *dst = (double) value;
02289 else
02290 {
02291 stat = DRMS_RANGE;
02292 *dst = DRMS_MISSING_DOUBLE;
02293 }
02294 }
02295 }
02296 }
02297 break;
02298 case DRMS_TYPE_LONGLONG:
02299 {
02300 long long *ssrc = (long long *) src;
02301 stat = DRMS_SUCCESS;
02302 for (i=0; i<n; i++, ssrc++, dst++)
02303 {
02304 if (*ssrc==DRMS_MISSING_LONGLONG)
02305 *dst = DRMS_MISSING_DOUBLE;
02306 else
02307 {
02308 value = bscale * *ssrc + bzero;
02309 if (!(value < -DBL_MAX || value > DBL_MAX))
02310 *dst = (double) value;
02311 else
02312 {
02313 stat = DRMS_RANGE;
02314 *dst = DRMS_MISSING_DOUBLE;
02315 }
02316 }
02317 }
02318 }
02319 break;
02320 case DRMS_TYPE_FLOAT:
02321 {
02322 float *ssrc = (float *) src;
02323 stat = DRMS_SUCCESS;
02324 for (i=0; i<n; i++, ssrc++, dst++)
02325 {
02326 if (isnan(*ssrc))
02327 *dst = DRMS_MISSING_DOUBLE;
02328 else
02329 {
02330 value = bscale * *ssrc + bzero;
02331 if (!(value < -DBL_MAX || value > DBL_MAX))
02332 *dst = (double) value;
02333 else
02334 {
02335 stat = DRMS_RANGE;
02336 *dst = DRMS_MISSING_DOUBLE;
02337 }
02338 }
02339 }
02340 }
02341 break;
02342 case DRMS_TYPE_TIME:
02343 {
02344 double *ssrc = (double *) src;
02345 stat = DRMS_SUCCESS;
02346 for (i=0; i<n; i++, ssrc++, dst++)
02347 {
02348 if (*ssrc==DRMS_MISSING_TIME)
02349 *dst = DRMS_MISSING_TIME;
02350 else
02351 {
02352 value = bscale * *ssrc + bzero;
02353 if (!(value < -DBL_MAX || value > DBL_MAX))
02354 *dst = (double) value;
02355 else
02356 {
02357 stat = DRMS_RANGE;
02358 *dst = DRMS_MISSING_TIME;
02359 }
02360 }
02361 }
02362 }
02363 break;
02364 case DRMS_TYPE_DOUBLE:
02365 {
02366 double *ssrc = (double *) src;
02367 stat = DRMS_SUCCESS;
02368 for (i=0; i<n; i++, ssrc++, dst++)
02369 {
02370 if (isnan(*ssrc))
02371 *dst = DRMS_MISSING_DOUBLE;
02372 else
02373 {
02374 value = bscale * *ssrc + bzero;
02375 if (!(value < -DBL_MAX || value > DBL_MAX))
02376 *dst = (double) value;
02377 else
02378 {
02379 stat = DRMS_RANGE;
02380 *dst = DRMS_MISSING_DOUBLE;
02381 }
02382 }
02383 }
02384 }
02385 break;
02386 case DRMS_TYPE_STRING:
02387 {
02388 char *endptr;
02389 char **ssrc = (char **) src;
02390 stat = DRMS_SUCCESS;
02391 for (i=0; i<n; i++, ssrc++, dst++)
02392 {
02393 value = strtold(*ssrc,&endptr);
02394 if (value==0.0 && endptr==*ssrc )
02395 {
02396 stat = DRMS_BADSTRING;
02397 *dst = DRMS_MISSING_DOUBLE;
02398 }
02399 else if ((value == HUGE_VAL || value == -HUGE_VAL) && errno==ERANGE)
02400 {
02401 stat = DRMS_RANGE;
02402 *dst = DRMS_MISSING_DOUBLE;
02403 }
02404 else
02405 {
02406 value = bscale*value + bzero;
02407 if ( value < -DBL_MAX || value > DBL_MAX)
02408 {
02409 stat = DRMS_RANGE;
02410 *dst = DRMS_MISSING_DOUBLE;
02411 }
02412 else
02413 *dst = (double) value;
02414 }
02415 }
02416 }
02417 break;
02418 default:
02419 stat = DRMS_ERROR_INVALIDTYPE;
02420 }
02421 }
02422 return stat;
02423 }
02424
02425
02426
02427 int drms_array2time(arraylen_t n, DRMS_Type_t src_type, double bzero, double bscale,
02428 void *src, double *dst)
02429 {
02430 long double value;
02431 int stat;
02432 arraylen_t i;
02433
02434 if (fabs(bzero)==0.0 && bscale==1.0)
02435 {
02436 switch(src_type)
02437 {
02438 case DRMS_TYPE_CHAR:
02439 {
02440 char *ssrc = (char *) src;
02441 stat = DRMS_SUCCESS;
02442 for (i=0; i<n; i++, ssrc++, dst++)
02443 if (*ssrc == DRMS_MISSING_CHAR)
02444 *dst = DRMS_MISSING_TIME;
02445 else
02446 *dst = (double) *ssrc;
02447 }
02448 break;
02449 case DRMS_TYPE_SHORT:
02450 {
02451 short *ssrc = (short *) src;
02452 stat = DRMS_SUCCESS;
02453 for (i=0; i<n; i++, ssrc++, dst++)
02454 if (*ssrc == DRMS_MISSING_SHORT)
02455 *dst = DRMS_MISSING_TIME;
02456 else
02457 *dst = (double) *ssrc;
02458 }
02459 break;
02460 case DRMS_TYPE_INT:
02461 {
02462 int *ssrc = (int *) src;
02463 stat = DRMS_SUCCESS;
02464 for (i=0; i<n; i++, ssrc++, dst++)
02465 if (*ssrc == DRMS_MISSING_INT)
02466 *dst = DRMS_MISSING_TIME;
02467 else
02468 *dst = (double) *ssrc;
02469 }
02470 break;
02471 case DRMS_TYPE_LONGLONG:
02472 {
02473 long long *ssrc = (long long *) src;
02474 stat = DRMS_SUCCESS;
02475 for (i=0; i<n; i++, ssrc++, dst++)
02476 if (*ssrc == DRMS_MISSING_LONGLONG)
02477 *dst = DRMS_MISSING_TIME;
02478 else
02479 *dst = (double) *ssrc;
02480 }
02481 break;
02482 case DRMS_TYPE_FLOAT:
02483 {
02484 float *ssrc = (float *) src;
02485 stat = DRMS_SUCCESS;
02486 for (i=0; i<n; i++, ssrc++, dst++)
02487 if (isnan(*ssrc))
02488 *dst = DRMS_MISSING_TIME;
02489 else
02490 *dst = (double) *ssrc;
02491 }
02492 break;
02493 case DRMS_TYPE_TIME:
02494 stat = DRMS_SUCCESS;
02495 memcpy(dst,src,n*sizeof(double));
02496 break;
02497 case DRMS_TYPE_DOUBLE:
02498 {
02499 double *ssrc = (double *) src;
02500 stat = DRMS_SUCCESS;
02501 for (i=0; i<n; i++, ssrc++, dst++)
02502 if (isnan(*ssrc))
02503 *dst = DRMS_MISSING_TIME;
02504 else
02505 *dst = (double) *ssrc;
02506 }
02507 break;
02508 case DRMS_TYPE_STRING:
02509 {
02510 double val;
02511 char **ssrc = (char **) src;
02512 stat = DRMS_SUCCESS;
02513 for (i=0; i<n; i++, ssrc++, dst++)
02514 {
02515 val = sscan_time(*ssrc);
02516 if (val < 0)
02517 {
02518 stat = DRMS_BADSTRING;
02519 *dst = DRMS_MISSING_TIME;
02520 }
02521 else
02522 *dst = val;
02523 }
02524 }
02525 break;
02526 default:
02527 stat = DRMS_ERROR_INVALIDTYPE;
02528 }
02529 }
02530 else
02531 {
02532 switch(src_type)
02533 {
02534 case DRMS_TYPE_CHAR:
02535 {
02536 char *ssrc = (char *) src;
02537 stat = DRMS_SUCCESS;
02538 for (i=0; i<n; i++, ssrc++, dst++)
02539 {
02540 if (*ssrc==DRMS_MISSING_CHAR)
02541 *dst = DRMS_MISSING_TIME;
02542 else
02543 {
02544 value = bscale * *ssrc + bzero;
02545 if (!(value < -DBL_MAX || value > DBL_MAX))
02546 *dst = (double) value;
02547 else
02548 {
02549 stat = DRMS_RANGE;
02550 *dst = DRMS_MISSING_TIME;
02551 }
02552 }
02553 }
02554 }
02555 break;
02556 case DRMS_TYPE_SHORT:
02557 {
02558 short *ssrc = (short *) src;
02559 stat = DRMS_SUCCESS;
02560 for (i=0; i<n; i++, ssrc++, dst++)
02561 {
02562 if (*ssrc==DRMS_MISSING_SHORT)
02563 *dst = DRMS_MISSING_TIME;
02564 else
02565 {
02566 value = bscale * *ssrc + bzero;
02567 if (!(value < -DBL_MAX || value > DBL_MAX))
02568 *dst = (double) value;
02569 else
02570 {
02571 stat = DRMS_RANGE;
02572 *dst = DRMS_MISSING_TIME;
02573 }
02574 }
02575 }
02576 }
02577 break;
02578 case DRMS_TYPE_INT:
02579 {
02580 int *ssrc = (int *) src;
02581 stat = DRMS_SUCCESS;
02582 for (i=0; i<n; i++, ssrc++, dst++)
02583 {
02584 if (*ssrc==DRMS_MISSING_INT)
02585 *dst = DRMS_MISSING_TIME;
02586 else
02587 {
02588 value = bscale * *ssrc + bzero;
02589 if (!(value < -DBL_MAX || value > DBL_MAX))
02590 *dst = (double) value;
02591 else
02592 {
02593 stat = DRMS_RANGE;
02594 *dst = DRMS_MISSING_TIME;
02595 }
02596 }
02597 }
02598 }
02599 break;
02600 case DRMS_TYPE_LONGLONG:
02601 {
02602 long long *ssrc = (long long *) src;
02603 stat = DRMS_SUCCESS;
02604 for (i=0; i<n; i++, ssrc++, dst++)
02605 {
02606 if (*ssrc==DRMS_MISSING_LONGLONG)
02607 *dst = DRMS_MISSING_TIME;
02608 else
02609 {
02610 value = bscale * *ssrc + bzero;
02611 if (!(value < -DBL_MAX || value > DBL_MAX))
02612 *dst = (double) value;
02613 else
02614 {
02615 stat = DRMS_RANGE;
02616 *dst = DRMS_MISSING_TIME;
02617 }
02618 }
02619 }
02620 }
02621 break;
02622 case DRMS_TYPE_FLOAT:
02623 {
02624 float *ssrc = (float *) src;
02625 stat = DRMS_SUCCESS;
02626 for (i=0; i<n; i++, ssrc++, dst++)
02627 {
02628 if (isnan(*ssrc))
02629 *dst = DRMS_MISSING_TIME;
02630 else
02631 {
02632 value = bscale * *ssrc + bzero;
02633 if (!(value < -DBL_MAX || value > DBL_MAX))
02634 *dst = (double) value;
02635 else
02636 {
02637 stat = DRMS_RANGE;
02638 *dst = DRMS_MISSING_TIME;
02639 }
02640 }
02641 }
02642 }
02643 break;
02644 case DRMS_TYPE_TIME:
02645 {
02646 double *ssrc = (double *) src;
02647 stat = DRMS_SUCCESS;
02648 for (i=0; i<n; i++, ssrc++, dst++)
02649 {
02650 if (*ssrc==DRMS_MISSING_TIME)
02651 *dst = DRMS_MISSING_TIME;
02652 else
02653 {
02654 value = bscale * *ssrc + bzero;
02655 if (!(value < -DBL_MAX || value > DBL_MAX))
02656 *dst = (double) value;
02657 else
02658 {
02659 stat = DRMS_RANGE;
02660 *dst = DRMS_MISSING_TIME;
02661 }
02662 }
02663 }
02664 }
02665 case DRMS_TYPE_DOUBLE:
02666 {
02667 double *ssrc = (double *) src;
02668 stat = DRMS_SUCCESS;
02669 for (i=0; i<n; i++, ssrc++, dst++)
02670 {
02671 if (isnan(*ssrc))
02672 *dst = DRMS_MISSING_TIME;
02673 else
02674 {
02675 value = bscale * *ssrc + bzero;
02676 if (!(value < -DBL_MAX || value > DBL_MAX))
02677 *dst = (double) value;
02678 else
02679 {
02680 stat = DRMS_RANGE;
02681 *dst = DRMS_MISSING_TIME;
02682 }
02683 }
02684 }
02685 }
02686 break;
02687 case DRMS_TYPE_STRING:
02688 {
02689 char **ssrc = (char **) src;
02690 stat = DRMS_SUCCESS;
02691 for (i=0; i<n; i++, ssrc++, dst++)
02692 {
02693 value = (long double)sscan_time(*ssrc);
02694 if (value < 0)
02695 {
02696 stat = DRMS_BADSTRING;
02697 *dst = DRMS_MISSING_TIME;
02698 }
02699 else
02700 {
02701 value = bscale*value + bzero;
02702 if ( value < -DBL_MAX || value > DBL_MAX)
02703 {
02704 stat = DRMS_RANGE;
02705 *dst = DRMS_MISSING_TIME;
02706 }
02707 else
02708 *dst = (double) value;
02709 }
02710 }
02711 }
02712 break;
02713 default:
02714 stat = DRMS_ERROR_INVALIDTYPE;
02715 }
02716 }
02717
02718 return stat;
02719 }
02720
02721
02722 int drms_array2string(arraylen_t n, DRMS_Type_t src_type, double bzero, double bscale,
02723 void *src, char **dst)
02724 {
02725 int stat;
02726 arraylen_t i;
02727 char *result;
02728
02729 result = NULL;
02730 stat = DRMS_SUCCESS;
02731 dst = malloc(n*sizeof(char *));
02732 XASSERT(dst);
02733 switch(src_type)
02734 {
02735 case DRMS_TYPE_CHAR:
02736 { char *ssrc = (char *) src;
02737 for (i=0; i<n; i++, ssrc++, dst++)
02738 {
02739 *dst = malloc(DRMS_ARRAY2STRING_LEN);
02740 XASSERT(*dst);
02741 if (*ssrc==DRMS_MISSING_CHAR)
02742 **dst=0;
02743 else
02744 CHECKSNPRINTF(snprintf(*dst, 2, "%24.17lg", bscale* *ssrc + bzero), 2);
02745 }
02746 }
02747 break;
02748 case DRMS_TYPE_SHORT:
02749 { short *ssrc = (short *) src;
02750 for (i=0; i<n; i++, ssrc++, dst++)
02751 {
02752 *dst = malloc(DRMS_ARRAY2STRING_LEN);
02753 XASSERT(*dst);
02754 if (*ssrc==DRMS_MISSING_SHORT)
02755 **dst=0;
02756 else
02757 CHECKSNPRINTF(snprintf(*dst, 2, "%24.17lg", bscale* *ssrc + bzero), 2);
02758 }
02759 }
02760 break;
02761 case DRMS_TYPE_INT:
02762 { int *ssrc = (int *) src;
02763 for (i=0; i<n; i++, ssrc++, dst++)
02764 {
02765 *dst = malloc(DRMS_ARRAY2STRING_LEN);
02766 XASSERT(*dst);
02767 if (*ssrc==DRMS_MISSING_INT)
02768 **dst=0;
02769 else
02770 CHECKSNPRINTF(snprintf(*dst, 2, "%24.17lg", bscale* *ssrc + bzero), 2);
02771 }
02772 }
02773 break;
02774 case DRMS_TYPE_LONGLONG:
02775 { long long *ssrc = (long long *) src;
02776 for (i=0; i<n; i++, ssrc++, dst++)
02777 {
02778 *dst = malloc(DRMS_ARRAY2STRING_LEN);
02779 XASSERT(*dst);
02780 if (*ssrc==DRMS_MISSING_LONGLONG)
02781 **dst=0;
02782 else
02783 CHECKSNPRINTF(snprintf(*dst, 2, "%24.17lg", bscale* *ssrc + bzero), 2);
02784 }
02785 }
02786 break;
02787 case DRMS_TYPE_FLOAT:
02788 { float *ssrc = (float *) src;
02789 for (i=0; i<n; i++, ssrc++, dst++)
02790 {
02791 *dst = malloc(DRMS_ARRAY2STRING_LEN);
02792 XASSERT(*dst);
02793 if (isnan(*ssrc))
02794 **dst=0;
02795 else
02796 CHECKSNPRINTF(snprintf(*dst, 2, "%24.17lg", bscale* *ssrc + bzero), 2);
02797 }
02798 }
02799 break;
02800 case DRMS_TYPE_DOUBLE:
02801 { double *ssrc = (double *) src;
02802 for (i=0; i<n; i++, ssrc++, dst++)
02803 {
02804 *dst = malloc(DRMS_ARRAY2STRING_LEN);
02805 XASSERT(*dst);
02806 if (isnan(*ssrc))
02807 **dst=0;
02808 else
02809 CHECKSNPRINTF(snprintf(*dst, 2, "%24.17lg", bscale* *ssrc + bzero), 2);
02810 }
02811 }
02812 break;
02813 case DRMS_TYPE_TIME:
02814 { double *ssrc = (double *) src;
02815 for (i=0; i<n; i++, ssrc++, dst++)
02816 {
02817 *dst = malloc(DRMS_ARRAY2STRING_LEN);
02818 XASSERT(*dst);
02819 if (*ssrc==DRMS_MISSING_TIME)
02820 **dst=0;
02821 else
02822 sprint_time(*dst, bscale * *ssrc + bzero, "TAI", 0);
02823 }
02824 }
02825 break;
02826 case DRMS_TYPE_STRING:
02827 { char **ssrc = (char **) src;
02828 for (i=0; i<n; i++, ssrc++, dst++)
02829 copy_string(dst, *ssrc);
02830 }
02831 default:
02832 stat = DRMS_ERROR_INVALIDTYPE;
02833 }
02834 return stat;
02835 }