CMake netCDF Compatibility with WPS (#2121)
[WRF.git] / frame / pack_utils.c
blobbdd608d3e1fcf2ede669d5604fea501e22fd77a6
1 #ifndef MS_SUA
2 # include <stdio.h>
3 # include <stdlib.h>
4 #endif
5 #include <string.h>
6 #include "../inc/streams.h"
8 #ifndef CRAY
9 # ifdef NOUNDERSCORE
10 # define INT_PACK_DATA int_pack_data
11 # define INT_GET_TI_HEADER_C int_get_ti_header_c
12 # define INT_GEN_TI_HEADER_C int_gen_ti_header_c
13 # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c
14 # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c
15 # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c
16 # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field
17 # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field
18 # define PERTURB_REAL perturb_real
19 # define INSPECT_HEADER inspect_header
20 # define RESET_MASK reset_mask
21 # define SET_MASK set_mask
22 # define GET_MASK get_mask
23 # else
24 # ifdef F2CSTYLE
25 # define INT_PACK_DATA int_pack_data__
26 # define INT_GET_TI_HEADER_C int_get_ti_header_c__
27 # define INT_GEN_TI_HEADER_C int_gen_ti_header_c__
28 # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c__
29 # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c__
30 # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c__
31 # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field__
32 # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field__
33 # define PERTURB_REAL perturb_real__
34 # define INSPECT_HEADER inspect_header__
35 # define RESET_MASK reset_mask__
36 # define SET_MASK set_mask__
37 # define GET_MASK get_mask__
38 # else
39 # define INT_PACK_DATA int_pack_data_
40 # define INT_GET_TI_HEADER_C int_get_ti_header_c_
41 # define INT_GEN_TI_HEADER_C int_gen_ti_header_c_
42 # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c_
43 # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c_
44 # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c_
45 # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field_
46 # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field_
47 # define PERTURB_REAL perturb_real_
48 # define INSPECT_HEADER inspect_header_
49 # define RESET_MASK reset_mask_
50 # define SET_MASK set_mask_
51 # define GET_MASK get_mask_
52 # endif
53 # endif
54 #endif
56 #ifdef MEMCPY_FOR_BCOPY
57 # define bcopy(A,B,C) memcpy((B),(A),(C))
58 #endif
60 /* CALL int_pack_data ( hdrbuf , hdrbufsiz * inttypesize , int_local_output_buffer, int_local_output_cursor ) */
62 void
63 INT_PACK_DATA ( unsigned char *buf , int *ninbytes , unsigned char *obuf, int *cursor )
65 int i, lcurs ;
66 lcurs = *cursor - 1 ;
67 for ( i = 0 ; i < *ninbytes ; i++ )
69 obuf[lcurs++] = buf[i] ;
71 *cursor = lcurs+1 ;
74 int
75 INT_GEN_TI_HEADER_C ( char * hdrbuf, int * hdrbufsize, /* hdrbufsize is in bytes */
76 int * itypesize, int * typesize,
77 int * DataHandle, char * Data,
78 int * Count, int * code )
80 int i ;
81 char * p ;
82 p = hdrbuf ;
83 p += sizeof(int) ;
84 bcopy( code, p, sizeof(int) ) ; p += sizeof(int) ; /* 2 */
85 bcopy( DataHandle, p, sizeof(int) ) ; p += sizeof(int) ; /* 3 */
86 bcopy( typesize, p, sizeof(int) ) ; p += sizeof(int) ; /* 4 */
87 bcopy( Count, p, sizeof(int) ) ; p += sizeof(int) ; /* 5 */
88 bcopy( Data, p, *Count * *typesize ) ; p += *Count * *typesize ; /* 6++ */
89 *hdrbufsize = (int) (p - hdrbuf) ;
90 bcopy( hdrbufsize, hdrbuf, sizeof(int) ) ;
91 return(0) ;
94 int
95 INT_GET_TI_HEADER_C ( char * hdrbuf, int * hdrbufsize, int * n, /* hdrbufsize and n are in bytes */
96 int * itypesize, int * typesize,
97 int * DataHandle, char * Data,
98 int * Count, int * code )
100 int i ;
101 char * p ;
102 p = hdrbuf ;
103 bcopy( p, hdrbufsize, sizeof(int) ) ; p += sizeof(int) ; /* 1 */
104 bcopy( p, code, sizeof(int) ) ; p += sizeof(int) ; /* 2 */
105 bcopy( p, DataHandle, sizeof(int) ) ; p += sizeof(int) ; /* 3 */
106 bcopy( p, typesize, sizeof(int) ) ; p += sizeof(int) ; /* 4 */
107 bcopy( p, Count, sizeof(int) ) ; p += sizeof(int) ; /* 5 */
108 if ( *Count * *typesize > 0 ) {
109 bcopy( p, Data, *Count * *typesize ) ; p += *Count * *typesize ; /* 6++ */
111 *n = (int)( p - hdrbuf ) ;
112 return(0) ;
115 #define MAX_FLDS 2000
116 static char fld_name[MAX_FLDS][256] ;
117 static char *fld_cache[MAX_FLDS] ;
118 static int fld_curs[MAX_FLDS] ;
119 static int fld_bufsize[MAX_FLDS] ;
120 static int fld = 0 ;
121 static int numflds = 0 ;
122 static int frst = 1 ;
124 int INIT_STORE_PIECE_OF_FIELD ()
126 int i ;
127 if ( frst ) {
128 for ( i = 0 ; i < MAX_FLDS ; i++ ) {
129 fld_cache[i] = NULL ;
131 frst = 0 ;
133 numflds = 0 ;
134 for ( i = 0 ; i < MAX_FLDS ; i++ ) {
135 strcpy( fld_name[i], "" ) ;
136 if ( fld_cache[i] != NULL ) free( fld_cache[i] ) ;
137 fld_cache[i] = NULL ;
138 fld_curs[i] = 0 ;
139 fld_bufsize[i] = 0 ;
141 return(0) ;
144 int INIT_RETRIEVE_PIECES_OF_FIELD ()
146 fld = 0 ;
147 return(0) ;
151 ADD_TO_BUFSIZE_FOR_FIELD_C ( int varname[], int * chunksize )
153 int i, n ;
154 int found ;
155 char vname[256] ;
157 n = varname[0] ;
158 for ( i = 1; i <= n ; i++ ) { vname[i-1] = varname[i] ; }
159 vname[n] = '\0' ;
161 found = -1 ;
162 for ( i = 0 ; i < numflds ; i++ ) { if ( !strcmp( fld_name[i], vname ) ) { found = i ; break ; } }
163 if ( found == -1 ) {
164 found = numflds++ ;
165 strcpy( fld_name[found], vname ) ;
166 fld_bufsize[found] = *chunksize ;
168 else
170 fld_bufsize[found] += *chunksize ;
172 if ( fld_cache[found] != NULL ) { free( fld_cache[found] ) ; }
173 fld_cache[found] = NULL ;
174 return(0) ;
178 STORE_PIECE_OF_FIELD_C ( char * buf , int varname[], int * chunksize, int *retval )
180 int i, n ;
181 int found ;
182 char vname[256] ;
184 n = varname[0] ;
185 for ( i = 1; i <= n ; i++ ) { vname[i-1] = varname[i] ; }
186 vname[n] = '\0' ;
188 found = -1 ;
189 for ( i = 0 ; i < numflds ; i++ ) { if ( !strcmp( fld_name[i], vname ) ) { found = i ; break ; } }
190 if ( found == -1 ) {
191 #ifndef MS_SUA
192 fprintf(stderr,"frame/pack_utils.c: field (%s) not found; was not set up with add_to_bufsize_for_field\n",vname ) ;
193 #endif
194 *retval = 1 ;
195 return(0) ;
198 if ( fld_cache[found] == NULL ) {
199 fld_cache[found] = (char *) malloc( fld_bufsize[found] ) ;
200 fld_curs[found] = 0 ;
203 if ( fld_curs[found] + *chunksize > fld_bufsize[found] ) {
204 #ifndef MS_SUA
205 fprintf(stderr,
206 "frame/pack_utils.c: %s would overwrite %d + %d > %d [%d]\n",vname, fld_curs[found], *chunksize, fld_bufsize[found], found ) ;
207 #endif
208 *retval = 1 ;
209 return(0) ;
212 bcopy( buf, fld_cache[found]+fld_curs[found], *chunksize ) ;
213 fld_curs[found] += *chunksize ;
214 *retval = 0 ;
215 return(0) ;
219 RETRIEVE_PIECES_OF_FIELD_C ( char * buf , int varname[], int * insize, int * outsize, int *retval )
221 int i, n ;
222 int found ;
223 char vname[256] ;
225 if ( fld < numflds ) {
226 #ifndef MS_SUA
227 if ( fld_curs[fld] > *insize ) {
228 fprintf(stderr,"retrieve: fld_curs[%d] (%d) > *insize (%d)\n",fld,fld_curs[fld], *insize ) ;
230 #endif
231 *outsize = ( fld_curs[fld] <= *insize ) ? fld_curs[fld] : *insize ;
232 bcopy( fld_cache[fld], buf, *outsize ) ;
233 varname[0] = (int) strlen( fld_name[fld] ) ;
234 for ( i = 1 ; i <= varname[0] ; i++ ) varname[i] = fld_name[fld][i-1] ;
235 if ( fld_cache[fld] != NULL ) free ( fld_cache[fld] ) ;
236 fld_cache[fld] = NULL ;
237 fld_bufsize[fld] = 0 ;
238 fld++ ;
239 *retval = 0 ;
241 else {
242 numflds = 0 ;
243 *retval = -1 ;
245 return(0) ;
248 #define INDEX_2(A,B,NB) ( (B) + (A)*(NB) )
249 #define INDEX_3(A,B,C) INDEX_2( (A), INDEX_2( (B), (C), (me[1]-ms[1]+1) ), (me[1]-ms[1]+1)*(me[0]-ms[0]+1) )
250 /* flip low order bit of fp number */
252 PERTURB_REAL ( float * field, int ds[], int de[], int ms[], int me[], int ps[], int pe[] )
254 int i,j,k ;
255 int le ; /* index of little end */
256 float x = 2.0 ;
257 unsigned int y ;
258 unsigned char a[4], *p ;
259 if ( sizeof(float) != 4 ) return(-1) ;
260 /* check endianness of machine */
261 bcopy ( &x, a, 4 ) ;
262 le = 0 ;
263 if ( a[0] == 0x40 ) le = 3 ;
264 for ( k = ps[2]-ms[2] ; k <= pe[2]-ms[2] ; k++ )
265 for ( j = ps[1]-ms[1] ; j <= pe[1]-ms[1] ; j++ )
266 for ( i = ps[0]-ms[0] ; i <= pe[0]-ms[0] ; i++ )
268 /* do not change zeros */
269 if ( field[ INDEX_3(k,j,i) ] != 0.0 ) {
270 p = (unsigned char *)&(field[ INDEX_3(k,j,i) ] ) ;
271 if ( *(p+le) & 1 ) { *(p+le) &= 0x7e ; }
272 else { *(p+le) |= 1 ; }
275 return(0) ;
278 int INSPECT_HEADER ( char * buf, int * sz, int * line )
280 int i ;
281 #ifndef MS_SUA
282 fprintf(stderr,"INSPECT_HEADER: line = %d ", *line ) ;
283 if ( buf != NULL && sz != NULL ) {
284 for ( i = 0 ; i < *sz && i < 256 ; i++ ) { if ( (buf[i] >= 'a' && buf[i] <= 'z') || buf[i] == '_' ||
285 (buf[i] >= 'A' && buf[i] <= 'Z') ||
286 (buf[i] >= '0' && buf[i] <= '9') ) fprintf(stderr,"%c",buf[i]) ;
288 fprintf(stderr,"\n") ;
290 #endif
291 return(0) ;
294 /* note that these work the same as the routines in tools/misc.c, but are Fortran callable.
295 They must be kept in sync, functionally. */
297 void
298 RESET_MASK ( unsigned int * mask , int *e )
300 int w ;
301 unsigned int m, n ;
303 w = *e / (8*sizeof(int)-1) ;
304 n = 1 ;
305 m = ~( n << *e % (8*sizeof(int)-1) ) ;
306 if ( w >= 0 && w < IO_MASK_SIZE ) {
307 mask[w] &= m ;
311 void
312 SET_MASK ( unsigned int * mask , int *e )
314 int w ;
315 unsigned int m, n ;
317 w = *e / (8*sizeof(int)-1) ;
318 n = 1 ;
319 m = ( n << *e % (8*sizeof(int)-1) ) ;
320 if ( w >= 0 && w < IO_MASK_SIZE ) {
321 mask[w] |= m ;
325 /* this is slightly different from in tools dir since it returns result as argument, not function */
326 /* definition of IO_MASK_SIZE comes from build and must be uniform with frame/module_domain_type.F and
327 version of this function in tools dir */
328 void
329 GET_MASK ( unsigned int * mask , int *e , int * retval )
331 int w ;
332 unsigned int m, n ;
334 w = *e / (8*sizeof(int)-1) ; /* 8 is number of bits per byte */
335 if ( w >= 0 && w < IO_MASK_SIZE ) {
336 m = mask[w] ;
337 n = ( 1 << *e % (8*sizeof(int)-1) ) ;;
338 *retval = ( (m & n) != 0 ) ;
339 } else {
340 *retval = 0 ;
344 #ifdef WRAP_MALLOC
345 # ifndef WRAP_MALLOC_ALIGNMENT
346 # define WRAP_MALLOC_ALIGNMENT 128
347 # endif
348 # define _XOPEN_SOURCE 600
349 # include <stdlib.h>
350 void *malloc(size_t size)
352 void *tmp;
353 if (posix_memalign(&tmp, WRAP_MALLOC_ALIGNMENT, size) == 0)
354 return tmp;
355 else {
356 errno = ENOMEM;
357 return NULL;
360 #endif
362 #ifndef DM_PARALLEL
363 # ifndef CRAY
364 # ifdef NOUNDERSCORE
365 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock
366 # else
367 # ifdef F2CSTYLE
368 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock__
369 # else
370 # define RSL_INTERNAL_MICROCLOCK rsl_internal_microclock_
371 # endif
372 # endif
373 # endif
374 # if !defined(MS_SUA) && !defined(_WIN32)
375 # include <sys/time.h>
376 int RSL_INTERNAL_MICROCLOCK ()
378 struct timeval tb ;
379 struct timezone tzp ;
380 int isec ; /* seconds */
381 int usec ; /* microseconds */
382 int msecs ;
383 gettimeofday( &tb, &tzp ) ;
384 isec = tb.tv_sec ;
385 usec = tb.tv_usec ;
386 msecs = 1000000 * isec + usec ;
387 return(msecs) ;
389 # endif
390 #endif