-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnctools.c
More file actions
263 lines (236 loc) · 6.51 KB
/
nctools.c
File metadata and controls
263 lines (236 loc) · 6.51 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* nctools.c -- useful NetCDF functions */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <netcdf.h>
#define MAX_STRING_LENGTH 512
/* Prepend "string" to the attribute "attname" of the variable with ID
* "varid" (where NC_GLOBAL indicates a global attribute) of NetCDF
* dataset with ID "ncid", using separator "separator". */
int
nct_prepend_attribute(int ncid, int varid, char *attname, char *string, char *separator)
{
size_t oldlen, newlen = strlen(string);
int status;
status = nc_inq_attlen(ncid, varid, attname, &oldlen);
if (status == NC_ENOTATT) {
/* New attribute */
return nc_put_att_text(ncid, varid, attname, newlen, string);
}
else if (status == NC_NOERR) {
/* Existing attribute */
size_t fulllen = oldlen + newlen + strlen(separator) + 1;
char *newstring = malloc(fulllen * sizeof(char));
if (!newstring) {
return NC_ENOMEM;
}
sprintf(newstring, "%s%s", string, separator);
status = nc_get_att_text(ncid, varid, attname, newstring+strlen(newstring));
if (status != NC_NOERR) {
free(newstring);
return status;
}
newstring[fulllen-1] = '\0';
status = nc_put_att_text(ncid, varid, attname, strlen(newstring), newstring);
free(newstring);
return status;
}
else {
/* An error occurred */
return status;
}
}
/* Append "string" to the attribute "attname" of the variable with ID
* "varid" (where NC_GLOBAL indicates a global attribute) of NetCDF
* dataset with ID "ncid", using separator "separator". */
int
nct_append_attribute(int ncid, int varid, char *attname, char *string, char *separator)
{
size_t oldlen, newlen = strlen(string);
int status;
status = nc_inq_attlen(ncid, varid, attname, &oldlen);
if (status == NC_ENOTATT) {
/* New attribute */
return nc_put_att_text(ncid, varid, attname, newlen, string);
}
else if (status == NC_NOERR) {
/* Existing attribute */
size_t fulllen = oldlen + newlen + strlen(separator) + 1;
char *newstring = malloc(fulllen * sizeof(char));
if (!newstring) {
return NC_ENOMEM;
}
status = nc_get_att_text(ncid, varid, attname, newstring);
newstring[oldlen] = '\0';
sprintf(newstring+strlen(newstring), "%s%s", separator, string);
if (status != NC_NOERR) {
free(newstring);
return status;
}
status = nc_put_att_text(ncid, varid, attname, strlen(newstring), newstring);
free(newstring);
return status;
}
else {
/* An error occurred */
return status;
}
}
/* Append information to the "history" global attribute of a NetCDF
* dataset. The information is of the form "$action at $time by $user
* on $host", where action and user are given as arguments (a value of
* NULL for user causes the username to be used), time is the current
* time and host is the name of the machine. Histories are separated
* by semicolons. */
int
nct_add_history(int ncid, char *action, char *user)
{
struct timeval tv;
char *name;
char hostname[MAX_STRING_LENGTH] = "unknown";
char history[MAX_STRING_LENGTH] = "";
if (gettimeofday(&tv, NULL)) {
name = "";
}
else {
name = ctime(&tv.tv_sec);
name[24] = ' '; /* Get rid of the newline */
}
gethostname(hostname, MAX_STRING_LENGTH);
if (!user) {
/* user string is NULL; get the username instead */
if (!(user = getenv("LOGNAME"))) {
if (!(user = getenv("USER"))) {
user = "anonymous";
}
}
}
snprintf(history, MAX_STRING_LENGTH, "%s- %s by %s on %s", name, action, user, hostname);
history[MAX_STRING_LENGTH-1] = '\0';
return nct_append_attribute(ncid, NC_GLOBAL, "history", history, "\n");
}
/* Append the full command line to the global attribute "command_line"
* of a NetCDF file, using semicolons as separators if several
* programs write to the same file. */
#define COMPARE_ARG_LENGTH 4
#define NUM_SIMILAR_ARGS 5
int
nct_add_command_line(int ncid, int argc, char **argv)
{
int i, j, len = 0;
char *cmdline;
char lastarg[COMPARE_ARG_LENGTH+1];
int status;
int numsimilarargs = 1;
if (argv == NULL) {
return NC_EINVAL;
}
for (i = 0; i < argc; i++) {
char *c = argv[i];
char need_quotes = 0;
for (j = 0; c[j] != '\0'; j++) {
if (c[j] <= ' ') {
need_quotes = 1;
}
}
len += (1 + j + need_quotes * 2);
}
cmdline = malloc(len * sizeof(char));
if (cmdline == NULL) {
return NC_ENOMEM;
}
len = 0;
lastarg[0] = '\0';
for (i = 0; i < argc; i++) {
char *c = argv[i];
char need_quotes = 0;
for (j = 0; c[j] != '\0'; j++) {
if (c[j] <= ' ') {
need_quotes = 1;
}
}
/* This next bit of code ensures that if more than
NUM_SIMILAR_ARGS are found that share the same first
COMPARE_ARG_LENGTH characters, then an elipsis (...) will be
output. This saves on excessively long command lines being
saved. */
if (j > COMPARE_ARG_LENGTH) {
if (c[0] != '-' && strncmp(lastarg, c, COMPARE_ARG_LENGTH) == 0) {
numsimilarargs++;
if (numsimilarargs == NUM_SIMILAR_ARGS+1) {
c = "...";
j = 3;
}
else if (numsimilarargs > NUM_SIMILAR_ARGS+1) {
continue;
}
}
else {
snprintf(lastarg, COMPARE_ARG_LENGTH+1, "%s", c);
lastarg[COMPARE_ARG_LENGTH] = '\0';
numsimilarargs = 1;
}
}
else {
numsimilarargs = 1;
}
if (need_quotes) {
sprintf(cmdline + len, "\"%s\"", c);
}
else {
sprintf(cmdline + len, "%s", c);
}
len += (1 + j + need_quotes * 2);
cmdline[len -1] = ' ';
}
cmdline[len-1] = '\0';
status = nct_append_attribute(ncid, NC_GLOBAL, "command_line", cmdline, "\n");
free(cmdline);
return status;
}
/* Remove trailing abreviations in parantheses from an attribute */
int
nct_strip_parentheses(int ncid, int varid, char *attname)
{
size_t len;
int status;
status = nc_inq_attlen(ncid, varid, attname, &len);
if (status == NC_NOERR) {
char *str = malloc(len * sizeof(char));
char *ch;
if (!str) {
return NC_ENOMEM;
}
status = nc_get_att_text(ncid, varid, attname, str);
if (status != NC_NOERR) {
return status;
}
ch = str + len - 1;
if (*ch == '\0' && len > 4) {
--ch;
}
if (*ch == ')') {
while ((--ch) > str) {
if (*ch == '(') {
ch--;
if (*ch == ' ') {
*ch = '\0';
}
break;
}
}
}
if (*ch == '\0') {
nc_put_att_text(ncid, varid, attname, strlen(str), str);
}
free(str);
return NC_NOERR;
}
else {
/* An error occurred */
return status;
}
}