SCALE-RM
Classes | Macros | Functions
gtool_file.h File Reference
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
Include dependency graph for gtool_file.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  datainfo_t
 

Macros

#define File_HSHORT   32
 
#define File_HMID   128
 
#define File_HLONG   1024
 
#define File_REAL4   0
 
#define File_REAL8   1
 
#define File_INTEGER2   2
 
#define File_INTEGER4   3
 
#define File_INTEGER8   4
 
#define File_FREAD   0
 
#define File_FWRITE   1
 
#define File_FAPPEND   2
 
#define ERROR_CODE   -1
 
#define SUCCESS_CODE   0
 
#define ALREADY_CLOSED_CODE   1
 
#define ALREADY_EXISTED_CODE   2
 
#define MAX_RANK   10
 

Functions

typedef float (real32_t)
 
typedef double (real64_t)
 
int32_t file_open (int32_t *fid, char *fname, int32_t mode, MPI_Comm comm)
 
int32_t file_set_option (int32_t fid, char *filetype, char *key, char *val)
 
int32_t file_get_datainfo (datainfo_t *dinfo, int32_t fid, char *varname, int32_t step, int32_t suppress)
 
int32_t file_read_data (void *var, datainfo_t *dinfo, int32_t precision)
 
int32_t file_read_data_par (void *var, datainfo_t *dinfo, MPI_Offset ntypes, MPI_Datatype dtype, MPI_Offset *start, MPI_Offset *count)
 
int32_t file_get_global_attribute_text (int32_t fid, char *key, char *value, int32_t len)
 
int32_t file_get_global_attribute_int (int32_t fid, char *key, int32_t *value, size_t len)
 
int32_t file_get_global_attribute_float (int32_t fid, char *key, float *value, size_t len)
 
int32_t file_get_global_attribute_double (int32_t fid, char *key, double *value, size_t len)
 
int32_t file_set_global_attribute_text (int32_t fid, char *key, char *value)
 
int32_t file_set_global_attribute_int (int32_t fid, char *key, int32_t *value, size_t len)
 
int32_t file_set_global_attribute_float (int32_t fid, char *key, float *value, size_t len)
 
int32_t file_set_global_attribute_double (int32_t fid, char *key, double *value, size_t len)
 
int32_t file_set_tunits (int32_t fid, char *time_units)
 
int32_t file_set_tattr (int32_t fid, char *vname, char *key, char *val)
 
int32_t file_put_axis (int32_t fid, char *name, char *desc, char *units, char *dim_name, int32_t dtype, void *val, int32_t size, int32_t precision)
 
int32_t file_def_axis (int32_t fid, char *name, char *desc, char *units, char *dim_name, int32_t dtype, int32_t dim_size)
 
int32_t file_write_axis (int32_t fid, char *name, void *val, int32_t precision, MPI_Offset *start, MPI_Offset *count)
 
int32_t file_put_associated_coordinates (int32_t fid, char *name, char *desc, char *units, char **dim_names, int32_t ndims, int32_t dtype, void *val, int32_t precision)
 
int32_t file_def_associated_coordinates (int32_t fid, char *name, char *desc, char *units, char **dim_names, int32_t ndims, int32_t dtype)
 
int32_t file_write_associated_coordinates (int32_t fid, char *name, void *val, int32_t precision, MPI_Offset *start, MPI_Offset *count)
 
int32_t file_add_variable (int32_t *vid, int32_t fid, char *varname, char *desc, char *units, char **dims, int32_t ndims, int32_t dtype, real64_t tint, int32_t tavg)
 
int32_t file_write_data (int32_t fid, int32_t vid, void *var, real64_t t_start, real64_t t_end, int32_t precision, MPI_Offset *start, MPI_Offset *count)
 
int32_t file_enddef (int32_t fid)
 
int32_t file_attach_buffer (int32_t fid, int32_t buf_amount)
 
int32_t file_detach_buffer (int32_t fid)
 
int32_t file_flush (int32_t fid)
 
int32_t file_close (int32_t fid)
 

Macro Definition Documentation

◆ File_HSHORT

#define File_HSHORT   32

Definition at line 12 of file gtool_file.h.

Referenced by file_set_option_(), and file_set_tattr_().

◆ File_HMID

#define File_HMID   128

Definition at line 13 of file gtool_file.h.

Referenced by file_set_option_(), and file_set_tunits_().

◆ File_HLONG

#define File_HLONG   1024

Definition at line 14 of file gtool_file.h.

Referenced by file_set_tattr_().

◆ File_REAL4

#define File_REAL4   0

Definition at line 17 of file gtool_file.h.

◆ File_REAL8

#define File_REAL8   1

Definition at line 18 of file gtool_file.h.

◆ File_INTEGER2

#define File_INTEGER2   2

Definition at line 19 of file gtool_file.h.

◆ File_INTEGER4

#define File_INTEGER4   3

Definition at line 20 of file gtool_file.h.

◆ File_INTEGER8

#define File_INTEGER8   4

Definition at line 21 of file gtool_file.h.

◆ File_FREAD

#define File_FREAD   0

Definition at line 24 of file gtool_file.h.

◆ File_FWRITE

#define File_FWRITE   1

Definition at line 25 of file gtool_file.h.

◆ File_FAPPEND

#define File_FAPPEND   2

Definition at line 26 of file gtool_file.h.

◆ ERROR_CODE

#define ERROR_CODE   -1

Definition at line 29 of file gtool_file.h.

◆ SUCCESS_CODE

#define SUCCESS_CODE   0

Definition at line 30 of file gtool_file.h.

Referenced by file_set_option().

◆ ALREADY_CLOSED_CODE

#define ALREADY_CLOSED_CODE   1

Definition at line 31 of file gtool_file.h.

◆ ALREADY_EXISTED_CODE

#define ALREADY_EXISTED_CODE   2

Definition at line 32 of file gtool_file.h.

◆ MAX_RANK

#define MAX_RANK   10

Definition at line 35 of file gtool_file.h.

Function Documentation

◆ float()

typedef float ( real32_t  )

◆ double()

typedef double ( real64_t  )

◆ file_open()

int32_t file_open ( int32_t *  fid,
char *  fname,
int32_t  mode,
MPI_Comm  comm 
)

Definition at line 132 of file gtool_netcdf.c.

Referenced by gtool_file::filemakefname().

136 {
137  int ncid;
138  int len;
139  int shared_mode;
140  char _fname[File_HLONG+4];
141 
142  if ( nfile >= FILE_MAX ) {
143  fprintf(stderr, "exceed max number of file limit\n");
144  return ERROR_CODE;
145  }
146 
147  len = strlen(fname);
148  strcpy(_fname, fname);
149  if (fname[len-3] != '.' || fname[len-2] != 'n' || fname[len-1] != 'c' )
150  strcat(_fname, ".nc");
151 
152  if ( comm == MPI_COMM_NULL || comm == MPI_COMM_SELF )
153  shared_mode = 0;
154  else
155  shared_mode = 1;
156 
157  switch ( mode ) {
158  case File_FREAD:
159  if ( shared_mode )
160  CHECK_PNC_ERROR( ncmpi_open(comm, _fname, NC_NOWRITE, MPI_INFO_NULL, &ncid) )
161  else
162  CHECK_ERROR( nc_open(_fname, NC_NOWRITE, &ncid) )
163  break;
164  case File_FWRITE:
165  if ( shared_mode )
166  CHECK_PNC_ERROR( ncmpi_create(comm, _fname, NC_CLOBBER|NC_64BIT_OFFSET, MPI_INFO_NULL, &ncid) )
167  else
168 #ifdef NETCDF3
169  CHECK_ERROR( nc_create(_fname, NC_CLOBBER|NC_64BIT_OFFSET, &ncid) )
170 #else
171  CHECK_ERROR( nc_create(_fname, NC_CLOBBER|NC_NETCDF4, &ncid) )
172 #endif
173  break;
174  case File_FAPPEND:
175  if ( shared_mode )
176  CHECK_PNC_ERROR( ncmpi_open(comm, _fname, NC_WRITE, MPI_INFO_NULL, &ncid) )
177  else
178  CHECK_ERROR( nc_open(_fname, NC_WRITE, &ncid) )
179  break;
180  default:
181  fprintf(stderr, "invalid mode type\n");
182  return ERROR_CODE;
183  }
184 
185  files[nfile] = (fileinfo_t*) malloc(sizeof(fileinfo_t));
186  files[nfile]->ncid = ncid;
187  files[nfile]->deflate_level = DEFAULT_DEFLATE_LEVEL;
188 #if defined(NETCDF3) || defined(PNETCDF)
189  if ( mode == File_FWRITE )
190  files[nfile]->defmode = 1;
191  else
192  files[nfile]->defmode = 0;
193 #endif
194  files[nfile]->shared_mode = shared_mode; /* shared-file I/O mode */
195  strcpy(files[nfile]->fname, fname);
196  *fid = nfile;
197  nfile++;
198 
199  return SUCCESS_CODE;
200 }
#define File_FAPPEND
Definition: gtool_file.h:26
#define FILE_MAX
Definition: gtool_netcdf.c:120
#define File_FREAD
Definition: gtool_file.h:24
#define ERROR_CODE
Definition: gtool_file.h:29
#define DEFAULT_DEFLATE_LEVEL
Definition: gtool_netcdf.c:86
int deflate_level
Definition: gtool_netcdf.c:91
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define SUCCESS_CODE
Definition: gtool_file.h:30
int shared_mode
Definition: gtool_netcdf.c:95
#define File_HLONG
Definition: gtool_file.h:14
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
#define File_FWRITE
Definition: gtool_file.h:25
Here is the caller graph for this function:

◆ file_set_option()

int32_t file_set_option ( int32_t  fid,
char *  filetype,
char *  key,
char *  val 
)

Definition at line 202 of file gtool_netcdf.c.

References SUCCESS_CODE.

Referenced by gtool_file::filesetoption().

206 {
207  if ( strcmp(filetype, "netcdf") != 0 ) return SUCCESS_CODE;
208 
209  if ( strcmp(key, "deflate_level") == 0 ) {
210  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
211  files[fid]->deflate_level = atoi(val);
212  return SUCCESS_CODE;
213  } else {
214  return ERROR_CODE;
215  }
216 }
#define ERROR_CODE
Definition: gtool_file.h:29
int deflate_level
Definition: gtool_netcdf.c:91
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
Here is the caller graph for this function:

◆ file_get_datainfo()

int32_t file_get_datainfo ( datainfo_t dinfo,
int32_t  fid,
char *  varname,
int32_t  step,
int32_t  suppress 
)

Definition at line 218 of file gtool_netcdf.c.

Referenced by gtool_file::filegetshape(), gtool_file::filereadvar1drealdp(), gtool_file::filereadvar2drealdp(), gtool_file::filereadvar2drealsp(), gtool_file::filereadvar3drealdp(), gtool_file::filereadvar3drealsp(), gtool_file::filereadvar4drealdp(), and gtool_file::filereadvar4drealsp().

223 {
224  int ncid, varid;
225  nc_type xtype;
226  int rank;
227  int dimids[MAX_RANK], tdim, uldims[NC_MAX_DIMS];
228  char name[NC_MAX_NAME+1];
229  char *buf;
230  size_t size, len;
231  int i, n;
232 
233  ERROR_SUPPRESS = suppress;
234 
235  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
236  ncid = files[fid]->ncid;
237  if ( files[fid]->shared_mode )
238  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, varname, &varid) )
239  else
240  CHECK_ERROR( nc_inq_varid(ncid, varname, &varid) )
241 
242  // fid
243  dinfo->fid = fid;
244  // varname
245  strcpy(dinfo->varname, varname);
246  if ( files[fid]->shared_mode ) {
247  MPI_Offset l;
248  // description
249  CHECK_PNC_ERROR( ncmpi_inq_attlen (ncid, varid, "long_name", &l) )
250  buf = (char*) malloc(l+1);
251  CHECK_PNC_ERROR( ncmpi_get_att_text(ncid, varid, "long_name", buf) )
252  for (i=0; i<MIN(File_HMID-1,l); i++)
253  dinfo->description[i] = buf[i];
254  dinfo->description[i] = '\0';
255  free(buf);
256  // units
257  CHECK_PNC_ERROR( ncmpi_inq_attlen (ncid, varid, "units", &l) )
258  buf = (char*) malloc(l+1);
259  CHECK_PNC_ERROR( ncmpi_get_att_text(ncid, varid, "units", buf) )
260  for (i=0; i<MIN(File_HSHORT-1,l); i++)
261  dinfo->units[i] = buf[i];
262  dinfo->units[i] = '\0';
263  free(buf);
264  // datatype
265  CHECK_PNC_ERROR( ncmpi_inq_vartype(ncid, varid, &xtype) )
266  NCTYPE2TYPE(xtype, dinfo->datatype);
267  // rank
268  CHECK_PNC_ERROR( ncmpi_inq_varndims(ncid, varid, &rank) )
269  CHECK_PNC_ERROR( ncmpi_inq_vardimid(ncid, varid, dimids) )
270 #if 1
271  CHECK_PNC_ERROR( ncmpi_inq_unlimdim(ncid, uldims) )
272  n = 1;
273 #else
274  CHECK_PNC_ERROR( ncmpi_inq_unlimdims(ncid, &n, uldims) )
275 #endif
276  }
277  else {
278  size_t l;
279  // description
280  CHECK_ERROR( nc_inq_attlen (ncid, varid, "long_name", &l) )
281  buf = (char*) malloc(l+1);
282  CHECK_ERROR( nc_get_att_text(ncid, varid, "long_name", buf) )
283  for (i=0; i<MIN(File_HMID-1,l); i++)
284  dinfo->description[i] = buf[i];
285  dinfo->description[i] = '\0';
286  free(buf);
287  // units
288  CHECK_ERROR( nc_inq_attlen (ncid, varid, "units", &l) )
289  buf = (char*) malloc(l+1);
290  CHECK_ERROR( nc_get_att_text(ncid, varid, "units", buf) )
291  for (i=0; i<MIN(File_HSHORT-1,l); i++)
292  dinfo->units[i] = buf[i];
293  dinfo->units[i] = '\0';
294  free(buf);
295  // datatype
296  CHECK_ERROR( nc_inq_vartype(ncid, varid, &xtype) )
297  NCTYPE2TYPE(xtype, dinfo->datatype);
298  // rank
299  CHECK_ERROR( nc_inq_varndims(ncid, varid, &rank) )
300  CHECK_ERROR( nc_inq_vardimid(ncid, varid, dimids) )
301 #ifdef NETCDF3
302  CHECK_ERROR( nc_inq_unlimdim(ncid, uldims) )
303  n = 1;
304 #else
305  CHECK_ERROR( nc_inq_unlimdims(ncid, &n, uldims) )
306 #endif
307  }
308 
309  tdim = -1;
310  for ( i=0; i<n; i++ ) {
311  if ( uldims[i] == dimids[0] ) {
312  tdim = uldims[i];
313  break;
314  }
315  }
316  if (rank > MAX_RANK) {
317  fprintf(stderr, "rank exceeds limit: %d\n", rank);
318  return ERROR_CODE;
319  }
320  dinfo->rank = tdim >= 0 ? rank -1 : rank; // do not count time dimension
321  // dim_name and dim_size
322  for (i=0; i<dinfo->rank; i++) {
323  // note: C and Fortran orders are opposite
324  if ( files[fid]->shared_mode ) {
325  MPI_Offset size_;
326  CHECK_PNC_ERROR( ncmpi_inq_dim(ncid, dimids[rank-i-1], name, &size_) )
327  size = (size_t)size_;
328  }
329  else
330  CHECK_ERROR( nc_inq_dim(ncid, dimids[rank-i-1], name, &size) )
331  if ( strlen(name) > File_HSHORT-1 ) {
332  fprintf(stderr, "Length of the dimension name (%s) is too long (should be < %d).\n", name, File_HSHORT);
333  return ERROR_CODE;
334  }
335  strncpy(dinfo->dim_name+i*File_HSHORT, name, File_HSHORT);
336  dinfo->dim_size[i] = size;
337  }
338 
339  dinfo->step = step;
340  if ( tdim >= 0 ) {
341  if ( files[fid]->shared_mode ) {
342  MPI_Offset idx[2];
343  MPI_Offset l;
344  // time_end
345  CHECK_PNC_ERROR( ncmpi_inq_dimname(ncid, tdim, name) )
346  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, name, &varid) )
347  idx[0] = step - 1;
348  CHECK_PNC_ERROR( ncmpi_get_var1_double_all(ncid, varid, idx, &(dinfo->time_end)) )
349  // time_start
350  strcat(name, "_bnds");
351  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, name, &varid) )
352  idx[1] = 0;
353  CHECK_PNC_ERROR( ncmpi_get_var1_double_all(ncid, varid, idx, &(dinfo->time_start)) )
354  // units
355  CHECK_PNC_ERROR( ncmpi_inq_attlen (ncid, varid, "units", &l) )
356  buf = (char*) malloc(l+1);
357  CHECK_PNC_ERROR( ncmpi_get_att_text(ncid, varid, "units", buf) )
358  for (i=0; i<MIN(File_HMID-1,l); i++)
359  dinfo->time_units[i] = buf[i];
360  dinfo->time_units[i] = '\0';
361  free(buf);
362  } else {
363  size_t idx[2];
364  size_t l;
365  // time_end
366  CHECK_ERROR( nc_inq_dimname(ncid, tdim, name) )
367  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) )
368  idx[0] = step - 1;
369  CHECK_ERROR( nc_get_var1_double(ncid, varid, idx, &(dinfo->time_end)) )
370  // time_start
371  strcat(name, "_bnds");
372  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) )
373  idx[1] = 0;
374  CHECK_ERROR( nc_get_var1_double(ncid, varid, idx, &(dinfo->time_start)) )
375  // units
376  CHECK_ERROR( nc_inq_attlen (ncid, varid, "units", &l) )
377  buf = (char*) malloc(l+1);
378  CHECK_ERROR( nc_get_att_text(ncid, varid, "units", buf) )
379  for (i=0; i<MIN(File_HMID-1,l); i++)
380  dinfo->time_units[i] = buf[i];
381  dinfo->time_units[i] = '\0';
382  free(buf);
383  }
384  }
385  ERROR_SUPPRESS = 0;
386 
387  return SUCCESS_CODE;
388 }
#define File_HMID
Definition: gtool_file.h:13
int32_t dim_size[MAX_RANK]
Definition: gtool_file.h:49
int32_t step
Definition: gtool_file.h:50
#define MAX_RANK
Definition: gtool_file.h:35
#define ERROR_CODE
Definition: gtool_file.h:29
char dim_name[File_HSHORT *MAX_RANK]
Definition: gtool_file.h:48
char description[File_HMID]
Definition: gtool_file.h:44
#define File_HSHORT
Definition: gtool_file.h:12
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define NCTYPE2TYPE(nctype, type)
Definition: gtool_netcdf.c:52
#define MIN(a, b)
Definition: gtool_netcdf.c:11
int32_t fid
Definition: gtool_file.h:54
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
real64_t time_end
Definition: gtool_file.h:52
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
char units[File_HSHORT]
Definition: gtool_file.h:45
char varname[File_HSHORT]
Definition: gtool_file.h:43
int32_t datatype
Definition: gtool_file.h:46
int32_t rank
Definition: gtool_file.h:47
Here is the caller graph for this function:

◆ file_read_data()

int32_t file_read_data ( void *  var,
datainfo_t dinfo,
int32_t  precision 
)

Definition at line 390 of file gtool_netcdf.c.

Referenced by gtool_file::filegetshape(), gtool_file::filereadvar1drealdp(), gtool_file::filereadvar2drealdp(), gtool_file::filereadvar2drealsp(), gtool_file::filereadvar3drealdp(), gtool_file::filereadvar3drealsp(), gtool_file::filereadvar4drealdp(), and gtool_file::filereadvar4drealsp().

393 {
394  int ncid, varid;
395  int rank;
396  size_t *start, *count;
397  int i;
398  int status;
399 
400  if ( files[dinfo->fid] == NULL ) return ALREADY_CLOSED_CODE;
401  ncid = files[dinfo->fid]->ncid;
402  CHECK_ERROR( nc_inq_varid(ncid, dinfo->varname, &varid) )
403 
404  CHECK_ERROR( nc_inq_varndims(ncid, varid, &rank) )
405  start = (size_t*) malloc(sizeof(size_t)*rank);
406  count = (size_t*) malloc(sizeof(size_t)*rank);
407  for (i=0; i<dinfo->rank; i++) {
408  // note: C and Fortran orders are opposite
409  start[rank -i-1] = 0;
410  count[rank -i-1] = dinfo->dim_size[i];
411  }
412  if (rank > dinfo->rank) { // have time dimension
413  start[0] = dinfo->step - 1;
414  count[0] = 1;
415  }
416  switch ( precision ) {
417  case 8:
418  status = nc_get_vara_double(ncid, varid, start, count, (double*)var);
419  free(start);
420  free(count);
421  CHECK_ERROR(status);
422  break;
423  case 4:
424  status = nc_get_vara_float(ncid, varid, start, count, (float*)var);
425  free(start);
426  free(count);
427  CHECK_ERROR(status);
428  break;
429  default:
430  free(start);
431  free(count);
432  fprintf(stderr, "unsupported data precision: %d\n", precision );
433  return ERROR_CODE;
434  }
435 
436  return SUCCESS_CODE;
437 }
int32_t dim_size[MAX_RANK]
Definition: gtool_file.h:49
int32_t step
Definition: gtool_file.h:50
#define ERROR_CODE
Definition: gtool_file.h:29
int32_t fid
Definition: gtool_file.h:54
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
char varname[File_HSHORT]
Definition: gtool_file.h:43
int32_t rank
Definition: gtool_file.h:47
Here is the caller graph for this function:

◆ file_read_data_par()

int32_t file_read_data_par ( void *  var,
datainfo_t dinfo,
MPI_Offset  ntypes,
MPI_Datatype  dtype,
MPI_Offset *  start,
MPI_Offset *  count 
)

Definition at line 439 of file gtool_netcdf.c.

Referenced by gtool_file::filegetshape(), gtool_file::filereadvar1drealdp(), gtool_file::filereadvar2drealdp(), gtool_file::filereadvar2drealsp(), gtool_file::filereadvar3drealdp(), gtool_file::filereadvar3drealsp(), gtool_file::filereadvar4drealdp(), and gtool_file::filereadvar4drealsp().

445 {
446  int ncid, varid, rank;
447 
448  if ( files[dinfo->fid] == NULL ) return ALREADY_CLOSED_CODE;
449  ncid = files[dinfo->fid]->ncid;
450  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, dinfo->varname, &varid) )
451 
452  CHECK_PNC_ERROR( ncmpi_inq_varndims(ncid, varid, &rank) )
453  if (rank > dinfo->rank) { // have time dimension
454  start[0] = dinfo->step - 1;
455  count[0] = 1;
456  } else {
457  start = start + 1;
458  count = count + 1;
459  }
460 
461  CHECK_PNC_ERROR( ncmpi_iget_vara(ncid, varid, start, count, var, ntypes, dtype, NULL) )
462 
463  return SUCCESS_CODE;
464 }
int32_t step
Definition: gtool_file.h:50
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
int32_t fid
Definition: gtool_file.h:54
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
char varname[File_HSHORT]
Definition: gtool_file.h:43
int32_t rank
Definition: gtool_file.h:47
Here is the caller graph for this function:

◆ file_get_global_attribute_text()

int32_t file_get_global_attribute_text ( int32_t  fid,
char *  key,
char *  value,
int32_t  len 
)

Definition at line 466 of file gtool_netcdf.c.

Referenced by gtool_file::filegetglobalattributetext().

470 {
471  int ncid;
472  size_t l;
473 
474  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
475  ncid = files[fid]->ncid;
476 
477  CHECK_ERROR( nc_inq_attlen(ncid, NC_GLOBAL, key, &l) )
478  if ( len < l+1 ) return ERROR_CODE;
479 
480  CHECK_ERROR( nc_get_att_text(ncid, NC_GLOBAL, key, value) )
481  value[l] = '\0';
482 
483  return SUCCESS_CODE;
484 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_get_global_attribute_int()

int32_t file_get_global_attribute_int ( int32_t  fid,
char *  key,
int32_t *  value,
size_t  len 
)

Referenced by gtool_file::filegetglobalattributetext().

Here is the caller graph for this function:

◆ file_get_global_attribute_float()

int32_t file_get_global_attribute_float ( int32_t  fid,
char *  key,
float value,
size_t  len 
)

Definition at line 504 of file gtool_netcdf.c.

Referenced by gtool_file::filegetglobalattributetext().

508 {
509  int ncid;
510  size_t l;
511 
512  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
513  ncid = files[fid]->ncid;
514 
515  CHECK_ERROR( nc_inq_attlen(ncid, NC_GLOBAL, key, &l) )
516  if ( len < l ) return ERROR_CODE;
517  CHECK_ERROR( nc_get_att_float(ncid, NC_GLOBAL, key, value) )
518 
519  return SUCCESS_CODE;
520 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_get_global_attribute_double()

int32_t file_get_global_attribute_double ( int32_t  fid,
char *  key,
double value,
size_t  len 
)

Definition at line 522 of file gtool_netcdf.c.

Referenced by gtool_file::filegetglobalattributetext().

526 {
527  int ncid;
528  size_t l;
529 
530  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
531  ncid = files[fid]->ncid;
532 
533  CHECK_ERROR( nc_inq_attlen(ncid, NC_GLOBAL, key, &l) )
534  if ( len < l ) return ERROR_CODE;
535  CHECK_ERROR( nc_get_att_double(ncid, NC_GLOBAL, key, value) )
536 
537  return SUCCESS_CODE;
538 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_set_global_attribute_text()

int32_t file_set_global_attribute_text ( int32_t  fid,
char *  key,
char *  value 
)

Definition at line 540 of file gtool_netcdf.c.

Referenced by gtool_file::filegetglobalattributetext().

543 {
544  int ncid;
545 
546  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
547  ncid = files[fid]->ncid;
548 
549 #ifdef NETCDF3
550  if (files[fid]->defmode == 0) {
551  CHECK_ERROR( nc_redef(ncid) )
552  files[fid]->defmode = 1;
553  }
554 #endif
555 
556  if ( files[fid]->shared_mode ) {
557  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, NC_GLOBAL, key, strlen(value), value) )
558  }
559  else {
560  CHECK_ERROR( nc_put_att_text(ncid, NC_GLOBAL, key, strlen(value), value) )
561  }
562 
563  return SUCCESS_CODE;
564 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_set_global_attribute_int()

int32_t file_set_global_attribute_int ( int32_t  fid,
char *  key,
int32_t *  value,
size_t  len 
)

Referenced by gtool_file::filegetglobalattributetext().

Here is the caller graph for this function:

◆ file_set_global_attribute_float()

int32_t file_set_global_attribute_float ( int32_t  fid,
char *  key,
float value,
size_t  len 
)

Definition at line 593 of file gtool_netcdf.c.

Referenced by gtool_file::filegetglobalattributetext().

597 {
598  int ncid;
599 
600  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
601  ncid = files[fid]->ncid;
602 
603 #ifdef NETCDF3
604  if (files[fid]->defmode == 0) {
605  CHECK_ERROR( nc_redef(ncid) )
606  files[fid]->defmode = 1;
607  }
608 #endif
609 
610  if ( files[fid]->shared_mode ) {
611  CHECK_PNC_ERROR( ncmpi_put_att_float(ncid, NC_GLOBAL, key, NC_FLOAT, len, value) )
612  }
613  else {
614  CHECK_ERROR( nc_put_att_float(ncid, NC_GLOBAL, key, NC_FLOAT, len, value) )
615  }
616 
617  return SUCCESS_CODE;
618 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_set_global_attribute_double()

int32_t file_set_global_attribute_double ( int32_t  fid,
char *  key,
double value,
size_t  len 
)

Definition at line 620 of file gtool_netcdf.c.

Referenced by gtool_file::filegetglobalattributetext().

624 {
625  int ncid;
626 
627  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
628  ncid = files[fid]->ncid;
629 
630 #ifdef NETCDF3
631  if (files[fid]->defmode == 0) {
632  CHECK_ERROR( nc_redef(ncid) )
633  files[fid]->defmode = 1;
634  }
635 #endif
636 
637  if ( files[fid]->shared_mode ) {
638  CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, NC_GLOBAL, key, NC_DOUBLE, len, value) )
639  }
640  else {
641  CHECK_ERROR( nc_put_att_double(ncid, NC_GLOBAL, key, NC_DOUBLE, len, value) )
642  }
643 
644  return SUCCESS_CODE;
645 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_set_tunits()

int32_t file_set_tunits ( int32_t  fid,
char *  time_units 
)

Definition at line 647 of file gtool_netcdf.c.

Referenced by gtool_file::filecreate().

649 {
650  strcpy(files[fid]->time_units, time_units);
651 
652  return SUCCESS_CODE;
653 }
#define SUCCESS_CODE
Definition: gtool_file.h:30
Here is the caller graph for this function:

◆ file_set_tattr()

int32_t file_set_tattr ( int32_t  fid,
char *  vname,
char *  key,
char *  val 
)

Definition at line 655 of file gtool_netcdf.c.

Referenced by gtool_file::filesettattr().

659 {
660  int ncid;
661  int varid;
662  int attid;
663 
664  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
665  ncid = files[fid]->ncid;
666 
667  if ( files[fid]->shared_mode ) {
668  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, vname, &varid) )
669 
670  if ( ncmpi_inq_attid(ncid, varid, key, &attid) == NC_NOERR ) // check if existed
671  return ALREADY_EXISTED_CODE;
672 
673  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, key, strlen(val), val) )
674  }
675  else {
676  CHECK_ERROR( nc_inq_varid(ncid, vname, &varid) )
677 
678  if ( nc_inq_attid(ncid, varid, key, &attid) == NC_NOERR ) // check if existed
679  return ALREADY_EXISTED_CODE;
680 
681 #ifdef NETCDF3
682  if (files[fid]->defmode == 0) {
683  CHECK_ERROR( nc_redef(ncid) )
684  files[fid]->defmode = 1;
685  }
686 #endif
687 
688  CHECK_ERROR( nc_put_att_text(ncid, varid, key, strlen(val), val) )
689  }
690 
691  return SUCCESS_CODE;
692 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:32
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define ncmpi_inq_attid(a, b, c, d)
Definition: gtool_netcdf.c:47
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_put_axis()

int32_t file_put_axis ( int32_t  fid,
char *  name,
char *  desc,
char *  units,
char *  dim_name,
int32_t  dtype,
void *  val,
int32_t  size,
int32_t  precision 
)

Definition at line 694 of file gtool_netcdf.c.

Referenced by gtool_file::fileopen().

703 {
704  int ncid, dimid, varid;
705  nc_type xtype = -1;
706 
707  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
708  ncid = files[fid]->ncid;
709 
710  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
711  return ALREADY_EXISTED_CODE;
712 
713 #ifdef NETCDF3
714  if (files[fid]->defmode == 0) {
715  CHECK_ERROR( nc_redef(ncid) )
716  files[fid]->defmode = 1;
717  }
718 #endif
719 
720  if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR ) // check if existed
721  CHECK_ERROR( nc_def_dim(ncid, dim_name, size, &dimid) )
722 
723  TYPE2NCTYPE(dtype, xtype);
724  CHECK_ERROR( nc_def_var(ncid, name, xtype, 1, &dimid, &varid) )
725  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
726  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) )
727 
728 #ifdef NETCDF3
729  CHECK_ERROR( nc_enddef(ncid) )
730  files[fid]->defmode = 0;
731 #endif
732 
733  switch ( precision ) {
734  case 8:
735  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) )
736  break;
737  case 4:
738  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) )
739  break;
740  default:
741  fprintf(stderr, "unsupported data precision: %d\n", precision);
742  return ERROR_CODE;
743  }
744 
745  return SUCCESS_CODE;
746 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:32
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:70
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_def_axis()

int32_t file_def_axis ( int32_t  fid,
char *  name,
char *  desc,
char *  units,
char *  dim_name,
int32_t  dtype,
int32_t  dim_size 
)

Definition at line 748 of file gtool_netcdf.c.

Referenced by gtool_file::filedefaxis().

755 {
756  int ncid, dimid, varid;
757  nc_type xtype = -1;
758 
759  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
760  ncid = files[fid]->ncid;
761 
762  if ( files[fid]->shared_mode ) {
763  if ( ncmpi_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
764  return ALREADY_EXISTED_CODE;
765 
766  if ( ncmpi_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR ) // check if existed
767  CHECK_PNC_ERROR( ncmpi_def_dim(ncid, dim_name, dim_size, &dimid) )
768 
769  TYPE2NCTYPE(dtype, xtype);
770  CHECK_PNC_ERROR( ncmpi_def_var(ncid, name, xtype, 1, &dimid, &varid) )
771  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
772  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "units", strlen(units), units) )
773  }
774  else {
775  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
776  return ALREADY_EXISTED_CODE;
777 
778 #ifdef NETCDF3
779  if (files[fid]->defmode == 0) {
780  CHECK_ERROR( nc_redef(ncid) )
781  files[fid]->defmode = 1;
782  }
783 #endif
784 
785  if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR ) // check if existed
786  CHECK_ERROR( nc_def_dim(ncid, dim_name, dim_size, &dimid) )
787 
788  TYPE2NCTYPE(dtype, xtype);
789  CHECK_ERROR( nc_def_var(ncid, name, xtype, 1, &dimid, &varid) )
790  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
791  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) )
792  }
793 
794  return SUCCESS_CODE;
795 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:32
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:70
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define ncmpi_inq_dimid(a, b, c)
Definition: gtool_netcdf.c:49
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_write_axis()

int32_t file_write_axis ( int32_t  fid,
char *  name,
void *  val,
int32_t  precision,
MPI_Offset *  start,
MPI_Offset *  count 
)

Definition at line 797 of file gtool_netcdf.c.

Referenced by gtool_file::filedefaxis().

803 {
804  int ncid, varid;
805 
806  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
807  ncid = files[fid]->ncid;
808 
809  if ( files[fid]->shared_mode )
810  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, name, &varid) )
811  else
812  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) )
813 
814 #ifdef NETCDF3
815  if ( (!files[fid]->shared_mode) && files[fid]->defmode == 1) {
816  CHECK_ERROR( nc_enddef(ncid) )
817  files[fid]->defmode = 0;
818  }
819 #endif
820 
821  switch ( precision ) {
822  case 8:
823  if ( files[fid]->shared_mode )
824  CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start, count, val, NULL) )
825  else
826  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) )
827  break;
828  case 4:
829  if ( files[fid]->shared_mode )
830  CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start, count, val, NULL) )
831  else
832  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) )
833  break;
834  default:
835  fprintf(stderr, "unsupported data precision: %d\n", precision);
836  return ERROR_CODE;
837  }
838 
839  return SUCCESS_CODE;
840 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_put_associated_coordinates()

int32_t file_put_associated_coordinates ( int32_t  fid,
char *  name,
char *  desc,
char *  units,
char **  dim_names,
int32_t  ndims,
int32_t  dtype,
void *  val,
int32_t  precision 
)

Definition at line 842 of file gtool_netcdf.c.

Referenced by gtool_file::filedefaxis().

851 {
852  int ncid, *dimids, varid;
853  nc_type xtype = -1;
854  int i;
855 
856  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
857  ncid = files[fid]->ncid;
858 
859  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
860  return ALREADY_EXISTED_CODE;
861 
862 #ifdef NETCDF3
863  if (files[fid]->defmode == 0) {
864  CHECK_ERROR( nc_redef(ncid) )
865  files[fid]->defmode = 1;
866  }
867 #endif
868 
869  dimids = malloc(sizeof(int)*ndims);
870  for (i=0; i<ndims; i++)
871  CHECK_ERROR( nc_inq_dimid(ncid, dim_names[i], dimids+ndims-i-1) )
872 
873  TYPE2NCTYPE(dtype, xtype);
874 
875  CHECK_ERROR( nc_def_var(ncid, name, xtype, ndims, dimids, &varid) )
876  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
877  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) )
878  free(dimids);
879 
880 #ifdef NETCDF3
881  CHECK_ERROR( nc_enddef(ncid) )
882  files[fid]->defmode = 0;
883 #endif
884 
885  switch ( precision ) {
886  case 8:
887  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) )
888  break;
889  case 4:
890  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) )
891  break;
892  default:
893  fprintf(stderr, "unsupported data precision: %d\n", precision);
894  return ERROR_CODE;
895  }
896 
897  return SUCCESS_CODE;
898 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:32
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:70
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_def_associated_coordinates()

int32_t file_def_associated_coordinates ( int32_t  fid,
char *  name,
char *  desc,
char *  units,
char **  dim_names,
int32_t  ndims,
int32_t  dtype 
)

Definition at line 900 of file gtool_netcdf.c.

Referenced by gtool_file::filedefassociatedcoordinates().

907 {
908  int ncid, *dimids, varid;
909  nc_type xtype = -1;
910  int i;
911 
912  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
913  ncid = files[fid]->ncid;
914 
915  if ( files[fid]->shared_mode ) {
916  if ( ncmpi_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
917  return ALREADY_EXISTED_CODE;
918 
919  dimids = malloc(sizeof(int)*ndims);
920  for (i=0; i<ndims; i++)
921  CHECK_PNC_ERROR( ncmpi_inq_dimid(ncid, dim_names[i], dimids+ndims-i-1) )
922 
923  TYPE2NCTYPE(dtype, xtype);
924 
925  CHECK_PNC_ERROR( ncmpi_def_var(ncid, name, xtype, ndims, dimids, &varid) )
926  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
927  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "units", strlen(units), units) )
928  free(dimids);
929  }
930  else {
931  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
932  return ALREADY_EXISTED_CODE;
933 
934 #ifdef NETCDF3
935  if (files[fid]->defmode == 0) {
936  CHECK_ERROR( nc_redef(ncid) )
937  files[fid]->defmode = 1;
938  }
939 #endif
940 
941  dimids = malloc(sizeof(int)*ndims);
942  for (i=0; i<ndims; i++)
943  CHECK_ERROR( nc_inq_dimid(ncid, dim_names[i], dimids+ndims-i-1) )
944 
945  TYPE2NCTYPE(dtype, xtype);
946 
947  CHECK_ERROR( nc_def_var(ncid, name, xtype, ndims, dimids, &varid) )
948  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
949  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) )
950  free(dimids);
951  }
952 
953  return SUCCESS_CODE;
954 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:32
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:70
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define ncmpi_inq_dimid(a, b, c)
Definition: gtool_netcdf.c:49
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_write_associated_coordinates()

int32_t file_write_associated_coordinates ( int32_t  fid,
char *  name,
void *  val,
int32_t  precision,
MPI_Offset *  start,
MPI_Offset *  count 
)

Definition at line 956 of file gtool_netcdf.c.

Referenced by gtool_file::filedefassociatedcoordinates().

962 {
963  int ncid, varid;
964 
965  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
966  ncid = files[fid]->ncid;
967 
968  if ( files[fid]->shared_mode )
969  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, name, &varid) )
970  else
971  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) )
972 
973 #ifdef NETCDF3
974  if ( (!files[fid]->shared_mode) && files[fid]->defmode == 1) {
975  CHECK_ERROR( nc_enddef(ncid) )
976  files[fid]->defmode = 0;
977  }
978 #endif
979 
980  switch ( precision ) {
981  case 8:
982  if ( files[fid]->shared_mode )
983  CHECK_PNC_ERROR( ncmpi_iput_vara_double(ncid, varid, start, count, (double*)val, NULL) )
984  else
985  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) )
986  break;
987  case 4:
988  if ( files[fid]->shared_mode )
989  CHECK_PNC_ERROR( ncmpi_iput_vara_float(ncid, varid, start, count, (float*)val, NULL) )
990  else
991  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) )
992  break;
993  default:
994  fprintf(stderr, "unsupported data precision: %d\n", precision);
995  return ERROR_CODE;
996  }
997 
998  return SUCCESS_CODE;
999 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_add_variable()

int32_t file_add_variable ( int32_t *  vid,
int32_t  fid,
char *  varname,
char *  desc,
char *  units,
char **  dims,
int32_t  ndims,
int32_t  dtype,
real64_t  tint,
int32_t  tavg 
)

Definition at line 1001 of file gtool_netcdf.c.

Referenced by gtool_file::filedefassociatedcoordinates(), and gtool_file::filedefinevariable().

1011 {
1012  int ncid, varid, acid, *acdimids;
1013  int dimids[NC_MAX_DIMS], dimid;
1014  char tname[File_HSHORT+1];
1015  int tdimid, tvarid;
1016  nc_type xtype = -1;
1017  char buf[File_HMID+1];
1018  int i, j, k, n, m, err;
1019  int nndims;
1020  size_t size;
1021  double rmiss = RMISS;
1022  char coord[File_HMID+1];
1023  int has_assoc;
1024  int new;
1025 
1026  if ( nvar >= VAR_MAX ) {
1027  fprintf(stderr, "exceed max number of variable limit\n");
1028  return ERROR_CODE;
1029  }
1030 
1031  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1032  ncid = files[fid]->ncid;
1033 
1034  vars[nvar] = (varinfo_t*) malloc(sizeof(varinfo_t));
1035  vars[nvar]->ncid = ncid;
1036  vars[nvar]->t = NULL;
1037  vars[nvar]->start = NULL;
1038  vars[nvar]->count = NULL;
1039 
1040 #if defined(NETCDF3) || defined(PNETCDF)
1041  if (files[fid]->defmode == 0) {
1042  if ( files[fid]->shared_mode )
1043  CHECK_PNC_ERROR( ncmpi_redef(ncid) )
1044  else
1045  CHECK_ERROR( nc_redef(ncid) )
1046  files[fid]->defmode = 1;
1047  }
1048 #endif
1049 
1050  // get time variable
1051  if ( tint > 0.0 ) {
1052  for ( i=0; i<nt; i++ ) {
1053  if ( tdims[i] != NULL && // still opened
1054  tdims[i]->ncid == ncid && // same file
1055  tdims[i]->tint == tint ) { // same time interval
1056  vars[nvar]->t = tdims[i];
1057  break;
1058  }
1059  }
1060  if ( vars[nvar]->t == NULL ) {
1061  tdims[nt] = (tdim_t*) malloc(sizeof(tdim_t));
1062  tdims[nt]->ncid = ncid;
1063  tdims[nt]->count = -1;
1064  tdims[nt]->tint = tint;
1065  tdims[nt]->tval = (double*) malloc(sizeof(double)*NTMAX);
1066  // generate name
1067  if ( nt == 0 )
1068  strcpy(tname, "time");
1069  else
1070  sprintf(tname, "time%d", nt);
1071  strcpy(tdims[nt]->name, tname);
1072  // define time dimension and variable
1073  if ( files[fid]->shared_mode ) {
1074  CHECK_PNC_ERROR( ncmpi_def_dim(ncid, tname, 0, &tdimid) )
1075  tdims[nt]->dimid = tdimid;
1076  CHECK_PNC_ERROR( ncmpi_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1077  tdims[nt]->varid = tvarid;
1078  strcpy(buf, "time");
1079  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid, "long_name", strlen(buf), buf) )
1080  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid, "units", strlen(files[fid]->time_units), files[fid]->time_units) )
1081  // define boundary variable
1082  if ( ncmpi_inq_dimid(ncid, "nv", &(dimids[1])) != NC_NOERR ) // first called
1083  CHECK_PNC_ERROR( ncmpi_def_dim(ncid, "nv", 2, &(dimids[1])) )
1084  sprintf(buf, "%s_bnds", tname);
1085  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid, "bounds", strlen(buf), buf) )
1086  dimids[0] = tdimid;
1087  CHECK_PNC_ERROR( ncmpi_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1088  tdims[nt]->bndsid = tvarid;
1089  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, tvarid, "units", strlen(files[fid]->time_units), files[fid]->time_units) )
1090  }
1091  else {
1092  CHECK_ERROR( nc_def_dim(ncid, tname, 0, &tdimid) )
1093  tdims[nt]->dimid = tdimid;
1094  CHECK_ERROR( nc_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) )
1095  tdims[nt]->varid = tvarid;
1096  strcpy(buf, "time");
1097  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "long_name", strlen(buf), buf) )
1098  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "units", strlen(files[fid]->time_units), files[fid]->time_units) )
1099  // define boundary variable
1100  if ( nc_inq_dimid(ncid, "nv", &(dimids[1])) != NC_NOERR ) // first called
1101  CHECK_ERROR( nc_def_dim(ncid, "nv", 2, &(dimids[1])) )
1102  sprintf(buf, "%s_bnds", tname);
1103  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "bounds", strlen(buf), buf) )
1104  dimids[0] = tdimid;
1105  CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) )
1106  tdims[nt]->bndsid = tvarid;
1107  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "units", strlen(files[fid]->time_units), files[fid]->time_units) )
1108  }
1109 
1110  vars[nvar]->t = tdims[nt];
1111  nt++;
1112  }
1113  }
1114 
1115  // get dimension IDs
1116  // note: C and Fortran order are opposite
1117  n = ndims;
1118  if ( tint > 0.0 ) { // add time dimension
1119  dimids[0] = vars[nvar]->t->dimid;
1120  ndims++;
1121  }
1122  for (i=ndims-n; i<ndims; i++) dimids[i] = -1;
1123 
1124  has_assoc = 0;
1125  nndims = 0;
1126  for (i=0; i<n; i++) {
1127  //printf("%d %s\n", i, dims[i]);
1128  if ( files[fid]->shared_mode )
1129  err = ncmpi_inq_dimid(ncid, dims[i], &dimid);
1130  else
1131  err = nc_inq_dimid(ncid, dims[i], &dimid);
1132  if ( err == NC_NOERR ) {
1133  //printf("not assoc\n");
1134  new = 1;
1135  for (k=0; k<nndims; k++) {
1136  if (dimid == dimids[k]) {
1137  new = 0;
1138  break;
1139  }
1140  }
1141  if (new) {
1142  dimids[ndims-(++nndims)] = dimid;
1143  }
1144  } else {
1145  //printf("assoc\n");
1146  if ( files[fid]->shared_mode ) {
1147  CHECK_PNC_ERROR( ncmpi_inq_varid(ncid, dims[i], &acid) )
1148  CHECK_PNC_ERROR( ncmpi_inq_varndims(ncid, acid, &m) )
1149  acdimids = (int*) malloc((sizeof(int)*m));
1150  CHECK_PNC_ERROR( ncmpi_inq_vardimid(ncid, acid, acdimids) )
1151  }
1152  else {
1153  CHECK_ERROR( nc_inq_varid(ncid, dims[i], &acid) )
1154  CHECK_ERROR( nc_inq_varndims(ncid, acid, &m) )
1155  acdimids = (int*) malloc((sizeof(int)*m));
1156  CHECK_ERROR( nc_inq_vardimid(ncid, acid, acdimids) )
1157  }
1158  for (j=m-1; j>=0; j--) {
1159  new = 1;
1160  for (k=0; k<ndims; k++) {
1161  if (acdimids[j] == dimids[k]) {
1162  new = 0;
1163  break;
1164  }
1165  }
1166  if (new) {
1167  if ( nndims >= ndims ) {
1168  fprintf(stderr, "Error: invalid associated coordinates\n");
1169  return ERROR_CODE;
1170  }
1171  dimids[ndims-(++nndims)] = acdimids[j];
1172  //nc_inq_dimname(ncid, acdimids[j], tname);
1173  //printf("add %s\n", tname);
1174  }
1175  }
1176  free(acdimids);
1177  has_assoc = 1;
1178  }
1179  }
1180  if (nndims != n) {
1181  fprintf(stderr, "Error: invalid associated coordinates: %d %d\n", ndims, nndims);
1182  return ERROR_CODE;
1183  }
1184 
1185  TYPE2NCTYPE(dtype, xtype);
1186  if ( files[fid]->shared_mode ) {
1187  CHECK_PNC_ERROR( ncmpi_def_var(ncid, varname, xtype, ndims, dimids, &varid) )
1188  // put variable attribute
1189  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
1190  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "units", strlen(units), units) )
1191 // CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid, _FillValue, xtype, 1, &rmiss) )
1192  CHECK_PNC_ERROR( ncmpi_put_att_double(ncid, varid, "missing_value", xtype, 1, &rmiss) )
1193  }
1194  else {
1195  CHECK_ERROR( nc_def_var(ncid, varname, xtype, ndims, dimids, &varid) )
1196  // put variable attribute
1197  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) )
1198  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) )
1199  CHECK_ERROR( nc_put_att_double(ncid, varid, _FillValue, xtype, 1, &rmiss) )
1200  CHECK_ERROR( nc_put_att_double(ncid, varid, "missing_value", xtype, 1, &rmiss) )
1201  }
1202  if ( has_assoc ) {
1203  strcpy(coord, dims[0]);
1204  for(i=1; i<n; i++) {
1205  if (strlen(coord)+strlen(dims[i])+1 < File_HMID) {
1206  strcat(coord, " ");
1207  strcat(coord, dims[i]);
1208  }
1209  }
1210  if ( ndims > n && strlen(coord)+6 < File_HMID) {
1211  strcat(coord, " ");
1212  strcat(coord, vars[nvar]->t->name);
1213  }
1214  if ( files[fid]->shared_mode )
1215  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "coordinates", strlen(coord), coord) )
1216  else
1217  CHECK_ERROR( nc_put_att_text(ncid, varid, "coordinates", strlen(coord), coord) )
1218  }
1219 
1220 
1221  if ( tavg ) {
1222  sprintf(buf, "%s: mean", vars[nvar]->t->name);
1223  if ( files[fid]->shared_mode )
1224  CHECK_PNC_ERROR( ncmpi_put_att_text(ncid, varid, "cell_methods", strlen(buf), buf) )
1225  else
1226  CHECK_ERROR( nc_put_att_text(ncid, varid, "cell_methods", strlen(buf), buf) )
1227  }
1228 
1229  // set start and count
1230  vars[nvar]->start = (size_t*) malloc(sizeof(size_t)*ndims);
1231  vars[nvar]->count = (size_t*) malloc(sizeof(size_t)*ndims);
1232  for ( i=0; i<ndims; i++ ) {
1233  if ( files[fid]->shared_mode ) {
1234  MPI_Offset dimlen;
1235  CHECK_PNC_ERROR( ncmpi_inq_dimlen(ncid, dimids[i], &dimlen) )
1236  size = (size_t) dimlen;
1237  }
1238  else
1239  CHECK_ERROR( nc_inq_dimlen(ncid, dimids[i], &size) )
1240  vars[nvar]->count[i] = size;
1241  vars[nvar]->start[i] = 0;
1242  }
1243  if ( tint > 0.0 ) vars[nvar]->count[0] = 1;
1244 
1245 #ifndef NETCDF3
1246  // set chunk size and deflate level (NetCDF-4 only)
1247  if ( ! files[fid]->shared_mode && files[fid]->deflate_level > 0 ) {
1248  CHECK_ERROR( nc_def_var_chunking(ncid, varid, NC_CHUNKED, vars[nvar]->count) )
1249  CHECK_ERROR( nc_def_var_deflate(ncid, varid, 0, 1, files[fid]->deflate_level) )
1250  }
1251 #endif
1252 
1253  vars[nvar]->varid = varid;
1254  *vid = nvar;
1255  nvar++;
1256 
1257  return SUCCESS_CODE;
1258 }
#define File_HMID
Definition: gtool_file.h:13
int dimid
Definition: gtool_netcdf.c:101
real(dp), parameter, public rmiss
Definition: gtool_file.f90:150
#define ERROR_CODE
Definition: gtool_file.h:29
#define NTMAX
Definition: gtool_netcdf.c:9
int count
Definition: gtool_netcdf.c:104
tdim_t * t
Definition: gtool_netcdf.c:114
#define File_HSHORT
Definition: gtool_file.h:12
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
int ncid
Definition: gtool_netcdf.c:100
real64_t tint
Definition: gtool_netcdf.c:106
int bndsid
Definition: gtool_netcdf.c:103
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:70
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define ncmpi_inq_dimid(a, b, c)
Definition: gtool_netcdf.c:49
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define VAR_MAX
Definition: gtool_netcdf.c:122
real64_t * tval
Definition: gtool_netcdf.c:107
size_t * start
Definition: gtool_netcdf.c:115
#define ncmpi_inq_varid(a, b, c)
Definition: gtool_netcdf.c:48
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
#define RMISS
Definition: gtool_netcdf.c:7
size_t * count
Definition: gtool_netcdf.c:116
int varid
Definition: gtool_netcdf.c:102
Here is the caller graph for this function:

◆ file_write_data()

int32_t file_write_data ( int32_t  fid,
int32_t  vid,
void *  var,
real64_t  t_start,
real64_t  t_end,
int32_t  precision,
MPI_Offset *  start,
MPI_Offset *  count 
)

Definition at line 1323 of file gtool_netcdf.c.

Referenced by gtool_file::filewrite1drealsp().

1331 {
1332  int ncid, varid;
1333  if ( vars[vid] == NULL ) return ALREADY_CLOSED_CODE;
1334  ncid = vars[vid]->ncid;
1335 
1336 #ifdef NETCDF3
1337  if ( files[fid]->defmode == 1 && ! files[fid]->shared_mode ) {
1338  CHECK_ERROR( nc_enddef(ncid) )
1339  files[fid]->defmode = 0;
1340  }
1341 #endif
1342 
1343  varid = vars[vid]->varid;
1344  if ( vars[vid]->t != NULL ) { // have time dimension
1345  if ( vars[vid]->t->count < 0 || // first time
1346  t_end > vars[vid]->t->t + TEPS ) { // time goes next step
1347  vars[vid]->t->count += 1;
1348  vars[vid]->t->t = t_end;
1349  if ( vars[vid]->t->count > NTMAX-1 ) {
1350  fprintf(stderr, "time count exceeds the max limit (%d)\n", NTMAX);
1351  return ERROR_CODE;
1352  }
1353  vars[vid]->t->tval[vars[vid]->t->count] = t_end;
1354  if ( files[fid]->shared_mode ) { // write a new value to variable time
1355  MPI_Offset index[2];
1356  index[0] = (MPI_Offset) vars[vid]->t->count;
1357  CHECK_PNC_ERROR( ncmpi_put_var1_double_all(ncid, vars[vid]->t->varid, index, &t_end) )
1358  index[1] = 0;
1359  CHECK_PNC_ERROR( ncmpi_put_var1_double_all(ncid, vars[vid]->t->bndsid, index, &t_start ) )
1360  index[1] = 1;
1361  CHECK_PNC_ERROR( ncmpi_put_var1_double_all(ncid, vars[vid]->t->bndsid, index, &t_end ) )
1362  } else {
1363  size_t index[2];
1364  index[0] = vars[vid]->t->count;
1365  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->varid, index, &t_end) )
1366  index[1] = 0;
1367  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_start) )
1368  index[1] = 1;
1369  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_end) )
1370  }
1371  vars[vid]->start[0] = vars[vid]->t->count;
1372  } else {
1373  size_t nt = vars[vid]->t->count + 1;
1374  int flag, n;
1375  flag = 1;
1376  for(n=nt-1;n>=0;n--) {
1377  if ( fabs(vars[vid]->t->tval[n]-t_end) < TEPS ) {
1378  vars[vid]->start[0] = n;
1379  flag = 0;
1380  break;
1381  }
1382  }
1383  if ( flag ) {
1384  fprintf(stderr, "cannot find time: %f\n", t_end);
1385  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);
1386  fprintf(stderr, " time is: ");
1387  for (n=0;n<nt;n++) fprintf(stderr, "%f, ", vars[vid]->t->tval[n]);
1388  fprintf(stderr, "\n");
1389  return ERROR_CODE;
1390  }
1391  }
1392  if ( files[fid]->shared_mode ) {
1393  // add time dimension to start[0] and count[0]
1394  int i, ndims;
1395  CHECK_PNC_ERROR( ncmpi_inq_varndims(ncid, varid, &ndims) )
1396  for (i=ndims-1; i>0; i--) {
1397  start[i] = start[i-1];
1398  count[i] = count[i-1];
1399  }
1400  start[0] = vars[vid]->start[0]; // start along the time dimension
1401  count[0] = vars[vid]->count[0];
1402  }
1403  }
1404 
1405  switch (precision) {
1406  case 8:
1407  if ( files[fid]->shared_mode )
1408  CHECK_PNC_ERROR( ncmpi_bput_vara_double(ncid, varid, start, count, (double*)var, NULL) )
1409  else
1410  CHECK_ERROR( nc_put_vara_double(ncid, varid, vars[vid]->start, vars[vid]->count, (double*)var) )
1411  break;
1412  case 4:
1413  if ( files[fid]->shared_mode )
1414  CHECK_PNC_ERROR( ncmpi_bput_vara_float(ncid, varid, start, count, (float*)var, NULL) )
1415  else
1416  CHECK_ERROR( nc_put_vara_float(ncid, varid, vars[vid]->start, vars[vid]->count, (float*)var) )
1417  break;
1418  default:
1419  fprintf(stderr, "unsupported data precision: %d\n", precision);
1420  return ERROR_CODE;
1421  }
1422 
1423  return SUCCESS_CODE;
1424 }
#define ERROR_CODE
Definition: gtool_file.h:29
#define NTMAX
Definition: gtool_netcdf.c:9
int count
Definition: gtool_netcdf.c:104
tdim_t * t
Definition: gtool_netcdf.c:114
#define TEPS
Definition: gtool_netcdf.c:8
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
real64_t * tval
Definition: gtool_netcdf.c:107
size_t * start
Definition: gtool_netcdf.c:115
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
real64_t t
Definition: gtool_netcdf.c:105
size_t * count
Definition: gtool_netcdf.c:116
Here is the caller graph for this function:

◆ file_enddef()

int32_t file_enddef ( int32_t  fid)

Definition at line 1260 of file gtool_netcdf.c.

Referenced by file_enddef_(), and gtool_file::fileenddef().

1261 {
1262  int ncid;
1263 
1264  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1265  ncid = files[fid]->ncid;
1266 
1267 #if defined(NETCDF3) || defined(PNETCDF)
1268  if (files[fid]->defmode == 1) {
1269  if ( files[fid]->shared_mode )
1270  CHECK_PNC_ERROR( ncmpi_enddef(ncid) )
1271  else
1272  CHECK_ERROR( nc_enddef(ncid) )
1273  files[fid]->defmode = 0;
1274  }
1275 #endif
1276 
1277  return SUCCESS_CODE;
1278 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_attach_buffer()

int32_t file_attach_buffer ( int32_t  fid,
int32_t  buf_amount 
)

Definition at line 1280 of file gtool_netcdf.c.

Referenced by file_attach_buffer_(), and gtool_file::fileattachbuffer().

1282 {
1283  int ncid;
1284  MPI_Offset buf_amount_ = buf_amount;
1285 
1286  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1287  ncid = files[fid]->ncid;
1288 
1289  if ( files[fid]->shared_mode )
1290  CHECK_PNC_ERROR( ncmpi_buffer_attach(ncid, buf_amount_) )
1291 
1292  return SUCCESS_CODE;
1293 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
Here is the caller graph for this function:

◆ file_detach_buffer()

int32_t file_detach_buffer ( int32_t  fid)

Definition at line 1295 of file gtool_netcdf.c.

Referenced by file_detach_buffer_(), and gtool_file::filedetachbuffer().

1296 {
1297  int ncid;
1298 
1299  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1300  ncid = files[fid]->ncid;
1301 
1302  if ( files[fid]->shared_mode )
1303  CHECK_PNC_ERROR( ncmpi_buffer_detach(ncid) )
1304 
1305  return SUCCESS_CODE;
1306 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
Here is the caller graph for this function:

◆ file_flush()

int32_t file_flush ( int32_t  fid)

Definition at line 1308 of file gtool_netcdf.c.

Referenced by file_flush_(), and gtool_file::fileflush().

1309 {
1310  int ncid;
1311 
1312  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1313  ncid = files[fid]->ncid;
1314 
1315  if ( files[fid]->shared_mode )
1316  CHECK_PNC_ERROR( ncmpi_wait_all(ncid, NC_REQ_ALL, NULL, NULL) )
1317  else
1318  CHECK_ERROR( nc_sync(ncid) )
1319 
1320  return SUCCESS_CODE;
1321 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function:

◆ file_close()

int32_t file_close ( int32_t  fid)

Definition at line 1426 of file gtool_netcdf.c.

Referenced by file_close_(), and gtool_file::fileclose().

1427 {
1428  int ncid;
1429  int i;
1430 
1431  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1432  ncid = files[fid]->ncid;
1433 
1434  for (i=0; i<nvar; i++) {
1435  if ( vars[i] != NULL && vars[i]->ncid == ncid ) {
1436  free( vars[i]->start );
1437  free( vars[i]->count );
1438  free( vars[i] );
1439  vars[i] = NULL;
1440  }
1441  }
1442 
1443  for (i=0; i<nt; i++) {
1444  if ( tdims[i] != NULL && tdims[i]->ncid == ncid ) {
1445  free( tdims[i]->tval );
1446  free( tdims[i] );
1447  tdims[i] = NULL;
1448  }
1449  }
1450 
1451  if ( files[fid]->shared_mode )
1452  CHECK_PNC_ERROR( ncmpi_close(ncid) )
1453  else
1454  CHECK_ERROR( nc_close(ncid) )
1455 
1456  free( files[fid] );
1457  files[fid] = NULL;
1458 
1459  return SUCCESS_CODE;
1460 }
#define CHECK_PNC_ERROR(func)
Definition: gtool_netcdf.c:41
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:31
#define SUCCESS_CODE
Definition: gtool_file.h:30
#define CHECK_ERROR(func)
Definition: gtool_netcdf.c:15
Here is the caller graph for this function: