Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for fregrid to properly sets NC4 compression #258

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions tools/libfrencutils/mpp_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define MAXFILE 200
#define MAXVAR 1024
#define STRING 255
#define VARNAME_MAXSIZE 64

typedef struct {
int fldid;
Expand Down Expand Up @@ -1021,14 +1022,14 @@ void mpp_def_var_att_double(int fid, int vid, const char *attname, double attval
* settings. If user doesn't specify (set to -1), the settings *
* of the input file are applied *
* ********************************************************************/
void mpp_set_deflation(int fid_in, int fid_out, int deflation, int shuffle) {
// return if deflation set to zero
if (deflation == 0) {
printf("Not compressing due to option\n");
void mpp_set_deflation(int fid_in, int fid_out, int user_deflation, int user_shuffle) {
// return if user is requesting no deflation
if (user_deflation == 0) {
//printf("Not compressing due to option\n");
ceblanton marked this conversation as resolved.
Show resolved Hide resolved
return;
}

// return if netcdf3
// return if netcdf3 which does not support compression
int format;
char errmsg[512];
int status;
Expand All @@ -1037,37 +1038,53 @@ void mpp_set_deflation(int fid_in, int fid_out, int deflation, int shuffle) {
sprintf(errmsg, "mpp_io(mpp_set_deflation): Error in getting determining netcdf version");
netcdf_error(errmsg, status);
}
printf("Input: filename=%s, nvar=%i, format=%i\n", files[fid_in].name, files[fid_in].nvar, format);
//printf("Input: filename=%s, nvar=%i, format=%i\n", files[fid_in].name, files[fid_in].nvar, format);
if (format == NC_FORMAT_CLASSIC || format == NC_FORMAT_64BIT) {
printf("Not compressing because input file is NetCDF3\n");
//printf("Not compressing because input file is NetCDF3\n");
ceblanton marked this conversation as resolved.
Show resolved Hide resolved
return;
}

int v, shuffle2, deflate2, deflation2;

// loop thru vars
for (v = 0; v < files[fid_in].nvar; ++v) {
// get existing compression settings
status = nc_inq_var_deflate(files[fid_in].ncid, files[fid_in].var[v].fldid, &shuffle2, &deflate2, &deflation2);
if (status != NC_NOERR) {
sprintf(errmsg, "mpp_io(mpp_set_deflation): Error in getting deflation level");
netcdf_error(errmsg, status);
// loop thru output vars
int vid_in;
char varname[VARNAME_MAXSIZE];
int shuffle_in, deflate_in, deflation_in;
int shuffle_out, deflation_out;

for (int vid_out = 0; vid_out < files[fid_out].nvar; ++vid_out) {
// if user has not specified options, look up and use settings in input file
if (user_deflation == -1 || user_shuffle == -1) {
mpp_get_varname(fid_in, vid_out, varname);
ceblanton marked this conversation as resolved.
Show resolved Hide resolved
vid_in = mpp_get_varid(fid_in, varname);

status = nc_inq_var_deflate(files[fid_in].ncid, files[fid_in].var[vid_in].fldid, &shuffle_in, &deflate_in, &deflation_in);
ceblanton marked this conversation as resolved.
Show resolved Hide resolved
if (status != NC_NOERR) {
sprintf(errmsg, "mpp_io(mpp_set_deflation): Error in getting deflation level");
netcdf_error(errmsg, status);
}
//printf("Input: var=%s, shuffle=%i, deflate=%i, deflation=%i\n", files[fid_in].var[vid_in].name, shuffle_in, deflate_in, deflation_in);

if (user_deflation == -1)
deflation_out = deflation_in;
else
deflation_out = user_deflation;
if (user_shuffle == -1)
shuffle_out = shuffle_in;
else
shuffle_out = user_shuffle;
}
// if user has specified options, then use them
else {
deflation_out = user_deflation;
shuffle_out = user_shuffle;
}
printf("Input: var=%s, shuffle=%i, deflate=%i, deflation=%i\n", files[fid_in].var[v].name, shuffle2, deflate2, deflation2);

// apply overrides
if (deflation == -1)
deflation = deflation2;
if (shuffle == -1)
shuffle = shuffle2;

// set compression level
status = nc_def_var_deflate(files[fid_out].ncid, files[fid_out].var[v].fldid, shuffle, deflation, deflation);
//printf("Output: var=%s, shuffle=%i, deflation=%i\n", files[fid_out].var[vid_out].name, shuffle_out, deflation_out);
status = nc_def_var_deflate(files[fid_out].ncid, files[fid_out].var[vid_out].fldid, shuffle_out, deflation_out, deflation_out);
if (status != NC_NOERR) {
sprintf(errmsg, "mpp_io(mpp_set_deflation): Error in setting deflation level");
netcdf_error(errmsg, status);
}
printf("Output: var=%s, shuffle=%i, deflation=%i\n", files[fid_in].var[v].name, shuffle, deflation);
}
}

Expand Down
Loading