SCALE-RM
Classes | Macros | Functions
gtool_netcdf.c File Reference
#include "netcdf.h"
#include "gtool_file.h"
Include dependency graph for gtool_netcdf.c:

Go to the source code of this file.

Classes

struct  fileinfo_t
 
struct  tdim_t
 
struct  varinfo_t
 

Macros

#define RMISS   -9.9999e+30
 
#define TEPS   1e-6
 
#define CHECK_ERROR(status)
 
#define NCTYPE2TYPE(nctype, type)
 
#define TYPE2NCTYPE(type, nctype)
 
#define DEFAULT_DEFLATE_LEVEL   2
 
#define FILE_MAX   512
 
#define VAR_MAX   40960
 

Functions

int32_t file_open (int32_t *fid, char *fname, int32_t mode)
 
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_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, int *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, int *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)
 
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)
 
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)
 
int32_t file_enddef (int32_t fid)
 
int32_t file_write_var (int32_t vid, void *var, real64_t t_start, real64_t t_end, int32_t precision)
 
int32_t file_close (int32_t fid)
 

Macro Definition Documentation

◆ RMISS

#define RMISS   -9.9999e+30

Definition at line 4 of file gtool_netcdf.c.

◆ TEPS

#define TEPS   1e-6

Definition at line 5 of file gtool_netcdf.c.

Referenced by file_write_data(), and file_write_var().

◆ CHECK_ERROR

#define CHECK_ERROR (   status)
Value:
{ \
if (status != NC_NOERR) { \
if ( ! ERROR_SUPPRESS ) { \
fprintf(stderr, "Error: at l%d in %s\n", __LINE__, __FILE__); \
fprintf(stderr, " %s\n", nc_strerror(status)); \
} \
return ERROR_CODE; \
} \
}
#define ERROR_CODE
Definition: gtool_file.h:28

Definition at line 9 of file gtool_netcdf.c.

Referenced by file_enddef(), file_set_tattr(), file_write_associated_coordinates(), file_write_axis(), file_write_data(), and file_write_var().

◆ NCTYPE2TYPE

#define NCTYPE2TYPE (   nctype,
  type 
)
Value:
{ \
switch ( nctype ) { \
case NC_FLOAT: \
type = File_REAL4; \
break; \
case NC_DOUBLE: \
type = File_REAL8; \
break; \
case NC_SHORT: \
type = File_INTEGER2; \
break; \
default: \
fprintf(stderr, "unsuppoted data type: %d\n", xtype); \
return ERROR_CODE; \
} \
}
#define File_INTEGER2
Definition: gtool_file.h:18
#define ERROR_CODE
Definition: gtool_file.h:28
#define File_REAL8
Definition: gtool_file.h:17
#define File_REAL4
Definition: gtool_file.h:16

Definition at line 20 of file gtool_netcdf.c.

◆ TYPE2NCTYPE

#define TYPE2NCTYPE (   type,
  nctype 
)
Value:
{ \
switch ( type ) { \
case File_REAL4: \
nctype = NC_FLOAT; \
break; \
case File_REAL8: \
nctype = NC_DOUBLE; \
break; \
default: \
fprintf(stderr, "unsuppoted data type: %d\n", xtype); \
return ERROR_CODE; \
} \
}
#define ERROR_CODE
Definition: gtool_file.h:28
#define File_REAL8
Definition: gtool_file.h:17
#define File_REAL4
Definition: gtool_file.h:16

Definition at line 38 of file gtool_netcdf.c.

◆ DEFAULT_DEFLATE_LEVEL

#define DEFAULT_DEFLATE_LEVEL   2

Definition at line 54 of file gtool_netcdf.c.

◆ FILE_MAX

#define FILE_MAX   512

Definition at line 85 of file gtool_netcdf.c.

◆ VAR_MAX

#define VAR_MAX   40960

Definition at line 87 of file gtool_netcdf.c.

Function Documentation

◆ file_open()

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

Definition at line 97 of file gtool_netcdf.c.

Referenced by gtool_file::filemakefname().

100 {
101  int ncid;
102  int len;
103  char _fname[File_HLONG+4];
104 
105  if ( nfile >= FILE_MAX ) {
106  fprintf(stderr, "exceed max number of file limit\n");
107  return ERROR_CODE;
108  }
109 
110  len = strlen(fname);
111  strcpy(_fname, fname);
112  if (fname[len-3] != '.' || fname[len-2] != 'n' || fname[len-1] != 'c' )
113  strcat(_fname, ".nc");
114 
115  switch ( mode ) {
116  case File_FREAD:
117  CHECK_ERROR( nc_open(_fname, NC_NOWRITE, &ncid) );
118  break;
119  case File_FWRITE:
120 #ifdef NETCDF3
121  CHECK_ERROR( nc_create(_fname, NC_CLOBBER, &ncid) );
122 #else
123  CHECK_ERROR( nc_create(_fname, NC_CLOBBER|NC_NETCDF4, &ncid) );
124 #endif
125  break;
126  case File_FAPPEND:
127  CHECK_ERROR( nc_open(_fname, NC_WRITE, &ncid) );
128  break;
129  default:
130  fprintf(stderr, "invalid mode type\n");
131  return ERROR_CODE;
132  }
133 
134  files[nfile] = (fileinfo_t*) malloc(sizeof(fileinfo_t));
135  files[nfile]->ncid = ncid;
136  files[nfile]->deflate_level = DEFAULT_DEFLATE_LEVEL;
137 #ifdef NETCDF3
138  files[nfile]->defmode = 1;
139 #endif
140  *fid = nfile;
141  nfile++;
142 
143  return SUCCESS_CODE;
144 }
#define File_FAPPEND
Definition: gtool_file.h:25
#define FILE_MAX
Definition: gtool_netcdf.c:85
#define File_FREAD
Definition: gtool_file.h:23
#define ERROR_CODE
Definition: gtool_file.h:28
#define DEFAULT_DEFLATE_LEVEL
Definition: gtool_netcdf.c:54
int deflate_level
Definition: gtool_netcdf.c:59
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define SUCCESS_CODE
Definition: gtool_file.h:29
#define File_HLONG
Definition: gtool_file.h:13
#define File_FWRITE
Definition: gtool_file.h:24
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 146 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, fileinfo_t::deflate_level, ERROR_CODE, and SUCCESS_CODE.

Referenced by gtool_file::filesetoption().

150 {
151  if ( strcmp(filetype, "netcdf") != 0 ) return SUCCESS_CODE;
152 
153  if ( strcmp(key, "deflate_level") == 0 ) {
154  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
155  files[fid]->deflate_level = atoi(val);
156  return SUCCESS_CODE;
157  } else {
158  return ERROR_CODE;
159  }
160 }
#define ERROR_CODE
Definition: gtool_file.h:28
int deflate_level
Definition: gtool_netcdf.c:59
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 162 of file gtool_netcdf.c.

Referenced by gtool_file::filegetalldatainfo(), gtool_file::filegetdatainfo(), and gtool_file::filegetshape().

167 {
168  int ncid, varid;
169  nc_type xtype;
170  int rank;
171  int dimids[MAX_RANK], tdim, uldims[NC_MAX_DIMS];
172  char name[File_HSHORT+1];
173  size_t size;
174  size_t idx[2];
175  int i, n;
176 
177  ERROR_SUPPRESS = suppress;
178 
179  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
180  ncid = files[fid]->ncid;
181  CHECK_ERROR( nc_inq_varid(ncid, varname, &varid) );
182 
183  // fid
184  dinfo->fid = fid;
185  // varname
186  strcpy(dinfo->varname, varname);
187  // description
188  CHECK_ERROR( nc_get_att_text(ncid, varid, "long_name", dinfo->description) );
189  // units
190  CHECK_ERROR( nc_get_att_text(ncid, varid, "units", dinfo->units) );
191  // datatype
192  CHECK_ERROR( nc_inq_vartype(ncid, varid, &xtype) );
193  NCTYPE2TYPE(xtype, dinfo->datatype);
194  // rank
195  CHECK_ERROR( nc_inq_varndims(ncid, varid, &rank) );
196  CHECK_ERROR( nc_inq_vardimid(ncid, varid, dimids) );
197 #ifdef NETCDF3
198  CHECK_ERROR( nc_inq_unlimdim(ncid, uldims) );
199  n = 1;
200 #else
201  CHECK_ERROR( nc_inq_unlimdims(ncid, &n, uldims) );
202 #endif
203  tdim = -1;
204  for ( i=0; i<n; i++ ) {
205  if ( uldims[i] == dimids[0] ) {
206  tdim = uldims[i];
207  break;
208  }
209  }
210  if (rank > MAX_RANK) {
211  fprintf(stderr, "rank exceeds limit: %d\n", rank);
212  return ERROR_CODE;
213  }
214  dinfo->rank = tdim >= 0 ? rank -1 : rank; // do not count time dimension
215  // dim_name and dim_size
216  for (i=0; i<dinfo->rank; i++) {
217  // note: C and Fortran orders are opposit
218  CHECK_ERROR( nc_inq_dim(ncid, dimids[rank-i-1], name, &size) );
219  strncpy(dinfo->dim_name+i*File_HSHORT, name, File_HSHORT);
220  dinfo->dim_size[i] = size;
221  }
222 
223  dinfo->step = step;
224  if ( tdim >= 0 ) {
225  // time_end
226  CHECK_ERROR( nc_inq_dimname(ncid, tdim, name) );
227  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) );
228  idx[0] = step - 1;
229  CHECK_ERROR( nc_get_var1_double(ncid, varid, idx, &(dinfo->time_end)) );
230  // time_start
231  strcat(name, "_bnds");
232  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) );
233  idx[1] = 0;
234  CHECK_ERROR( nc_get_var1_double(ncid, varid, idx, &(dinfo->time_start)) );
235  // units
236  CHECK_ERROR( nc_get_att_text(ncid, varid, "units", dinfo->time_units) );
237  } else {
238  }
239 
240  ERROR_SUPPRESS = 0;
241 
242  return SUCCESS_CODE;
243 }
int32_t dim_size[MAX_RANK]
Definition: gtool_file.h:48
int32_t step
Definition: gtool_file.h:49
#define MAX_RANK
Definition: gtool_file.h:34
#define ERROR_CODE
Definition: gtool_file.h:28
char dim_name[File_HSHORT *MAX_RANK]
Definition: gtool_file.h:47
char description[File_HMID]
Definition: gtool_file.h:43
#define File_HSHORT
Definition: gtool_file.h:11
real64_t time_start
Definition: gtool_file.h:50
#define NCTYPE2TYPE(nctype, type)
Definition: gtool_netcdf.c:20
int32_t fid
Definition: gtool_file.h:53
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
char time_units[File_HMID]
Definition: gtool_file.h:52
real64_t time_end
Definition: gtool_file.h:51
char units[File_HSHORT]
Definition: gtool_file.h:44
char varname[File_HSHORT]
Definition: gtool_file.h:42
int32_t datatype
Definition: gtool_file.h:45
int32_t rank
Definition: gtool_file.h:46
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 245 of file gtool_netcdf.c.

Referenced by gtool_file::filegetalldatainfo().

248 {
249  int ncid, varid;
250  int rank;
251  size_t *start, *count;
252  int i;
253  int status;
254 
255  if ( files[dinfo->fid] == NULL ) return ALREADY_CLOSED_CODE;
256  ncid = files[dinfo->fid]->ncid;
257  CHECK_ERROR( nc_inq_varid(ncid, dinfo->varname, &varid) );
258 
259  CHECK_ERROR( nc_inq_varndims(ncid, varid, &rank) );
260  start = (size_t*) malloc(sizeof(size_t)*rank);
261  count = (size_t*) malloc(sizeof(size_t)*rank);
262  for (i=0; i<dinfo->rank; i++) {
263  // note: C and Fortran orders are opposit
264  start[rank -i-1] = 0;
265  count[rank -i-1] = dinfo->dim_size[i];
266  }
267  if (rank > dinfo->rank) { // have time dimension
268  start[0] = dinfo->step - 1;
269  count[0] = 1;
270  }
271  switch ( precision ) {
272  case 8:
273  status = nc_get_vara_double(ncid, varid, start, count, (double*)var);
274  free(start);
275  free(count);
276  CHECK_ERROR(status);
277  break;
278  case 4:
279  status = nc_get_vara_float(ncid, varid, start, count, (float*)var);
280  free(start);
281  free(count);
282  CHECK_ERROR(status);
283  break;
284  default:
285  free(start);
286  free(count);
287  fprintf(stderr, "unsuppoted data precision: %d\n", precision );
288  return ERROR_CODE;
289  }
290 
291  return SUCCESS_CODE;
292 }
int32_t dim_size[MAX_RANK]
Definition: gtool_file.h:48
int32_t step
Definition: gtool_file.h:49
#define ERROR_CODE
Definition: gtool_file.h:28
int32_t fid
Definition: gtool_file.h:53
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
char varname[File_HSHORT]
Definition: gtool_file.h:42
int32_t rank
Definition: gtool_file.h:46
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 294 of file gtool_netcdf.c.

Referenced by gtool_file::filecreate().

298 {
299  int ncid;
300  size_t l;
301 
302  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
303  ncid = files[fid]->ncid;
304 
305  CHECK_ERROR( nc_inq_attlen(ncid, NC_GLOBAL, key, &l) );
306  if ( len < l+1 ) return ERROR_CODE;
307 
308  CHECK_ERROR( nc_get_att_text(ncid, NC_GLOBAL, key, value) );
309  value[l] = '\0';
310 
311  return SUCCESS_CODE;
312 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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,
int *  value,
size_t  len 
)

Definition at line 314 of file gtool_netcdf.c.

318 {
319  int ncid;
320  size_t l;
321 
322  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
323  ncid = files[fid]->ncid;
324 
325  CHECK_ERROR( nc_inq_attlen(ncid, NC_GLOBAL, key, &l) );
326  if ( len < l ) return ERROR_CODE;
327  CHECK_ERROR( nc_get_att_int(ncid, NC_GLOBAL, key, value) );
328 
329  return SUCCESS_CODE;
330 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29

◆ 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 332 of file gtool_netcdf.c.

Referenced by gtool_file::filecreate().

336 {
337  int ncid;
338  size_t l;
339 
340  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
341  ncid = files[fid]->ncid;
342 
343  CHECK_ERROR( nc_inq_attlen(ncid, NC_GLOBAL, key, &l) );
344  if ( len < l ) return ERROR_CODE;
345  CHECK_ERROR( nc_get_att_float(ncid, NC_GLOBAL, key, value) );
346 
347  return SUCCESS_CODE;
348 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 350 of file gtool_netcdf.c.

Referenced by gtool_file::filecreate().

354 {
355  int ncid;
356  size_t l;
357 
358  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
359  ncid = files[fid]->ncid;
360 
361  CHECK_ERROR( nc_inq_attlen(ncid, NC_GLOBAL, key, &l) );
362  if ( len < l ) return ERROR_CODE;
363  CHECK_ERROR( nc_get_att_double(ncid, NC_GLOBAL, key, value) );
364 
365  return SUCCESS_CODE;
366 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 368 of file gtool_netcdf.c.

Referenced by gtool_file::filecreate().

371 {
372  int ncid;
373 
374  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
375  ncid = files[fid]->ncid;
376 
377 #ifdef NETCDF3
378  if (files[fid]->defmode == 0) {
379  CHECK_ERROR( nc_redef(ncid) );
380  files[fid]->defmode = 1;
381  }
382 #endif
383 
384  CHECK_ERROR( nc_put_att_text(ncid, NC_GLOBAL, key, strlen(value), value) );
385 
386  return SUCCESS_CODE;
387 }
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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,
int *  value,
size_t  len 
)

Definition at line 389 of file gtool_netcdf.c.

393 {
394  int ncid;
395 
396  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
397  ncid = files[fid]->ncid;
398 
399 #ifdef NETCDF3
400  if (files[fid]->defmode == 0) {
401  CHECK_ERROR( nc_redef(ncid) );
402  files[fid]->defmode = 1;
403  }
404 #endif
405 
406  CHECK_ERROR( nc_put_att_int(ncid, NC_GLOBAL, key, NC_INT, len, value) );
407 
408  return SUCCESS_CODE;
409 }
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29

◆ 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 411 of file gtool_netcdf.c.

Referenced by gtool_file::filecreate().

415 {
416  int ncid;
417 
418  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
419  ncid = files[fid]->ncid;
420 
421 #ifdef NETCDF3
422  if (files[fid]->defmode == 0) {
423  CHECK_ERROR( nc_redef(ncid) );
424  files[fid]->defmode = 1;
425  }
426 #endif
427 
428  CHECK_ERROR( nc_put_att_float(ncid, NC_GLOBAL, key, NC_FLOAT, len, value) );
429 
430  return SUCCESS_CODE;
431 }
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 433 of file gtool_netcdf.c.

Referenced by gtool_file::filecreate().

437 {
438  int ncid;
439 
440  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
441  ncid = files[fid]->ncid;
442 
443 #ifdef NETCDF3
444  if (files[fid]->defmode == 0) {
445  CHECK_ERROR( nc_redef(ncid) );
446  files[fid]->defmode = 1;
447  }
448 #endif
449 
450  CHECK_ERROR( nc_put_att_double(ncid, NC_GLOBAL, key, NC_DOUBLE, len, value) );
451 
452  return SUCCESS_CODE;
453 }
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 455 of file gtool_netcdf.c.

References SUCCESS_CODE.

Referenced by gtool_file::filecreate().

457 {
458  strcpy(files[fid]->time_units, time_units);
459 
460  return SUCCESS_CODE;
461 }
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 463 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, ALREADY_EXISTED_CODE, CHECK_ERROR, fileinfo_t::ncid, and SUCCESS_CODE.

Referenced by gtool_file::filesettattr().

467 {
468  int ncid;
469  int varid;
470  int attid;
471 
472  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
473  ncid = files[fid]->ncid;
474 
475  CHECK_ERROR( nc_inq_varid(ncid, vname, &varid) );
476 
477  if ( nc_inq_attid(ncid, varid, key, &attid) == NC_NOERR ) // check if existed
478  return ALREADY_EXISTED_CODE;
479 
480 #ifdef NETCDF3
481  if (files[fid]->defmode == 0) {
482  CHECK_ERROR( nc_redef(ncid) );
483  files[fid]->defmode = 1;
484  }
485 #endif
486 
487  CHECK_ERROR( nc_put_att_text(ncid, varid, key, strlen(val), val) );
488 
489  return SUCCESS_CODE;
490 }
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:31
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 492 of file gtool_netcdf.c.

Referenced by gtool_file::fileopen().

501 {
502  int ncid, dimid, varid;
503  nc_type xtype = -1;
504 
505  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
506  ncid = files[fid]->ncid;
507 
508  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
509  return ALREADY_EXISTED_CODE;
510 
511 #ifdef NETCDF3
512  if (files[fid]->defmode == 0) {
513  CHECK_ERROR( nc_redef(ncid) );
514  files[fid]->defmode = 1;
515  }
516 #endif
517 
518  if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR ) // check if existed
519  CHECK_ERROR( nc_def_dim(ncid, dim_name, size, &dimid) );
520 
521  TYPE2NCTYPE(dtype, xtype);
522  CHECK_ERROR( nc_def_var(ncid, name, xtype, 1, &dimid, &varid) );
523  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) );
524  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) );
525 
526 #ifdef NETCDF3
527  CHECK_ERROR( nc_enddef(ncid) );
528  files[fid]->defmode = 0;
529 #endif
530 
531  switch ( precision ) {
532  case 8:
533  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) );
534  break;
535  case 4:
536  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) );
537  break;
538  default:
539  fprintf(stderr, "unsuppoted data precision: %d\n", precision);
540  return ERROR_CODE;
541  }
542 
543  return SUCCESS_CODE;
544 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:31
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:38
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 546 of file gtool_netcdf.c.

Referenced by gtool_file::filedefaxis().

553 {
554  int ncid, dimid, varid;
555  nc_type xtype = -1;
556 
557  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
558  ncid = files[fid]->ncid;
559 
560  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
561  return ALREADY_EXISTED_CODE;
562 
563 #ifdef NETCDF3
564  if (files[fid]->defmode == 0) {
565  CHECK_ERROR( nc_redef(ncid) );
566  files[fid]->defmode = 1;
567  }
568 #endif
569 
570  if ( nc_inq_dimid(ncid, dim_name, &dimid) != NC_NOERR ) // check if existed
571  CHECK_ERROR( nc_def_dim(ncid, dim_name, dim_size, &dimid) );
572 
573  TYPE2NCTYPE(dtype, xtype);
574  CHECK_ERROR( nc_def_var(ncid, name, xtype, 1, &dimid, &varid) );
575  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) );
576  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) );
577 
578  return SUCCESS_CODE;
579 }
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:31
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:38
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 
)

Definition at line 581 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, CHECK_ERROR, and fileinfo_t::ncid.

Referenced by gtool_file::filedefaxis().

585 {
586  int ncid, varid;
587 
588  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
589  ncid = files[fid]->ncid;
590 
591  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) )
592 
593 #ifdef NETCDF3
594  if (files[fid]->defmode == 1) {
595  CHECK_ERROR( nc_enddef(ncid) );
596  files[fid]->defmode = 0;
597  }
598 #endif
599 
600  switch ( precision ) {
601  case 8:
602  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) );
603  break;
604  case 4:
605  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) );
606  break;
607  default:
608  fprintf(stderr, "unsuppoted data precision: %d\n", precision);
609  return ERROR_CODE;
610  }
611 
612  return SUCCESS_CODE;
613 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 615 of file gtool_netcdf.c.

Referenced by gtool_file::filedefaxis().

624 {
625  int ncid, *dimids, varid;
626  nc_type xtype = -1;
627  int i;
628 
629  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
630  ncid = files[fid]->ncid;
631 
632  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
633  return ALREADY_EXISTED_CODE;
634 
635 #ifdef NETCDF3
636  if (files[fid]->defmode == 0) {
637  CHECK_ERROR( nc_redef(ncid) );
638  files[fid]->defmode = 1;
639  }
640 #endif
641 
642  dimids = malloc(sizeof(int)*ndims);
643  for (i=0; i<ndims; i++)
644  CHECK_ERROR( nc_inq_dimid(ncid, dim_names[i], dimids+ndims-i-1) );
645 
646  TYPE2NCTYPE(dtype, xtype);
647 
648  CHECK_ERROR( nc_def_var(ncid, name, xtype, ndims, dimids, &varid) );
649  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) );
650  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) );
651  free(dimids);
652 
653 #ifdef NETCDF3
654  CHECK_ERROR( nc_enddef(ncid) );
655  files[fid]->defmode = 0;
656 #endif
657 
658  switch ( precision ) {
659  case 8:
660  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) );
661  break;
662  case 4:
663  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) );
664  break;
665  default:
666  fprintf(stderr, "unsuppoted data precision: %d\n", precision);
667  return ERROR_CODE;
668  }
669 
670  return SUCCESS_CODE;
671 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:31
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:38
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 673 of file gtool_netcdf.c.

Referenced by gtool_file::filedefassociatedcoordinates().

680 {
681  int ncid, *dimids, varid;
682  nc_type xtype = -1;
683  int i;
684 
685  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
686  ncid = files[fid]->ncid;
687 
688  if ( nc_inq_varid(ncid, name, &varid) == NC_NOERR ) // check if existed
689  return ALREADY_EXISTED_CODE;
690 
691 #ifdef NETCDF3
692  if (files[fid]->defmode == 0) {
693  CHECK_ERROR( nc_redef(ncid) );
694  files[fid]->defmode = 1;
695  }
696 #endif
697 
698  dimids = malloc(sizeof(int)*ndims);
699  for (i=0; i<ndims; i++)
700  CHECK_ERROR( nc_inq_dimid(ncid, dim_names[i], dimids+ndims-i-1) );
701 
702  TYPE2NCTYPE(dtype, xtype);
703 
704  CHECK_ERROR( nc_def_var(ncid, name, xtype, ndims, dimids, &varid) );
705  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) );
706  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) );
707  free(dimids);
708 
709  return SUCCESS_CODE;
710 }
#define ALREADY_EXISTED_CODE
Definition: gtool_file.h:31
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:38
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 
)

Definition at line 712 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, CHECK_ERROR, and fileinfo_t::ncid.

Referenced by gtool_file::filedefassociatedcoordinates().

716 {
717  int ncid, varid;
718 
719  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
720  ncid = files[fid]->ncid;
721 
722  CHECK_ERROR( nc_inq_varid(ncid, name, &varid) );
723 
724 #ifdef NETCDF3
725  if (files[fid]->defmode == 1) {
726  CHECK_ERROR( nc_enddef(ncid) );
727  files[fid]->defmode = 0;
728  }
729 #endif
730 
731  switch ( precision ) {
732  case 8:
733  CHECK_ERROR( nc_put_var_double(ncid, varid, (double*)val) );
734  break;
735  case 4:
736  CHECK_ERROR( nc_put_var_float(ncid, varid, (float*)val) );
737  break;
738  default:
739  fprintf(stderr, "unsuppoted data precision: %d\n", precision);
740  return ERROR_CODE;
741  }
742 
743  return SUCCESS_CODE;
744 }
#define ERROR_CODE
Definition: gtool_file.h:28
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
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 746 of file gtool_netcdf.c.

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

756 {
757  int ncid, varid, acid, *acdimids;
758  int dimids[NC_MAX_DIMS], dimid;
759  char tname[File_HSHORT+1];
760  int tdimid, tvarid;
761  nc_type xtype = -1;
762  char buf[File_HMID+1];
763  int i, j, k, n, m;
764  int nndims;
765  size_t size;
766  double rmiss = RMISS;
767  char coord[File_HMID+1];
768  int has_assoc;
769  int new;
770 
771  if ( nvar >= VAR_MAX ) {
772  fprintf(stderr, "exceed max number of variable limit\n");
773  return ERROR_CODE;
774  }
775 
776  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
777  ncid = files[fid]->ncid;
778 
779  vars[nvar] = (varinfo_t*) malloc(sizeof(varinfo_t));
780  vars[nvar]->ncid = ncid;
781  vars[nvar]->t = NULL;
782  vars[nvar]->start = NULL;
783  vars[nvar]->count = NULL;
784 
785 #ifdef NETCDF3
786  if (files[fid]->defmode == 0) {
787  CHECK_ERROR( nc_redef(ncid) );
788  files[fid]->defmode = 1;
789  }
790 #endif
791 
792  // get time variable
793  if ( tint > 0.0 ) {
794  for ( i=0; i<nt; i++ ) {
795  if ( tdims[i] != NULL && // still opened
796  tdims[i]->ncid == ncid && // same file
797  tdims[i]->tint == tint ) { // same time interval
798  vars[nvar]->t = tdims[i];
799  break;
800  }
801  }
802  if ( vars[nvar]->t == NULL ) {
803  tdims[nt] = (tdim_t*) malloc(sizeof(tdim_t));
804  tdims[nt]->ncid = ncid;
805  tdims[nt]->count = -1;
806  tdims[nt]->tint = tint;
807  // generate name
808  if ( nt == 0 )
809  strcpy(tname, "time");
810  else
811  sprintf(tname, "time%d", nt);
812  strcpy(tdims[nt]->name, tname);
813  // define time dimension and variable
814  CHECK_ERROR( nc_def_dim(ncid, tname, 0, &tdimid) );
815  tdims[nt]->dimid = tdimid;
816  CHECK_ERROR( nc_def_var(ncid, tname, NC_DOUBLE, 1, &tdimid, &tvarid) );
817  tdims[nt]->varid = tvarid;
818  strcpy(buf, "time");
819  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "long_name", strlen(buf), buf) );
820  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "units", strlen(files[fid]->time_units), files[fid]->time_units) );
821  // define boundary variable
822  if ( nc_inq_dimid(ncid, "nv", &(dimids[1])) != NC_NOERR ) // first called
823  CHECK_ERROR( nc_def_dim(ncid, "nv", 2, &(dimids[1])) ); // number of vertices
824  sprintf(buf, "%s_bnds", tname);
825  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "bounds", strlen(buf), buf) );
826  dimids[0] = tdimid;
827  CHECK_ERROR( nc_def_var(ncid, buf, NC_DOUBLE, 2, dimids, &tvarid) );
828  tdims[nt]->bndsid = tvarid;
829  CHECK_ERROR( nc_put_att_text(ncid, tvarid, "units", strlen(files[fid]->time_units), files[fid]->time_units) );
830 
831  vars[nvar]->t = tdims[nt];
832  nt++;
833  }
834  }
835 
836  // get dimension IDs
837  // note: C and Fortran order are opposit
838  n = ndims;
839  if ( tint > 0.0 ) { // add time dimension
840  dimids[0] = vars[nvar]->t->dimid;
841  ndims++;
842  }
843  for (i=ndims-n; i<ndims; i++) dimids[i] = -1;
844 
845  has_assoc = 0;
846  nndims = 0;
847  for (i=0; i<n; i++) {
848  //printf("%d %s\n", i, dims[i]);
849  if ( nc_inq_dimid(ncid, dims[i], &dimid) == NC_NOERR ) {
850  //printf("not assoc\n");
851  new = 1;
852  for (k=0; k<nndims; k++) {
853  if (dimid == dimids[k]) {
854  new = 0;
855  break;
856  }
857  }
858  if (new) {
859  dimids[ndims-(++nndims)] = dimid;
860  }
861  } else {
862  //printf("assoc\n");
863  CHECK_ERROR( nc_inq_varid(ncid, dims[i], &acid) );
864  CHECK_ERROR( nc_inq_varndims(ncid, acid, &m) );
865  acdimids = (int*) malloc((sizeof(int)*m));
866  CHECK_ERROR( nc_inq_vardimid(ncid, acid, acdimids) );
867  for (j=m-1; j>=0; j--) {
868  new = 1;
869  for (k=0; k<ndims; k++) {
870  if (acdimids[j] == dimids[k]) {
871  new = 0;
872  break;
873  }
874  }
875  if (new) {
876  if ( nndims >= ndims ) {
877  fprintf(stderr, "Error: invalid associated coordinates\n");
878  return ERROR_CODE;
879  }
880  dimids[ndims-(++nndims)] = acdimids[j];
881  //nc_inq_dimname(ncid, acdimids[j], tname);
882  //printf("add %s\n", tname);
883  }
884  }
885  free(acdimids);
886  has_assoc = 1;
887  }
888  }
889  if (nndims != n) {
890  fprintf(stderr, "Error: invalid associated coordinates: %d %d\n", ndims, nndims);
891  return ERROR_CODE;
892  }
893 
894  TYPE2NCTYPE(dtype, xtype);
895  CHECK_ERROR( nc_def_var(ncid, varname, xtype, ndims, dimids, &varid) );
896 
897  // put variable attribute
898  CHECK_ERROR( nc_put_att_text(ncid, varid, "long_name", strlen(desc), desc) );
899  CHECK_ERROR( nc_put_att_text(ncid, varid, "units", strlen(units), units) );
900  CHECK_ERROR( nc_put_att_double(ncid, varid, _FillValue, xtype, 1, &rmiss) );
901  CHECK_ERROR( nc_put_att_double(ncid, varid, "missing_value", xtype, 1, &rmiss) );
902  if ( has_assoc ) {
903  strcpy(coord, dims[0]);
904  for(i=1; i<n; i++) {
905  if (strlen(coord)+strlen(dims[i])+1 < File_HMID) {
906  strcat(coord, " ");
907  strcat(coord, dims[i]);
908  }
909  }
910  if ( ndims > n && strlen(coord)+6 < File_HMID) {
911  strcat(coord, " ");
912  strcat(coord, vars[nvar]->t->name);
913  }
914  CHECK_ERROR( nc_put_att_text(ncid, varid, "coordinates", strlen(coord), coord) );
915  }
916 
917 
918  if ( tavg ) {
919  sprintf(buf, "%s: mean", vars[nvar]->t->name);
920  CHECK_ERROR( nc_put_att_text(ncid, varid, "cell_methods", strlen(buf), buf) );
921  }
922 
923  // set start and count
924  vars[nvar]->start = (size_t*) malloc(sizeof(size_t)*ndims);
925  vars[nvar]->count = (size_t*) malloc(sizeof(size_t)*ndims);
926  for ( i=0; i<ndims; i++ ) {
927  CHECK_ERROR( nc_inq_dimlen(ncid, dimids[i], &size) );
928  vars[nvar]->count[i] = size;
929  vars[nvar]->start[i] = 0;
930  }
931  if ( tint > 0.0 ) vars[nvar]->count[0] = 1;
932 
933 #ifndef NETCDF3
934  // set chunk size and deflate level
935  if ( files[fid]->deflate_level > 0 ) {
936  CHECK_ERROR( nc_def_var_chunking(ncid, varid, NC_CHUNKED, vars[nvar]->count) );
937  CHECK_ERROR( nc_def_var_deflate(ncid, varid, 0, 1, files[fid]->deflate_level) );
938  }
939 #endif
940 
941  vars[nvar]->varid = varid;
942  *vid = nvar;
943  nvar++;
944 
945  return SUCCESS_CODE;
946 }
#define File_HMID
Definition: gtool_file.h:12
int dimid
Definition: gtool_netcdf.c:67
real(dp), parameter, public rmiss
Definition: gtool_file.f90:141
#define ERROR_CODE
Definition: gtool_file.h:28
int count
Definition: gtool_netcdf.c:70
tdim_t * t
Definition: gtool_netcdf.c:79
#define File_HSHORT
Definition: gtool_file.h:11
int ncid
Definition: gtool_netcdf.c:66
real64_t tint
Definition: gtool_netcdf.c:72
int bndsid
Definition: gtool_netcdf.c:69
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define TYPE2NCTYPE(type, nctype)
Definition: gtool_netcdf.c:38
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
#define VAR_MAX
Definition: gtool_netcdf.c:87
size_t * start
Definition: gtool_netcdf.c:80
#define RMISS
Definition: gtool_netcdf.c:4
size_t * count
Definition: gtool_netcdf.c:81
int varid
Definition: gtool_netcdf.c:68
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 
)

Definition at line 948 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, CHECK_ERROR, tdim_t::count, varinfo_t::count, varinfo_t::ncid, varinfo_t::start, tdim_t::t, varinfo_t::t, TEPS, and varinfo_t::varid.

Referenced by file_write_data_(), and gtool_file::filegetalldatainfo().

954 {
955  int ncid, varid;
956 
957  if ( vars[vid] == NULL ) return ALREADY_CLOSED_CODE;
958 
959  ncid = vars[vid]->ncid;
960 
961 #ifdef NETCDF3
962  if (files[fid]->defmode == 1) {
963  CHECK_ERROR( nc_enddef(ncid) );
964  files[fid]->defmode = 0;
965  }
966 #endif
967 
968  varid = vars[vid]->varid;
969  if ( vars[vid]->t != NULL ) { // have time dimension
970  if ( vars[vid]->t->count < 0 || // first time
971  t_end > vars[vid]->t->t + TEPS ) { // time goes next step
972  vars[vid]->t->count += 1;
973  vars[vid]->t->t = t_end;
974  size_t index[2];
975  index[0] = vars[vid]->t->count;
976  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->varid, index, &t_end) );
977  index[1] = 0;
978  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_start) );
979  index[1] = 1;
980  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_end) );
981  vars[vid]->start[0] = vars[vid]->t->count;
982  } else {
983  size_t nt = vars[vid]->t->count + 1;
984  double t[nt];
985  size_t s[1];
986  int flag, n;
987  s[0] = 0;
988  CHECK_ERROR( nc_get_vara_double(ncid, vars[vid]->t->varid, s, &nt, t) );
989  flag = 1;
990  for(n=nt-1;n>=0;n--) {
991  if ( fabs(t[n]-t_end) < TEPS ) {
992  vars[vid]->start[0] = n;
993  flag = 0;
994  break;
995  }
996  }
997  if ( flag ) {
998  fprintf(stderr, "cannot find time: %f\n", t_end);
999  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);
1000  fprintf(stderr, " time is: ");
1001  for (n=0;n<nt;n++) fprintf(stderr, "%f, ", t[n]);
1002  fprintf(stderr, "\n");
1003  return ERROR_CODE;
1004  }
1005  }
1006  }
1007 
1008  switch (precision) {
1009  case 8:
1010  CHECK_ERROR( nc_put_vara_double(ncid, varid, vars[vid]->start, vars[vid]->count, (double*)var) );
1011  break;
1012  case 4:
1013  CHECK_ERROR( nc_put_vara_float(ncid, varid, vars[vid]->start, vars[vid]->count, (float*)var) );
1014  break;
1015  default:
1016  fprintf(stderr, "unsuppoted data precision: %d\n", precision);
1017  return ERROR_CODE;
1018  }
1019 
1020  CHECK_ERROR( nc_sync(ncid) );
1021 
1022  return SUCCESS_CODE;
1023 }
#define ERROR_CODE
Definition: gtool_file.h:28
int count
Definition: gtool_netcdf.c:70
tdim_t * t
Definition: gtool_netcdf.c:79
#define TEPS
Definition: gtool_netcdf.c:5
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
size_t * start
Definition: gtool_netcdf.c:80
real64_t t
Definition: gtool_netcdf.c:71
Here is the caller graph for this function:

◆ file_enddef()

int32_t file_enddef ( int32_t  fid)

Definition at line 1025 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, CHECK_ERROR, fileinfo_t::ncid, and SUCCESS_CODE.

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

1026 {
1027  int ncid;
1028 
1029  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1030  ncid = files[fid]->ncid;
1031 
1032 #ifdef NETCDF3
1033  if (files[fid]->defmode == 1) {
1034  CHECK_ERROR( nc_enddef(ncid) );
1035  files[fid]->defmode = 0;
1036  }
1037 #endif
1038 
1039  return SUCCESS_CODE;
1040 }
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
Here is the caller graph for this function:

◆ file_write_var()

int32_t file_write_var ( int32_t  vid,
void *  var,
real64_t  t_start,
real64_t  t_end,
int32_t  precision 
)

Definition at line 1042 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, CHECK_ERROR, tdim_t::count, varinfo_t::count, varinfo_t::ncid, varinfo_t::start, tdim_t::t, varinfo_t::t, TEPS, and varinfo_t::varid.

Referenced by file_write_var_(), and gtool_file::filegetalldatainfo().

1047 {
1048  int ncid, varid;
1049 
1050  if ( vars[vid] == NULL ) return ALREADY_CLOSED_CODE;
1051  ncid = vars[vid]->ncid;
1052  varid = vars[vid]->varid;
1053  if ( vars[vid]->t != NULL ) { // have time dimension
1054  if ( vars[vid]->t->count < 0 || // first time
1055  t_end > vars[vid]->t->t + TEPS ) { // time goes next step
1056  vars[vid]->t->count += 1;
1057  vars[vid]->t->t = t_end;
1058  size_t index[2];
1059  index[0] = vars[vid]->t->count;
1060  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->varid, index, &t_end) );
1061  index[1] = 0;
1062  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_start) );
1063  index[1] = 1;
1064  CHECK_ERROR( nc_put_var1_double(ncid, vars[vid]->t->bndsid, index, &t_end) );
1065  vars[vid]->start[0] = vars[vid]->t->count;
1066  } else {
1067  size_t nt = vars[vid]->t->count + 1;
1068  double t[nt];
1069  size_t s[1];
1070  int flag, n;
1071  s[0] = 0;
1072  CHECK_ERROR( nc_get_vara_double(ncid, vars[vid]->t->varid, s, &nt, t) );
1073  flag = 1;
1074  for(n=nt-1;n>=0;n--) {
1075  if ( fabs(t[n]-t_end) < TEPS ) {
1076  vars[vid]->start[0] = n;
1077  flag = 0;
1078  break;
1079  }
1080  }
1081  if ( flag ) {
1082  fprintf(stderr, "cannot find time: %f\n", t_end);
1083  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);
1084  fprintf(stderr, " time is: ");
1085  for (n=0;n<nt;n++) fprintf(stderr, "%f, ", t[n]);
1086  fprintf(stderr, "\n");
1087  return ERROR_CODE;
1088  }
1089  }
1090  }
1091 
1092  switch (precision) {
1093  case 8:
1094  CHECK_ERROR( nc_put_vara_double(ncid, varid, vars[vid]->start, vars[vid]->count, (double*)var) );
1095  break;
1096  case 4:
1097  CHECK_ERROR( nc_put_vara_float(ncid, varid, vars[vid]->start, vars[vid]->count, (float*)var) );
1098  break;
1099  default:
1100  fprintf(stderr, "unsuppoted data precision: %d\n", precision);
1101  return ERROR_CODE;
1102  }
1103 
1104  CHECK_ERROR( nc_sync(ncid) );
1105 
1106  return SUCCESS_CODE;
1107 }
#define ERROR_CODE
Definition: gtool_file.h:28
int count
Definition: gtool_netcdf.c:70
tdim_t * t
Definition: gtool_netcdf.c:79
#define TEPS
Definition: gtool_netcdf.c:5
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
size_t * start
Definition: gtool_netcdf.c:80
real64_t t
Definition: gtool_netcdf.c:71
Here is the caller graph for this function:

◆ file_close()

int32_t file_close ( int32_t  fid)

Definition at line 1109 of file gtool_netcdf.c.

References ALREADY_CLOSED_CODE, and fileinfo_t::ncid.

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

1110 {
1111  int ncid;
1112  int i;
1113 
1114  if ( files[fid] == NULL ) return ALREADY_CLOSED_CODE;
1115  ncid = files[fid]->ncid;
1116 
1117  free( files[fid] );
1118  files[fid] = NULL;
1119 
1120  for (i=0; i<nvar; i++) {
1121  if ( vars[i] != NULL && vars[i]->ncid == ncid ) {
1122  free( vars[i]->start );
1123  free( vars[i]->count );
1124  free( vars[i] );
1125  vars[i] = NULL;
1126  }
1127  }
1128 
1129  for (i=0; i<nt; i++) {
1130  if ( tdims[i] != NULL && tdims[i]->ncid == ncid ) {
1131  free( tdims[i] );
1132  tdims[i] = NULL;
1133  }
1134  }
1135 
1136  CHECK_ERROR( nc_close(ncid) );
1137 
1138  return SUCCESS_CODE;
1139 }
#define CHECK_ERROR(status)
Definition: gtool_netcdf.c:9
#define ALREADY_CLOSED_CODE
Definition: gtool_file.h:30
#define SUCCESS_CODE
Definition: gtool_file.h:29
Here is the caller graph for this function: