Commit d5231906 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

cgribexlib update.

parent a4ef3f6c
2017-09-27 Uwe Schulzweida
* using CGRIBEX library version 1.9.0
* Version 1.9.1 released
2017-09-18 Uwe Schulzweida
......
/* Automatically generated by m214003 at 2017-07-12, do not edit */
/* Automatically generated by m214003 at 2017-09-19, do not edit */
/* CGRIBEXLIB_VERSION="1.8.1" */
/* CGRIBEXLIB_VERSION="1.9.0" */
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5) || defined (__clang__)
#pragma GCC diagnostic push
......@@ -54,23 +54,23 @@
#if ! defined (CGRIBEX_H)
# include "cgribex.h"
#include "cgribex.h"
#endif
#if ! defined (ERROR_H)
# include "error.h"
#include "error.h"
#endif
#if ! defined (DTYPES_H)
# include "dtypes.h"
#include "dtypes.h"
#endif
#if ! defined (UCHAR)
# define UCHAR unsigned char
#define UCHAR unsigned char
#endif
#if defined (CRAY) || defined (SX) || defined (__uxpch__)
# define VECTORCODE
#define VECTORCODE
#endif
......@@ -114,12 +114,13 @@
/* dummy use of unused parameters to silence compiler warnings */
#ifndef UNUSED
# define UNUSED(x) (void)(x)
#define UNUSED(x) (void)(x)
#endif
#define JP23SET 0x7FFFFF /* 2**23 - 1 (---> 8388607) */
#define JP24SET 0xFFFFFF /* 2**24 (---> 16777215) */
#define JP23SET 0x7FFFFF /* 2**23 - 1 (---> 8388607) */
#define POW_2_M24 0.000000059604644775390625 /* pow(2.0, -24.0) */
#define POW_2_M24 0.000000059604644775390625 /* pow(2.0, -24.0) */
#ifdef __cplusplus
extern "C" {
......@@ -127,8 +128,7 @@ extern "C" {
#define intpow2(x) (ldexp(1.0, (x)))
int gribrec_len(unsigned b1, unsigned b2, unsigned b3);
int correct_bdslen(int bdslen, long recsize, long gribpos);
unsigned correct_bdslen(unsigned bdslen, long recsize, long gribpos);
/* CDI converter routines */
......@@ -149,11 +149,12 @@ int cdiEncodeTime(int hour, int minute, int second);
/* CALENDAR types */
#define CALENDAR_STANDARD 0 /* don't change this value (used also in cgribexlib)! */
#define CALENDAR_PROLEPTIC 1
#define CALENDAR_360DAYS 2
#define CALENDAR_365DAYS 3
#define CALENDAR_366DAYS 4
#define CALENDAR_NONE 5
#define CALENDAR_GREGORIAN 1
#define CALENDAR_PROLEPTIC 2
#define CALENDAR_360DAYS 3
#define CALENDAR_365DAYS 4
#define CALENDAR_366DAYS 5
#define CALENDAR_NONE 6
extern FILE *grprsm;
......@@ -229,10 +230,10 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
#define GET_INT1(a) ((1-(int) ((unsigned) (a & 128) >> 6)) * (int) (a&127))
/* this requires a 32-bit default integer machine */
#define GET_UINT4(a,b,c,d) ((int) ((a << 24) + (b << 16) + (c << 8) + (d)))
#define GET_UINT3(a,b,c) ((int) ((a << 16) + (b << 8) + (c)))
#define GET_UINT2(a,b) ((int) ((a << 8) + (b)))
#define GET_UINT1(a) ((int) (a))
#define GET_UINT4(a,b,c,d) ((unsigned) ((a << 24) + (b << 16) + (c << 8) + (d)))
#define GET_UINT3(a,b,c) ((unsigned) ((a << 16) + (b << 8) + (c)))
#define GET_UINT2(a,b) ((unsigned) ((a << 8) + (b)))
#define GET_UINT1(a) ((unsigned) (a))
#define BUDG_START(s) (s[0]=='B' && s[1]=='U' && s[2]=='D' && s[3]=='G')
#define TIDE_START(s) (s[0]=='T' && s[1]=='I' && s[2]=='D' && s[3]=='E')
......@@ -241,7 +242,7 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
/* GRIB1 Section 0: Indicator Section (IS) */
#define GRIB1_SECLEN(s) GET_INT3(s[ 4], s[ 5], s[ 6])
#define GRIB1_SECLEN(s) GET_UINT3(s[ 4], s[ 5], s[ 6])
#define GRIB_EDITION(s) GET_UINT1(s[ 7])
/* GRIB1 Section 1: Product Definition Section (PDS) */
......@@ -279,7 +280,7 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
/* GRIB1 Section 2: Grid Description Section (GDS) */
#define GDS_Len ((gds) == NULL ? 0 : GET_UINT3(gds[ 0], gds[ 1], gds[ 2]))
#define GDS_Len ((gds) == NULL ? 0 : GET_UINT3(gds[0], gds[1], gds[2]))
#define GDS_NV GET_UINT1(gds[ 3])
#define GDS_PVPL GET_UINT1(gds[ 4])
#define GDS_PV ((gds[3] == 0) ? -1 : (int) gds[4] - 1)
......@@ -333,7 +334,7 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
/* GRIB1 Section 3: Bit Map Section (BMS) */
#define BMS_Len ((bms) == NULL ? 0 : (int) (bms[0]<<16)+(bms[1]<<8)+bms[2])
#define BMS_Len ((bms) == NULL ? 0 : GET_UINT3(bms[0], bms[1], bms[2]))
#define BMS_UnusedBits (bms[3])
#define BMS_Numeric
#define BMS_Bitmap ((bms) == NULL ? NULL : (bms)+6)
......@@ -341,10 +342,10 @@ int grib2Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **i
/* GRIB1 Section 4: Binary Data Section (BDS) */
#define BDS_Len ((int) ((bds[0]<<16)+(bds[1]<<8)+bds[2]))
#define BDS_Len GET_UINT3(bds[0], bds[1], bds[2])
#define BDS_Flag (bds[3])
#define BDS_BinScale GET_INT2(bds[ 4], bds[ 5])
#define BDS_RefValue (decfp2((int)bds[ 6], GET_UINT3(bds[ 7], bds[ 8], bds[ 9])))
#define BDS_RefValue (decfp2((int)bds[ 6], GET_UINT3(bds[7], bds[8], bds[9])))
#define BDS_NumBits ((int) bds[10])
#define BDS_RealCoef (decfp2((int)bds[zoff+11], GET_UINT3(bds[zoff+12], bds[zoff+13], bds[zoff+14])))
#define BDS_PackData ((int) ((bds[zoff+11]<<8) + bds[zoff+12]))
......@@ -3720,7 +3721,6 @@ void gribPrintSec4Wave(int *isec4)
}
}
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#include <string.h>
......@@ -3755,34 +3755,6 @@ off_t gribGetPos(int fileID)
}
int gribCheckFiletype(int fileID)
{
int found = 0;
char buffer[4];
if ( fileRead(fileID, buffer, 4) != 4 ) return found;
if ( memcmp(buffer, "GRIB", 4) == 0 )
{
found = 1;
if ( CGRIBEX_Debug ) Message("found GRIB file = %s", fileInqName(fileID));
}
else
{
long offset;
int ierr = gribFileSeek(fileID, &offset);
fileRewind(fileID);
if ( !ierr )
{
found = 1;
if ( CGRIBEX_Debug ) Message("found seek GRIB file = %s", fileInqName(fileID));
}
}
return found;
}
int gribCheckSeek(int fileID, long *offset, int *version)
{
int ierr = gribFileSeek(fileID, offset);
......@@ -3799,59 +3771,6 @@ int gribCheckSeek(int fileID, long *offset, int *version)
}
int gribFileSeekOld(int fileID, long *offset)
{
/* position file pointer after GRIB */
enum { buffersize = 4096 };
unsigned char buffer[buffersize];
int retry = 4096;
*offset = 0;
void *fileptr = filePtr(fileID);
for ( size_t i = 0; i < 4; ++i)
{
int ch = filePtrGetc(fileptr);
if ( ch == EOF ) return (-1);
buffer[i] = (unsigned char)ch;
}
/*
fileRead(fileID, buffer, 4);
*/
while ( retry-- )
{
size_t i;
for ( i = 0; i < buffersize-4; ++i )
{
if (buffer[i ] == 'G' &&
buffer[i+1] == 'R' &&
buffer[i+2] == 'I' &&
buffer[i+3] == 'B')
{
if ( CGRIBEX_Debug )
Message("record offset = %d", (int) *offset);
return (0);
}
else
{
int ch = filePtrGetc(fileptr); if ( ch == EOF ) return (-1); buffer[i+4] = (unsigned char)ch;
(*offset)++;
}
}
buffer[0] = buffer[i ];
buffer[1] = buffer[i+1];
buffer[2] = buffer[i+2];
buffer[3] = buffer[i+3];
}
if ( CGRIBEX_Debug ) Message("record offset = %d", (int) *offset);
return 1;
}
int gribFileSeek(int fileID, long *offset)
{
/* position file pointer after GRIB */
......@@ -3867,80 +3786,30 @@ int gribFileSeek(int fileID, long *offset)
while ( retry-- )
{
ch = filePtrGetc(fileptr);
if ( ch == EOF ) return (-1);
if ( ch == EOF ) return -1;
code = ( (code << 8) + ch ) & 0xFFFFFFFF;
if ( code == GRIB )
{
if ( CGRIBEX_Debug )
Message("record offset = %d", (int) *offset);
return (0);
}
(*offset)++;
}
if ( CGRIBEX_Debug ) Message("record offset = %d", (int) *offset);
return 1;
}
int gribFileSeekTest(int fileID, long *offset)
{
/* position file pointer after GRIB */
const long GRIB = 0x47524942L;
long code = 0;
int ch;
int i = 0;
enum { buffersize = 8 };
unsigned char buffer[buffersize];
unsigned long retry = 4096L*4096L;
int nread = 0;
*offset = 0;
void *fileptr = filePtr(fileID);
while ( retry-- )
{
if ( i >= nread )
{
nread = (int) filePtrRead(fileptr, buffer, buffersize);
if ( nread == 0 ) return (-1);
i = 0;
}
ch = buffer[i++];
code = ( (code << 8) + ch ) & 0xFFFFFFFFL;
if ( code == GRIB )
{
/* printf("end: %d %d\n", nread, i); */
if ( CGRIBEX_Debug )
Message("record offset = %d", (int) *offset);
if ( i != nread ) fileSetPos(fileID, (off_t) i-nread, SEEK_CUR);
return (0);
if ( CGRIBEX_Debug ) Message("record offset = %ld", *offset);
return 0;
}
(*offset)++;
}
if ( CGRIBEX_Debug ) Message("record offset = %d", (int) *offset);
if ( CGRIBEX_Debug ) Message("record offset = %ld", *offset);
return 1;
}
static inline int
read3ByteMSBFirst(void *fileptr)
static inline
unsigned read3ByteMSBFirst(void *fileptr)
{
unsigned b1 = (unsigned)(filePtrGetc(fileptr));
unsigned b2 = (unsigned)(filePtrGetc(fileptr));
unsigned b3 = (unsigned)(filePtrGetc(fileptr));
return (int)((b1 << 16) + (b2 << 8) + b3);
return GET_UINT3(b1, b2, b3);
}
......@@ -3950,31 +3819,23 @@ size_t gribReadSize(int fileID)
void *fileptr = filePtr(fileID);
off_t pos = fileGetPos(fileID);
unsigned b1 = (unsigned) filePtrGetc(fileptr);
unsigned b2 = (unsigned) filePtrGetc(fileptr);
unsigned b3 = (unsigned) filePtrGetc(fileptr);
unsigned gribsize = read3ByteMSBFirst(fileptr);
int gribsize = gribrec_len(b1, b2, b3);
int gribversion = filePtrGetc(fileptr);
if ( gribsize == 24 )
{
if ( gribversion != 1 && gribversion != 2 ) gribversion = 0;
}
if ( gribsize == 24 && gribversion != 1 && gribversion != 2 ) gribversion = 0;
if ( CGRIBEX_Debug )
Message("gribversion = %d", gribversion);
if ( CGRIBEX_Debug ) Message("gribversion = %d", gribversion);
if ( gribversion == 0 )
{
int pdssize = 0, gdssize = 0, bmssize = 0, bdssize = 0;
int issize = 4, essize = 4;
int flag = 0;
unsigned gdssize = 0, bmssize = 0;
unsigned issize = 4, essize = 4;
pdssize = gribsize;
unsigned pdssize = gribsize;
fileSetPos(fileID, (off_t) 3, SEEK_CUR);
if ( CGRIBEX_Debug ) Message("pdssize = %d", pdssize);
flag = filePtrGetc(fileptr);
if ( CGRIBEX_Debug ) Message("pdssize = %u", pdssize);
int flag = filePtrGetc(fileptr);
if ( CGRIBEX_Debug ) Message("flag = %d", flag);
fileSetPos(fileID, (off_t) pdssize-8, SEEK_CUR);
......@@ -3983,57 +3844,63 @@ size_t gribReadSize(int fileID)
{
gdssize = read3ByteMSBFirst(fileptr);
fileSetPos(fileID, (off_t) gdssize-3, SEEK_CUR);
if ( CGRIBEX_Debug ) Message("gdssize = %d", gdssize);
if ( CGRIBEX_Debug ) Message("gdssize = %u", gdssize);
}
if ( flag & 64 )
{
bmssize = read3ByteMSBFirst(fileptr);
fileSetPos(fileID, (off_t) bmssize-3, SEEK_CUR);
if ( CGRIBEX_Debug ) Message("bmssize = %d", bmssize);
if ( CGRIBEX_Debug ) Message("bmssize = %u", bmssize);
}
bdssize = read3ByteMSBFirst(fileptr);
if ( CGRIBEX_Debug ) Message("bdssize = %d", bdssize);
unsigned bdssize = read3ByteMSBFirst(fileptr);
if ( CGRIBEX_Debug ) Message("bdssize = %u", bdssize);
gribsize = issize + pdssize + gdssize + bmssize + bdssize + essize;
rgribsize = (size_t) gribsize;
}
else if ( gribversion == 1 )
{
if ( gribsize > JP23SET ) /* Large GRIB record */
if ( gribsize > JP23SET ) // Large GRIB record
{
int pdssize = 0, gdssize = 0, bmssize = 0, bdssize = 0;
int issize = 4, essize = 4;
int flag;
pdssize = read3ByteMSBFirst(fileptr);
if ( CGRIBEX_Debug ) Message("pdssize = %d", pdssize);
unsigned pdssize = read3ByteMSBFirst(fileptr);
if ( CGRIBEX_Debug ) Message("pdssize = %u", pdssize);
int flag;
for ( int i = 0; i < 5; ++i ) flag = filePtrGetc(fileptr);
if ( CGRIBEX_Debug ) Message("flag = %d", flag);
fileSetPos(fileID, (off_t) pdssize-8, SEEK_CUR);
unsigned gdssize = 0;
if ( flag & 128 )
{
gdssize = read3ByteMSBFirst(fileptr);
fileSetPos(fileID, (off_t) gdssize-3, SEEK_CUR);
if ( CGRIBEX_Debug ) Message("gdssize = %d", gdssize);
if ( CGRIBEX_Debug ) Message("gdssize = %u", gdssize);
}
unsigned bmssize = 0;
if ( flag & 64 )
{
bmssize = read3ByteMSBFirst(fileptr);
fileSetPos(fileID, (off_t) bmssize-3, SEEK_CUR);
if ( CGRIBEX_Debug ) Message("bmssize = %d", bmssize);
if ( CGRIBEX_Debug ) Message("bmssize = %u", bmssize);
}
bdssize = read3ByteMSBFirst(fileptr);
bdssize = correct_bdslen(bdssize, gribsize, issize+pdssize+gdssize+bmssize);
if ( CGRIBEX_Debug ) Message("bdssize = %d", bdssize);
unsigned bdssize = read3ByteMSBFirst(fileptr);
if ( CGRIBEX_Debug ) Message("bdssize = %u", bdssize);
if ( bdssize <= 120 )
{
const int issize = 4;
gribsize &= JP23SET;
gribsize *= 120;
bdssize = correct_bdslen(bdssize, gribsize, issize+pdssize+gdssize+bmssize);
if ( CGRIBEX_Debug ) Message("bdssize = %u", bdssize);
gribsize = issize + pdssize + gdssize + bmssize + bdssize + essize;
gribsize = issize + pdssize + gdssize + bmssize + bdssize + 4;
}
}
rgribsize = (size_t) gribsize;
}
......@@ -4064,7 +3931,7 @@ size_t gribReadSize(int fileID)
size_t gribGetSize(int fileID)
{
long offset;
int ierr = gribFileSeek(fileID, &offset); /* position file pointer after GRIB */
int ierr = gribFileSeek(fileID, &offset); // position file pointer after GRIB
if ( ierr > 0 )
{
Warning("GRIB record not found!");
......@@ -4087,11 +3954,11 @@ size_t gribGetSize(int fileID)
int gribRead(int fileID, unsigned char *buffer, size_t *buffersize)
{
long offset;
int ierr = gribFileSeek(fileID, &offset); /* position file pointer after GRIB */
int ierr = gribFileSeek(fileID, &offset); // position file pointer after GRIB
if ( ierr > 0 )
{
Warning("GRIB record not found!");
return (-2);
return -2;
}
if ( ierr == -1 ) { *buffersize = 0; return -1; }
......@@ -4126,7 +3993,7 @@ int gribRead(int fileID, unsigned char *buffer, size_t *buffersize)
int gribWrite(int fileID, unsigned char *buffer, size_t buffersize)
{
int nwrite = 0;
int nwrite = 0;
if ( (nwrite = (int)(fileWrite(fileID, buffer, buffersize))) != (int) buffersize )
{
......@@ -4136,25 +4003,6 @@ int gribWrite(int fileID, unsigned char *buffer, size_t buffersize)
return nwrite;
}
int gribrec_len(unsigned b1, unsigned b2, unsigned b3)
{
/*
If bit 7 of b1 is set, we have to rescale by factor of 120.
This is a fixup to get round the restriction on product lengths
due to the count being only 24 bits. It is only possible because
the (default) rounding for GRIB products is 120 bytes.
*/
int needRescaling = b1 & (1 << 7);
int gribsize = (int)((((b1&127) << 16)+(b2<<8) + b3));
if ( needRescaling ) gribsize *= 120;
return gribsize;
}
#include <string.h>
#include <ctype.h>
......@@ -5016,7 +4864,7 @@ void ref2ibm(double *pref, int kbits)
#include <string.h>
int correct_bdslen(int bdslen, long recsize, long gribpos)
unsigned correct_bdslen(unsigned bdslen, long recsize, long gribpos)
{
/*
If a very large product, the section 4 length field holds
......@@ -5026,7 +4874,7 @@ int correct_bdslen(int bdslen, long recsize, long gribpos)
due to the count being only 24 bits. It is only possible because
the (default) rounding for GRIB products is 120 bytes.
*/
if ( recsize > JP23SET ) bdslen = (int)(recsize - gribpos - bdslen);
if ( recsize > JP23SET && bdslen <= 120 ) bdslen = (unsigned)(recsize - gribpos - bdslen);
return bdslen;
}
......@@ -5034,20 +4882,14 @@ int correct_bdslen(int bdslen, long recsize, long gribpos)
int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **pdsp,
unsigned char **gdsp, unsigned char **bmsp, unsigned char **bdsp, long *gribrecsize)
{
unsigned char *pds, *gds, *bms, *bds;
unsigned char *bufpointer, *is, *section;
int gribversion, grib1offset;
long gribsize = 0, recsize;
int bdslen;
*gribrecsize = 0;
*pdsp = NULL;
*gdsp = NULL;
*bmsp = NULL;
*bdsp = NULL;
section = gribbuffer;
is = gribbuffer;
unsigned char *section = gribbuffer;
unsigned char *is = gribbuffer;
if ( ! GRIB_START(section) )
{
fprintf(stderr, "Wrong GRIB indicator section: found >%c%c%c%c<\n",
......@@ -5055,45 +4897,41 @@ int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **p
return -1;
}
recsize = gribrec_len(section[4], section[5], section[6]);
unsigned recsize = GET_UINT3(section[4], section[5], section[6]);
gribversion = GRIB_EDITION(section);
if ( GRIB1_SECLEN(section) == 24 && gribversion == 0 ) gribversion = 0;
int gribversion = GRIB_EDITION(section);
if ( recsize == 24 && gribversion == 0 ) gribversion = 0;
if ( gribversion == 1 )
grib1offset = 4;
else
grib1offset = 0;
unsigned grib1offset = (gribversion == 1) ? 4 : 0;
pds = is + 4 + grib1offset;
bufpointer = pds + PDS_Len;
gribsize += 4 + grib1offset + PDS_Len;
unsigned char *pds = is + 4 + grib1offset;
unsigned char *bufpointer = pds + PDS_Len;
unsigned gribsize = 4 + grib1offset + PDS_Len;
unsigned char *gds = NULL;
if ( PDS_HAS_GDS )
{
gds = bufpointer;
bufpointer += GDS_Len;
gribsize += GDS_Len;
}
else
{
gds = NULL;
}
unsigned char *bms = NULL;
if ( PDS_HAS_BMS )
{
bms = bufpointer;
bufpointer += BMS_Len;
gribsize += BMS_Len;
}
else
unsigned char *bds = bufpointer;
unsigned bdslen = BDS_Len;
if ( recsize > JP23SET && bdslen <= 120 )
{
bms = NULL;
recsize &= JP23SET;
recsize *= 120;
bdslen = correct_bdslen(bdslen, recsize, gribsize);
}
bds = bufpointer;
bdslen = BDS_Len;
bdslen = correct_bdslen(bdslen, recsize, gribsize);
bufpointer += bdslen;
gribsize += bdslen;
gribsize += 4;
......@@ -5104,15 +4942,13 @@ int grib1Sections(unsigned char *gribbuffer, long gribbufsize, unsigned char **p
*bdsp = bds;
*gribrecsize = gribsize;
if ( gribbufsize < gribsize )
{
fprintf(stderr, "Length of GRIB message is inconsistent (grib_buffer_size=%ld < grib_record_size=%ld)!\n", gribbufsize, gribsize);
fprintf(stderr, "Inconsistent length of GRIB message (grib_buffer_size=%ld < grib_record_size=%u)!\n", gribbufsize, gribsize);
return 1;
}
/* end section - "7777" in ascii */