diff --git a/src/grib_api.h b/src/grib_api.h index 5a8dcfc9d..fa9763e55 100644 --- a/src/grib_api.h +++ b/src/grib_api.h @@ -1497,14 +1497,15 @@ typedef struct grib_util_grid_spec2 { } grib_util_grid_spec2; -#define GRIB_UTIL_PACKING_TYPE_SAME_AS_INPUT 0 -#define GRIB_UTIL_PACKING_TYPE_SPECTRAL_COMPLEX 1 -#define GRIB_UTIL_PACKING_TYPE_SPECTRAL_SIMPLE 2 -#define GRIB_UTIL_PACKING_TYPE_JPEG 3 -#define GRIB_UTIL_PACKING_TYPE_GRID_COMPLEX 4 -#define GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE 5 -#define GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE_MATRIX 6 -#define GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER 7 +#define GRIB_UTIL_PACKING_TYPE_SAME_AS_INPUT 0 +#define GRIB_UTIL_PACKING_TYPE_SPECTRAL_COMPLEX 1 +#define GRIB_UTIL_PACKING_TYPE_SPECTRAL_SIMPLE 2 +#define GRIB_UTIL_PACKING_TYPE_JPEG 3 +#define GRIB_UTIL_PACKING_TYPE_GRID_COMPLEX 4 +#define GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE 5 +#define GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE_MATRIX 6 +#define GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER 7 +#define GRIB_UTIL_PACKING_TYPE_CCSDS 8 #define GRIB_UTIL_PACKING_SAME_AS_INPUT 0 #define GRIB_UTIL_PACKING_USE_PROVIDED 1 diff --git a/src/grib_util.c b/src/grib_util.c index 63c7f3a03..b5bd0db27 100644 --- a/src/grib_util.c +++ b/src/grib_util.c @@ -857,6 +857,7 @@ grib_handle* grib_util_set_spec2(grib_handle* h, int packingTypeIsSet=0; int setSecondOrder=0; int setJpegPacking=0; + int setCcsdsPacking=0; int convertEditionEarlier=0;/* For cases when we cannot set some keys without converting */ size_t slen=17; int grib1_high_resolution_fix = 0; /* boolean: See GRIB-863 */ @@ -925,6 +926,10 @@ grib_handle* grib_util_set_spec2(grib_handle* h, if (strcmp(input_packing_type,"grid_jpeg") && !strcmp(input_packing_type,"grid_simple")) SET_STRING_VALUE("packingType","grid_jpeg"); break; + case GRIB_UTIL_PACKING_TYPE_CCSDS: + if (strcmp(input_packing_type,"grid_ccsds") && !strcmp(input_packing_type,"grid_simple")) + SET_STRING_VALUE("packingType","grid_ccsds"); + break; case GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER: /* we delay the set of grid_second_order because we don't want to do it on a field with bitsPerValue=0 */ @@ -1327,6 +1332,14 @@ grib_handle* grib_util_set_spec2(grib_handle* h, if (strcmp(input_packing_type,"grid_jpeg") && !strcmp(input_packing_type,"grid_simple")) setJpegPacking = 1; break; + case GRIB_UTIL_PACKING_TYPE_CCSDS: + /* Have to delay CCSDS packing: + * Reason 1: It is not available in GRIB1 and so we have to wait until we change edition + * Reason 2: It has to be done AFTER we set the data values + */ + if (strcmp(input_packing_type,"grid_ccsds") && !strcmp(input_packing_type,"grid_simple")) + setCcsdsPacking = 1; + break; case GRIB_UTIL_PACKING_TYPE_GRID_SECOND_ORDER: /* we delay the set of grid_second_order because we don't want to do it on a field with bitsPerValue=0 */ @@ -1606,6 +1619,14 @@ grib_handle* grib_util_set_spec2(grib_handle* h, goto cleanup; } } + if (setCcsdsPacking == 1) { + *err = grib_set_string(outh, "packingType", "grid_ccsds", &slen); + if (*err != GRIB_SUCCESS) { + fprintf(stderr,"GRIB_UTIL_SET_SPEC: Failed to change packingType to CCSDS: %s\n", + grib_get_error_message(*err)); + goto cleanup; + } + } } if (packing_spec->deleteLocalDefinition) { diff --git a/tests/grib_util_set_spec.c b/tests/grib_util_set_spec.c index 6bf315070..a7b073e7e 100644 --- a/tests/grib_util_set_spec.c +++ b/tests/grib_util_set_spec.c @@ -23,6 +23,8 @@ static int get_packing_type_code(const char* packingType) if (STR_EQUAL(packingType, "grid_jpeg")) result = GRIB_UTIL_PACKING_TYPE_JPEG; + if (STR_EQUAL(packingType, "grid_ccsds")) + result = GRIB_UTIL_PACKING_TYPE_CCSDS; else if (STR_EQUAL(packingType, "grid_simple")) result = GRIB_UTIL_PACKING_TYPE_GRID_SIMPLE; else if (STR_EQUAL(packingType, "grid_second_order")) @@ -178,6 +180,7 @@ static void test_regular_ll(int remove_local_def, int edition, const char* packi spec.Nj = 14; spec.Ni = 17; outlen = spec.Nj * spec.Ni; + /* outlen = inlen; */ spec.iDirectionIncrementInDegrees = 1.5; spec.jDirectionIncrementInDegrees = 1.5; spec.latitudeOfFirstGridPointInDegrees = 60.0001; @@ -330,7 +333,7 @@ static void test_grid_complex_spatial_differencing(int remove_local_def, int edi static void usage(const char *prog) { fprintf(stderr, "%s: [-p packingType] [-r] [-e edition] in.grib out.grib\n", prog); - fprintf(stderr, "-p packingType: one of grid_jpeg, grid_second_order or grid_simple\n"); + fprintf(stderr, "-p packingType: one of grid_jpeg, grid_ccsds, grid_second_order or grid_simple\n"); fprintf(stderr, "-r remove local definition\n"); fprintf(stderr, "-e edition: 1 or 2\n"); exit(1); diff --git a/tests/grib_util_set_spec.sh b/tests/grib_util_set_spec.sh index 35d0a81b0..70cd5c841 100755 --- a/tests/grib_util_set_spec.sh +++ b/tests/grib_util_set_spec.sh @@ -58,6 +58,14 @@ if [ $HAVE_JPEG -eq 1 ]; then [ "$res" = "2 1 grid_jpeg" ] fi +# Convert to edition2 and use CCSDS for packing +if [ $HAVE_AEC -eq 1 ]; then + infile=../data/latlon.grib + $EXEC $grib_util_set_spec -e 2 -p grid_ccsds $infile $outfile > /dev/null + res=`${tools_dir}/grib_get -p edition,section2Used,packingType $outfile` + [ "$res" = "2 1 grid_ccsds" ] +fi + # -------------------------------------------------- # Reduced Gaussian Grid N=32 second order packing # --------------------------------------------------