/
mjrNetCDF.c
45 lines (37 loc) · 1.61 KB
/
mjrNetCDF.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
@file mjrNetCDF.c
@author Mitch Richling <https://www.mitchr.me/>
@Copyright Copyright 1996 by Mitch Richling. All rights reserved.
@brief Simple functions to help with NetCDF programs.@EOL
@Keywords NetCDF
@Std C89
*/
#include <netcdf.h> /* NetCDF Files */
#include <stdio.h> /* I/O lib ISOC */
#include <string.h> /* Strings ISOC */
#include <stdlib.h> /* Standard Lib ISOC */
#include "mjrNetCDF.h"
/* Exit upon any nc_* error */
void mjrNC_chkError(int status) {
if (status != NC_NOERR) {
fprintf(stderr, "NC ERROR: %s\n", nc_strerror(status));
exit(1);
} /* end if */
} /* end func mjrNC_chkError */
/* Wrapper for nc_put_att_text() that figures out the length of c-strings
and makes sure the ending \0 will be stored in the string. */
int mjrNC_putAttCStr(int ncid, int varid, char *attName, char *attVal) {
return nc_put_att_text(ncid, varid, attName, strlen(attVal)+1, attVal);
} /* end func mjrNC_putAttCStr */
/* Convert an nc_type into a string. */
void mjrNC_type2str(nc_type ncXType, char *ncXTypeStr) {
switch(ncXType) {
case NC_BYTE: strcpy(ncXTypeStr, "byte"); break;
case NC_CHAR: strcpy(ncXTypeStr, "char"); break;
case NC_SHORT: strcpy(ncXTypeStr, "short"); break;
case NC_INT: strcpy(ncXTypeStr, "int"); break;
case NC_FLOAT: strcpy(ncXTypeStr, "float"); break;
case NC_DOUBLE: strcpy(ncXTypeStr, "double"); break;
default: strcpy(ncXTypeStr, ""); break;
} /* end switch */
} /* end func mjrNC_type2str */