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;
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 ) {
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);
392 dinfo->
units[i] = buf[i];
393 dinfo->
units[i] =
'\0';
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) )
460 dinfo->
units[i] = buf[i];
461 dinfo->
units[i] =
'\0';
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++) {
488 CHECK_ERROR( nc_inq_attname(ncid, varid, i, name) )
489 if ( strcmp(name,
"long_name") &&
490 strcmp(name,
"description") &&
491 strcmp(name,
"units") &&
492 strcmp(name,
"standard_name") ) {
494 CHECK_ERROR( nc_inq_atttype(ncid, varid, name, &xtype) )
496 CHECK_ERROR( nc_inq_attlen(ncid, varid, name, &l) )
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_;
525 CHECK_ERROR( nc_inq_dim(ncid, dimids[rank-i-1], name, &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;
706 ncid = files[fid]->
ncid;
707 if ( files[fid]->shared_mode ) {
710 strp = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*rank);
711 cntp = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*rank);
716 str = (
size_t*) malloc(
sizeof(
size_t)*rank);
717 cnt = (
size_t*) malloc(
sizeof(
size_t)*rank);
719 if ( start == NULL || count == NULL ) {
720 for (i=0; i<dinfo->
rank; i++) {
726 for (i=0; i<dinfo->
rank; i++) {
728 str[rank -i-1] = start[
i] - 1;
729 cnt[rank -i-1] = count[
i];
732 if (rank > dinfo->
rank) {
733 str[0] = dinfo->
step - 1;
738 for (i=0; i<rank; i++) size *= cnt[i];
740 if ( files[fid]->shared_mode ) {
742 for (i=0; i<rank; i++) {
743 strp[
i] = (MPI_Offset) str[i];
744 cntp[
i] = (MPI_Offset) cnt[i];
748 CHECK_PNC_ERROR( ncmpi_iget_vara(ncid, varid, strp, cntp, var, ntypes, dtype, NULL) )
751 if ( dtype == MPI_FLOAT ) {
752 float factor, offset;
754 if ( ncmpi_get_att_float(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
758 if ( ncmpi_get_att_float(ncid, varid,
"add_offset", &offset) != NC_NOERR )
762 if ( l_rescale )
for (i=0; i<size; i++) ((
float*)var)[
i] = ((
float*)var)[
i] * factor + offset;
763 }
else if ( dtype == MPI_DOUBLE ) {
764 double factor, offset;
766 if ( ncmpi_get_att_double(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
770 if ( ncmpi_get_att_double(ncid, varid,
"add_offset", &offset) != NC_NOERR )
774 if ( l_rescale )
for (i=0; i<size; i++) ((
double*)var)[i] = ((
double*)var)[i] * factor + offset;
776 float factor, offset;
777 if ( ( ncmpi_get_att_float(ncid, varid,
"scale_factor", &factor) == NC_NOERR )
778 || ( ncmpi_get_att_float(ncid, varid,
"add_offset", &offset) == NC_NOERR ) ) {
779 fprintf(stderr,
"scale_factor and add_offset is not supported with a MPI derived type\n");
787 switch ( precision ) {
789 CHECK_ERROR( nc_get_vara_double(ncid, varid, str, cnt, (
double*)var) )
791 double factor, offset;
793 if ( nc_get_att_double(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
797 if ( nc_get_att_double(ncid, varid,
"add_offset", &offset) != NC_NOERR )
801 if ( l_rescale )
for (i=0; i<size; i++) ((
double*)var)[
i] = ((
double*)var)[
i] * factor + offset;
805 CHECK_ERROR( nc_get_vara_float(ncid, varid, str, cnt, (
float*)var) )
807 float factor, offset;
809 if ( nc_get_att_float(ncid, varid,
"scale_factor", &factor) != NC_NOERR )
813 if ( nc_get_att_float(ncid, varid,
"add_offset", &offset) != NC_NOERR )
817 if ( l_rescale )
for (i=0; i<size; i++) ((
float*)var)[
i] = ((
float*)var)[
i] * factor + offset;
823 fprintf(stderr,
"unsupported data precision: %d\n", precision );
837 const int32_t suppress,
845 ncid = files[fid]->
ncid;
847 ERROR_SUPPRESS = suppress;
849 if ( files[fid]->shared_mode ) {
851 if ( strcmp(vname,
"global") == 0 ) {
864 if ( strcmp(vname,
"global") == 0 ) {
872 CHECK_ERROR( nc_get_att_text(ncid, varid, key, value) )
884 const int32_t suppress,
892 ncid = files[fid]->
ncid;
894 ERROR_SUPPRESS = suppress;
896 if ( files[fid]->shared_mode ) {
898 if ( strcmp(vname,
"global") == 0 ) {
909 if ( strcmp(vname,
"global") == 0 ) {
916 CHECK_ERROR( nc_get_att_int(ncid, varid, key, value) )
927 const int32_t suppress,
935 ncid = files[fid]->
ncid;
937 ERROR_SUPPRESS = suppress;
939 if ( files[fid]->shared_mode ) {
941 if ( strcmp(vname,
"global") == 0 ) {
952 if ( strcmp(vname,
"global") == 0 ) {
959 CHECK_ERROR( nc_get_att_float(ncid, varid, key, value) )
970 const int32_t suppress,
978 ncid = files[fid]->
ncid;
980 ERROR_SUPPRESS = suppress;
982 if ( files[fid]->shared_mode ) {
984 if ( strcmp(vname,
"global") == 0 ) {
995 if ( strcmp(vname,
"global") == 0 ) {
1000 CHECK_ERROR( nc_inq_attlen(ncid, varid, key, &l) )
1002 CHECK_ERROR( nc_get_att_double(ncid, varid, key, value) )
1021 ncid = files[fid]->
ncid;
1023 if ( files[fid]->shared_mode ) {
1024 if ( strcmp(vname,
"global") == 0 ) {
1032 if ( strcmp(vname,
"global") == 0 ) {
1040 ret = file_redef(fid, ncid);
1043 if ( files[fid]->shared_mode )
1044 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, key, strlen(val), val) )
1046 CHECK_ERROR( nc_put_att_text(ncid, varid, key, strlen(val), val) )
1054 const int32_t *value,
1063 ncid = files[fid]->
ncid;
1065 if ( files[fid]->shared_mode ) {
1066 if ( strcmp(vname,
"global") == 0 ) {
1074 if ( strcmp(vname,
"global") == 0 ) {
1082 ret = file_redef(fid, ncid);
1085 if ( files[fid]->shared_mode )
1086 CHECK_PNC_ERROR( ncmpi_put_att_int(ncid, varid, key, NC_INT, len, value) )
1088 CHECK_ERROR( nc_put_att_int(ncid, varid, key, NC_INT, len, value) )
1106 ncid = files[fid]->
ncid;
1108 if ( files[fid]->shared_mode ) {
1109 if ( strcmp(vname,
"global") == 0 ) {
1117 if ( strcmp(vname,
"global") == 0 ) {
1125 ret = file_redef(fid, ncid);
1128 if ( files[fid]->shared_mode )
1129 CHECK_PNC_ERROR( ncmpi_put_att_float(ncid, varid, key, NC_FLOAT, len, value) )
1131 CHECK_ERROR( nc_put_att_float(ncid, varid, key, NC_FLOAT, len, value) )
1139 const double *value,
1147 ncid = files[fid]->
ncid;
1149 if ( files[fid]->shared_mode ) {
1150 if ( strcmp(vname,
"global") == 0 ) {
1158 if ( strcmp(vname,
"global") == 0 ) {
1166 if ( files[fid]->shared_mode )
1167 CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid, key, NC_DOUBLE, len, value) )
1169 CHECK_ERROR( nc_put_att_double(ncid, varid, key, NC_DOUBLE, len, value) )
1181 ncid = files[fid]->
ncid;
1183 if ( nc_inq_varid(ncid, vname, &varid) == NC_NOERR )
1186 ret = file_redef(fid, ncid);
1189 if ( files[fid]->shared_mode )
1192 CHECK_ERROR( nc_def_var(ncid, vname, NC_INT, 0, 0, &varid) )
1198 const char *time_units,
1199 const char *calendar)
1201 strcpy(files[fid]->time_units, time_units);
1202 strcpy(files[fid]->calendar, calendar);
1211 const char *dim_name,
1212 const int32_t dtype,
1215 const int32_t precision)
1217 int ncid, dimid, varid;
1222 ncid = files[fid]->
ncid;
1224 if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR )
1227 ret = file_redef(fid, ncid);
1230 if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR )
1231 CHECK_ERROR( nc_def_dim(ncid, dim_name, size, &dimid) )
1234 CHECK_ERROR( nc_def_var(ncid, name, xtype, 1, &dimid, &varid) )
1235 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1236 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1238 ret = file_enddef(fid, ncid);
1241 switch ( precision ) {
1243 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1246 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1249 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1260 const char *dim_name,
1261 const int32_t dtype,
1262 const int32_t dim_size,
1263 const int32_t bounds)
1265 int ncid, dimid, varid;
1272 ncid = files[fid]->
ncid;
1274 if ( files[fid]->shared_mode ) {
1280 ret = file_redef(fid, ncid);
1284 if ( files[fid]->shared_mode ) {
1288 CHECK_PNC_ERROR( ncmpi_def_var(ncid, name, xtype, 1, &dimid, &varid) )
1289 if ( strlen(desc)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1290 if ( strlen(units)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1296 sprintf(buf,
"%s_bnds", dim_name);
1297 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"bounds", strlen(buf), buf) )
1298 CHECK_PNC_ERROR( ncmpi_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &varid) )
1301 if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR )
1302 CHECK_ERROR( nc_def_dim(ncid, dim_name, dim_size, &dimid) )
1304 CHECK_ERROR( nc_def_var(ncid, name, xtype, 1, &dimid, &varid) )
1305 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1306 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1310 if ( nc_inq_dimid(ncid,
"nv", &(dimids[1])) != NC_NOERR )
1311 CHECK_ERROR( nc_def_dim(ncid,
"nv", 2, &(dimids[1])) )
1312 sprintf(buf,
"%s_bnds", dim_name);
1313 CHECK_ERROR( nc_put_att_text(ncid, varid,
"bounds", strlen(buf), buf) )
1314 CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &varid) )
1324 const int32_t precision,
1325 const MPI_Offset *start,
1326 const MPI_Offset *count)
1332 ncid = files[fid]->
ncid;
1334 if ( files[fid]->shared_mode )
1339 ret = file_enddef(fid, ncid);
1342 switch ( precision ) {
1344 if ( files[fid]->shared_mode )
1345 CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start, count, val, NULL) )
1347 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1350 if ( files[fid]->shared_mode )
1351 CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start, count, val, NULL) )
1353 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1356 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1367 const char **dim_names,
1368 const int32_t ndims,
1369 const int32_t dtype,
1371 const int32_t precision)
1373 int ncid, *dimids, varid;
1379 ncid = files[fid]->
ncid;
1381 if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR )
1384 ret = file_redef(fid, ncid);
1387 dimids = malloc(
sizeof(
int)*ndims);
1388 for (i=0; i<ndims; i++)
1389 CHECK_ERROR( nc_inq_dimid(ncid, dim_names[i], dimids+ndims-i-1) )
1393 CHECK_ERROR( nc_def_var(ncid, name, xtype, ndims, dimids, &varid) )
1394 if ( strlen(desc)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1395 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1398 ret = file_enddef(fid, ncid);
1401 switch ( precision ) {
1403 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1406 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1409 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1420 const char **dim_names,
1421 const int32_t ndims,
1422 const int32_t dtype)
1431 ncid = files[fid]->
ncid;
1433 if ( files[fid]->shared_mode ) {
1437 if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR )
1441 ret = file_redef(fid, ncid);
1446 if ( files[fid]->shared_mode ) {
1447 for (i=0; i<ndims; i++)
1450 CHECK_PNC_ERROR( ncmpi_def_var(ncid, name, xtype, ndims, dimids, &varid) )
1451 if ( strlen(desc) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1452 if ( strlen(units)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1455 for (i=0; i<ndims; i++)
1456 CHECK_ERROR( nc_inq_dimid(ncid, dim_names[i], dimids+ndims-i-1) )
1458 CHECK_ERROR( nc_def_var(ncid, name, xtype, ndims, dimids, &varid) )
1459 if ( strlen(desc) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1460 if ( strlen(units)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1469 const int32_t precision,
1470 const MPI_Offset *start,
1471 const MPI_Offset *count)
1477 ncid = files[fid]->
ncid;
1479 if ( files[fid]->shared_mode )
1484 ret = file_enddef(fid, ncid);
1487 switch ( precision ) {
1489 if ( files[fid]->shared_mode )
1490 CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start, count, (
double*)val, NULL) )
1492 CHECK_ERROR( nc_put_var_double(ncid, varid, (
double*)val) )
1495 if ( files[fid]->shared_mode )
1496 CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start, count, (
float*)val, NULL) )
1498 CHECK_ERROR( nc_put_var_float(ncid, varid, (
float*)val) )
1501 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1509 const char *varname,
1512 const char *stdname,
1514 const int32_t ndims,
1515 const int32_t dtype,
1516 const real64_t tint,
1520 int ncid, varid, acid, *acdimids;
1521 int dimids[NC_MAX_DIMS], dimid;
1526 int i,
j,
k, m, err;
1527 int ndims_t, nndims;
1529 double rmiss =
RMISS;
1536 fprintf(stderr,
"exceed max number of variable limit\n");
1541 ncid = files[fid]->
ncid;
1544 vars[nvar]->
ncid = ncid;
1545 vars[nvar]->
t = NULL;
1546 vars[nvar]->
start = NULL;
1547 vars[nvar]->
count = NULL;
1548 vars[nvar]->
ndims = ndims;
1550 ret = file_redef(fid, ncid);
1555 for ( i=0; i<nt; i++ ) {
1556 if ( tdims[i] != NULL &&
1557 tdims[i]->ncid == ncid &&
1558 tdims[i]->tint == tint ) {
1559 vars[nvar]->
t = tdims[
i];
1563 if ( vars[nvar]->t == NULL ) {
1565 tdims[nt]->
ncid = ncid;
1566 tdims[nt]->
count = -1;
1567 tdims[nt]->
tint = tint;
1568 tdims[nt]->
tval = (
double*) malloc(
sizeof(
double)*
NTMAX);
1571 for (i=0; i<nt; i++) {
1572 if ( tdims[i] != NULL && tdims[i]->ncid == ncid ) m++;
1575 strcpy(tname,
"time");
1577 sprintf(tname,
"time%d", m);
1579 strcpy(tdims[nt]->name, tname);
1581 if ( files[fid]->shared_mode ) {
1583 tdims[nt]->
dimid = tdimid;
1584 CHECK_PNC_ERROR( ncmpi_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1585 tdims[nt]->
varid = tvarid;
1586 strcpy(buf,
"time");
1587 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"long_name", strlen(buf), buf) )
1588 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"units", strlen(files[fid]->time_units), files[fid]->time_units) )
1589 if ( strlen(files[fid]->calendar) > 0 )
1590 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"calendar", strlen(files[fid]->calendar), files[fid]->calendar) )
1594 sprintf(buf,
"%s_bnds", tname);
1595 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid,
"bounds", strlen(buf), buf) )
1597 CHECK_PNC_ERROR( ncmpi_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1598 tdims[nt]->
bndsid = tvarid;
1602 CHECK_ERROR( nc_def_dim(ncid, tname, 0, &tdimid) )
1603 tdims[nt]->
dimid = tdimid;
1604 CHECK_ERROR( nc_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1605 tdims[nt]->
varid = tvarid;
1606 strcpy(buf,
"time");
1607 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"long_name", strlen(buf), buf) )
1608 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"units", strlen(files[fid]->time_units), files[fid]->time_units) )
1609 if ( strlen(files[fid]->calendar) > 0 )
1610 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"calendar", strlen(files[fid]->calendar), files[fid]->calendar) )
1612 if ( nc_inq_dimid(ncid,
"nv", &(dimids[1])) != NC_NOERR )
1613 CHECK_ERROR( nc_def_dim(ncid,
"nv", 2, &(dimids[1])) )
1614 sprintf(buf,
"%s_bnds", tname);
1615 CHECK_ERROR( nc_put_att_text(ncid, tvarid,
"bounds", strlen(buf), buf) )
1617 CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1618 tdims[nt]->
bndsid = tvarid;
1622 vars[nvar]->
t = tdims[nt];
1631 dimids[0] = vars[nvar]->
t->
dimid;
1634 for (i=ndims_t-ndims; i<ndims_t; i++) dimids[i] = -1;
1638 for (i=0; i<ndims; i++) {
1640 if ( files[fid]->shared_mode )
1643 err = nc_inq_dimid(ncid, dims[i], &dimid);
1644 if ( err == NC_NOERR ) {
1647 for (k=0; k<nndims; k++) {
1648 if (dimid == dimids[k]) {
1654 dimids[ndims_t-(++nndims)] = dimid;
1658 if ( files[fid]->shared_mode ) {
1661 acdimids = (
int*) malloc((
sizeof(
int)*m));
1667 acdimids = (
int*) malloc((
sizeof(
int)*m));
1668 CHECK_ERROR( nc_inq_vardimid(ncid, acid, acdimids) )
1670 for (j=m-1; j>=0; j--) {
1672 for (k=0; k<ndims_t; k++) {
1673 if (acdimids[j] == dimids[k]) {
1679 if ( nndims >= ndims_t ) {
1680 fprintf(stderr,
"Error: invalid associated coordinates\n");
1683 dimids[ndims_t-(++nndims)] = acdimids[j];
1692 if (nndims != ndims) {
1693 fprintf(stderr,
"Error: invalid associated coordinates: %d %d\n", ndims_t, nndims);
1698 if ( files[fid]->shared_mode ) {
1699 CHECK_PNC_ERROR( ncmpi_def_var(ncid, varname, xtype, ndims_t, dimids, &varid) )
1701 if ( strlen(desc) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1702 if ( strlen(units) >0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"units", strlen(units), units) )
1703 if ( strlen(stdname)>0 )
CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"standard_name", strlen(stdname), stdname) )
1706 CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid,
"missing_value", xtype, 1, &rmiss) )
1709 CHECK_ERROR( nc_def_var(ncid, varname, xtype, ndims_t, dimids, &varid) )
1711 if ( strlen(desc) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"long_name", strlen(desc), desc) )
1712 if ( strlen(units) >0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"units", strlen(units), units) )
1713 if ( strlen(stdname)>0 )
CHECK_ERROR( nc_put_att_text(ncid, varid,
"standard_name", strlen(stdname), stdname) )
1714 CHECK_ERROR( nc_put_att_double(ncid, varid, _FillValue, xtype, 1, &rmiss) )
1715 CHECK_ERROR( nc_put_att_double(ncid, varid,
"missing_value", xtype, 1, &rmiss) )
1718 strcpy(coord, dims[0]);
1719 for(i=1; i<ndims; i++) {
1720 if (strlen(coord)+strlen(dims[i])+1 <
File_HMID) {
1722 strcat(coord, dims[i]);
1725 if ( ndims_t > ndims && strlen(coord)+6 <
File_HMID) {
1727 strcat(coord, vars[nvar]->t->name);
1729 if ( files[fid]->shared_mode )
1730 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"coordinates", strlen(coord), coord) )
1732 CHECK_ERROR( nc_put_att_text(ncid, varid,
"coordinates", strlen(coord), coord) )
1737 sprintf(buf,
"%s: mean", vars[nvar]->t->name);
1738 if ( files[fid]->shared_mode )
1739 CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid,
"cell_methods", strlen(buf), buf) )
1741 CHECK_ERROR( nc_put_att_text(ncid, varid,
"cell_methods", strlen(buf), buf) )
1745 vars[nvar]->
ndims_t = ndims_t;
1746 vars[nvar]->
start = (
size_t*) malloc(
sizeof(
size_t)*ndims_t);
1747 vars[nvar]->
count = (
size_t*) malloc(
sizeof(
size_t)*ndims_t);
1748 for ( i=0; i<ndims_t; i++ ) {
1749 if ( files[fid]->shared_mode ) {
1752 size = (size_t) dimlen;
1755 CHECK_ERROR( nc_inq_dimlen(ncid, dimids[i], &size) )
1756 vars[nvar]->
count[
i] = size;
1757 vars[nvar]->
start[
i] = 0;
1759 if ( tint > 0.0 ) vars[nvar]->
count[0] = 1;
1763 if ( ! files[fid]->shared_mode && files[fid]->deflate_level > 0 ) {
1764 CHECK_ERROR( nc_def_var_chunking(ncid, varid, NC_CHUNKED, vars[nvar]->count) )
1765 CHECK_ERROR( nc_def_var_deflate(ncid, varid, 0, 1, files[fid]->deflate_level) )
1769 vars[nvar]->
varid = varid;
1782 ncid = files[fid]->
ncid;
1784 return file_enddef(fid, ncid);
1788 const int64_t buf_amount )
1793 ncid = files[fid]->
ncid;
1795 if ( files[fid]->shared_mode )
1806 ncid = files[fid]->
ncid;
1808 if ( files[fid]->shared_mode )
1819 ncid = files[fid]->
ncid;
1821 if ( files[fid]->shared_mode )
1832 const real64_t t_start,
1833 const real64_t t_end,
1834 const int32_t precision,
1835 const int32_t ndims,
1836 const int32_t *start,
1837 const int32_t *count)
1840 MPI_Offset *str, *cnt;
1844 ncid = vars[vid]->
ncid;
1846 if ( ndims != vars[vid]->ndims ) {
1847 fprintf(stderr,
"Error: at line %d in %s\n", __LINE__, __FILE__);
1848 fprintf(stderr,
" dimension size %d is not consistent that was added by file_add_variable %d\n", ndims, (
int)vars[vid]->ndims );
1852 ret = file_enddef(fid, ncid);
1855 varid = vars[vid]->
varid;
1856 if ( vars[vid]->t != NULL ) {
1857 if ( vars[vid]->t->count < 0 ||
1858 t_end > vars[vid]->
t->
t +
TEPS ) {
1859 vars[vid]->
t->
count += 1;
1860 vars[vid]->
t->
t = t_end;
1861 if ( vars[vid]->t->count >
NTMAX-1 ) {
1862 fprintf(stderr,
"time count exceeds the max limit (%d)\n",
NTMAX);
1866 if ( files[fid]->shared_mode ) {
1867 MPI_Offset index[2];
1868 index[0] = (MPI_Offset) vars[vid]->t->count;
1869 CHECK_PNC_ERROR( ncmpi_put_var1_double_all(ncid, vars[vid]->t->varid, index, &t_end) )
1871 CHECK_PNC_ERROR( ncmpi_put_var1_double_all(ncid, vars[vid]->t->bndsid, index, &t_start ) )
1873 CHECK_PNC_ERROR( ncmpi_put_var1_double_all(ncid, vars[vid]->t->bndsid, index, &t_end ) )
1876 index[0] = vars[vid]->
t->
count;
1877 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->varid, index, &t_end) )
1879 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_start) )
1881 CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_end) )
1885 size_t nt = vars[vid]->
t->
count + 1;
1888 for(n=nt-1;n>=0;n--) {
1889 if ( fabs(vars[vid]->t->tval[n]-t_end) <
TEPS ) {
1890 vars[vid]->
start[0] = n;
1896 fprintf(stderr,
"cannot find time: %f\n", t_end);
1897 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);
1898 fprintf(stderr,
" time is: ");
1899 for (n=0;n<nt;n++) fprintf(stderr,
"%f, ", vars[vid]->t->tval[n]);
1900 fprintf(stderr,
"\n");
1906 if ( files[fid]->shared_mode ) {
1908 int ndims_t = vars[vid]->
ndims_t;
1909 str = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*(ndims_t));
1910 cnt = (MPI_Offset*) malloc(
sizeof(MPI_Offset)*(ndims_t));
1911 if ( vars[vid]->t != NULL ) {
1913 str[0] = vars[vid]->
start[0];
1914 cnt[0] = vars[vid]->
count[0];
1915 for (i=0; i<ndims; i++) {
1916 str[ndims_t-i-1] = start[
i] - 1;
1917 cnt[ndims_t-i-1] = count[
i];
1920 for (i=0; i<ndims; i++) {
1921 str[ndims-i-1] = start[
i] - 1;
1922 cnt[ndims-i-1] = count[
i];
1927 switch (precision) {
1929 if ( files[fid]->shared_mode )
1930 CHECK_PNC_ERROR( ncmpi_bput_vara_double(ncid, varid, str, cnt, (
double*)var, NULL) )
1932 CHECK_ERROR( nc_put_vara_double(ncid, varid, vars[vid]->start, vars[vid]->count, (
double*)var) )
1935 if ( files[fid]->shared_mode )
1936 CHECK_PNC_ERROR( ncmpi_bput_vara_float(ncid, varid, str, cnt, (
float*)var, NULL) )
1938 CHECK_ERROR( nc_put_vara_float(ncid, varid, vars[vid]->start, vars[vid]->count, (
float*)var) )
1941 fprintf(stderr,
"unsupported data precision: %d\n", precision);
1945 if ( files[fid]->shared_mode) {
1959 ncid = files[fid]->
ncid;
1961 for (i=0; i<nvar; i++) {
1962 if ( vars[i] != NULL && vars[i]->ncid == ncid ) {
1963 free( vars[i]->start );
1964 free( vars[i]->count );
1970 for (i=0; i<nt; i++) {
1971 if ( tdims[i] != NULL && tdims[i]->ncid == ncid ) {
1972 free( tdims[i]->tval );
1978 if ( files[fid]->shared_mode )
char att_name[File_HSHORT *ATT_MAX]
char standard_name[File_HMID]
int32_t file_add_associatedvariable_c(const int32_t fid, const char *vname)
int32_t file_get_dim_length_c(const int32_t fid, const char *dimname, int32_t *len)
int32_t file_get_step_size_c(const int32_t fid, const char *varname, int32_t *len)
int32_t file_get_varname_c(const int32_t fid, const int32_t vid, char *name, const int32_t len)
int32_t file_get_attribute_double_c(const int32_t fid, const char *vname, const char *key, const int32_t suppress, double *value, const size_t len)
int32_t file_get_attribute_float_c(const int32_t fid, const char *vname, const char *key, const int32_t suppress, float *value, const size_t len)
int32_t file_open_c(int32_t *fid, const char *fname, const int32_t mode, const MPI_Comm comm)
int32_t file_get_attribute_int_c(const int32_t fid, const char *vname, const char *key, const int32_t suppress, int *value, const size_t len)
#define TYPE2NCTYPE(type, nctype)
char calendar[File_HSHORT]
char dim_name[File_HSHORT *RANK_MAX]
int32_t file_set_attribute_float_c(const int32_t fid, const char *vname, const char *key, const float *value, const size_t len)
int32_t file_detach_buffer_c(const int32_t fid)
int32_t file_put_associatedcoordinate_c(const int32_t fid, const char *name, const char *desc, const char *units, const char **dim_names, const int32_t ndims, const int32_t dtype, const void *val, const int32_t precision)
char description[File_HMID]
int32_t file_set_tunits_c(const int32_t fid, const char *time_units, const char *calendar)
real(rp), allocatable, target, public k
#define NCTYPE2TYPE(nctype, type)
int32_t file_set_attribute_int_c(const int32_t fid, const char *vname, const char *key, const int32_t *value, const size_t len)
#define ncmpi_inq_varid(a, b, c)
int32_t file_set_option_c(const int32_t fid, const char *filetype, const char *key, const char *val)
#define ncmpi_inq_dimid(a, b, c)
int32_t dim_size[RANK_MAX]
#define CHECK_ERROR(func)
#define ALREADY_EXISTED_CODE
int32_t file_write_axis_c(const int32_t fid, const char *name, const void *val, const int32_t precision, const MPI_Offset *start, const MPI_Offset *count)
int32_t file_enddef_c(const int32_t fid)
int32_t att_type[ATT_MAX]
int32_t file_set_attribute_double_c(const int32_t fid, const char *vname, const char *key, const double *value, const size_t len)
#define DEFAULT_DEFLATE_LEVEL
#define ALREADY_CLOSED_CODE
int32_t file_get_attribute_text_c(const int32_t fid, const char *vname, const char *key, const int32_t suppress, char *value, const int32_t len)
char time_units[File_HMID]
int32_t file_close_c(const int32_t fid)
int32_t file_put_axis_c(const int32_t fid, const char *name, const char *desc, const char *units, const char *dim_name, const int32_t dtype, const void *val, const int32_t size, const int32_t precision)
int32_t file_read_data_c(void *var, const datainfo_t *dinfo, const int32_t precision, const MPI_Offset ntypes, const MPI_Datatype dtype, const int32_t *start, const int32_t *count)
int32_t file_get_datainfo_c(datainfo_t *dinfo, const int32_t fid, const char *varname, const int32_t step, const int32_t suppress)
int32_t file_set_attribute_text_c(const int32_t fid, const char *vname, const char *key, const char *val)
int32_t file_flush_c(const int32_t fid)
#define CHECK_PNC_ERROR(func)
char varname[File_HSHORT]
int32_t file_def_associatedcoordinate_c(const int32_t fid, const char *name, const char *desc, const char *units, const char **dim_names, const int32_t ndims, const int32_t dtype)
int32_t file_add_variable_c(const int32_t fid, const char *varname, const char *desc, const char *units, const char *stdname, const char **dims, const int32_t ndims, const int32_t dtype, const real64_t tint, const int32_t tavg, int32_t *vid)
real(rp), allocatable, target, public j
#define ncmpi_inq_attid(a, b, c, d)
int32_t file_def_axis_c(const int32_t fid, const char *name, const char *desc, const char *units, const char *dim_name, const int32_t dtype, const int32_t dim_size, const int32_t bounds)
int32_t file_write_associatedcoordinate_c(const int32_t fid, const char *name, const void *val, const int32_t precision, const MPI_Offset *start, const MPI_Offset *count)
int32_t file_get_nvars_c(const int32_t fid, int32_t *nvars)
int32_t file_write_data_c(const int32_t fid, const int32_t vid, const void *var, const real64_t t_start, const real64_t t_end, const int32_t precision, const int32_t ndims, const int32_t *start, const int32_t *count)
int32_t file_attach_buffer_c(const int32_t fid, const int64_t buf_amount)