10 #define MIN(a,b) ((a)<(b) ? (a) : (b))
12 static int32_t 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 int32_t file_enddef(
const int32_t 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 int32_t
file_redef(
const int32_t 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;
173 const MPI_Comm comm )
182 fprintf(stderr,
"exceed max number of file limit\n");
187 strcpy(_fname, fname);
190 FILE *fp = fopen(_fname,
"r");
201 if (fname[len-3] !=
'.' || fname[len-2] !=
'n' || fname[len-1] !=
'c' )
202 strcat(_fname,
".nc");
204 if ( comm == MPI_COMM_NULL || comm == MPI_COMM_SELF )
212 CHECK_PNC_ERROR( ncmpi_open(comm, _fname, NC_NOWRITE, MPI_INFO_NULL, &ncid) )
218 CHECK_PNC_ERROR( ncmpi_create(comm, _fname, NC_CLOBBER|NC_64BIT_OFFSET, MPI_INFO_NULL, &ncid) )
221 CHECK_ERROR( nc_create(_fname, NC_CLOBBER|NC_64BIT_OFFSET, &ncid) )
223 CHECK_ERROR( nc_create(_fname, NC_CLOBBER|NC_NETCDF4, &ncid) )
228 CHECK_PNC_ERROR( ncmpi_open(comm, _fname, NC_WRITE, MPI_INFO_NULL, &ncid) )
233 fprintf(stderr,
"invalid mode type\n");
238 files[nfile]->ncid = ncid;
240 #if defined(NETCDF3) || defined(PNETCDF)
242 files[nfile]->defmode = 1;
244 files[nfile]->defmode = 0;
247 files[nfile]->shared_mode = shared_mode;
248 strcpy(files[nfile]->fname, fname);
263 ncid = files[fid]->ncid;
265 if ( files[fid]->shared_mode ) {
281 const char* filetype,
285 if ( strcmp(filetype,
"netcdf") != 0 )
return SUCCESS_CODE;
287 if ( strcmp(key,
"deflate_level") == 0 ) {
289 files[fid]->deflate_level = atoi(val);
300 int ndims, ngatts, unlimdim;
303 ncid = files[fid]->ncid;
305 if ( files[fid]->shared_mode )
308 CHECK_ERROR( nc_inq(ncid, &ndims, nvars, &ngatts, &unlimdim) )
319 char buf[MAX_NC_NAME+1];
323 ncid = files[fid]->ncid;
326 if ( files[fid]->shared_mode )
331 for (
i=0;
i<
MIN(len-1,strlen(buf));
i++)
342 const int32_t suppress)
347 int dimids[
RANK_MAX], tdim, uldims[NC_MAX_DIMS];
348 char name[NC_MAX_NAME+1];
354 ERROR_SUPPRESS = suppress;
357 ncid = files[fid]->ncid;
358 if ( files[fid]->shared_mode )
366 strcpy(dinfo->
varname, varname);
368 if ( files[fid]->shared_mode ) {
378 if ( ncmpi_inq_attlen(ncid, varid,
"long_name", &l) == NC_NOERR ) {
379 buf = (
char*) malloc(l+1);
388 if ( ncmpi_inq_attlen(ncid, varid,
"units", &l) == NC_NOERR ) {
389 buf = (
char*) malloc(l+1);
396 dinfo->
units[0] =
'\0';
398 if ( ncmpi_inq_attlen(ncid, varid,
"standard_name", &l) == NC_NOERR ) {
399 buf = (
char*) malloc(l+1);
400 CHECK_PNC_ERROR( ncmpi_get_att_text(ncid, varid,
"standard_name", buf) )
420 for (
i=0;
i<natts;
i++) {
422 if ( strcmp(
name,
"long_name") &&
423 strcmp(
name,
"description") &&
424 strcmp(
name,
"units") &&
425 strcmp(
name,
"standard_name") ) {
443 if ( nc_inq_attlen(ncid, varid,
"long_name", &l) == NC_NOERR ) {
444 buf = (
char*) malloc(l+1);
445 CHECK_ERROR( nc_get_att_text(ncid, varid,
"long_name", buf) )
446 }
else if ( nc_inq_attlen(ncid, varid,
"description", &l) == NC_NOERR ) {
447 buf = (
char*) malloc(l+1);
448 CHECK_ERROR( nc_get_att_text(ncid, varid,
"description", buf) )
454 if ( l>0 ) free(buf);
456 if ( nc_inq_attlen(ncid, varid,
"units", &l) == NC_NOERR ) {
457 buf = (
char*) malloc(l+1);
458 CHECK_ERROR( nc_get_att_text(ncid, varid,
"units", buf) )
464 dinfo->
units[0] =
'\0';
466 if ( nc_inq_attlen(ncid, varid,
"standard_name", &l) == NC_NOERR ) {
467 buf = (
char*) malloc(l+1);
468 CHECK_ERROR( nc_get_att_text(ncid, varid,
"standard_name", buf) )
476 CHECK_ERROR( nc_inq_vardimid(ncid, varid, dimids) )
486 CHECK_ERROR( nc_inq_varnatts(ncid, varid, &natts) )
487 for (
i=0;
i<natts;
i++) {
489 if ( strcmp(
name,
"long_name") &&
490 strcmp(
name,
"description") &&
491 strcmp(
name,
"units") &&
492 strcmp(
name,
"standard_name") ) {
505 for (
i=0;
i<n;
i++ ) {
506 if ( uldims[
i] == dimids[0] ) {
512 fprintf(stderr,
"rank exceeds limit: %d\n", rank);
515 dinfo->
rank = tdim >= 0 ? rank -1 : rank;
517 for (
i=0;
i<dinfo->
rank;
i++) {
519 if ( files[fid]->shared_mode ) {
522 size = (size_t)size_;
527 fprintf(stderr,
"Length of the dimension name (%s) is too long (should be < %d).\n",
name,
File_HSHORT);
536 if ( files[fid]->shared_mode ) {
546 buf = (
char*) malloc(l+1);
554 if ( ncmpi_inq_attlen(ncid, varid,
"calendar", &l) == NC_NOERR ) {
555 buf = (
char*) malloc(l+1);
565 strcat(
name,
"_bnds");
574 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR ) {
578 CHECK_ERROR( nc_inq_attlen (ncid, varid,
"units", &l) )
579 buf = (
char*) malloc(l+1);
580 CHECK_ERROR( nc_get_att_text(ncid, varid,
"units", buf) )
586 if ( nc_inq_attlen(ncid, varid,
"calendar", &l) == NC_NOERR ) {
587 buf = (
char*) malloc(l+1);
588 CHECK_ERROR( nc_get_att_text(ncid, varid,
"calendar", buf) )
596 strcat(
name,
"_bnds");
609 fprintf(stderr,
"requested step is larger than tdim: step=%d tdim=%d\n", step, tdim);
620 dinfo->
units[0] =
'\0';
639 int dimids[
RANK_MAX], uldims[NC_MAX_DIMS], tdim;
643 ncid = files[fid]->ncid;
644 if ( files[fid]->shared_mode )
649 if ( files[fid]->shared_mode ) {
652 n = uldims[0] < 0 ? 0 : 1;
654 CHECK_ERROR( nc_inq_vardimid(ncid, varid, dimids) )
657 n = uldims[0] < 0 ? 0 : 1;
664 for (
i=0;
i<n;
i++ ) {
665 if ( uldims[
i] == dimids[0] ) {
672 if ( files[fid]->shared_mode ) {
689 const int32_t precision,
690 const MPI_Offset ntypes,
691 const MPI_Datatype dtype,
692 const int32_t *start,
693 const int32_t *count )
700 MPI_Offset *strp, *cntp;
705 ncid = files[fid]->ncid;
706 if ( files[fid]->shared_mode ) {
709 strp = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*rank);
710 cntp = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*rank);
715 str = (
size_t*) malloc(
sizeof(
size_t)*rank);
716 cnt = (
size_t*) malloc(
sizeof(
size_t)*rank);
718 if ( start == NULL || count == NULL ) {
719 for (
i=0;
i<dinfo->
rank;
i++) {
725 for (
i=0;
i<dinfo->
rank;
i++) {
727 str[rank -
i-1] = start[
i] - 1;
728 cnt[rank -
i-1] = count[
i];
731 if (rank > dinfo->
rank) {
732 str[0] = dinfo->
step - 1;
737 for (
i=0;
i<rank;
i++) size *= cnt[
i];
739 if ( files[fid]->shared_mode ) {
741 for (
i=0;
i<rank;
i++) {
742 strp[
i] = (MPI_Offset) str[
i];
743 cntp[
i] = (MPI_Offset) cnt[
i];
747 CHECK_PNC_ERROR( ncmpi_iget_vara(ncid, varid, strp, cntp, var, ntypes, dtype, NULL) )
750 if ( dtype == MPI_FLOAT ) {
751 float factor,
offset, misval;
753 if ( ncmpi_get_att_float(ncid, varid,
"missing_value", &misval) == NC_NOERR )
754 if ( misval !=
RMISS )
755 for (
i=0;
i<size;
i++)
if ( ((
float*)var)[
i] == misval ) ((
float*)var)[
i] =
RMISS;
756 if ( ncmpi_get_att_float(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
760 if ( ncmpi_get_att_float(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
764 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
float*)var)[
i] = ((
float*)var)[
i] * factor +
offset;
765 }
else if ( dtype == MPI_DOUBLE ) {
766 double factor,
offset, misval;
768 if ( ncmpi_get_att_double(ncid, varid,
"missing_value", &misval) == NC_NOERR )
770 for (
i=0;
i<size;
i++)
if ( ((
double*)var)[
i] == misval ) ((
double*)var)[
i] =
RMISS;
771 if ( ncmpi_get_att_double(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
775 if ( ncmpi_get_att_double(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
779 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
double*)var)[
i] = ((
double*)var)[
i] * factor +
offset;
781 float factor,
offset, misval;
782 if ( ( ncmpi_get_att_float(ncid, varid,
"missing_value", &misval) == NC_NOERR ) )
783 if ( misval != (
float)
RMISS ) {
784 fprintf(stderr,
"missing_value (!=UNDEF) is not supported with a MPI derived type\n");
787 if ( ( ncmpi_get_att_float(ncid, varid,
"scale_factor", &factor) == NC_NOERR )
788 || ( ncmpi_get_att_float(ncid, varid,
"add_offset", &
offset) == NC_NOERR ) ) {
789 fprintf(stderr,
"scale_factor and add_offset is not supported with a MPI derived type\n");
797 switch ( precision ) {
799 CHECK_ERROR( nc_get_vara_double(ncid, varid, str, cnt, (
double*)var) )
801 double factor,
offset, misval;
804 nc_get_att_float(ncid, varid,
"missing_value", &a);
805 if ( nc_get_att_double(ncid, varid,
"missing_value", &misval) == NC_NOERR )
806 if ( (
float)misval != (
float)
RMISS )
807 for (
i=0;
i<size;
i++)
if ( ((
double*)var)[
i] == misval ) ((
double*)var)[
i] =
RMISS;
808 if ( nc_get_att_double(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
812 if ( nc_get_att_double(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
816 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
double*)var)[
i] = ((
double*)var)[
i] * factor +
offset;
820 CHECK_ERROR( nc_get_vara_float(ncid, varid, str, cnt, (
float*)var) )
822 float factor,
offset, misval;
824 if ( nc_get_att_float(ncid, varid,
"missing_value", &misval) == NC_NOERR )
825 if ( misval != (
float)
RMISS )
826 for (
i=0;
i<size;
i++)
if ( ((
float*)var)[
i] == misval ) ((
float*)var)[
i] =
RMISS;
827 if ( nc_get_att_float(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
831 if ( nc_get_att_float(ncid, varid,
"add_offset", &
offset) != NC_NOERR )
835 if ( l_rescale )
for (
i=0;
i<size;
i++) ((
float*)var)[
i] = ((
float*)var)[
i] * factor +
offset;
841 fprintf(stderr,
"unsupported data precision: %d\n", precision );
855 const int32_t suppress,
863 ncid = files[fid]->ncid;
865 ERROR_SUPPRESS = suppress;
867 if ( files[fid]->shared_mode ) {
869 if ( strcmp(vname,
"global") == 0 ) {
882 if ( strcmp(vname,
"global") == 0 ) {
890 CHECK_ERROR( nc_get_att_text(ncid, varid, key, value) )
902 const int32_t suppress,
910 ncid = files[fid]->ncid;
912 ERROR_SUPPRESS = suppress;
914 if ( files[fid]->shared_mode ) {
916 if ( strcmp(vname,
"global") == 0 ) {
927 if ( strcmp(vname,
"global") == 0 ) {
934 CHECK_ERROR( nc_get_att_int(ncid, varid, key, value) )
945 const int32_t suppress,
953 ncid = files[fid]->ncid;
955 ERROR_SUPPRESS = suppress;
957 if ( files[fid]->shared_mode ) {
959 if ( strcmp(vname,
"global") == 0 ) {
970 if ( strcmp(vname,
"global") == 0 ) {
977 CHECK_ERROR( nc_get_att_float(ncid, varid, key, value) )
988 const int32_t suppress,
996 ncid = files[fid]->ncid;
998 ERROR_SUPPRESS = suppress;
1000 if ( files[fid]->shared_mode ) {
1002 if ( strcmp(vname,
"global") == 0 ) {
1013 if ( strcmp(vname,
"global") == 0 ) {
1018 CHECK_ERROR( nc_inq_attlen(ncid, varid, key, &l) )
1020 CHECK_ERROR( nc_get_att_double(ncid, varid, key, value) )
1039 ncid = files[fid]->ncid;
1041 if ( files[fid]->shared_mode ) {
1042 if ( strcmp(vname,
"global") == 0 ) {
1050 if ( strcmp(vname,
"global") == 0 ) {
1058 ret = file_redef(fid, ncid);
1061 if ( files[fid]->shared_mode )
1062 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, key, strlen(val), val) )
1064 CHECK_ERROR( nc_put_att_text(ncid, varid, key, strlen(val), val) )
1072 const int32_t *value,
1081 ncid = files[fid]->ncid;
1083 if ( files[fid]->shared_mode ) {
1084 if ( strcmp(vname,
"global") == 0 ) {
1092 if ( strcmp(vname,
"global") == 0 ) {
1100 ret = file_redef(fid, ncid);
1103 if ( files[fid]->shared_mode )
1104 CHECK_PNC_ERROR( ncmpi_put_att_int(ncid, varid, key, NC_INT, len, value) )
1106 CHECK_ERROR( nc_put_att_int(ncid, varid, key, NC_INT, len, value) )
1124 ncid = files[fid]->ncid;
1126 if ( files[fid]->shared_mode ) {
1127 if ( strcmp(vname,
"global") == 0 ) {
1135 if ( strcmp(vname,
"global") == 0 ) {
1143 ret = file_redef(fid, ncid);
1146 if ( files[fid]->shared_mode )
1147 CHECK_PNC_ERROR( ncmpi_put_att_float(ncid, varid, key, NC_FLOAT, len, value) )
1149 CHECK_ERROR( nc_put_att_float(ncid, varid, key, NC_FLOAT, len, value) )
1157 const double *value,
1165 ncid = files[fid]->ncid;
1167 if ( files[fid]->shared_mode ) {
1168 if ( strcmp(vname,
"global") == 0 ) {
1176 if ( strcmp(vname,
"global") == 0 ) {
1184 if ( files[fid]->shared_mode )
1185 CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid, key, NC_DOUBLE, len, value) )
1187 CHECK_ERROR( nc_put_att_double(ncid, varid, key, NC_DOUBLE, len, value) )
1199 ncid = files[fid]->ncid;
1201 if ( nc_inq_varid(ncid, vname, &varid) == NC_NOERR )
1204 ret = file_redef(fid, ncid);
1207 if ( files[fid]->shared_mode )
1210 CHECK_ERROR( nc_def_var(ncid, vname, NC_INT, 0, 0, &varid) )
1216 const char *time_units,
1217 const char *calendar)
1219 strcpy(files[fid]->time_units, time_units);
1220 strcpy(files[fid]->calendar, calendar);
1229 const char *dim_name,
1230 const int32_t dtype,
1233 const int32_t precision)
1235 int ncid, dimid, varid;
1240 ncid = files[fid]->ncid;
1242 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR )
1245 ret = file_redef(fid, ncid);
1248 if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR )
1249 CHECK_ERROR( nc_def_dim(ncid, dim_name, size, &dimid) )
1253 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1254 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1256 ret = file_enddef(fid, ncid);
1259 switch ( precision ) {
1261 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1264 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1267 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1278 const char *dim_name,
1279 const int32_t dtype,
1280 const int32_t dim_size,
1281 const int32_t bounds)
1283 int ncid, dimid, varid;
1290 ncid = files[fid]->ncid;
1292 if ( files[fid]->shared_mode ) {
1298 ret = file_redef(fid, ncid);
1302 if ( files[fid]->shared_mode ) {
1307 if ( strlen(desc)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1308 if ( strlen(units)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1314 sprintf(buf,
"%s_bnds", dim_name);
1315 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"bounds", strlen(buf), buf) )
1316 CHECK_PNC_ERROR( ncmpi_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &varid) )
1319 if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR )
1320 CHECK_ERROR( nc_def_dim(ncid, dim_name, dim_size, &dimid) )
1323 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1324 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1328 if ( nc_inq_dimid(ncid,
"nv", &(dimids[1])) != NC_NOERR )
1329 CHECK_ERROR( nc_def_dim(ncid,
"nv", 2, &(dimids[1])) )
1330 sprintf(buf,
"%s_bnds", dim_name);
1331 CHECK_ERROR( nc_put_att_text(ncid, varid,
"bounds", strlen(buf), buf) )
1332 CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &varid) )
1342 const int32_t precision,
1343 const MPI_Offset *start,
1344 const MPI_Offset *count)
1350 ncid = files[fid]->ncid;
1352 if ( files[fid]->shared_mode )
1357 ret = file_enddef(fid, ncid);
1360 switch ( precision ) {
1362 if ( files[fid]->shared_mode )
1363 CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start, count, val, NULL) )
1365 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1368 if ( files[fid]->shared_mode )
1369 CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start, count, val, NULL) )
1371 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1374 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1385 const char **dim_names,
1386 const int32_t ndims,
1387 const int32_t dtype,
1389 const int32_t precision)
1391 int ncid, *dimids, varid;
1397 ncid = files[fid]->ncid;
1399 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR )
1402 ret = file_redef(fid, ncid);
1405 dimids = malloc(
sizeof(
int)*ndims);
1406 for (
i=0;
i<ndims;
i++)
1407 CHECK_ERROR( nc_inq_dimid(ncid, dim_names[
i], dimids+ndims-
i-1) )
1412 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1413 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1416 ret = file_enddef(fid, ncid);
1419 switch ( precision ) {
1421 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1424 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1427 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1438 const char **dim_names,
1439 const int32_t ndims,
1440 const int32_t dtype)
1449 ncid = files[fid]->ncid;
1451 if ( files[fid]->shared_mode ) {
1455 if ( nc_inq_varid(ncid,
name, &varid) == NC_NOERR )
1459 ret = file_redef(fid, ncid);
1464 if ( files[fid]->shared_mode ) {
1465 for (
i=0;
i<ndims;
i++)
1469 if ( strlen(desc) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1470 if ( strlen(units)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1473 for (
i=0;
i<ndims;
i++)
1474 CHECK_ERROR( nc_inq_dimid(ncid, dim_names[
i], dimids+ndims-
i-1) )
1477 if ( strlen(desc) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1478 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1487 const int32_t precision,
1488 const MPI_Offset *start,
1489 const MPI_Offset *count)
1495 ncid = files[fid]->ncid;
1497 if ( files[fid]->shared_mode )
1502 ret = file_enddef(fid, ncid);
1505 switch ( precision ) {
1507 if ( files[fid]->shared_mode )
1508 CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start, count, (
double*)val, NULL) )
1510 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1513 if ( files[fid]->shared_mode )
1514 CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start, count, (
float*)val, NULL) )
1516 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1519 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1527 const char *varname,
1530 const char *stdname,
1532 const int32_t ndims,
1533 const int32_t dtype,
1534 const real64_t tint,
1538 int ncid, varid, acid, *acdimids;
1539 int dimids[NC_MAX_DIMS], dimid;
1544 int i,
j,
k, m, err;
1545 int ndims_t, nndims;
1547 double rmiss =
RMISS;
1554 fprintf(stderr,
"exceed max number of variable limit\n");
1559 ncid = files[fid]->ncid;
1562 vars[nvar]->
ncid = ncid;
1563 vars[nvar]->
t = NULL;
1564 vars[nvar]->
start = NULL;
1565 vars[nvar]->
count = NULL;
1566 vars[nvar]->
ndims = ndims;
1568 ret = file_redef(fid, ncid);
1573 for (
i=0;
i<nt;
i++ ) {
1574 if ( tdims[
i] != NULL &&
1575 tdims[
i]->ncid == ncid &&
1576 tdims[
i]->tint == tint ) {
1577 vars[nvar]->
t = tdims[
i];
1581 if ( vars[nvar]->t == NULL ) {
1583 tdims[nt]->
ncid = ncid;
1584 tdims[nt]->
count = -1;
1585 tdims[nt]->
tint = tint;
1586 tdims[nt]->
tval = (
double*) malloc(
sizeof(
double)*
NTMAX);
1589 for (
i=0;
i<nt;
i++) {
1590 if ( tdims[
i] != NULL && tdims[
i]->ncid == ncid ) m++;
1593 strcpy(tname,
"time");
1595 sprintf(tname,
"time%d", m);
1597 strcpy(tdims[nt]->
name, tname);
1599 if ( files[fid]->shared_mode ) {
1601 tdims[nt]->
dimid = tdimid;
1602 CHECK_PNC_ERROR( ncmpi_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1603 tdims[nt]->
varid = tvarid;
1604 strcpy(buf,
"time");
1605 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"long_name", strlen(buf), buf) )
1606 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"units", strlen(files[fid]->time_units), files[fid]->time_units) )
1607 if ( strlen(files[fid]->calendar) > 0 )
1608 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"calendar", strlen(files[fid]->calendar), files[fid]->calendar) )
1612 sprintf(buf,
"%s_bnds", tname);
1613 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"bounds", strlen(buf), buf) )
1615 CHECK_PNC_ERROR( ncmpi_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1616 tdims[nt]->
bndsid = tvarid;
1620 CHECK_ERROR( nc_def_dim(ncid, tname, 0, &tdimid) )
1621 tdims[nt]->
dimid = tdimid;
1622 CHECK_ERROR( nc_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1623 tdims[nt]->
varid = tvarid;
1624 strcpy(buf,
"time");
1625 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"long_name", strlen(buf), buf) )
1626 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"units", strlen(files[fid]->time_units), files[fid]->time_units) )
1627 if ( strlen(files[fid]->calendar) > 0 )
1628 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"calendar", strlen(files[fid]->calendar), files[fid]->calendar) )
1630 if ( nc_inq_dimid(ncid,
"nv", &(dimids[1])) != NC_NOERR )
1631 CHECK_ERROR( nc_def_dim(ncid,
"nv", 2, &(dimids[1])) )
1632 sprintf(buf,
"%s_bnds", tname);
1633 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"bounds", strlen(buf), buf) )
1635 CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1636 tdims[nt]->
bndsid = tvarid;
1640 vars[nvar]->
t = tdims[nt];
1649 dimids[0] = vars[nvar]->
t->
dimid;
1652 for (
i=ndims_t-ndims;
i<ndims_t;
i++) dimids[
i] = -1;
1656 for (
i=0;
i<ndims;
i++) {
1658 if ( files[fid]->shared_mode )
1661 err = nc_inq_dimid(ncid, dims[
i], &dimid);
1662 if ( err == NC_NOERR ) {
1665 for (
k=0;
k<nndims;
k++) {
1666 if (dimid == dimids[
k]) {
1672 dimids[ndims_t-(++nndims)] = dimid;
1676 if ( files[fid]->shared_mode ) {
1679 acdimids = (
int*) malloc((
sizeof(
int)*m));
1685 acdimids = (
int*) malloc((
sizeof(
int)*m));
1686 CHECK_ERROR( nc_inq_vardimid(ncid, acid, acdimids) )
1688 for (
j=m-1;
j>=0;
j--) {
1690 for (
k=0;
k<ndims_t;
k++) {
1691 if (acdimids[
j] == dimids[
k]) {
1697 if ( nndims >= ndims_t ) {
1698 fprintf(stderr,
"Error: invalid associated coordinates\n");
1701 dimids[ndims_t-(++nndims)] = acdimids[
j];
1710 if (nndims != ndims) {
1711 fprintf(stderr,
"Error: invalid associated coordinates: %d %d\n", ndims_t, nndims);
1716 if ( files[fid]->shared_mode ) {
1717 CHECK_PNC_ERROR( ncmpi_def_var(ncid, varname, xtype, ndims_t, dimids, &varid) )
1719 if ( strlen(desc) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1720 if ( strlen(units) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1721 if ( strlen(stdname)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"standard_name", strlen(stdname), stdname) )
1724 CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid,
"missing_value", xtype, 1, &rmiss) )
1727 CHECK_ERROR( nc_def_var(ncid, varname, xtype, ndims_t, dimids, &varid) )
1729 if ( strlen(desc) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1730 if ( strlen(units) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1731 if ( strlen(stdname)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"standard_name", strlen(stdname), stdname) )
1732 CHECK_ERROR( nc_put_att_double(ncid, varid, _FillValue, xtype, 1, &rmiss) )
1733 CHECK_ERROR( nc_put_att_double(ncid, varid,
"missing_value", xtype, 1, &rmiss) )
1736 strcpy(coord, dims[0]);
1737 for(
i=1;
i<ndims;
i++) {
1738 if (strlen(coord)+strlen(dims[
i])+1 <
File_HMID) {
1740 strcat(coord, dims[
i]);
1743 if ( ndims_t > ndims && strlen(coord)+6 <
File_HMID) {
1745 strcat(coord, vars[nvar]->t->name);
1747 if ( files[fid]->shared_mode )
1748 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"coordinates", strlen(coord), coord) )
1750 CHECK_ERROR( nc_put_att_text(ncid, varid,
"coordinates", strlen(coord), coord) )
1755 sprintf(buf,
"%s: mean", vars[nvar]->t->name);
1756 if ( files[fid]->shared_mode )
1757 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"cell_methods", strlen(buf), buf) )
1759 CHECK_ERROR( nc_put_att_text(ncid, varid,
"cell_methods", strlen(buf), buf) )
1763 vars[nvar]->
ndims_t = ndims_t;
1764 vars[nvar]->
start = (
size_t*) malloc(
sizeof(
size_t)*ndims_t);
1765 vars[nvar]->
count = (
size_t*) malloc(
sizeof(
size_t)*ndims_t);
1766 for (
i=0;
i<ndims_t;
i++ ) {
1767 if ( files[fid]->shared_mode ) {
1770 size = (size_t) dimlen;
1774 vars[nvar]->
count[
i] = size;
1775 vars[nvar]->
start[
i] = 0;
1777 if ( tint > 0.0 ) vars[nvar]->
count[0] = 1;
1781 if ( ! files[fid]->shared_mode && files[fid]->deflate_level > 0 ) {
1782 CHECK_ERROR( nc_def_var_chunking(ncid, varid, NC_CHUNKED, vars[nvar]->count) )
1783 CHECK_ERROR( nc_def_var_deflate(ncid, varid, 0, 1, files[fid]->deflate_level) )
1787 vars[nvar]->
varid = varid;
1800 ncid = files[fid]->ncid;
1802 return file_enddef(fid, ncid);
1811 ncid = files[fid]->ncid;
1813 return file_redef(fid, ncid);
1817 const int64_t buf_amount )
1822 ncid = files[fid]->ncid;
1824 if ( files[fid]->shared_mode )
1835 ncid = files[fid]->ncid;
1837 if ( files[fid]->shared_mode )
1848 ncid = files[fid]->ncid;
1850 if ( files[fid]->shared_mode )
1861 const real64_t t_start,
1862 const real64_t t_end,
1863 const int32_t precision,
1864 const int32_t ndims,
1865 const int32_t *start,
1866 const int32_t *count)
1869 MPI_Offset *str, *cnt;
1873 ncid = vars[vid]->
ncid;
1875 if ( ndims != vars[vid]->ndims ) {
1876 fprintf(stderr,
"Error: at line %d in %s\n", __LINE__, __FILE__);
1877 fprintf(stderr,
" dimension size %d is not consistent that was added by file_add_variable %d\n", ndims, (
int)vars[vid]->ndims );
1881 ret = file_enddef(fid, ncid);
1884 varid = vars[vid]->
varid;
1885 if ( vars[vid]->t != NULL ) {
1886 if ( vars[vid]->t->count < 0 ||
1887 t_end > vars[vid]->
t->
t +
TEPS ) {
1888 vars[vid]->
t->
count += 1;
1889 vars[vid]->
t->
t = t_end;
1890 if ( vars[vid]->t->count >
NTMAX-1 ) {
1891 fprintf(stderr,
"time count exceeds the max limit (%d)\n",
NTMAX);
1895 if ( files[fid]->shared_mode ) {
1896 MPI_Offset
index[2];
1897 index[0] = (MPI_Offset) vars[vid]->t->count;
1906 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->varid,
index, &t_end) )
1908 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid,
index, &t_start) )
1910 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid,
index, &t_end) )
1914 size_t nt = vars[vid]->
t->
count + 1;
1917 for(n=nt-1;n>=0;n--) {
1918 if ( fabs(vars[vid]->t->tval[n]-t_end) <
TEPS ) {
1919 vars[vid]->
start[0] = n;
1925 fprintf(stderr,
"cannot find time: %f\n", t_end);
1926 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);
1927 fprintf(stderr,
" time is: ");
1928 for (n=0;n<nt;n++) fprintf(stderr,
"%f, ", vars[vid]->t->tval[n]);
1929 fprintf(stderr,
"\n");
1935 if ( files[fid]->shared_mode ) {
1937 int ndims_t = vars[vid]->
ndims_t;
1938 str = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*(ndims_t));
1939 cnt = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*(ndims_t));
1940 if ( vars[vid]->t != NULL ) {
1942 str[0] = vars[vid]->
start[0];
1943 cnt[0] = vars[vid]->
count[0];
1944 for (
i=0;
i<ndims;
i++) {
1945 str[ndims_t-
i-1] = start[
i] - 1;
1946 cnt[ndims_t-
i-1] = count[
i];
1949 for (
i=0;
i<ndims;
i++) {
1950 str[ndims-
i-1] = start[
i] - 1;
1951 cnt[ndims-
i-1] = count[
i];
1956 switch (precision) {
1958 if ( files[fid]->shared_mode )
1959 CHECK_PNC_ERROR( ncmpi_bput_vara_double(ncid, varid, str, cnt, (
double*)var, NULL) )
1961 CHECK_ERROR( nc_put_vara_double(ncid, varid, vars[vid]->start, vars[vid]->count, (
double*)var) )
1964 if ( files[fid]->shared_mode )
1965 CHECK_PNC_ERROR( ncmpi_bput_vara_float(ncid, varid, str, cnt, (
float*)var, NULL) )
1967 CHECK_ERROR( nc_put_vara_float(ncid, varid, vars[vid]->start, vars[vid]->count, (
float*)var) )
1970 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1974 if ( files[fid]->shared_mode) {
1983 const int32_t abort )
1989 ncid = files[fid]->ncid;
1991 for (
i=0;
i<nvar;
i++) {
1992 if ( vars[
i] != NULL && vars[
i]->ncid == ncid ) {
1993 free( vars[
i]->start );
1994 free( vars[
i]->count );
2000 for (
i=0;
i<nt;
i++) {
2001 if ( tdims[
i] != NULL && tdims[
i]->ncid == ncid ) {
2002 free( tdims[
i]->tval );
2008 if ( files[fid]->shared_mode ) {