source: trunk/WRF.COMMON/WRFV2/external/io_grib1/MEL_grib1/gribgetbms.c

Last change on this file was 11, checked in by aslmd, 14 years ago

spiga@svn-planeto:ajoute le modele meso-echelle martien

File size: 4.8 KB
Line 
1/*  gribgetbms.c                June 17, 1996 by Alice Nakajima, SAIC   */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include "dprints.h"            /* for dprints */
6#include "gribfuncs.h"          /* prototypes */
7
8/*
9*
10************************************************************************
11* A.  FUNCTION:   gribgetbms
12*       decode the Bitmap Section from the provided pointer location
13*       and store its info in the internal BMS structure.
14*       Pre-defined Bitmap case is not supported.
15*               
16*    INTERFACE:
17*       int   gribgetbms ( curr_ptr, bms, gds_flag, ulGrid_size, errmsg)
18*
19*    ARGUMENTS (I=input, O=output, I&O=input and output):
20*      (I)   char *curr_ptr;
21*            pointer to location where Bitmap Section to decode is expected;
22*      (O)   BMS_INPUT *bms;
23*            pointer to empty BMS structure; will hold decoded BMS info;
24*      (I)   int gds_flag;     
25*            flag set if GDS is present
26*      (I)   unsigned long ulGrid_size; 
27*            size of grid as in Binary Data Section struct
28*      (O)   char *errmsg
29*            returned filled if error occurred;
30*
31*     RETURN CODE:
32*     0>  BMS info stored in BMS structure if not using pre-defined bitmap;
33*     1>  error, corrupted bms; msg in errmsg;
34************************************************************************
35*/
36
37#if PROTOTYPE_NEEDED
38int   gribgetbms ( char *curr_ptr, BMS_INPUT *bms, int gds_flag,
39                unsigned long ulGrid_size, char *errmsg)
40#else
41int   gribgetbms (curr_ptr, bms, gds_flag, ulGrid_size, errmsg)
42                char *curr_ptr; 
43                BMS_INPUT *bms; 
44                int gds_flag;
45                unsigned long ulGrid_size; 
46                char *errmsg;
47#endif
48{
49  char *func= "gribgetbms";
50  char  *pp;
51  int totbits,val, bitpos,stopbit;  /* tmp working vars */
52  unsigned long SectLength;       /* message and section size */
53  unsigned long ulvar;              /* tmp var */
54  unsigned long skip=0;
55
56/*
57*
58* A.0      INIT Status to no error
59*/
60int   status=0;
61
62     DPRINT0 ("Entering gribgetbms():\n");
63/*
64*
65* A.1      FUNCTION gbyte   !get bitmap length
66*/
67    skip=0;
68    gbyte(curr_ptr,(unsigned long *)&SectLength,&skip,24); 
69    DPRINT0 ("SectLength\n");
70    bms->uslength= (unsigned long) SectLength;
71
72/*
73*
74* A.2      FUNCTION gbyte   !get number of unused bits
75*/
76    gbyte(curr_ptr,&ulvar,&skip,8); 
77    DPRINT0 ("bms->usUnused_bits\n");
78    bms->usUnused_bits= (unsigned short) ulvar;
79
80/*
81*
82* A.3      FUNCTION gbyte   !get bitmap id (non-zero for a pre-defined bitmap)
83*/
84    gbyte(curr_ptr,&ulvar,&skip,16); 
85    DPRINT0 ("bms->usBMS_id\n");
86    bms->usBMS_id= (unsigned short) ulvar;
87
88/*
89*
90* A.4      IF (Bitmap follows)   !not a predefined bitmap
91*/
92    if ( bms->uslength > 6)     /* Bitmap follows */
93       {
94
95/*
96* A.4.1       CALCULATE Num of bits in bitmap
97*/
98         /* = (BMS length)*8 bits - 48 header bits - # of unsused bits */
99         totbits=SectLength*8 - 48 - bms->usUnused_bits;
100
101/*
102* A.4.2       IF (GDS is present AND
103*                      #bits differs from Grid Size)        !Corrupted BMS
104*                 RETURN 1
105*             ENDIF
106*/
107          if (gds_flag && totbits != ulGrid_size) {
108                DPRINT3( "%s: corrupted BMS, gds_flag set but "\
109                "totbits %d != ulgrid_sz %d\n" 
110                , func, totbits, ulGrid_size);
111
112                sprintf(errmsg, "%s: corrupted BMS, gds_flag set but "\
113                "totbits %d != ulgrid_sz %d\n" , func, totbits, ulGrid_size);
114                status=  (1); /* Corrupted BMS */
115                goto BYE;
116                }
117
118/*
119* A.4.3       ASSIGN bitmap pointer to 6th byte of BMS
120*/
121          bms->bit_map =  curr_ptr + 6;
122          pp= bms->bit_map; 
123          bms->ulbits_set= 0; 
124/*
125*
126* A.4.4       !SUM up total number of bits set
127*             FOR (Each 8-bit block of Total Bits Present in BMS)
128*/
129          for ( ; totbits > 0 ; totbits-=8) 
130          {
131/*
132* A.4.4.1       IF (any of the 8 bits are set)
133*/
134             if ((val=(int)*pp++) != 0) 
135               {
136
137/*
138* A.4.4.1.1        IF (not within 8 bits of end of bitmap)
139*                      SET stopbit to 0
140*                  ELSE
141*                      SET stopbit to end of bitmap
142*                  ENDIF
143*/
144                 if (totbits > 8) stopbit=0;   /* check all 8 bits */
145                 else stopbit= 7-totbits+1;    /* stop at end of bitmap */
146
147/*
148* A.4.4.1.2        SUM up number of bits set in this BMS byte
149*/
150                 for (bitpos= 7; bitpos >= stopbit; bitpos--)
151                    if (val >> bitpos & 0x0001) bms->ulbits_set += 1;
152/*
153* A.4.4.1       ENDIF  ! any of 8 exists
154*/
155               }
156/*
157* A.4.4     ENDFOR   !each 8-bit loop
158*/
159           }
160/*
161* A.4      ENDIF        !Bitmap follows
162*/
163        } 
164
165/*  else {
166        / * Predefined Bitmap - not supported!! Could add function here
167            to load a bitmap from local storage * /
168         bms->uslength=6;       
169         bms->bit_map= Load_predefined_bms (bms->usBMS_id);
170       }
171*/
172
173
174BYE:
175/*
176*
177* A.5     DEBUG Print
178*
179* A.6     RETURN Status
180*/
181    DPRINT2 ("Exiting %s, Status=%d;\n", func, status);
182    return (status);
183/*
184* END OF FUNCTION
185*/ 
186}
Note: See TracBrowser for help on using the repository browser.