Commit 958fd568 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

added support for GRIB2 JPEG compression

parent e0c145ca
2009-11-01 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* added support for GRIB2 JPEG compression
2009-10-28 Uwe Schulzweida <Uwe.Schulzweida@zmaw.de>
* grbWriteVar: bug fix
......
......@@ -58,7 +58,7 @@ int Zlevel = 0;
static
void version(void)
{
fprintf(stderr, "CDI version 1.7.0\n");
fprintf(stderr, "CDI version 1.7.1\n");
cdiPrintVersion();
fprintf(stderr, "\n");
/*
......@@ -77,6 +77,7 @@ void version(void)
1.6.2 3 Jan 2008 : changes for CDI library version 1.1.0 (compress)
1.6.3 26 Mar 2008 : call streamDefTimestep also if ntsteps = 0 (buf fix)
1.7.0 11 Apr 2008 : add option -z zip for deflate compression of netCDF4 variables
1.7.1 1 Nov 2009 : add option -z jpeg for JPEG compression of GRIB2 records
*/
}
......@@ -101,7 +102,8 @@ void usage(void)
fprintf(stderr, "\n");
fprintf(stderr, " -V Print version number\n");
fprintf(stderr, " -z szip Compress GRIB records with szip\n");
fprintf(stderr, " -z szip SZIP compression of GRIB1 records\n");
fprintf(stderr, " jpeg JPEG compression of GRIB2 records\n");
fprintf(stderr, " zip Deflate compression of netCDF4 variables\n");
fprintf(stderr, "\n");
fprintf(stderr, " Report bugs to <Uwe.Schulzweida@zmaw.de>\n");
......@@ -545,6 +547,7 @@ void setDefaultFileType(char *filetypestr)
char *ftstr = filetypestr;
if ( memcmp(filetypestr, "grb2", 4) == 0 ) { ftstr += 4; DefaultFileType = FILETYPE_GRB2;}
else if ( memcmp(filetypestr, "grb1", 4) == 0 ) { ftstr += 4; DefaultFileType = FILETYPE_GRB; }
else if ( memcmp(filetypestr, "grb", 3) == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_GRB; }
else if ( memcmp(filetypestr, "nc2", 3) == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_NC2; }
else if ( memcmp(filetypestr, "nc4", 3) == 0 ) { ftstr += 3; DefaultFileType = FILETYPE_NC4; }
......@@ -606,6 +609,10 @@ void defineCompress(const char *arg)
{
Ztype = COMPRESS_SZIP;
}
else if ( strncmp(arg, "jpeg", len) == 0 )
{
Ztype = COMPRESS_JPEG;
}
else if ( strncmp(arg, "gzip", len) == 0 )
{
Ztype = COMPRESS_GZIP;
......@@ -617,7 +624,7 @@ void defineCompress(const char *arg)
Zlevel = 1;
}
else
fprintf(stderr, "Compression %s unsupported!\n", arg);
fprintf(stderr, "%s compression unsupported!\n", arg);
}
......
......@@ -69,6 +69,26 @@ void printFiletype(int streamID, int vlistID)
}
}
if ( filetype == FILETYPE_GRB2 )
{
int nvars, varID;
int ztype;
nvars = vlistNvars(vlistID);
for ( varID = 0; varID < nvars; varID++ )
{
ztype = vlistInqVarZtype(vlistID, varID);
if ( ztype )
{
if ( ztype == COMPRESS_JPEG )
printf(" JPEG");
break;
}
}
}
printf("\n");
}
......@@ -266,7 +286,7 @@ static void printGridInfo(int vlistID)
fprintf(stdout, " originLon = %g originLat = %g lonParY = %g\n",
originLon, originLat, lonParY);
fprintf(stdout, "%*s", nbyte0, "");
fprintf(stdout, " lat1 = %g lat2 = %g xinc = %gm yinc = %gm\n",
fprintf(stdout, " lat1 = %g lat2 = %g xinc = %g m yinc = %g m\n",
lat1, lat2, xincm, yincm);
}
else /* if ( gridtype == GRID_GENERIC ) */
......
......@@ -43,6 +43,7 @@ extern "C" {
#define COMPRESS_GZIP 2
#define COMPRESS_BZIP2 3
#define COMPRESS_ZIP 4
#define COMPRESS_JPEG 5
/* external data types */
......
......@@ -372,7 +372,7 @@ void grbReadVarSliceDP(int streamID, int varID, int levelID, double *data, int *
static
size_t grbEncode(int filetype, int varID, int levelID, int vlistID, int gridID, int zaxisID, int date, int time, int numavg,
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize)
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize, int ljpeg)
{
size_t nbytes;
......@@ -384,7 +384,7 @@ size_t grbEncode(int filetype, int varID, int levelID, int vlistID, int gridID,
else
{
nbytes = gribapiEncode(varID, levelID, vlistID, gridID, zaxisID, date, time, numavg,
datasize, data, nmiss, gribbuffer, gribbuffersize);
datasize, data, nmiss, gribbuffer, gribbuffersize, ljpeg);
}
return (nbytes);
......@@ -397,6 +397,7 @@ size_t grbSzip(int filetype, unsigned char *gribbuffer, size_t gribbuffersize)
size_t nbytes = 0;
unsigned char *buffer;
size_t buffersize;
static int lszip_warn = 1;
buffersize = gribbuffersize;
buffer = (unsigned char *) malloc(buffersize);
......@@ -409,7 +410,8 @@ size_t grbSzip(int filetype, unsigned char *gribbuffer, size_t gribbuffersize)
}
else
{
Warning(func, "Szip compression of GRIB2 not implemented!");
if ( lszip_warn ) Warning(func, "Szip compression of GRIB2 records not implemented!");
lszip_warn = 0;
nbytes = gribbuffersize;
}
......@@ -436,6 +438,8 @@ int grbWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
size_t nbytes;
int filetype;
stream_t *streamptr;
int ljpeg = 0;
int ljpeg_warn = 1;
streamptr = stream_to_pointer(streamID);
......@@ -462,8 +466,19 @@ int grbWriteVarSliceDP(int streamID, int varID, int levelID, const double *data,
gribbuffersize = datasize*4+3000;
gribbuffer = (unsigned char *) malloc(gribbuffersize);
if ( streamptr->ztype == COMPRESS_JPEG )
{
if ( filetype == FILETYPE_GRB2 )
ljpeg = 1;
else
{
if ( ljpeg_warn ) Warning(func, "Jpeg compression of GRIB1 records not available!");
ljpeg_warn = 0;
}
}
nbytes = grbEncode(filetype, varID, levelID, vlistID, gridID, zaxisID, date, time, numavg,
datasize, data, nmiss, gribbuffer, gribbuffersize);
datasize, data, nmiss, gribbuffer, gribbuffersize, ljpeg);
if ( streamptr->ztype == COMPRESS_SZIP )
nbytes = grbSzip(filetype, gribbuffer, nbytes);
......
......@@ -692,7 +692,7 @@ void gribapiScanTimestep1(int streamID)
if ( gribGetZip(recsize, gribbuffer, &unzipsize) > 0 )
{
ztype = COMPRESS_SZIP;
unzipsize += 100; /* need 0 to 1 bytes for rounding of bds */
unzipsize += 100;
if ( (long) buffersize < unzipsize )
{
buffersize = unzipsize;
......@@ -725,6 +725,12 @@ void gribapiScanTimestep1(int streamID)
}
else
{
size_t len = 256;
char typeOfPacking[256];
GRIB_CHECK(grib_get_string(gh, "typeOfPacking", typeOfPacking, &len), 0);
// fprintf(stderr, "typeOfPacking %d %s\n", len, typeOfPacking);
if ( strncmp(typeOfPacking, "grid_jpeg", len) == 0 ) ztype = COMPRESS_JPEG;
GRIB_CHECK(grib_get_long(gh, "discipline", &lpar), 0);
discip = (int) lpar;
/*
......@@ -1686,7 +1692,7 @@ void gribapiDefTime(grib_handle *gh , int date, int time, int numavg, int timeID
}
static
void gribapiDefGrid(grib_handle *gh, int gridID)
void gribapiDefGrid(grib_handle *gh, int gridID, int ljpeg)
{
static char func[] = "gribapiDefGrid";
int gridtype;
......@@ -1822,7 +1828,10 @@ void gribapiDefGrid(grib_handle *gh, int gridID)
/* South -> North */
//if ( ISEC2_LastLat > ISEC2_FirstLat ) ISEC2_ScanFlag += 64;
GRIB_CHECK(grib_set_string(gh, "typeOfPacking", "grid_simple", &len), 0);
if ( ljpeg )
GRIB_CHECK(grib_set_string(gh, "typeOfPacking", "grid_jpeg", &len), 0);
else
GRIB_CHECK(grib_set_string(gh, "typeOfPacking", "grid_simple", &len), 0);
break;
}
......@@ -2076,7 +2085,7 @@ void gribapiDefLevel(grib_handle *gh, int code, int zaxisID, int levelID)
size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisID, int date, int time, int numavg,
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize)
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize, int ljpeg)
{
static char func[] = "gribapiEncode";
size_t nbytes = 0;
......@@ -2103,7 +2112,7 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
gribapiDefCode(gh, code, tableInqNum(tableID));
gribapiDefTime(gh, date, time, numavg, vlistInqTaxis(vlistID));
gribapiDefGrid(gh, gridID);
gribapiDefGrid(gh, gridID, ljpeg);
gribapiDefLevel(gh, code, zaxisID, levelID);
if ( nmiss > 0 )
......
......@@ -9,6 +9,6 @@ int gribapiDecode(unsigned char *gribbuffer, int gribsize, double *data, int gri
int unreduced, int *nmiss, int *zip);
size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisID, int date, int time, int numavg,
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize);
long datasize, const double *data, int nmiss, unsigned char *gribbuffer, size_t gribbuffersize, int ljpeg);
#endif /* _STREAM_GRIBAPI_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment