Skip to content

Commit 1d45655

Browse files
Merge branch 'develop' into feature/sfc_climo_gen.frac
Fixes ufs-community#709.
2 parents 17fa799 + 6af8f1a commit 1d45655

18 files changed

+3172
-1545
lines changed

sorc/fre-nctools.fd/shared_lib/mpp_io.c

+206-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,41 @@ int mpp_open(const char *file, int action) {
6565
char curfile[STRING];
6666
char errmsg[512];
6767
int ncid, status, istat, n, fid;
68-
/* size_t blksz=65536; */
69-
size_t blksz=1048576;
68+
static int first_call = 1;
69+
static size_t blksz=1048576;
70+
71+
/* read the blksz from environment variable for the first_call */
72+
if(first_call) {
73+
char *blkstr;
74+
int len;
75+
first_call = 0;
76+
blkstr=getenv ("NC_BLKSZ");
77+
if(blkstr) {
78+
len=strlen(blkstr);
79+
/* check to make sure each character is either number of 'K' or 'M' */
80+
for(n=0; n<len; n++) {
81+
if( n == len-1 ) { /* the last character might be K or M */
82+
if( (blkstr[n] > '9' || blkstr[n] < '0') && blkstr[n] != 'K' && blkstr[n] != 'M' ) {
83+
sprintf( errmsg, "mpp_io(mpp_open): the last charactor of environment variable NC_BLKSZ = %s "
84+
"should be digit, 'K' or 'M'", blkstr);
85+
mpp_error(errmsg);
86+
}
87+
}
88+
else if( blkstr[n] > '9' || blkstr[n] < '0' ) {
89+
sprintf( errmsg, "mpp_io(mpp_open): environment variable NC_BLKSZ = %s "
90+
"should only contain digit except the last character", blkstr);
91+
printf("error 2 = %s\n", errmsg);
92+
mpp_error(errmsg);
93+
}
94+
}
95+
blksz = atoi(blkstr);
96+
if( blkstr[len-1] == 'K' )
97+
blksz *= 1024;
98+
else if( blkstr[len-1] == 'M' )
99+
blksz *= (1024*1024);
100+
}
101+
102+
}
70103

71104
/* write only from root pe. */
72105
if(action != MPP_READ && mpp_pe() != mpp_root_pe() ) return -1;
@@ -209,7 +242,35 @@ void mpp_get_varname(int fid, int varid, char *name)
209242
}
210243

211244
}
245+
246+
int mpp_get_record_name(int fid, char *name)
247+
{
248+
int dimid, status;
249+
char errmsg[512];
250+
int record_exist;
251+
if(fid<0 || fid >=nfiles) mpp_error("mpp_io(mpp_get_record_name): invalid id number, id should be "
252+
"a nonnegative integer that less than nfiles");
253+
status = nc_inq_unlimdim(files[fid].ncid, &dimid);
254+
if(status != NC_NOERR) {
255+
sprintf(errmsg, "mpp_io(mpp_get_record_name): error in get record id from file %s", files[fid].name);
256+
netcdf_error(errmsg, status);
257+
}
258+
if(dimid >=0) {
259+
record_exist = 1;
260+
status = nc_inq_dimname(files[fid].ncid, dimid, name);
261+
if(status != NC_NOERR) {
262+
sprintf(errmsg, "mpp_io(mpp_get_record_name): error in get record name from file %s", files[fid].name);
263+
netcdf_error(errmsg, status);
264+
}
265+
}
266+
else {
267+
record_exist = 0;
268+
}
269+
return record_exist;
270+
}
271+
212272

273+
213274
/*******************************************************************************/
214275
/* */
215276
/* The following are routines that retrieve information */
@@ -665,12 +726,13 @@ char mpp_get_var_cart(int fid, int vid)
665726
fldid = files[fid].var[vid].fldid;
666727
status = nc_get_att_text(ncid, fldid, "cartesian_axis", &cart);
667728
if(status != NC_NOERR)status = nc_get_att_text(ncid, fldid, "axis", &cart);
729+
/*
668730
if(status != NC_NOERR){
669731
sprintf(errmsg, "mpp_io(mpp_get_var_cart): Error in getting attribute cartesian_axis/axis of "
670732
"dimension variable %s from file %s", files[fid].var[vid].name, files[fid].name );
671733
netcdf_error(errmsg, status);
672734
}
673-
735+
*/
674736
return cart;
675737
}
676738

@@ -937,6 +999,63 @@ void mpp_def_var_att_double(int fid, int vid, const char *attname, double attval
937999

9381000

9391001

1002+
/**********************************************************************
1003+
* void mpp_set_deflation(fid_in, fid_out, deflation, shuffle) *
1004+
* Sets netcdf4 deflation on the output file. If NetCDF3, exits. *
1005+
* If user requests deflation and shuffle settings, applies those *
1006+
* settings. If user doesn't specify (set to -1), the settings *
1007+
* of the input file are applied *
1008+
* ********************************************************************/
1009+
void mpp_set_deflation(int fid_in, int fid_out, int deflation, int shuffle) {
1010+
// return if deflation set to zero
1011+
if (deflation == 0) {
1012+
printf("Not compressing due to option\n");
1013+
return;
1014+
}
1015+
1016+
// return if netcdf3
1017+
int format;
1018+
char errmsg[512];
1019+
int status;
1020+
status = nc_inq_format(files[fid_in].ncid, &format);
1021+
if (status != NC_NOERR) {
1022+
sprintf(errmsg, "mpp_io(mpp_set_deflation): Error in getting determining netcdf version");
1023+
netcdf_error(errmsg, status);
1024+
}
1025+
printf("Input: filename=%s, nvar=%i, format=%i\n", files[fid_in].name, files[fid_in].nvar, format);
1026+
if (format == NC_FORMAT_CLASSIC || format == NC_FORMAT_64BIT) {
1027+
printf("Not compressing because input file is NetCDF3\n");
1028+
return;
1029+
}
1030+
1031+
int v, shuffle2, deflate2, deflation2;
1032+
1033+
// loop thru vars
1034+
for (v = 0; v < files[fid_in].nvar; ++v) {
1035+
// get existing compression settings
1036+
status = nc_inq_var_deflate(files[fid_in].ncid, files[fid_in].var[v].fldid, &shuffle2, &deflate2, &deflation2);
1037+
if (status != NC_NOERR) {
1038+
sprintf(errmsg, "mpp_io(mpp_set_deflation): Error in getting deflation level");
1039+
netcdf_error(errmsg, status);
1040+
}
1041+
printf("Input: var=%s, shuffle=%i, deflate=%i, deflation=%i\n", files[fid_in].var[v].name, shuffle2, deflate2, deflation2);
1042+
1043+
// apply overrides
1044+
if (deflation == -1)
1045+
deflation = deflation2;
1046+
if (shuffle == -1)
1047+
shuffle = shuffle2;
1048+
1049+
// set compression level
1050+
status = nc_def_var_deflate(files[fid_out].ncid, files[fid_out].var[v].fldid, shuffle, deflation, deflation);
1051+
if (status != NC_NOERR) {
1052+
sprintf(errmsg, "mpp_io(mpp_set_deflation): Error in setting deflation level");
1053+
netcdf_error(errmsg, status);
1054+
}
1055+
printf("Output: var=%s, shuffle=%i, deflation=%i\n", files[fid_in].var[v].name, shuffle, deflation);
1056+
}
1057+
}
1058+
9401059
/**********************************************************************
9411060
void mpp_copy_var_att(fid_in, fid_out)
9421061
copy all the field attribute from infile to outfile
@@ -981,9 +1100,55 @@ void mpp_copy_var_att(int fid_in, int vid_in, int fid_out, int vid_out)
9811100
}
9821101
}
9831102

984-
}; /* mpp_copy_field_att */
1103+
} /* mpp_copy_field_att */
9851104

1105+
/**********************************************************************
1106+
void mpp_copy_var(fid_in, vid_in, fid_out)
1107+
copy one field from fid_in to fid_out
1108+
**********************************************************************/
1109+
void mpp_copy_data(int fid_in, int vid_in, int fid_out, int vid_out)
1110+
{
1111+
int status;
1112+
int ndim, dims[5], i;
1113+
size_t dsize, size;
1114+
char errmsg[512];
1115+
double *data=NULL;
1116+
if( mpp_pe() != mpp_root_pe() ) return;
1117+
1118+
if(fid_in<0 || fid_in >=nfiles) mpp_error("mpp_io(mpp_copy_var): invalid fid_in number, fid should be "
1119+
"a nonnegative integer that less than nfiles");
1120+
if(fid_out<0 || fid_out >=nfiles) mpp_error("mpp_io(mpp_copy_var): invalid fid_out number, fid should be "
1121+
"a nonnegative integer that less than nfiles");
1122+
/*
1123+
ncid_in = files[fid_in].ncid;
1124+
ncid_out = files[fid_out].ncid;
1125+
fldid_in = files[fid_in].var[vid_in].fldid;
1126+
fldid_out = files[fid_out].var[vid_out].fldid;
1127+
*/
1128+
ndim = mpp_get_var_ndim(fid_in, vid_in);
1129+
status = nc_inq_vardimid(files[fid_in].ncid, files[fid_in].var[vid_in].fldid,dims);
1130+
if(status != NC_NOERR) {
1131+
sprintf(errmsg, "mpp_io(mpp_copy_data): Error in getting dimid of var %s from file %s",
1132+
files[fid_in].var[vid_in].name, files[vid_in].name );
1133+
netcdf_error(errmsg, status);
1134+
}
1135+
dsize = 1;
1136+
for(i=0; i<ndim; i++) {
1137+
status = nc_inq_dimlen(files[fid_in].ncid, dims[i], &size);
1138+
if(status != NC_NOERR) {
1139+
sprintf(errmsg, "mpp_io(mpp_copy_data): error in inquiring dimlen from file %s", files[fid_in].name);
1140+
netcdf_error(errmsg, status);
1141+
}
1142+
dsize *= size;
1143+
}
1144+
1145+
data = (void *)malloc(dsize*sizeof(double));
9861146

1147+
mpp_get_var_value(fid_in, vid_in, data);
1148+
mpp_put_var_value(fid_out, vid_out, data);
1149+
free(data);
1150+
}
1151+
9871152
int mpp_get_var_natts(int fid, int vid)
9881153
{
9891154
int natts, ncid, fldid, status;
@@ -1318,3 +1483,40 @@ int get_great_circle_algorithm(int fid)
13181483
return great_circle_algorithm;
13191484
}
13201485

1486+
void set_in_format(char *format)
1487+
{
1488+
char errmsg[128];
1489+
1490+
1491+
if(!format) return;
1492+
if(!strcmp(format, "netcdf4"))
1493+
in_format = NC_FORMAT_NETCDF4;
1494+
else if(!strcmp(format, "netcdf4_classic"))
1495+
in_format = NC_FORMAT_NETCDF4_CLASSIC;
1496+
else if(!strcmp(format, "64bit_offset"))
1497+
in_format = NC_FORMAT_64BIT;
1498+
else if(!strcmp(format, "classic"))
1499+
in_format = NC_FORMAT_CLASSIC;
1500+
else {
1501+
sprintf(errmsg, "mpp_io(mpp_open): format = %s is not a valid option", format);
1502+
mpp_error(errmsg);
1503+
}
1504+
}
1505+
1506+
/**
1507+
void reset_in_format(int format)
1508+
Checks for validity of "format", prints a warning if needed, otherwise
1509+
resets the global variable in_format to the input argument "format".
1510+
**/
1511+
void reset_in_format(int format) {
1512+
char errmsg[128];
1513+
1514+
if ((format != NC_FORMAT_NETCDF4) && (format != NC_FORMAT_NETCDF4_CLASSIC) && (format != NC_FORMAT_64BIT) &&
1515+
(format != NC_FORMAT_CLASSIC)) {
1516+
sprintf(errmsg, "mpp_io(reset_in_format): format = %d is not a valid format", format);
1517+
mpp_error(errmsg);
1518+
} else {
1519+
in_format = format;
1520+
}
1521+
}
1522+

sorc/fre-nctools.fd/shared_lib/mpp_io.h

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ char mpp_get_var_cart(int fid, int vid);
3737
void mpp_get_var_dimname(int fid, int vid, int ind, char *name);
3838
char mpp_get_dim_cart(int fid, const char *name);
3939
void mpp_get_var_bndname(int fid, int vid, char *bndname);
40+
int mpp_get_record_name(int fid, char *name);
4041
int mpp_var_att_exist(int fid, int vid, const char *att);
4142
int mpp_global_att_exist(int fid, const char *att);
4243
int mpp_def_dim(int fid, const char* name, int size);
@@ -45,6 +46,7 @@ void mpp_def_global_att(int fid, const char *name, const char *val);
4546
void mpp_def_global_att_double(int fid, const char *name, size_t len, const double *val);
4647
void mpp_def_var_att(int fid, int vid, const char *attname, const char *attval);
4748
void mpp_def_var_att_double(int fid, int vid, const char *attname, double attval);
49+
void mpp_copy_data(int fid_in, int vid_in, int fid_out, int vid_out);
4850
void mpp_copy_var_att(int fid_in, int vid_in, int fid_out, int vid_out);
4951
void mpp_copy_global_att(int fid_in, int fid_out);
5052
void mpp_put_var_value(int fid, int vid, const void* data);
@@ -56,4 +58,7 @@ int mpp_field_exist(const char *file, const char *field);
5658
int mpp_var_exist(int fid, const char *field);
5759
int mpp_dim_exist(int fid, const char *dimname);
5860
int get_great_circle_algorithm(int fid);
61+
void mpp_set_deflation(int fid_in, int fid_out, int deflation, int shuffle);
62+
void set_in_format(char *format);
63+
void reset_in_format(int format);
5964
#endif

0 commit comments

Comments
 (0)