10 #define MIN(a,b) ((a)<(b) ? (a) : (b))
12 static bool ERROR_SUPPRESS = 0;
14 #define CHECK_ERROR(func) \
16 int status_ = (func); \
17 if (status_ != NC_NOERR) { \
18 if ( ! ERROR_SUPPRESS ) { \
19 fprintf(stderr, "Error: at line %d in %s\n", __LINE__, __FILE__); \
20 fprintf(stderr, " %s\n", nc_strerror(status_)); \
28 #define CHECK_PNC_ERROR(func) \
30 int status_ = (func); \
31 if (status_ != NC_NOERR) { \
32 if ( ! ERROR_SUPPRESS ) { \
33 fprintf(stderr, "Error: at line %d in %s\n", __LINE__, __FILE__); \
34 fprintf(stderr, " %s\n", ncmpi_strerror(status_)); \
40 #define CHECK_PNC_ERROR(func) \
42 fprintf(stderr, "pnetCDF is necessary for shared_mode\n"); \
43 fprintf(stderr, "Please re-compile with pnetCDF\n"); \
46 #define ncmpi_inq_attid(a,b,c,d) NC2_ERR
47 #define ncmpi_inq_varid(a,b,c) NC2_ERR
48 #define ncmpi_inq_dimid(a,b,c) NC2_ERR
51 #define NCTYPE2TYPE(nctype, type) \
61 type = File_INTEGER2; \
64 type = File_INTEGER4; \
70 fprintf(stderr, "unsupported data type: %d\n", xtype); \
75 #define TYPE2NCTYPE(type, nctype) \
85 fprintf(stderr, "unsupported data type: %d\n", xtype); \
91 #define DEFAULT_DEFLATE_LEVEL 2
98 #if defined(NETCDF3) || defined(PNETCDF)
128 static int nfile = 0;
135 static inline int file_enddef(
const int fid,
const int ncid )
137 #if defined(NETCDF3) || defined(PNETCDF)
138 if (files[fid]->defmode == 1) {
139 if ( files[fid]->shared_mode )
145 files[fid]->defmode = 0;
152 static inline int file_redef(
const int fid,
const int ncid )
154 #if defined(NETCDF3) || defined(PNETCDF)
155 if (files[fid]->defmode == 0) {
156 if (files[fid]->shared_mode)
162 files[fid]->defmode = 1;
183 fprintf(stderr,
"exceed max number of file limit\n");
188 strcpy(_fname, fname);
191 FILE *fp = fopen(_fname,
"r");
202 if (fname[len-3] !=
'.' || fname[len-2] !=
'n' || fname[len-1] !=
'c' )
203 strcat(_fname,
".nc");
205 comm = MPI_Comm_f2c(icomm);
206 if ( comm == MPI_COMM_NULL || comm == MPI_COMM_SELF )
214 CHECK_PNC_ERROR( ncmpi_open(comm, _fname, NC_NOWRITE, MPI_INFO_NULL, &ncid) )
220 CHECK_PNC_ERROR( ncmpi_create(comm, _fname, NC_CLOBBER|NC_64BIT_OFFSET, MPI_INFO_NULL, &ncid) )
223 CHECK_ERROR( nc_create(_fname, NC_CLOBBER|NC_64BIT_OFFSET, &ncid) )
225 CHECK_ERROR( nc_create(_fname, NC_CLOBBER|NC_NETCDF4, &ncid) )
230 CHECK_PNC_ERROR( ncmpi_open(comm, _fname, NC_WRITE, MPI_INFO_NULL, &ncid) )
235 fprintf(stderr,
"invalid mode type\n");
240 files[nfile]->ncid = ncid;
242 #if defined(NETCDF3) || defined(PNETCDF)
244 files[nfile]->defmode = 1;
246 files[nfile]->defmode = 0;
249 files[nfile]->shared_mode = shared_mode;
250 strcpy(files[nfile]->fname, fname);
265 ERROR_SUPPRESS = suppress;
268 ncid = files[fid]->ncid;
270 if ( files[fid]->shared_mode ) {
288 const char *filetype,
292 if ( strcmp(filetype,
"netcdf") != 0 )
return SUCCESS_CODE;
294 if ( strcmp(key,
"deflate_level") == 0 ) {
296 files[fid]->deflate_level = atoi(val);
307 int ndims, ngatts, unlimdim;
310 ncid = files[fid]->ncid;
312 if ( files[fid]->shared_mode )
315 CHECK_ERROR( nc_inq(ncid, &ndims, nvars, &ngatts, &unlimdim) )
326 char buf[MAX_NC_NAME+1];
330 ncid = files[fid]->ncid;
333 if ( files[fid]->shared_mode )
338 for (
i=0;
i<
MIN(len-1,strlen(buf));
i++)
354 int dimids[
RANK_MAX], tdim, uldims[NC_MAX_DIMS];
355 char name[NC_MAX_NAME+1];
361 ERROR_SUPPRESS = suppress;
364 ncid = files[fid]->ncid;
365 if ( files[fid]->shared_mode )
373 strcpy(dinfo->
varname, varname);
375 if ( files[fid]->shared_mode ) {
385 if ( ncmpi_inq_attlen(ncid, varid,
"long_name", &l) == NC_NOERR ) {
386 buf = (
char*) malloc(l+1);
395 if ( ncmpi_inq_attlen(ncid, varid,
"units", &l) == NC_NOERR ) {
396 buf = (
char*) malloc(l+1);
403 dinfo->
units[0] =
'\0';
405 if ( ncmpi_inq_attlen(ncid, varid,
"standard_name", &l) == NC_NOERR ) {
406 buf = (
char*) malloc(l+1);
407 CHECK_PNC_ERROR( ncmpi_get_att_text(ncid, varid,
"standard_name", buf) )
427 for (
i=0;
i<natts;
i++) {
429 if ( strcmp(
name,
"long_name") &&
430 strcmp(
name,
"description") &&
431 strcmp(
name,
"units") &&
432 strcmp(
name,
"standard_name") ) {
450 if ( nc_inq_attlen(ncid, varid,
"long_name", &l) == NC_NOERR ) {
451 buf = (
char*) malloc(l+1);
452 CHECK_ERROR( nc_get_att_text(ncid, varid,
"long_name", buf) )
453 }
else if ( nc_inq_attlen(ncid, varid,
"description", &l) == NC_NOERR ) {
454 buf = (
char*) malloc(l+1);
455 CHECK_ERROR( nc_get_att_text(ncid, varid,
"description", buf) )
461 if ( l>0 ) free(buf);
463 if ( nc_inq_attlen(ncid, varid,
"units", &l) == NC_NOERR ) {
464 buf = (
char*) malloc(l+1);
465 CHECK_ERROR( nc_get_att_text(ncid, varid,
"units", buf) )
471 dinfo->
units[0] =
'\0';
473 if ( nc_inq_attlen(ncid, varid,
"standard_name", &l) == NC_NOERR ) {
474 buf = (
char*) malloc(l+1);
475 CHECK_ERROR( nc_get_att_text(ncid, varid,
"standard_name", buf) )
483 CHECK_ERROR( nc_inq_vardimid(ncid, varid, dimids) )
493 CHECK_ERROR( nc_inq_varnatts(ncid, varid, &natts) )
494 for (
i=0;
i<natts;
i++) {
496 if ( strcmp(
name,
"long_name") &&
497 strcmp(
name,
"description") &&
498 strcmp(
name,
"units") &&
499 strcmp(
name,
"standard_name") ) {
512 for (
i=0;
i<n;
i++ ) {
513 if ( uldims[
i] == dimids[0] ) {
519 fprintf(stderr,
"rank exceeds limit: %d\n", rank);
522 dinfo->
rank = tdim >= 0 ? rank -1 : rank;
524 for (
i=0;
i<dinfo->
rank;
i++) {
526 if ( files[fid]->shared_mode ) {
529 size = (size_t)size_;
534 fprintf(stderr,
"Length of the dimension name (%s) is too long (should be < %d).\n",
name,
File_HSHORT);
543 if ( files[fid]->shared_mode ) {
553 buf = (
char*) malloc(l+1);
561 if ( ncmpi_inq_attlen(ncid, varid,
"calendar", &l) == NC_NOERR ) {
562 buf = (
char*) malloc(l+1);
572 strcat(
name,
"_bnds");
581 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR ) {
585 CHECK_ERROR( nc_inq_attlen (ncid, varid,
"units", &l) )
586 buf = (
char*) malloc(l+1);
587 CHECK_ERROR( nc_get_att_text(ncid, varid,
"units", buf) )
593 if ( nc_inq_attlen(ncid, varid,
"calendar", &l) == NC_NOERR ) {
594 buf = (
char*) malloc(l+1);
595 CHECK_ERROR( nc_get_att_text(ncid, varid,
"calendar", buf) )
603 strcat(
name,
"_bnds");
617 fprintf(stderr,
"requested step is larger than tdim: step=%d tdim=%d\n", step, tdim);
629 dinfo->
units[0] =
'\0';
649 int dimids[
RANK_MAX], uldims[NC_MAX_DIMS], tdim;
653 ncid = files[fid]->ncid;
654 if ( files[fid]->shared_mode )
659 if ( files[fid]->shared_mode ) {
662 n = uldims[0] < 0 ? 0 : 1;
664 CHECK_ERROR( nc_inq_vardimid(ncid, varid, dimids) )
667 n = uldims[0] < 0 ? 0 : 1;
674 for (
i=0;
i<n;
i++ ) {
675 if ( uldims[
i] == dimids[0] ) {
682 if ( files[fid]->shared_mode ) {
711 MPI_Offset *strp, *cntp;
716 ncid = files[fid]->ncid;
717 if ( files[fid]->shared_mode ) {
720 strp = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*rank);
721 cntp = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*rank);
726 str = (
size_t*) malloc(
sizeof(
size_t)*rank);
727 cnt = (
size_t*) malloc(
sizeof(
size_t)*rank);
729 if ( start==NULL || start[0]<=0 || count==NULL || count[0]<=0 ) {
730 for (
i=0;
i<dinfo->
rank;
i++) {
736 for (
i=0;
i<dinfo->
rank;
i++) {
738 str[rank -
i-1] = start[
i] - 1;
739 cnt[rank -
i-1] = count[
i];
742 if (rank > dinfo->
rank) {
743 str[0] = dinfo->
step - 1;
748 for (
i=0;
i<rank;
i++) size *= cnt[
i];
750 if ( files[fid]->shared_mode ) {
752 for (
i=0;
i<rank;
i++) {
753 strp[
i] = (MPI_Offset) str[
i];
754 cntp[
i] = (MPI_Offset) cnt[
i];
758 dtype_ = dtype == 0 ? MPI_DATATYPE_NULL : MPI_Type_f2c(dtype);
759 CHECK_PNC_ERROR( ncmpi_iget_vara(ncid, varid, strp, cntp, var, (MPI_Offset) ntypes, dtype_, NULL) )
762 if ( dtype_ == MPI_FLOAT ) {
763 float factor,
offset, misval;
765 if ( ncmpi_get_att_float(ncid, varid,
"missing_value", &misval) == NC_NOERR )
766 if ( misval !=
RMISS )
767 for (
i=0;
i<size;
i++)
if ( ((
float*)var)[
i] == misval ) ((
float*)var)[
i] =
RMISS;
768 if ( ncmpi_get_att_float(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
772 if ( ncmpi_get_att_float(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
776 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
float*)var)[
i] = ((
float*)var)[
i] * factor +
offset;
777 }
else if ( dtype_ == MPI_DOUBLE ) {
778 double factor,
offset, misval;
780 if ( ncmpi_get_att_double(ncid, varid,
"missing_value", &misval) == NC_NOERR )
782 for (
i=0;
i<size;
i++)
if ( ((
double*)var)[
i] == misval ) ((
double*)var)[
i] =
RMISS;
783 if ( ncmpi_get_att_double(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
787 if ( ncmpi_get_att_double(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
791 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
double*)var)[
i] = ((
double*)var)[
i] * factor +
offset;
793 float factor,
offset, misval;
794 if ( ( ncmpi_get_att_float(ncid, varid,
"missing_value", &misval) == NC_NOERR ) )
795 if ( misval != (
float)
RMISS ) {
796 fprintf(stderr,
"missing_value (!=UNDEF) is not supported with a MPI derived type\n");
799 if ( ( ncmpi_get_att_float(ncid, varid,
"scale_factor", &factor) == NC_NOERR )
800 || ( ncmpi_get_att_float(ncid, varid,
"add_offset", &
offset) == NC_NOERR ) ) {
801 fprintf(stderr,
"scale_factor and add_offset is not supported with a MPI derived type\n");
809 switch ( precision ) {
811 CHECK_ERROR( nc_get_vara_double(ncid, varid, str, cnt, (
double*)var) )
813 double factor,
offset, misval;
816 nc_get_att_float(ncid, varid,
"missing_value", &a);
817 if ( nc_get_att_double(ncid, varid,
"missing_value", &misval) == NC_NOERR )
818 if ( (
float)misval != (
float)
RMISS )
819 for (
i=0;
i<size;
i++)
if ( ((
double*)var)[
i] == misval ) ((
double*)var)[
i] =
RMISS;
820 if ( nc_get_att_double(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
824 if ( nc_get_att_double(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
828 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
double*)var)[
i] = ((
double*)var)[
i] * factor +
offset;
832 CHECK_ERROR( nc_get_vara_float(ncid, varid, str, cnt, (
float*)var) )
834 float factor,
offset, misval;
836 if ( nc_get_att_float(ncid, varid,
"missing_value", &misval) == NC_NOERR )
837 if ( misval != (
float)
RMISS )
838 for (
i=0;
i<size;
i++)
if ( ((
float*)var)[
i] == misval ) ((
float*)var)[
i] =
RMISS;
839 if ( nc_get_att_float(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
843 if ( nc_get_att_float(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
847 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
float*)var)[
i] = ((
float*)var)[
i] * factor +
offset;
853 fprintf(stderr,
"unsupported data precision: %d\n", precision );
875 ncid = files[fid]->ncid;
877 ERROR_SUPPRESS = suppress;
879 if ( files[fid]->shared_mode ) {
881 if ( strcmp(vname,
"global") == 0 ) {
894 if ( strcmp(vname,
"global") == 0 ) {
902 CHECK_ERROR( nc_get_att_text(ncid, varid, key, value) )
922 ncid = files[fid]->ncid;
924 ERROR_SUPPRESS = suppress;
926 if ( files[fid]->shared_mode ) {
928 if ( strcmp(vname,
"global") == 0 ) {
939 if ( strcmp(vname,
"global") == 0 ) {
946 CHECK_ERROR( nc_get_att_int(ncid, varid, key, value) )
965 ncid = files[fid]->ncid;
967 ERROR_SUPPRESS = suppress;
969 if ( files[fid]->shared_mode ) {
971 if ( strcmp(vname,
"global") == 0 ) {
982 if ( strcmp(vname,
"global") == 0 ) {
989 CHECK_ERROR( nc_get_att_float(ncid, varid, key, value) )
1001 const bool suppress,
1008 ncid = files[fid]->ncid;
1010 ERROR_SUPPRESS = suppress;
1012 if ( files[fid]->shared_mode ) {
1014 if ( strcmp(vname,
"global") == 0 ) {
1025 if ( strcmp(vname,
"global") == 0 ) {
1030 CHECK_ERROR( nc_inq_attlen(ncid, varid, key, &l) )
1032 CHECK_ERROR( nc_get_att_double(ncid, varid, key, value) )
1051 ncid = files[fid]->ncid;
1053 if ( files[fid]->shared_mode ) {
1054 if ( strcmp(vname,
"global") == 0 ) {
1062 if ( strcmp(vname,
"global") == 0 ) {
1070 ret = file_redef(fid, ncid);
1073 if ( files[fid]->shared_mode )
1074 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, key, strlen(val), val) )
1076 CHECK_ERROR( nc_put_att_text(ncid, varid, key, strlen(val), val) )
1093 ncid = files[fid]->ncid;
1095 if ( files[fid]->shared_mode ) {
1096 if ( strcmp(vname,
"global") == 0 ) {
1104 if ( strcmp(vname,
"global") == 0 ) {
1112 ret = file_redef(fid, ncid);
1115 if ( files[fid]->shared_mode )
1116 CHECK_PNC_ERROR( ncmpi_put_att_int(ncid, varid, key, NC_INT, len, value) )
1118 CHECK_ERROR( nc_put_att_int(ncid, varid, key, NC_INT, len, value) )
1136 ncid = files[fid]->ncid;
1138 if ( files[fid]->shared_mode ) {
1139 if ( strcmp(vname,
"global") == 0 ) {
1147 if ( strcmp(vname,
"global") == 0 ) {
1155 ret = file_redef(fid, ncid);
1158 if ( files[fid]->shared_mode )
1159 CHECK_PNC_ERROR( ncmpi_put_att_float(ncid, varid, key, NC_FLOAT, len, value) )
1161 CHECK_ERROR( nc_put_att_float(ncid, varid, key, NC_FLOAT, len, value) )
1169 const double *value,
1177 ncid = files[fid]->ncid;
1179 if ( files[fid]->shared_mode ) {
1180 if ( strcmp(vname,
"global") == 0 ) {
1188 if ( strcmp(vname,
"global") == 0 ) {
1196 if ( files[fid]->shared_mode )
1197 CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid, key, NC_DOUBLE, len, value) )
1199 CHECK_ERROR( nc_put_att_double(ncid, varid, key, NC_DOUBLE, len, value) )
1211 ncid = files[fid]->ncid;
1213 if ( nc_inq_varid(ncid, vname, &varid) == NC_NOERR )
1216 ret = file_redef(fid, ncid);
1219 if ( files[fid]->shared_mode )
1222 CHECK_ERROR( nc_def_var(ncid, vname, NC_INT, 0, 0, &varid) )
1228 const char *time_units,
1229 const char *calendar)
1231 strcpy(files[fid]->time_units, time_units);
1232 strcpy(files[fid]->calendar, calendar);
1241 const char *dim_name,
1245 const int precision)
1247 int ncid, dimid, varid;
1252 ncid = files[fid]->ncid;
1254 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR )
1257 ret = file_redef(fid, ncid);
1260 if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR )
1261 CHECK_ERROR( nc_def_dim(ncid, dim_name, size, &dimid) )
1265 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1266 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1268 ret = file_enddef(fid, ncid);
1271 switch ( precision ) {
1273 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1276 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1279 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1290 const char *dim_name,
1295 int ncid, dimid, varid;
1302 ncid = files[fid]->ncid;
1304 if ( files[fid]->shared_mode ) {
1310 ret = file_redef(fid, ncid);
1314 if ( files[fid]->shared_mode ) {
1319 if ( strlen(desc)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1320 if ( strlen(units)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1326 sprintf(buf,
"%s_bnds", dim_name);
1327 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"bounds", strlen(buf), buf) )
1328 CHECK_PNC_ERROR( ncmpi_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &varid) )
1331 if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR )
1332 CHECK_ERROR( nc_def_dim(ncid, dim_name, dim_size, &dimid) )
1335 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1336 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1340 if ( nc_inq_dimid(ncid,
"nv", &(dimids[1])) != NC_NOERR )
1341 CHECK_ERROR( nc_def_dim(ncid,
"nv", 2, &(dimids[1])) )
1342 sprintf(buf,
"%s_bnds", dim_name);
1343 CHECK_ERROR( nc_put_att_text(ncid, varid,
"bounds", strlen(buf), buf) )
1344 CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &varid) )
1354 const int precision,
1360 MPI_Offset start_[1], count_[1];
1363 ncid = files[fid]->ncid;
1365 if ( files[fid]->shared_mode )
1370 ret = file_enddef(fid, ncid);
1373 start_[0] = (MPI_Offset) start[0];
1374 count_[0] = (MPI_Offset) count[0];
1376 switch ( precision ) {
1378 if ( files[fid]->shared_mode )
1379 CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start_, count_, val, NULL) )
1381 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1384 if ( files[fid]->shared_mode )
1385 CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start_, count_, val, NULL) )
1387 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1390 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1401 const char **dim_names,
1405 const int precision)
1407 int ncid, *dimids, varid;
1413 ncid = files[fid]->ncid;
1415 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR )
1418 ret = file_redef(fid, ncid);
1421 dimids = malloc(
sizeof(
int)*ndims);
1422 for (
i=0;
i<ndims;
i++)
1423 CHECK_ERROR( nc_inq_dimid(ncid, dim_names[
i], dimids+ndims-
i-1) )
1428 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1429 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1432 ret = file_enddef(fid, ncid);
1435 switch ( precision ) {
1437 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1440 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1443 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1454 const char **dim_names,
1465 ncid = files[fid]->ncid;
1467 if ( files[fid]->shared_mode ) {
1471 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR )
1475 ret = file_redef(fid, ncid);
1480 if ( files[fid]->shared_mode ) {
1481 for (
i=0;
i<ndims;
i++)
1485 if ( strlen(desc) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1486 if ( strlen(units)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1489 for (
i=0;
i<ndims;
i++)
1490 CHECK_ERROR( nc_inq_dimid(ncid, dim_names[
i], dimids+ndims-
i-1) )
1493 if ( strlen(desc) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1494 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1504 const int precision,
1510 MPI_Offset start_[ndims], count_[ndims];
1514 ncid = files[fid]->ncid;
1516 if ( files[fid]->shared_mode )
1521 ret = file_enddef(fid, ncid);
1524 for (
i=0;
i<ndims;
i++) {
1525 start_[
i] = (MPI_Offset) start[
i];
1526 count_[
i] = (MPI_Offset) count[
i];
1529 switch ( precision ) {
1531 if ( files[fid]->shared_mode )
1532 CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start_, count_, (
double*)val, NULL) )
1534 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1537 if ( files[fid]->shared_mode )
1538 CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start_, count_, (
float*)val, NULL) )
1540 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1543 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1552 const char *varname,
1555 const char *stdname,
1562 int ncid, varid, acid, *acdimids;
1563 int dimids[NC_MAX_DIMS], dimid;
1568 int i,
j,
k, m, err;
1569 int ndims_t, nndims;
1571 double rmiss =
RMISS;
1578 fprintf(stderr,
"exceed max number of variable limit\n");
1583 ncid = files[fid]->ncid;
1586 vars[nvar]->
ncid = ncid;
1587 vars[nvar]->
t = NULL;
1588 vars[nvar]->
start = NULL;
1589 vars[nvar]->
count = NULL;
1590 vars[nvar]->
ndims = ndims;
1592 ret = file_redef(fid, ncid);
1597 for (
i=0;
i<nt;
i++ ) {
1598 if ( tdims[
i] != NULL &&
1599 tdims[
i]->ncid == ncid &&
1600 tdims[
i]->tint == tint ) {
1601 vars[nvar]->
t = tdims[
i];
1605 if ( vars[nvar]->t == NULL ) {
1607 tdims[nt]->
ncid = ncid;
1608 tdims[nt]->
count = -1;
1609 tdims[nt]->
tint = tint;
1610 tdims[nt]->
tval = (
double*) malloc(
sizeof(
double)*
NTMAX);
1613 for (
i=0;
i<nt;
i++) {
1614 if ( tdims[
i] != NULL && tdims[
i]->ncid == ncid ) m++;
1617 strcpy(tname,
"time");
1619 sprintf(tname,
"time%d", m);
1621 strcpy(tdims[nt]->
name, tname);
1623 if ( files[fid]->shared_mode ) {
1625 tdims[nt]->
dimid = tdimid;
1626 CHECK_PNC_ERROR( ncmpi_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1627 tdims[nt]->
varid = tvarid;
1628 strcpy(buf,
"time");
1629 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"long_name", strlen(buf), buf) )
1630 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"units", strlen(files[fid]->time_units), files[fid]->time_units) )
1631 if ( strlen(files[fid]->calendar) > 0 )
1632 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"calendar", strlen(files[fid]->calendar), files[fid]->calendar) )
1636 sprintf(buf,
"%s_bnds", tname);
1637 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"bounds", strlen(buf), buf) )
1639 CHECK_PNC_ERROR( ncmpi_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1640 tdims[nt]->
bndsid = tvarid;
1644 CHECK_ERROR( nc_def_dim(ncid, tname, 0, &tdimid) )
1645 tdims[nt]->
dimid = tdimid;
1646 CHECK_ERROR( nc_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1647 tdims[nt]->
varid = tvarid;
1648 strcpy(buf,
"time");
1649 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"long_name", strlen(buf), buf) )
1650 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"units", strlen(files[fid]->time_units), files[fid]->time_units) )
1651 if ( strlen(files[fid]->calendar) > 0 )
1652 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"calendar", strlen(files[fid]->calendar), files[fid]->calendar) )
1654 if ( nc_inq_dimid(ncid,
"nv", &(dimids[1])) != NC_NOERR )
1655 CHECK_ERROR( nc_def_dim(ncid,
"nv", 2, &(dimids[1])) )
1656 sprintf(buf,
"%s_bnds", tname);
1657 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"bounds", strlen(buf), buf) )
1659 CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1660 tdims[nt]->
bndsid = tvarid;
1664 vars[nvar]->
t = tdims[nt];
1673 dimids[0] = vars[nvar]->
t->
dimid;
1676 for (
i=ndims_t-ndims;
i<ndims_t;
i++) dimids[
i] = -1;
1680 for (
i=0;
i<ndims;
i++) {
1682 if ( files[fid]->shared_mode )
1685 err = nc_inq_dimid(ncid, dims[
i], &dimid);
1686 if ( err == NC_NOERR ) {
1689 for (
k=0;
k<nndims;
k++) {
1690 if (dimid == dimids[
k]) {
1696 dimids[ndims_t-(++nndims)] = dimid;
1700 if ( files[fid]->shared_mode ) {
1703 acdimids = (
int*) malloc((
sizeof(
int)*m));
1709 acdimids = (
int*) malloc((
sizeof(
int)*m));
1710 CHECK_ERROR( nc_inq_vardimid(ncid, acid, acdimids) )
1712 for (
j=m-1;
j>=0;
j--) {
1714 for (
k=0;
k<ndims_t;
k++) {
1715 if (acdimids[
j] == dimids[
k]) {
1721 if ( nndims >= ndims_t ) {
1722 fprintf(stderr,
"Error: invalid associated coordinates\n");
1725 dimids[ndims_t-(++nndims)] = acdimids[
j];
1734 if (nndims != ndims) {
1735 fprintf(stderr,
"Error: invalid associated coordinates: %d %d %d\n", ndims_t, nndims, ndims);
1740 if ( files[fid]->shared_mode ) {
1741 CHECK_PNC_ERROR( ncmpi_def_var(ncid, varname, xtype, ndims_t, dimids, &varid) )
1743 if ( strlen(desc) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1744 if ( strlen(units) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1745 if ( strlen(stdname)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"standard_name", strlen(stdname), stdname) )
1748 CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid,
"missing_value", xtype, 1, &rmiss) )
1751 CHECK_ERROR( nc_def_var(ncid, varname, xtype, ndims_t, dimids, &varid) )
1753 if ( strlen(desc) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1754 if ( strlen(units) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1755 if ( strlen(stdname)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"standard_name", strlen(stdname), stdname) )
1756 CHECK_ERROR( nc_put_att_double(ncid, varid, _FillValue, xtype, 1, &rmiss) )
1757 CHECK_ERROR( nc_put_att_double(ncid, varid,
"missing_value", xtype, 1, &rmiss) )
1760 strcpy(coord, dims[0]);
1761 for(
i=1;
i<ndims;
i++) {
1762 if (strlen(coord)+strlen(dims[
i])+1 <
File_HMID) {
1764 strcat(coord, dims[
i]);
1767 if ( ndims_t > ndims && strlen(coord)+6 <
File_HMID) {
1769 strcat(coord, vars[nvar]->t->name);
1771 if ( files[fid]->shared_mode )
1772 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"coordinates", strlen(coord), coord) )
1774 CHECK_ERROR( nc_put_att_text(ncid, varid,
"coordinates", strlen(coord), coord) )
1778 if ( strcmp(tstats,
"none") != 0 ) {
1779 sprintf(buf,
"%s: %s", vars[nvar]->t->name, tstats);
1780 if ( files[fid]->shared_mode )
1781 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"cell_methods", strlen(buf), buf) )
1783 CHECK_ERROR( nc_put_att_text(ncid, varid,
"cell_methods", strlen(buf), buf) )
1787 vars[nvar]->
ndims_t = ndims_t;
1788 vars[nvar]->
start = (
size_t*) malloc(
sizeof(
size_t)*ndims_t);
1789 vars[nvar]->
count = (
size_t*) malloc(
sizeof(
size_t)*ndims_t);
1790 for (
i=0;
i<ndims_t;
i++ ) {
1791 if ( files[fid]->shared_mode ) {
1794 size = (size_t) dimlen;
1798 vars[nvar]->
count[
i] = size;
1799 vars[nvar]->
start[
i] = 0;
1801 if ( tint > 0.0 ) vars[nvar]->
count[0] = 1;
1805 if ( ! files[fid]->shared_mode && files[fid]->deflate_level > 0 ) {
1806 CHECK_ERROR( nc_def_var_chunking(ncid, varid, NC_CHUNKED, vars[nvar]->count) )
1807 CHECK_ERROR( nc_def_var_deflate(ncid, varid, 0, 1, files[fid]->deflate_level) )
1811 vars[nvar]->
varid = varid;
1824 ncid = files[fid]->ncid;
1826 return file_enddef(fid, ncid);
1835 ncid = files[fid]->ncid;
1837 return file_redef(fid, ncid);
1841 const int64_t buf_amount )
1846 ncid = files[fid]->ncid;
1848 if ( files[fid]->shared_mode )
1859 ncid = files[fid]->ncid;
1861 if ( files[fid]->shared_mode )
1872 ncid = files[fid]->ncid;
1874 if ( files[fid]->shared_mode )
1885 const double t_start,
1888 const int precision,
1893 MPI_Offset *str, *cnt;
1897 ncid = vars[vid]->
ncid;
1899 if ( ndims != vars[vid]->ndims ) {
1900 fprintf(stderr,
"Error: at line %d in %s\n", __LINE__, __FILE__);
1901 fprintf(stderr,
" dimension size %d is not consistent that was added by file_add_variable %d\n", ndims, (
int)vars[vid]->ndims );
1905 ret = file_enddef(fid, ncid);
1908 varid = vars[vid]->
varid;
1909 if ( vars[vid]->t != NULL ) {
1910 if ( vars[vid]->t->count < 0 ||
1911 t_end > vars[vid]->
t->
t +
TEPS ) {
1912 vars[vid]->
t->
count += 1;
1913 vars[vid]->
t->
t = t_end;
1914 if ( vars[vid]->t->count >
NTMAX-1 ) {
1915 fprintf(stderr,
"time count exceeds the max limit (%d)\n",
NTMAX);
1919 if ( files[fid]->shared_mode ) {
1920 MPI_Offset
index[2];
1921 index[0] = (MPI_Offset) vars[vid]->t->count;
1930 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->varid,
index, &t_end) )
1932 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid,
index, &t_start) )
1934 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid,
index, &t_end) )
1938 size_t nt = vars[vid]->
t->
count + 1;
1941 for(n=nt-1;n>=0;n--) {
1942 if ( fabs(vars[vid]->t->tval[n]-t_end) <
TEPS ) {
1943 vars[vid]->
start[0] = n;
1949 fprintf(stderr,
"cannot find time: %f\n", t_end);
1950 fprintf(stderr,
" time count is : %d, last time is: %f, diff is: %e\n", vars[vid]->t->count < 0, vars[vid]->
t->
t, vars[vid]->
t->
t-t_end);
1951 fprintf(stderr,
" time is: ");
1952 for (n=0;n<nt;n++) fprintf(stderr,
"%f, ", vars[vid]->t->tval[n]);
1953 fprintf(stderr,
"\n");
1959 if ( files[fid]->shared_mode ) {
1961 int ndims_t = vars[vid]->
ndims_t;
1962 str = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*(ndims_t));
1963 cnt = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*(ndims_t));
1964 if ( vars[vid]->t != NULL ) {
1966 str[0] = vars[vid]->
start[0];
1967 cnt[0] = vars[vid]->
count[0];
1968 for (
i=0;
i<ndims;
i++) {
1969 str[ndims_t-
i-1] = start[
i] - 1;
1970 cnt[ndims_t-
i-1] = count[
i];
1973 for (
i=0;
i<ndims;
i++) {
1974 str[ndims-
i-1] = start[
i] - 1;
1975 cnt[ndims-
i-1] = count[
i];
1980 switch (precision) {
1982 if ( files[fid]->shared_mode )
1983 CHECK_PNC_ERROR( ncmpi_bput_vara_double(ncid, varid, str, cnt, (
double*)var, NULL) )
1985 CHECK_ERROR( nc_put_vara_double(ncid, varid, vars[vid]->start, vars[vid]->count, (
double*)var) )
1988 if ( files[fid]->shared_mode )
1989 CHECK_PNC_ERROR( ncmpi_bput_vara_float(ncid, varid, str, cnt, (
float*)var, NULL) )
1991 CHECK_ERROR( nc_put_vara_float(ncid, varid, vars[vid]->start, vars[vid]->count, (
float*)var) )
1994 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1998 if ( files[fid]->shared_mode) {
2013 ncid = files[fid]->ncid;
2015 for (
i=0;
i<nvar;
i++) {
2016 if ( vars[
i] != NULL && vars[
i]->ncid == ncid ) {
2017 free( vars[
i]->start );
2018 free( vars[
i]->count );
2024 for (
i=0;
i<nt;
i++) {
2025 if ( tdims[
i] != NULL && tdims[
i]->ncid == ncid ) {
2026 free( tdims[
i]->tval );
2032 if ( files[fid]->shared_mode ) {