1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
#include <stdlib.h>
#include <math.h>
#include "grib2.h"
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
rdieee(idrstmpl+7,&rmissp,1);
if (missopt == 2) 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));
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);
if ( jfld != 0 ) free(jfld);
if ( ifldmiss != 0 ) free(ifldmiss);
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[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
}
}
|