void misspack(g2float *fld,g2int ndpts,g2int idrsnum,g2int *idrstmpl, unsigned char *cpack, g2int *lcpack) /*$$$ SUBPROGRAM DOCUMENTATION BLOCK // . . . . // SUBPROGRAM: misspack // PRGMMR: Gilbert ORG: W/NP11 DATE: 2000-06-21 // // ABSTRACT: This subroutine packs up a data field using a complex // packing algorithm as defined in the GRIB2 documention. It // supports GRIB2 complex packing templates with or without // spatial differences (i.e. DRTs 5.2 and 5.3). // It also fills in GRIB2 Data Representation Template 5.2 or 5.3 // with the appropriate values. // This version assumes that Missing Value Management is being used and that // 1 or 2 missing values appear in the data. // // PROGRAM HISTORY LOG: // 2000-06-21 Gilbert // // USAGE: misspack(g2float *fld,g2int ndpts,g2int idrsnum,g2int *idrstmpl, // unsigned char *cpack, g2int *lcpack) // INPUT ARGUMENT LIST: // fld[] - Contains the data values to pack // ndpts - The number of data values in array fld[] // idrsnum - Data Representation Template number 5.N // Must equal 2 or 3. // idrstmpl - Contains the array of values for Data Representation // Template 5.2 or 5.3 // [0] = Reference value - ignored on input // [1] = Binary Scale Factor // [2] = Decimal Scale Factor // . // . // [6] = Missing value management // [7] = Primary missing value // [8] = Secondary missing value // . // . // [16] = Order of Spatial Differencing ( 1 or 2 ) // . // . // // OUTPUT ARGUMENT LIST: // idrstmpl - Contains the array of values for Data Representation // Template 5.3 // [0] = Reference value - set by misspack routine. // [1] = Binary Scale Factor - unchanged from input // [2] = Decimal Scale Factor - unchanged from input // . // . // cpack - The packed data field (character*1 array) // *lcpack - length of packed field cpack(). // // REMARKS: None // // ATTRIBUTES: // LANGUAGE: C // MACHINE: // //$$$*/ { g2int *ifld, *ifldmiss, *jfld; g2int *jmin, *jmax, *lbit; static g2int zero=0; g2int *gref, *gwidth, *glen; g2int glength, grpwidth; g2int i, n, iofst, imin, ival1, ival2, isd, minsd, nbitsd; g2int nbitsgref, left, iwmax, ngwidthref, nbitsgwidth, ilmax; g2int nglenref, nglenlast, nbitsglen, ij; g2int j, missopt, nonmiss, itemp, maxorig, nbitorig, miss1, miss2; g2int ngroups, ng, num0, num1, num2; g2int imax, lg, mtemp, ier, igmax; g2int kfildo, minpk, inc, maxgrps, ibit, jbit, kbit, novref, lbitref; g2float rmissp, rmisss, bscale, dscale, rmin, temp; static g2int simple_alg = 0; static g2float alog2=0.69314718; /* ln(2.0) */ static g2int one=1; bscale=int_power(2.0,-idrstmpl[1]); dscale=int_power(10.0,idrstmpl[2]); missopt=idrstmpl[6]; if ( missopt != 1 && missopt != 2 ) { printf("misspack: Unrecognized option.\n"); *lcpack=-1; return; } else { /* Get missing values */ g2_rdieee(idrstmpl+7,&rmissp,1); if (missopt == 2) g2_rdieee(idrstmpl+8,&rmisss,1); } /* // Find min value of non-missing values in the data, // AND set up missing value mapping of the field. */ ifldmiss = calloc(ndpts,sizeof(g2int)); rmin=1E+37; if ( missopt == 1 ) { /* Primary missing value only */ for ( j=0; j<ndpts; j++) { if (fld[j] == rmissp) { ifldmiss[j]=1; } else { ifldmiss[j]=0; if (fld[j] < rmin) rmin=fld[j]; } } } if ( missopt == 2 ) { /* Primary and secondary missing values */ for ( j=0; j<ndpts; j++ ) { if (fld[j] == rmissp) { ifldmiss[j]=1; } else if (fld[j] == rmisss) { ifldmiss[j]=2; } else { ifldmiss[j]=0; if (fld[j] < rmin) rmin=fld[j]; } } } /* // Allocate work arrays: // Note: -ifldmiss[j],j=0,ndpts-1 is a map of original field indicating // which of the original data values // are primary missing (1), sencondary missing (2) or non-missing (0). // -jfld[j],j=0,nonmiss-1 is a subarray of just the non-missing values // from the original field. */ /*if (rmin != rmax) { */ iofst=0; ifld = calloc(ndpts,sizeof(g2int)); jfld = calloc(ndpts,sizeof(g2int)); gref = calloc(ndpts,sizeof(g2int)); gwidth = calloc(ndpts,sizeof(g2int)); glen = calloc(ndpts,sizeof(g2int)); /* // Scale original data */ nonmiss=0; if (idrstmpl[1] == 0) { /* No binary scaling */ imin=(g2int)rint(rmin*dscale); /*imax=(g2int)rint(rmax*dscale); */ rmin=(g2float)imin; for ( j=0; j<ndpts; j++) { if (ifldmiss[j] == 0) { jfld[nonmiss]=(g2int)rint(fld[j]*dscale)-imin; nonmiss++; } } } else { /* Use binary scaling factor */ rmin=rmin*dscale; /*rmax=rmax*dscale; */ for ( j=0; j<ndpts; j++ ) { if (ifldmiss[j] == 0) { jfld[nonmiss]=(g2int)rint(((fld[j]*dscale)-rmin)*bscale); nonmiss++; } } } /* // Calculate Spatial differences, if using DRS Template 5.3 */ if (idrsnum == 3) { /* spatial differences */ if (idrstmpl[16]!=1 && idrstmpl[16]!=2) idrstmpl[16]=2; if (idrstmpl[16] == 1) { /* first order */ ival1=jfld[0]; for ( j=nonmiss-1; j>0; j--) jfld[j]=jfld[j]-jfld[j-1]; jfld[0]=0; } else if (idrstmpl[16] == 2) { /* second order */ ival1=jfld[0]; ival2=jfld[1]; for ( j=nonmiss-1; j>1; j--) jfld[j]=jfld[j]-(2*jfld[j-1])+jfld[j-2]; jfld[0]=0; jfld[1]=0; } /* // subtract min value from spatial diff field */ isd=idrstmpl[16]; minsd=jfld[isd]; for ( j=isd; j<nonmiss; j++ ) if ( jfld[j] < minsd ) minsd=jfld[j]; for ( j=isd; j<nonmiss; j++ ) jfld[j]=jfld[j]-minsd; /* // find num of bits need to store minsd and add 1 extra bit // to indicate sign */ temp=log((double)(abs(minsd)+1))/alog2; nbitsd=(g2int)ceil(temp)+1; /* // find num of bits need to store ifld[0] ( and ifld[1] // if using 2nd order differencing ) */ maxorig=ival1; if (idrstmpl[16]==2 && ival2>ival1) maxorig=ival2; temp=log((double)(maxorig+1))/alog2; nbitorig=(g2int)ceil(temp)+1; if (nbitorig > nbitsd) nbitsd=nbitorig; /* increase number of bits to even multiple of 8 ( octet ) */ if ( (nbitsd%8) != 0) nbitsd=nbitsd+(8-(nbitsd%8)); /* // Store extra spatial differencing info into the packed // data section. */ if (nbitsd != 0) { /* pack first original value */ if (ival1 >= 0) { sbit(cpack,&ival1,iofst,nbitsd); iofst=iofst+nbitsd; } else { sbit(cpack,&one,iofst,1); iofst=iofst+1; itemp=abs(ival1); sbit(cpack,&itemp,iofst,nbitsd-1); iofst=iofst+nbitsd-1; } if (idrstmpl[16] == 2) { /* pack second original value */ if (ival2 >= 0) { sbit(cpack,&ival2,iofst,nbitsd); iofst=iofst+nbitsd; } else { sbit(cpack,&one,iofst,1); iofst=iofst+1; itemp=abs(ival2); sbit(cpack,&itemp,iofst,nbitsd-1); iofst=iofst+nbitsd-1; } } /* pack overall min of spatial differences */ if (minsd >= 0) { sbit(cpack,&minsd,iofst,nbitsd); iofst=iofst+nbitsd; } else { sbit(cpack,&one,iofst,1); iofst=iofst+1; itemp=abs(minsd); sbit(cpack,&itemp,iofst,nbitsd-1); iofst=iofst+nbitsd-1; } } /*print *,'SDp ',ival1,ival2,minsd,nbitsd*/ } /* end of spatial diff section */ /* // Expand non-missing data values to original grid. */ miss1=jfld[0]; for ( j=0; j<nonmiss; j++) if (jfld[j] < miss1) miss1 = jfld[j]; miss1--; miss2=miss1-1; n=0; for ( j=0; j<ndpts; j++) { if ( ifldmiss[j] == 0 ) { ifld[j]=jfld[n]; n++; } else if ( ifldmiss[j] == 1 ) { ifld[j]=miss1; } else if ( ifldmiss[j] == 2 ) { ifld[j]=miss2; } } /* // Determine Groups to be used. */ if ( simple_alg == 1 ) { /* set group length to 10 : calculate number of groups */ /* and length of last group */ ngroups=ndpts/10; for (j=0;j<ngroups;j++) glen[j]=10; itemp=ndpts%10; if (itemp != 0) { ngroups++; glen[ngroups-1]=itemp; } } else { /* Use Dr. Glahn's algorithm for determining grouping. */ kfildo=6; minpk=10; inc=1; maxgrps=(ndpts/minpk)+1; jmin = calloc(maxgrps,sizeof(g2int)); jmax = calloc(maxgrps,sizeof(g2int)); lbit = calloc(maxgrps,sizeof(g2int)); g2_pack_gp(&kfildo,ifld,&ndpts,&missopt,&minpk,&inc,&miss1,&miss2, jmin,jmax,lbit,glen,&maxgrps,&ngroups,&ibit,&jbit, &kbit,&novref,&lbitref,&ier); /*printf("SAGier = %d %d %d %d %d %d\n",ier,ibit,jbit,kbit,novref,lbitref);*/ for ( ng=0; ng<ngroups; ng++) glen[ng]=glen[ng]+novref; free(jmin); free(jmax); free(lbit); } /* // For each group, find the group's reference value (min) // and the number of bits needed to hold the remaining values */ n=0; for ( ng=0; ng<ngroups; ng++) { /* how many of each type? */ num0=num1=num2=0; for (j=n; j<n+glen[ng]; j++) { if (ifldmiss[j] == 0 ) num0++; if (ifldmiss[j] == 1 ) num1++; if (ifldmiss[j] == 2 ) num2++; } if ( num0 == 0 ) { /* all missing values */ if ( num1 == 0 ) { /* all secondary missing */ gref[ng]=-2; gwidth[ng]=0; } else if ( num2 == 0 ) { /* all primary missing */ gref[ng]=-1; gwidth[ng]=0; } else { /* both primary and secondary */ gref[ng]=0; gwidth[ng]=1; } } else { /* contains some non-missing data */ /* find max and min values of group */ gref[ng]=2147483647; imax=-2147483647; j=n; for ( lg=0; lg<glen[ng]; lg++ ) { if ( ifldmiss[j] == 0 ) { if (ifld[j] < gref[ng]) gref[ng]=ifld[j]; if (ifld[j] > imax) imax=ifld[j]; } j++; } if (missopt == 1) imax=imax+1; if (missopt == 2) imax=imax+2; /* calc num of bits needed to hold data */ if ( gref[ng] != imax ) { temp=log((double)(imax-gref[ng]+1))/alog2; gwidth[ng]=(g2int)ceil(temp); } else { gwidth[ng]=0; } } /* Subtract min from data */ j=n; mtemp=(g2int)int_power(2.,gwidth[ng]); for ( lg=0; lg<glen[ng]; lg++ ) { if (ifldmiss[j] == 0) /* non-missing */ ifld[j]=ifld[j]-gref[ng]; else if (ifldmiss[j] == 1) /* primary missing */ ifld[j]=mtemp-1; else if (ifldmiss[j] == 2) /* secondary missing */ ifld[j]=mtemp-2; j++; } /* increment fld array counter */ n=n+glen[ng]; } /* // Find max of the group references and calc num of bits needed // to pack each groups reference value, then // pack up group reference values */ /*printf(" GREFS: "); */ /*for (j=0;j<ngroups;j++) printf(" %d",gref[j]); printf("\n"); */ igmax=gref[0]; for (j=1;j<ngroups;j++) if (gref[j] > igmax) igmax=gref[j]; if (missopt == 1) igmax=igmax+1; if (missopt == 2) igmax=igmax+2; if (igmax != 0) { temp=log((double)(igmax+1))/alog2; nbitsgref=(g2int)ceil(temp); /* reset the ref values of any "missing only" groups. */ mtemp=(g2int)int_power(2.,nbitsgref); for ( j=0; j<ngroups; j++ ) { if (gref[j] == -1) gref[j]=mtemp-1; if (gref[j] == -2) gref[j]=mtemp-2; } sbits(cpack,gref,iofst,nbitsgref,0,ngroups); itemp=nbitsgref*ngroups; iofst=iofst+itemp; /* Pad last octet with Zeros, if necessary, */ if ( (itemp%8) != 0) { left=8-(itemp%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } } else { nbitsgref=0; } /* // Find max/min of the group widths and calc num of bits needed // to pack each groups width value, then // pack up group width values */ /*write(77,*)'GWIDTHS: ',(gwidth(j),j=1,ngroups)*/ iwmax=gwidth[0]; ngwidthref=gwidth[0]; for (j=1;j<ngroups;j++) { if (gwidth[j] > iwmax) iwmax=gwidth[j]; if (gwidth[j] < ngwidthref) ngwidthref=gwidth[j]; } if (iwmax != ngwidthref) { temp=log((double)(iwmax-ngwidthref+1))/alog2; nbitsgwidth=(g2int)ceil(temp); for ( i=0; i<ngroups; i++) gwidth[i]=gwidth[i]-ngwidthref; sbits(cpack,gwidth,iofst,nbitsgwidth,0,ngroups); itemp=nbitsgwidth*ngroups; iofst=iofst+itemp; /* Pad last octet with Zeros, if necessary,*/ if ( (itemp%8) != 0) { left=8-(itemp%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } } else { nbitsgwidth=0; for (i=0;i<ngroups;i++) gwidth[i]=0; } /* // Find max/min of the group lengths and calc num of bits needed // to pack each groups length value, then // pack up group length values */ /*printf(" GLENS: ");*/ /*for (j=0;j<ngroups;j++) printf(" %d",glen[j]); printf("\n");*/ ilmax=glen[0]; nglenref=glen[0]; for (j=1;j<ngroups-1;j++) { if (glen[j] > ilmax) ilmax=glen[j]; if (glen[j] < nglenref) nglenref=glen[j]; } nglenlast=glen[ngroups-1]; if (ilmax != nglenref) { temp=log((double)(ilmax-nglenref+1))/alog2; nbitsglen=(g2int)ceil(temp); for ( i=0; i<ngroups-1; i++) glen[i]=glen[i]-nglenref; sbits(cpack,glen,iofst,nbitsglen,0,ngroups); itemp=nbitsglen*ngroups; iofst=iofst+itemp; /* Pad last octet with Zeros, if necessary,*/ if ( (itemp%8) != 0) { left=8-(itemp%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } } else { nbitsglen=0; for (i=0;i<ngroups;i++) glen[i]=0; } /* // For each group, pack data values */ /*write(77,*)'IFLDS: ',(ifld(j),j=1,ndpts)*/ n=0; ij=0; for ( ng=0; ng<ngroups; ng++) { glength=glen[ng]+nglenref; if (ng == (ngroups-1) ) glength=nglenlast; grpwidth=gwidth[ng]+ngwidthref; /*write(77,*)'NGP ',ng,grpwidth,glength,gref(ng)*/ if ( grpwidth != 0 ) { sbits(cpack,ifld+n,iofst,grpwidth,0,glength); iofst=iofst+(grpwidth*glength); } /* do kk=1,glength*/ /* ij=ij+1*/ /*write(77,*)'SAG ',ij,fld(ij),ifld(ij),gref(ng),bscale,rmin,dscale*/ /* enddo*/ n=n+glength; } /* Pad last octet with Zeros, if necessary,*/ if ( (iofst%8) != 0) { left=8-(iofst%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } *lcpack=iofst/8; if ( ifld != 0 ) free(ifld); free(jfld); if ( ifldmiss != 0 ) free(ifldmiss); free(gref); free(gwidth); free(glen); /*} //else { // Constant field ( max = min ) // nbits=0; // *lcpack=0; // nbitsgref=0; // ngroups=0; }*/ /* // Fill in ref value and number of bits in Template 5.2 */ mkieee(&rmin,idrstmpl+0,1); /* ensure reference value is IEEE format*/ idrstmpl[3]=nbitsgref; idrstmpl[4]=0; /* original data were reals*/ idrstmpl[5]=1; /* general group splitting*/ idrstmpl[9]=ngroups; /* Number of groups*/ idrstmpl[10]=ngwidthref; /* reference for group widths*/ idrstmpl[11]=nbitsgwidth; /* num bits used for group widths*/ idrstmpl[12]=nglenref; /* Reference for group lengths*/ idrstmpl[13]=1; /* length increment for group lengths*/ idrstmpl[14]=nglenlast; /* True length of last group*/ idrstmpl[15]=nbitsglen; /* num bits used for group lengths*/ if (idrsnum == 3) { idrstmpl[17]=nbitsd/8; /* num bits used for extra spatial*/ /* differencing values*/ } }
void compack(g2float *fld,g2int ndpts,g2int idrsnum,g2int *idrstmpl, unsigned char *cpack,g2int *lcpack) //$$$ SUBPROGRAM DOCUMENTATION BLOCK // . . . . // SUBPROGRAM: compack // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-11-07 // // ABSTRACT: This subroutine packs up a data field using a complex // packing algorithm as defined in the GRIB2 documentation. It // supports GRIB2 complex packing templates with or without // spatial differences (i.e. DRTs 5.2 and 5.3). // It also fills in GRIB2 Data Representation Template 5.2 or 5.3 // with the appropriate values. // // PROGRAM HISTORY LOG: // 2002-11-07 Gilbert // // USAGE: void compack(g2float *fld,g2int ndpts,g2int idrsnum, // g2int *idrstmpl,unsigned char *cpack,g2int *lcpack) // // INPUT ARGUMENTS: // fld[] - Contains the data values to pack // ndpts - The number of data values in array fld[] // idrsnum - Data Representation Template number 5.N // Must equal 2 or 3. // idrstmpl - Contains the array of values for Data Representation // Template 5.2 or 5.3 // [0] = Reference value - ignored on input // [1] = Binary Scale Factor // [2] = Decimal Scale Factor // . // . // [6] = Missing value management // [7] = Primary missing value // [8] = Secondary missing value // . // . // [16] = Order of Spatial Differencing ( 1 or 2 ) // . // . // // OUTPUT ARGUMENTS: // idrstmpl - Contains the array of values for Data Representation // Template 5.3 // [0] = Reference value - set by compack routine. // [1] = Binary Scale Factor - unchanged from input // [2] = Decimal Scale Factor - unchanged from input // . // . // cpack - The packed data field // lcpack - length of packed field cpack. // // REMARKS: None // // ATTRIBUTES: // LANGUAGE: C // MACHINE: // //$$$ { const g2int zero=0; g2int *ifld,*gref,*glen,*gwidth; g2int *jmin, *jmax, *lbit; g2int i,j,n, /* nbits, */ imin,imax,left; g2int isd,itemp,ilmax,ngwidthref=0,nbitsgwidth=0; g2int nglenref=0,nglenlast=0,iofst,ival1,ival2; g2int minsd,nbitsd=0,maxorig,nbitorig,ngroups; g2int lg,ng,igmax,iwmax,nbitsgref; g2int glength,grpwidth,nbitsglen=0; g2int kfildo, minpk, inc, maxgrps, ibit, jbit, kbit, novref, lbitref; g2int missopt, miss1, miss2, ier; g2float bscale,dscale,rmax,rmin,temp; const g2int simple_alg = 0; const g2float alog2=0.69314718f; // ln(2.0) const g2int one=1; bscale=(float)int_power(2.0,-idrstmpl[1]); dscale=(float)int_power(10.0,idrstmpl[2]); // // Find max and min values in the data // rmax=fld[0]; rmin=fld[0]; for (j=1;j<ndpts;j++) { if (fld[j] > rmax) rmax=fld[j]; if (fld[j] < rmin) rmin=fld[j]; } // // If max and min values are not equal, pack up field. // If they are equal, we have a constant field, and the reference // value (rmin) is the value for each point in the field and // set nbits to 0. // if (rmin != rmax) { iofst=0; ifld=calloc(ndpts,sizeof(g2int)); gref=calloc(ndpts,sizeof(g2int)); gwidth=calloc(ndpts,sizeof(g2int)); glen=calloc(ndpts,sizeof(g2int)); // // Scale original data // if (idrstmpl[1] == 0) { // No binary scaling imin=(g2int)RINT(rmin*dscale); //imax=(g2int)rint(rmax*dscale); rmin=(g2float)imin; for (j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(fld[j]*dscale)-imin; } else { // Use binary scaling factor rmin=rmin*dscale; //rmax=rmax*dscale; for (j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(((fld[j]*dscale)-rmin)*bscale); } // // Calculate spatial differences, if using DRS Template 5.3. // if (idrsnum == 3) { // spatial differences if (idrstmpl[16]!=1 && idrstmpl[16]!=2) idrstmpl[16]=1; if (idrstmpl[16] == 1) { // first order ival1=ifld[0]; for (j=ndpts-1;j>0;j--) ifld[j]=ifld[j]-ifld[j-1]; ifld[0]=0; } else if (idrstmpl[16] == 2) { // second order ival1=ifld[0]; ival2=ifld[1]; for (j=ndpts-1;j>1;j--) ifld[j]=ifld[j]-(2*ifld[j-1])+ifld[j-2]; ifld[0]=0; ifld[1]=0; } // // subtract min value from spatial diff field // isd=idrstmpl[16]; minsd=ifld[isd]; for (j=isd;j<ndpts;j++) if ( ifld[j] < minsd ) minsd=ifld[j]; for (j=isd;j<ndpts;j++) ifld[j]=ifld[j]-minsd; // // find num of bits need to store minsd and add 1 extra bit // to indicate sign // temp=(float)(log((double)(abs(minsd)+1))/alog2); nbitsd=(g2int)ceil(temp)+1; // // find num of bits need to store ifld[0] ( and ifld[1] // if using 2nd order differencing ) // maxorig=ival1; if (idrstmpl[16]==2 && ival2>ival1) maxorig=ival2; temp=(float)(log((double)(maxorig+1))/alog2); nbitorig=(g2int)ceil(temp)+1; if (nbitorig > nbitsd) nbitsd=nbitorig; // increase number of bits to even multiple of 8 ( octet ) if ( (nbitsd%8) != 0) nbitsd=nbitsd+(8-(nbitsd%8)); // // Store extra spatial differencing info into the packed // data section. // if (nbitsd != 0) { // pack first original value if (ival1 >= 0) { sbit(cpack,&ival1,iofst,nbitsd); iofst=iofst+nbitsd; } else { sbit(cpack,&one,iofst,1); iofst=iofst+1; itemp=abs(ival1); sbit(cpack,&itemp,iofst,nbitsd-1); iofst=iofst+nbitsd-1; } if (idrstmpl[16] == 2) { // pack second original value if (ival2 >= 0) { sbit(cpack,&ival2,iofst,nbitsd); iofst=iofst+nbitsd; } else { sbit(cpack,&one,iofst,1); iofst=iofst+1; itemp=abs(ival2); sbit(cpack,&itemp,iofst,nbitsd-1); iofst=iofst+nbitsd-1; } } // pack overall min of spatial differences if (minsd >= 0) { sbit(cpack,&minsd,iofst,nbitsd); iofst=iofst+nbitsd; } else { sbit(cpack,&one,iofst,1); iofst=iofst+1; itemp=abs(minsd); sbit(cpack,&itemp,iofst,nbitsd-1); iofst=iofst+nbitsd-1; } } //printf("SDp %ld %ld %ld %ld\n",ival1,ival2,minsd,nbitsd); } // end of spatial diff section // // Determine Groups to be used. // if ( simple_alg == 1 ) { // set group length to 10; calculate number of groups // and length of last group ngroups=ndpts/10; for (j=0;j<ngroups;j++) glen[j]=10; itemp=ndpts%10; if (itemp != 0) { ngroups=ngroups+1; glen[ngroups-1]=itemp; } } else { // Use Dr. Glahn's algorithm for determining grouping. // kfildo=6; minpk=10; inc=1; maxgrps=(ndpts/minpk)+1; jmin = calloc(maxgrps,sizeof(g2int)); jmax = calloc(maxgrps,sizeof(g2int)); lbit = calloc(maxgrps,sizeof(g2int)); missopt=0; pack_gp(&kfildo,ifld,&ndpts,&missopt,&minpk,&inc,&miss1,&miss2, jmin,jmax,lbit,glen,&maxgrps,&ngroups,&ibit,&jbit, &kbit,&novref,&lbitref,&ier); //print *,'SAGier = ',ier,ibit,jbit,kbit,novref,lbitref for ( ng=0; ng<ngroups; ng++) glen[ng]=glen[ng]+novref; free(jmin); free(jmax); free(lbit); } // // For each group, find the group's reference value // and the number of bits needed to hold the remaining values // n=0; for (ng=0;ng<ngroups;ng++) { // find max and min values of group gref[ng]=ifld[n]; imax=ifld[n]; j=n+1; for (lg=1;lg<glen[ng];lg++) { if (ifld[j] < gref[ng]) gref[ng]=ifld[j]; if (ifld[j] > imax) imax=ifld[j]; j++; } // calc num of bits needed to hold data if ( gref[ng] != imax ) { temp=(float)(log((double)(imax-gref[ng]+1))/alog2); gwidth[ng]=(g2int)ceil(temp); } else gwidth[ng]=0; // Subtract min from data j=n; for (lg=0;lg<glen[ng];lg++) { ifld[j]=ifld[j]-gref[ng]; j++; } // increment fld array counter n=n+glen[ng]; } // // Find max of the group references and calc num of bits needed // to pack each groups reference value, then // pack up group reference values // igmax=gref[0]; for (j=1;j<ngroups;j++) if (gref[j] > igmax) igmax=gref[j]; if (igmax != 0) { temp=(float)(log((double)(igmax+1))/alog2); nbitsgref=(g2int)ceil(temp); sbits(cpack,gref,iofst,nbitsgref,0,ngroups); itemp=nbitsgref*ngroups; iofst=iofst+itemp; // Pad last octet with Zeros, if necessary, if ( (itemp%8) != 0) { left=8-(itemp%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } } else nbitsgref=0; // // Find max/min of the group widths and calc num of bits needed // to pack each groups width value, then // pack up group width values // iwmax=gwidth[0]; ngwidthref=gwidth[0]; for (j=1;j<ngroups;j++) { if (gwidth[j] > iwmax) iwmax=gwidth[j]; if (gwidth[j] < ngwidthref) ngwidthref=gwidth[j]; } if (iwmax != ngwidthref) { temp=(float)(log((double)(iwmax-ngwidthref+1))/alog2); nbitsgwidth=(g2int)ceil(temp); for (i=0;i<ngroups;i++) gwidth[i]=gwidth[i]-ngwidthref; sbits(cpack,gwidth,iofst,nbitsgwidth,0,ngroups); itemp=nbitsgwidth*ngroups; iofst=iofst+itemp; // Pad last octet with Zeros, if necessary, if ( (itemp%8) != 0) { left=8-(itemp%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } } else { nbitsgwidth=0; for (i=0;i<ngroups;i++) gwidth[i]=0; } // // Find max/min of the group lengths and calc num of bits needed // to pack each groups length value, then // pack up group length values // //write(77,*)'GLENS: ',(glen(j),j=1,ngroups) ilmax=glen[0]; nglenref=glen[0]; for (j=1;j<ngroups-1;j++) { if (glen[j] > ilmax) ilmax=glen[j]; if (glen[j] < nglenref) nglenref=glen[j]; } nglenlast=glen[ngroups-1]; if (ilmax != nglenref) { temp=(float)(log((double)(ilmax-nglenref+1))/alog2); nbitsglen=(g2int)ceil(temp); for (i=0;i<ngroups-1;i++) glen[i]=glen[i]-nglenref; sbits(cpack,glen,iofst,nbitsglen,0,ngroups); itemp=nbitsglen*ngroups; iofst=iofst+itemp; // Pad last octet with Zeros, if necessary, if ( (itemp%8) != 0) { left=8-(itemp%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } } else { nbitsglen=0; for (i=0;i<ngroups;i++) glen[i]=0; } // // For each group, pack data values // n=0; for (ng=0;ng<ngroups;ng++) { glength=glen[ng]+nglenref; if (ng == (ngroups-1) ) glength=nglenlast; grpwidth=gwidth[ng]+ngwidthref; if ( grpwidth != 0 ) { sbits(cpack,ifld+n,iofst,grpwidth,0,glength); iofst=iofst+(grpwidth*glength); } n=n+glength; } // Pad last octet with Zeros, if necessary, if ( (iofst%8) != 0) { left=8-(iofst%8); sbit(cpack,&zero,iofst,left); iofst=iofst+left; } *lcpack=iofst/8; // if ( ifld!=0 ) free(ifld); if ( gref!=0 ) free(gref); if ( gwidth!=0 ) free(gwidth); if ( glen!=0 ) free(glen); } else { // Constant field ( max = min ) /* nbits=0; */ *lcpack=0; nbitsgref=0; ngroups=0; } // // Fill in ref value and number of bits in Template 5.2 // mkieee(&rmin,idrstmpl+0,1); // ensure reference value is IEEE format idrstmpl[3]=nbitsgref; idrstmpl[4]=0; // original data were reals idrstmpl[5]=1; // general group splitting idrstmpl[6]=0; // No internal missing values idrstmpl[7]=0; // Primary missing value idrstmpl[8]=0; // secondary missing value idrstmpl[9]=ngroups; // Number of groups idrstmpl[10]=ngwidthref; // reference for group widths idrstmpl[11]=nbitsgwidth; // num bits used for group widths idrstmpl[12]=nglenref; // Reference for group lengths idrstmpl[13]=1; // length increment for group lengths idrstmpl[14]=nglenlast; // True length of last group idrstmpl[15]=nbitsglen; // num bits used for group lengths if (idrsnum == 3) { idrstmpl[17]=nbitsd/8; // num bits used for extra spatial // differencing values } }
void simpack(g2float *fld,g2int ndpts,g2int *idrstmpl,unsigned char *cpack,g2int *lcpack) //$$$ SUBPROGRAM DOCUMENTATION BLOCK // . . . . // SUBPROGRAM: simpack // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-11-06 // // ABSTRACT: This subroutine packs up a data field using the simple // packing algorithm as defined in the GRIB2 documentation. It // also fills in GRIB2 Data Representation Template 5.0 with the // appropriate values. // // PROGRAM HISTORY LOG: // 2002-11-06 Gilbert // // USAGE: CALL simpack(fld,ndpts,idrstmpl,cpack,lcpack) // INPUT ARGUMENT LIST: // fld[] - Contains the data values to pack // ndpts - The number of data values in array fld[] // idrstmpl - Contains the array of values for Data Representation // Template 5.0 // [0] = Reference value - ignored on input // [1] = Binary Scale Factor // [2] = Decimal Scale Factor // [3] = Number of bits used to pack data, if value is // > 0 and <= 31. // If this input value is 0 or outside above range // then the num of bits is calculated based on given // data and scale factors. // [4] = Original field type - currently ignored on input // Data values assumed to be reals. // // OUTPUT ARGUMENT LIST: // idrstmpl - Contains the array of values for Data Representation // Template 5.0 // [0] = Reference value - set by simpack routine. // [1] = Binary Scale Factor - unchanged from input // [2] = Decimal Scale Factor - unchanged from input // [3] = Number of bits used to pack data, unchanged from // input if value is between 0 and 31. // If this input value is 0 or outside above range // then the num of bits is calculated based on given // data and scale factors. // [4] = Original field type - currently set = 0 on output. // Data values assumed to be reals. // cpack - The packed data field // lcpack - length of packed field starting at cpack. // // REMARKS: None // // ATTRIBUTES: // LANGUAGE: C // MACHINE: // //$$$ { const g2int zero=0; g2int *ifld; g2int j,nbits,imin,imax,maxdif,nbittot,left; g2float bscale,dscale,rmax,rmin,temp; double maxnum; const g2float alog2=0.69314718f; // ln(2.0) bscale=(float)int_power(2.0,-idrstmpl[1]); dscale=(float)int_power(10.0,idrstmpl[2]); if (idrstmpl[3] <= 0 || idrstmpl[3] > 31) nbits=0; else nbits=idrstmpl[3]; // // Find max and min values in the data // rmax=fld[0]; rmin=fld[0]; for (j=1;j<ndpts;j++) { if (fld[j] > rmax) rmax=fld[j]; if (fld[j] < rmin) rmin=fld[j]; } ifld=calloc(ndpts,sizeof(g2int)); // // If max and min values are not equal, pack up field. // If they are equal, we have a constant field, and the reference // value (rmin) is the value for each point in the field and // set nbits to 0. // if (rmin != rmax) { // // Determine which algorithm to use based on user-supplied // binary scale factor and number of bits. // if (nbits==0 && idrstmpl[1]==0) { // // No binary scaling and calculate minimum number of // bits in which the data will fit. // imin=(g2int)RINT(rmin*dscale); imax=(g2int)RINT(rmax*dscale); maxdif=imax-imin; temp=(float)(log((double)(maxdif+1))/alog2); nbits=(g2int)ceil(temp); rmin=(g2float)imin; // scale data for(j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(fld[j]*dscale)-imin; } else if (nbits!=0 && idrstmpl[1]==0) { // // Use minimum number of bits specified by user and // adjust binary scaling factor to accommodate data. // rmin=rmin*dscale; rmax=rmax*dscale; maxnum=int_power(2.0,nbits)-1; temp=(float)(log(maxnum/(rmax-rmin))/alog2); idrstmpl[1]=(g2int)ceil(-1.0*temp); bscale=(float)int_power(2.0,-idrstmpl[1]); // scale data for (j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(((fld[j]*dscale)-rmin)*bscale); } else if (nbits==0 && idrstmpl[1]!=0) { // // Use binary scaling factor and calculate minimum number of // bits in which the data will fit. // rmin=rmin*dscale; rmax=rmax*dscale; maxdif=(g2int)RINT((rmax-rmin)*bscale); temp=(float)(log((double)(maxdif+1))/alog2); nbits=(g2int)ceil(temp); // scale data for (j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(((fld[j]*dscale)-rmin)*bscale); } else if (nbits!=0 && idrstmpl[1]!=0) { // // Use binary scaling factor and use minimum number of // bits specified by user. Dangerous - may loose // information if binary scale factor and nbits not set // properly by user. // rmin=rmin*dscale; // scale data for (j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(((fld[j]*dscale)-rmin)*bscale); } // // Pack data, Pad last octet with Zeros, if necessary, // and calculate the length of the packed data in bytes // sbits(cpack,ifld+0,0,nbits,0,ndpts); nbittot=nbits*ndpts; left=8-(nbittot%8); if (left != 8) { sbit(cpack,&zero,nbittot,left); // Pad with zeros to fill Octet nbittot=nbittot+left; } *lcpack=nbittot/8; } else { nbits=0; *lcpack=0; } // // Fill in ref value and number of bits in Template 5.0 // //printf("SAGmkieee %f\n",rmin); mkieee(&rmin,idrstmpl+0,1); // ensure reference value is IEEE format //printf("SAGmkieee %ld\n",idrstmpl[0]); idrstmpl[3]=nbits; idrstmpl[4]=0; // original data were reals free(ifld); }
void pngpack(g2float *fld,g2int width,g2int height,g2int *idrstmpl, unsigned char *cpack,g2int *lcpack) //$$$ SUBPROGRAM DOCUMENTATION BLOCK // . . . . // SUBPROGRAM: pngpack // PRGMMR: Gilbert ORG: W/NP11 DATE: 2003-08-27 // // ABSTRACT: This subroutine packs up a data field into PNG image format. // After the data field is scaled, and the reference value is subtracted out, // it is treated as a grayscale image and passed to a PNG encoder. // It also fills in GRIB2 Data Representation Template 5.41 or 5.40010 with // the appropriate values. // // PROGRAM HISTORY LOG: // 2003-08-27 Gilbert // // USAGE: pngpack(g2float *fld,g2int width,g2int height,g2int *idrstmpl, // unsigned char *cpack,g2int *lcpack); // INPUT ARGUMENT LIST: // fld[] - Contains the data values to pack // width - number of points in the x direction // height - number of points in the y direction // idrstmpl - Contains the array of values for Data Representation // Template 5.41 or 5.40010 // [0] = Reference value - ignored on input // [1] = Binary Scale Factor // [2] = Decimal Scale Factor // [3] = number of bits for each data value - ignored on input // [4] = Original field type - currently ignored on input // Data values assumed to be reals. // // OUTPUT ARGUMENT LIST: // idrstmpl - Contains the array of values for Data Representation // Template 5.41 or 5.40010 // [0] = Reference value - set by pngpack routine. // [1] = Binary Scale Factor - unchanged from input // [2] = Decimal Scale Factor - unchanged from input // [3] = Number of bits containing each grayscale pixel value // [4] = Original field type - currently set = 0 on output. // Data values assumed to be reals. // cpack - The packed data field // lcpack - length of packed field cpack. // // REMARKS: None // // ATTRIBUTES: // LANGUAGE: C // MACHINE: IBM SP // //$$$ { g2int *ifld; const g2float alog2=0.69314718f; // ln(2.0) g2int j,nbits,imin,imax,maxdif; g2int ndpts,nbytes; g2float bscale,dscale,rmax,rmin,temp; unsigned char *ctemp; ifld=0; ndpts=width*height; bscale=(g2float)int_power(2.0,-idrstmpl[1]); dscale=(g2float)int_power(10.0,idrstmpl[2]); // // Find max and min values in the data // rmax=fld[0]; rmin=fld[0]; for (j=1;j<ndpts;j++) { if (fld[j] > rmax) rmax=fld[j]; if (fld[j] < rmin) rmin=fld[j]; } maxdif = (g2int)RINT( (rmax-rmin)*dscale*bscale ); // // If max and min values are not equal, pack up field. // If they are equal, we have a constant field, and the reference // value (rmin) is the value for each point in the field and // set nbits to 0. // if (rmin != rmax && maxdif != 0 ) { ifld=(g2int *)malloc(ndpts*sizeof(g2int)); // // Determine which algorithm to use based on user-supplied // binary scale factor and number of bits. // if (idrstmpl[1] == 0) { // // No binary scaling and calculate minimum number of // bits in which the data will fit. // imin=(g2int)RINT(rmin*dscale); imax=(g2int)RINT(rmax*dscale); maxdif=imax-imin; temp=(g2float)log((double)(maxdif+1))/alog2; nbits=(g2int)ceil(temp); rmin=(g2float)imin; // scale data for(j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(fld[j]*dscale)-imin; } else { // // Use binary scaling factor and calculate minimum number of // bits in which the data will fit. // rmin=rmin*dscale; rmax=rmax*dscale; maxdif=(g2int)RINT((rmax-rmin)*bscale); temp=(g2float)log((double)(maxdif+1))/alog2; nbits=(g2int)ceil(temp); // scale data for (j=0;j<ndpts;j++) ifld[j]=(g2int)RINT(((fld[j]*dscale)-rmin)*bscale); } // // Pack data into full octets, then do PNG encode. // and calculate the length of the packed data in bytes // if (nbits <= 8) { nbits=8; } else if (nbits <= 16) { nbits=16; } else if (nbits <= 24) { nbits=24; } else { nbits=32; } nbytes=(nbits/8)*ndpts; ctemp=calloc(nbytes,1); sbits(ctemp,ifld,0,nbits,0,ndpts); // // Encode data into PNG Format. // *lcpack=(g2int)enc_png((char *)ctemp,width,height,nbits,(char *)cpack); if (*lcpack <= 0) { printf("pngpack: ERROR Packing PNG = %d\n",(int)*lcpack); } free(ctemp); } else { nbits=0; *lcpack=0; } // // Fill in ref value and number of bits in Template 5.0 // mkieee(&rmin,idrstmpl+0,1); // ensure reference value is IEEE format idrstmpl[3]=nbits; idrstmpl[4]=0; // original data were reals if (ifld != 0) free(ifld); }
void specpack(g2float *fld,g2int ndpts,g2int JJ,g2int KK,g2int MM, g2int *idrstmpl,unsigned char *cpack,g2int *lcpack) //$$$ SUBPROGRAM DOCUMENTATION BLOCK // . . . . // SUBPROGRAM: specpack // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-12-19 // // ABSTRACT: This subroutine packs a spectral data field using the complex // packing algorithm for spherical harmonic data as // defined in the GRIB2 Data Representation Template 5.51. // // PROGRAM HISTORY LOG: // 2002-12-19 Gilbert // // USAGE: void specpack(g2float *fld,g2int ndpts,g2int JJ,g2int KK,g2int MM, // g2int *idrstmpl,insigned char *cpack,g2int *lcpack) // INPUT ARGUMENT LIST: // fld[] - Contains the packed data values // ndpts - The number of data values to pack // JJ - J - pentagonal resolution parameter // KK - K - pentagonal resolution parameter // MM - M - pentagonal resolution parameter // idrstmpl - Contains the array of values for Data Representation // Template 5.51 // // OUTPUT ARGUMENT LIST: // cpack - The packed data field (character*1 array) // lcpack - length of packed field cpack(). // // REMARKS: None // // ATTRIBUTES: // LANGUAGE: C // MACHINE: IBM SP // //$$$ { g2int *ifld,tmplsim[5]; g2float *unpk,*tfld; // g2float bscale, dscale; g2float *pscale,tscale; g2int Js,Ks,Ms,Ts,Ns,inc,incu,incp,n,Nm,m,ipos; /* bscale = int_power(2.0,-idrstmpl[1]); NOT used */ /* dscale = int_power(10.0,idrstmpl[2]); NOT used */ Js=idrstmpl[5]; Ks=idrstmpl[6]; Ms=idrstmpl[7]; Ts=idrstmpl[8]; // // Calculate Laplacian scaling factors for each possible wave number. // pscale=(g2float *)malloc((JJ+MM)*sizeof(g2float)); tscale=(g2float)idrstmpl[4]*1E-6; for (n=Js;n<=JJ+MM;n++) pscale[n]=pow((g2float)(n*(n+1)),tscale); // // Separate spectral coeffs into two lists; one to contain unpacked // values within the sub-spectrum Js, Ks, Ms, and the other with values // outside of the sub-spectrum to be packed. // tfld=(g2float *)malloc(ndpts*sizeof(g2float)); unpk=(g2float *)malloc(ndpts*sizeof(g2float)); ifld=(g2int *)malloc(ndpts*sizeof(g2int)); inc=0; incu=0; incp=0; for (m=0;m<=MM;m++) { Nm=JJ; // triangular or trapezoidal if ( KK == JJ+MM ) Nm=JJ+m; // rhombodial Ns=Js; // triangular or trapezoidal if ( Ks == Js+Ms ) Ns=Js+m; // rhombodial for (n=m;n<=Nm;n++) { if (n<=Ns && m<=Ms) { // save unpacked value unpk[incu++]=fld[inc++]; // real part unpk[incu++]=fld[inc++]; // imaginary part } else { // Save value to be packed and scale // Laplacian scale factor tfld[incp++]=fld[inc++]*pscale[n]; // real part tfld[incp++]=fld[inc++]*pscale[n]; // imaginary part } } } free(pscale); if (incu != Ts) { printf("specpack: Incorrect number of unpacked values %d given:\n",(int)Ts); printf("specpack: Resetting idrstmpl[8] to %d\n",(int)incu); Ts=incu; } // // Add unpacked values to the packed data array in 32-bit IEEE format // mkieee(unpk,(g2int *)cpack,Ts); ipos=4*Ts; // // Scale and pack the rest of the coefficients // tmplsim[1]=idrstmpl[1]; tmplsim[2]=idrstmpl[2]; tmplsim[3]=idrstmpl[3]; simpack(tfld,ndpts-Ts,tmplsim,cpack+ipos,lcpack); *lcpack=(*lcpack)+ipos; // // Fill in Template 5.51 // idrstmpl[0]=tmplsim[0]; idrstmpl[1]=tmplsim[1]; idrstmpl[2]=tmplsim[2]; idrstmpl[3]=tmplsim[3]; idrstmpl[8]=Ts; idrstmpl[9]=1; // Unpacked spectral data is 32-bit IEEE free(tfld); free(unpk); free(ifld); return; }
g2int g2_addfield(unsigned char *cgrib,g2int ipdsnum,g2int *ipdstmpl, g2float *coordlist,g2int numcoord,g2int idrsnum,g2int *idrstmpl, g2float *fld,g2int ngrdpts,g2int ibmap,g2int *bmap) //$$$ SUBPROGRAM DOCUMENTATION BLOCK // . . . . // SUBPROGRAM: g2_addfield // PRGMMR: Gilbert ORG: W/NP11 DATE: 2002-11-05 // // ABSTRACT: This routine packs up Sections 4 through 7 for a given field // and adds them to a GRIB2 message. They are Product Definition Section, // Data Representation Section, Bit-Map Section and Data Section, // respectively. // This routine is used with routines "g2_create", "g2_addlocal", // "g2_addgrid", and "g2_gribend" to create a complete GRIB2 message. // g2_create must be called first to initialize a new GRIB2 message. // Also, routine g2_addgrid must be called after g2_create and // before this routine to add the appropriate grid description to // the GRIB2 message. Also, a call to g2_gribend is required to complete // GRIB2 message after all fields have been added. // // PROGRAM HISTORY LOG: // 2002-11-05 Gilbert // 2002-12-23 Gilbert - Added complex spherical harmonic packing // 2003-08-27 Gilbert - Added support for new templates using // PNG and JPEG2000 algorithms/templates. // 2004-11-29 Gilbert - JPEG2000 now allowed to use WMO Template no. 5.40 // PNG now allowed to use WMO Template no. 5.41 // - Added check to determine if packing algorithm failed. // 2005-05-10 Gilbert - Imposed minimum size on cpack, used to hold encoded // bit string. // // USAGE: int g2_addfield(unsigned char *cgrib,g2int ipdsnum,g2int *ipdstmpl, // g2float *coordlist,g2int numcoord,g2int idrsnum,g2int *idrstmpl, // g2float *fld,g2int ngrdpts,g2int ibmap,g2int *bmap) // INPUT ARGUMENT LIST: // cgrib - Char array that contains the GRIB2 message to which sections // 4 through 7 should be added. // ipdsnum - Product Definition Template Number ( see Code Table 4.0) // ipdstmpl - Contains the data values for the specified Product Definition // Template ( N=ipdsnum ). Each element of this integer // array contains an entry (in the order specified) of Product // Defintion Template 4.N // coordlist- Array containg floating point values intended to document // the vertical discretisation associated to model data // on hybrid coordinate vertical levels. // numcoord - number of values in array coordlist. // idrsnum - Data Representation Template Number ( see Code Table 5.0 ) // idrstmpl - Contains the data values for the specified Data Representation // Template ( N=idrsnum ). Each element of this integer // array contains an entry (in the order specified) of Data // Representation Template 5.N // Note that some values in this template (eg. reference // values, number of bits, etc...) may be changed by the // data packing algorithms. // Use this to specify scaling factors and order of // spatial differencing, if desired. // fld[] - Array of data points to pack. // ngrdpts - Number of data points in grid. // i.e. size of fld and bmap. // ibmap - Bitmap indicator ( see Code Table 6.0 ) // 0 = bitmap applies and is included in Section 6. // 1-253 = Predefined bitmap applies // 254 = Previously defined bitmap applies to this field // 255 = Bit map does not apply to this product. // bmap[] - Integer array containing bitmap to be added. ( if ibmap=0 ) // // OUTPUT ARGUMENT LIST: // cgrib - Character array to contain the updated GRIB2 message. // Must be allocated large enough to store the entire // GRIB2 message. // // RETURN VALUES: // ierr - Return code. // > 0 = Current size of updated GRIB2 message // -1 = GRIB message was not initialized. Need to call // routine g2_create first. // -2 = GRIB message already complete. Cannot add new section. // -3 = Sum of Section byte counts doesn't add to total byte count // -4 = Previous Section was not 3 or 7. // -5 = Could not find requested Product Definition Template. // -6 = Section 3 (GDS) not previously defined in message // -7 = Tried to use unsupported Data Representationi Template // -8 = Specified use of a previously defined bitmap, but one // does not exist in the GRIB message. // -9 = GDT of one of 5.50 through 5.53 required to pack field // using DRT 5.51. // -10 = Error packing data field. // // REMARKS: Note that the Sections 4 through 7 can only follow // Section 3 or Section 7 in a GRIB2 message. // // ATTRIBUTES: // LANGUAGE: C // MACHINE: // //$$$ { g2int ierr; static unsigned char G=0x47; // 'G' static unsigned char R=0x52; // 'R' static unsigned char I=0x49; // 'I' static unsigned char B=0x42; // 'B' static unsigned char s7=0x37; // '7' unsigned char *cpack; static g2int zero=0,one=1,four=4,five=5,six=6,seven=7; const g2int minsize=50000; g2int iofst,ibeg,lencurr,len,nsize; g2int ilen,isecnum,i,nbits,temp,left; g2int ibmprev,j,lcpack,ioctet,newlen,ndpts; g2int lensec4,lensec5,lensec6,lensec7; g2int issec3,isprevbmap,lpos3=0,JJ,KK,MM; g2int *coordieee; g2int width,height,iscan,itemp; g2float *pfld; xxtemplate *mappds,*mapdrs; unsigned int allones=4294967295u; ierr=0; // // Check to see if beginning of GRIB message exists // if ( cgrib[0]!=G || cgrib[1]!=R || cgrib[2]!=I || cgrib[3]!=B ) { printf("g2_addfield: GRIB not found in given message.\n"); printf("g2_addfield: Call to routine g2_create required to initialize GRIB messge.\n"); ierr=-1; return(ierr); } // // Get current length of GRIB message // gbit(cgrib,&lencurr,96,32); // // Check to see if GRIB message is already complete // if ( cgrib[lencurr-4]==s7 && cgrib[lencurr-3]==s7 && cgrib[lencurr-2]==s7 && cgrib[lencurr-1]==s7 ) { printf("g2_addfield: GRIB message already complete. Cannot add new section.\n"); ierr=-2; return(ierr); } // // Loop through all current sections of the GRIB message to // find the last section number. // issec3=0; isprevbmap=0; len=16; // length of Section 0 for (;;) { // Get number and length of next section iofst=len*8; gbit(cgrib,&ilen,iofst,32); iofst=iofst+32; gbit(cgrib,&isecnum,iofst,8); iofst=iofst+8; // Check if previous Section 3 exists if (isecnum == 3) { issec3=1; lpos3=len; } // Check if a previous defined bitmap exists if (isecnum == 6) { gbit(cgrib,&ibmprev,iofst,8); iofst=iofst+8; if ((ibmprev >= 0) && (ibmprev <= 253)) isprevbmap=1; } len=len+ilen; // Exit loop if last section reached if ( len == lencurr ) break; // If byte count for each section doesn't match current // total length, then there is a problem. if ( len > lencurr ) { printf("g2_addfield: Section byte counts don''t add to total.\n"); printf("g2_addfield: Sum of section byte counts = %d\n",len); printf("g2_addfield: Total byte count in Section 0 = %d\n",lencurr); ierr=-3; return(ierr); } } // // Sections 4 through 7 can only be added after section 3 or 7. // if ( (isecnum != 3) && (isecnum != 7) ) { printf("g2_addfield: Sections 4-7 can only be added after Section 3 or 7.\n"); printf("g2_addfield: Section ',isecnum,' was the last found in given GRIB message.\n"); ierr=-4; return(ierr); // // Sections 4 through 7 can only be added if section 3 was previously defined. // } else if ( ! issec3) { printf("g2_addfield: Sections 4-7 can only be added if Section 3 was previously included.\n"); printf("g2_addfield: Section 3 was not found in given GRIB message.\n"); printf("g2_addfield: Call to routine addgrid required to specify Grid definition.\n"); ierr=-6; return(ierr); } // // Add Section 4 - Product Definition Section // ibeg=lencurr*8; // Calculate offset for beginning of section 4 iofst=ibeg+32; // leave space for length of section sbit(cgrib,&four,iofst,8); // Store section number ( 4 ) iofst=iofst+8; sbit(cgrib,&numcoord,iofst,16); // Store num of coordinate values iofst=iofst+16; sbit(cgrib,&ipdsnum,iofst,16); // Store Prod Def Template num. iofst=iofst+16; // // Get Product Definition Template // mappds=getpdstemplate(ipdsnum); if (mappds == 0) { // undefined template ierr=-5; return(ierr); } // // Extend the Product Definition Template, if necessary. // The number of values in a specific template may vary // depending on data specified in the "static" part of the // template. // if ( mappds->needext ) { free(mappds); mappds=extpdstemplate(ipdsnum,ipdstmpl); } // // Pack up each input value in array ipdstmpl into the // the appropriate number of octets, which are specified in // corresponding entries in array mappds. // for (i=0;i<mappds->maplen;i++) { nbits=abs(mappds->map[i])*8; if ( (mappds->map[i] >= 0) || (ipdstmpl[i] >= 0) ) sbit(cgrib,ipdstmpl+i,iofst,nbits); else { sbit(cgrib,&one,iofst,1); temp=abs(ipdstmpl[i]); sbit(cgrib,&temp,iofst+1,nbits-1); } iofst=iofst+nbits; } // Pack template extension, if appropriate j=mappds->maplen; if ( mappds->needext && (mappds->extlen > 0) ) { for (i=0;i<mappds->extlen;i++) { nbits=abs(mappds->ext[i])*8; if ( (mappds->ext[i] >= 0) || (ipdstmpl[j] >= 0) ) sbit(cgrib,ipdstmpl+j,iofst,nbits); else { sbit(cgrib,&one,iofst,1); temp=abs(ipdstmpl[j]); sbit(cgrib,&temp,iofst+1,nbits-1); } iofst=iofst+nbits; j++; } } free(mappds); // // Add Optional list of vertical coordinate values // after the Product Definition Template, if necessary. // if ( numcoord != 0 ) { coordieee=(g2int *)calloc(numcoord,sizeof(g2int)); mkieee(coordlist,coordieee,numcoord); sbits(cgrib,coordieee,iofst,32,0,numcoord); iofst=iofst+(32*numcoord); free(coordieee); } // // Calculate length of section 4 and store it in octets // 1-4 of section 4. // lensec4=(iofst-ibeg)/8; sbit(cgrib,&lensec4,ibeg,32); // // Pack Data using appropriate algorithm // // // Get Data Representation Template // mapdrs=getdrstemplate(idrsnum); if (mapdrs == 0) { ierr=-5; return(ierr); } // // contract data field, removing data at invalid grid points, // if bit-map is provided with field. // if ( ibmap == 0 || ibmap==254 ) { pfld=(g2float *)malloc(ngrdpts*sizeof(g2float)); ndpts=0; for (j=0;j<ngrdpts;j++) { if ( bmap[j]==1 ) pfld[ndpts++]=fld[j]; } } else { ndpts=ngrdpts; pfld=fld; } nsize=ndpts*4; if ( nsize < minsize ) nsize=minsize; cpack=malloc(nsize); if (idrsnum == 0) // Simple Packing simpack(pfld,ndpts,idrstmpl,cpack,&lcpack); else if (idrsnum==2 || idrsnum==3) // Complex Packing cmplxpack(pfld,ndpts,idrsnum,idrstmpl,cpack,&lcpack); else if (idrsnum == 50) { // Sperical Harmonic Simple Packing simpack(pfld+1,ndpts-1,idrstmpl,cpack,&lcpack); mkieee(pfld+0,idrstmpl+4,1); // ensure RE(0,0) value is IEEE format } else if (idrsnum == 51) { // Sperical Harmonic Complex Packing getpoly(cgrib+lpos3,&JJ,&KK,&MM); if ( JJ!=0 && KK!=0 && MM!=0 ) specpack(pfld,ndpts,JJ,KK,MM,idrstmpl,cpack,&lcpack); else { printf("g2_addfield: Cannot pack DRT 5.51.\n"); return (-9); } } else if (idrsnum == 40 || idrsnum == 40000) { /* JPEG2000 encoding */ if (ibmap == 255) { getdim(cgrib+lpos3,&width,&height,&iscan); if ( width==0 || height==0 ) { width=ndpts; height=1; } else if ( width==allones || height==allones ) { width=ndpts; height=1; } else if ( (iscan&32) == 32) { /* Scanning mode: bit 3 */ itemp=width; width=height; height=itemp; } } else { width=ndpts; height=1; } lcpack=nsize; jpcpack(pfld,width,height,idrstmpl,cpack,&lcpack); } #ifdef USE_PNG else if (idrsnum == 41 || idrsnum == 40010) { /* PNG encoding */ if (ibmap == 255) { getdim(cgrib+lpos3,&width,&height,&iscan); if ( width==0 || height==0 ) { width=ndpts; height=1; } else if ( width==allones || height==allones ) { width=ndpts; height=1; } else if ( (iscan&32) == 32) { /* Scanning mode: bit 3 */ itemp=width; width=height; height=itemp; } } else { width=ndpts; height=1; } pngpack(pfld,width,height,idrstmpl,cpack,&lcpack); } #endif /* USE_PNG */ else { printf("g2_addfield: Data Representation Template 5.%d not yet implemented.\n",idrsnum); ierr=-7; return(ierr); } if ( ibmap == 0 || ibmap==254 ) { // free temp space if (fld != pfld) free(pfld); } if ( lcpack < 0 ) { if( cpack != 0 ) free(cpack); ierr=-10; return(ierr); } // // Add Section 5 - Data Representation Section // ibeg=iofst; // Calculate offset for beginning of section 5 iofst=ibeg+32; // leave space for length of section sbit(cgrib,&five,iofst,8); // Store section number ( 5 ) iofst=iofst+8; sbit(cgrib,&ndpts,iofst,32); // Store num of actual data points iofst=iofst+32; sbit(cgrib,&idrsnum,iofst,16); // Store Data Repr. Template num. iofst=iofst+16; // // Pack up each input value in array idrstmpl into the // the appropriate number of octets, which are specified in // corresponding entries in array mapdrs. // for (i=0;i<mapdrs->maplen;i++) { nbits=abs(mapdrs->map[i])*8; if ( (mapdrs->map[i] >= 0) || (idrstmpl[i] >= 0) ) sbit(cgrib,idrstmpl+i,iofst,nbits); else { sbit(cgrib,&one,iofst,1); temp=abs(idrstmpl[i]); sbit(cgrib,&temp,iofst+1,nbits-1); } iofst=iofst+nbits; } free(mapdrs); // // Calculate length of section 5 and store it in octets // 1-4 of section 5. // lensec5=(iofst-ibeg)/8; sbit(cgrib,&lensec5,ibeg,32); // // Add Section 6 - Bit-Map Section // ibeg=iofst; // Calculate offset for beginning of section 6 iofst=ibeg+32; // leave space for length of section sbit(cgrib,&six,iofst,8); // Store section number ( 6 ) iofst=iofst+8; sbit(cgrib,&ibmap,iofst,8); // Store Bit Map indicator iofst=iofst+8; // // Store bitmap, if supplied // if (ibmap == 0) { sbits(cgrib,bmap,iofst,1,0,ngrdpts); // Store BitMap iofst=iofst+ngrdpts; } // // If specifying a previously defined bit-map, make sure // one already exists in the current GRIB message. // if ((ibmap==254) && ( ! isprevbmap)) { printf("g2_addfield: Requested previously defined bitmap,"); printf(" but one does not exist in the current GRIB message.\n"); ierr=-8; return(ierr); } // // Calculate length of section 6 and store it in octets // 1-4 of section 6. Pad to end of octect, if necessary. // left=8-(iofst%8); if (left != 8) { sbit(cgrib,&zero,iofst,left); // Pad with zeros to fill Octet iofst=iofst+left; } lensec6=(iofst-ibeg)/8; sbit(cgrib,&lensec6,ibeg,32); // // Add Section 7 - Data Section // ibeg=iofst; // Calculate offset for beginning of section 7 iofst=ibeg+32; // leave space for length of section sbit(cgrib,&seven,iofst,8); // Store section number ( 7 ) iofst=iofst+8; // Store Packed Binary Data values, if non-constant field if (lcpack != 0) { ioctet=iofst/8; //cgrib(ioctet+1:ioctet+lcpack)=cpack(1:lcpack) for (j=0;j<lcpack;j++) cgrib[ioctet+j]=cpack[j]; iofst=iofst+(8*lcpack); } // // Calculate length of section 7 and store it in octets // 1-4 of section 7. // lensec7=(iofst-ibeg)/8; sbit(cgrib,&lensec7,ibeg,32); if( cpack != 0 ) free(cpack); // // Update current byte total of message in Section 0 // newlen=lencurr+lensec4+lensec5+lensec6+lensec7; sbit(cgrib,&newlen,96,32); return(newlen); }