Line data Source code
1 : #include <float.h> 2 : #include <stdio.h> 3 : #include <stdlib.h> 4 : #include "grib2.h" 5 : 6 : #ifndef DoubleToFloatClamp_defined 7 : #define DoubleToFloatClamp_defined 8 72 : static float DoubleToFloatClamp(double val) { 9 72 : if (val >= FLT_MAX) return FLT_MAX; 10 72 : if (val <= -FLT_MAX) return -FLT_MAX; 11 72 : return (float)val; 12 : } 13 : #endif 14 : 15 36 : g2int simunpack(unsigned char *cpack,g2int cpack_length,g2int *idrstmpl,g2int ndpts,g2float *fld) 16 : ////$$$ SUBPROGRAM DOCUMENTATION BLOCK 17 : // . . . . 18 : // SUBPROGRAM: simunpack 19 : // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-10-29 20 : // 21 : // ABSTRACT: This subroutine unpacks a data field that was packed using a 22 : // simple packing algorithm as defined in the GRIB2 documentation, 23 : // using info from the GRIB2 Data Representation Template 5.0. 24 : // 25 : // PROGRAM HISTORY LOG: 26 : // 2002-10-29 Gilbert 27 : // 28 : // USAGE: int simunpack(unsigned char *cpack,g2int *idrstmpl,g2int ndpts, 29 : // g2float *fld) 30 : // INPUT ARGUMENT LIST: 31 : // cpack - pointer to the packed data field. 32 : // idrstmpl - pointer to the array of values for Data Representation 33 : // Template 5.0 34 : // ndpts - The number of data values to unpack 35 : // 36 : // OUTPUT ARGUMENT LIST: 37 : // fld - Contains the unpacked data values. fld must be allocated 38 : // with at least ndpts*sizeof(g2float) bytes before 39 : // calling this routine. 40 : // 41 : // REMARKS: None 42 : // 43 : // ATTRIBUTES: 44 : // LANGUAGE: C 45 : // MACHINE: 46 : // 47 : //$$$// 48 : { 49 : 50 : g2int *ifld; 51 : g2int j,nbits /* ,itype */; 52 : g2float ref,bscale,dscale; 53 : 54 36 : rdieee(idrstmpl+0,&ref,1); 55 36 : bscale = DoubleToFloatClamp(int_power(2.0,idrstmpl[1])); 56 36 : dscale = DoubleToFloatClamp(int_power(10.0,-idrstmpl[2])); 57 36 : nbits = idrstmpl[3]; 58 : /* itype = idrstmpl[4]; */ 59 : 60 36 : ifld=(g2int *)calloc(ndpts,sizeof(g2int)); 61 36 : if ( ifld == 0 ) { 62 0 : fprintf(stderr,"Could not allocate space in simunpack.\n" 63 : "Data field NOT unpacked.\n"); 64 0 : return(1); 65 : } 66 : 67 : // 68 : // if nbits equals 0, we have a constant field where the reference value 69 : // is the data value at each gridpoint 70 : // 71 36 : if (nbits != 0) { 72 33 : gbits(cpack,cpack_length,ifld,0,nbits,0,ndpts); 73 35359 : for (j=0;j<ndpts;j++) { 74 35326 : fld[j]=(((g2float)ifld[j]*bscale)+ref)*dscale; 75 : } 76 : } 77 : else { 78 6 : for (j=0;j<ndpts;j++) { 79 3 : fld[j]=ref * dscale; 80 : } 81 : } 82 : 83 36 : free(ifld); 84 36 : return(0); 85 : }