-
Notifications
You must be signed in to change notification settings - Fork 343
/
mgd77netcdfhelper.sh
executable file
·280 lines (242 loc) · 11.6 KB
/
mgd77netcdfhelper.sh
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
#
# Author: P. Wessel
# Date: 2005-OCT-14
# Revised: 2007-JUN-06: Now store _REVISED attributes set by E77
#
# This script will automatically create three functions from info in mgd77.h:
#
# MGD77_Read_Header_Params : Read the MGD77 header attributes from the netCDF file
# MGD77_Write_Header_Params : Write the MGD77 header attributes to the netCDF file
# MGD77_Dump_Header_Params : Display individual header attributes, one per line
#
# Code is placed in the file mgd77_functions.h which is included in mgd77_functions.c
#
# The script should be run manually when necessary
#
cat << EOF > mgd77_functions.h
/*--------------------------------------------------------------------
*
* Copyright (c) 2005-2024 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
* See LICENSE.TXT file for copying and redistribution conditions.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3 or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* Contact info: www.generic-mapping-tools.org
*--------------------------------------------------------------------*/
/* Code automatically generated by mgd77netcdfhelper.sh
* To be included by mgd77_functions.c
*/
#ifndef _MGD77_FUNCTIONS_H
#define _MGD77_FUNCTIONS_H
#include "mgd77.h"
struct MGD77_HEADER_LOOKUP { /* Book-keeping for one header parameter */
char name[64]; /* Name of this parameter (e.g., "Gravity_Sampling_Rate") */
GMT_LONG length; /* Number of bytes to use */
int record; /* Header record number where it occurs (1-24) */
int item; /* Sequential item order in this record (1->) */
GMT_LONG check; /* TRUE if we actually do a test on this item */
GMT_LONG revised; /* TRUE if read in via the _REVISED attribute */
char *ptr[2]; /* Pointers to the corresponding named variable in struct MGD77_HEADER_PARAMS (orig and revised) */
};
struct MGD77_HEADER_PARAMS { /* See MGD-77 Documentation from NGDC for details */
/* Sequence No 01: */
char Record_Type;
EOF
cat << EOF > mgd77_functions.c
/*--------------------------------------------------------------------
*
* Copyright (c) 2005-2024 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
* See LICENSE.TXT file for copying and redistribution conditions.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3 or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* Contact info: www.generic-mapping-tools.org
*--------------------------------------------------------------------*/
/* Code automatically generated by mgd77netcdfhelper.sh
*/
#include "mgd77_functions.h"
void MGD77_Read_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F, struct MGD77_HEADER_PARAMS **P)
{
/* Read the netCDF-encoded MGD77 header parameters as attributes of the data set.
* If orig is TRUE we will recover the original MGD77 parameters; otherwise we first
* look for revised parameters and fall back on the original if no revision is found. */
struct MGD77_HEADER_LOOKUP *L;
int i;
L = MGD77_Header_Lookup;
EOF
# 1. strip out the structure members (except Record_Type)
key=0
n_check=0
type="char"
last="01"
egrep -v '^#|Record_Type' mgd77_header.txt > $$.1
while read name rec item size check; do
if [ ! $last = $rec ]; then
echo " /* Sequence No $rec: */" >> mgd77_functions.h
fi
# We need a separate read/write statement for each attribute
pre="" # Normally, no prefix for character arrays
cast=""
n_item=1
fmt="%s"
R=$(echo $rec | awk '{printf "%d\n", $1}')
I=$(echo $item | awk '{printf "%d\n", $1}')
REV="L[MGD77_Param_Key(C,$R,$I)].revised"
n=$(echo $size | awk -F'*' '{print NF}')
if [ $n -eq 1 ]; then # Single item
if [ $size -eq 1 ]; then
echo " char $name;" >> mgd77_functions.h
else
echo " char $name[$size];" >> mgd77_functions.h
fi
L=$size
M=0
else # 2-D array
L=$(echo $size | awk -F'*' '{print $1}')
M=$(echo $size | awk -F'*' '{print $2}')
echo " char $name[$L][$M];" >> mgd77_functions.h
fi
if [ $L -eq 1 ]; then # Single character
length1=1
length2=1
pre="&" # We need to take address of a single char
# fmt="%c"
elif [ $M -eq 0 ]; then # Single text length given
length1="strlen (${pre}P[0]->$name)"
length2="strlen (${pre}P[1]->$name)"
else # 2-D text array, dim and length given, calc total size
n_item=$L
length1=$(echo $M $L | awk '{print $1*$2}')
length2=$length1
cast="(char *)"
fi
if [ $L -eq 1 ]; then # Special handling since these are single characters that may be \0
echo " $REV = MGD77_Get_Param (C, F, "\"$name\"", ${cast}${pre}P[0]->$name, ${cast}${pre}P[1]->$name);" >> mgd77_functions.c
echo " MGD77_Put_Param (C, F, "\"$name\"", (size_t)$length1, ${cast}${pre}P[0]->$name, (size_t)$length2, ${cast}${pre}P[1]->$name, $REV);" >> $$.2
echo " (void) nc_del_att (F->nc_id, NC_GLOBAL, "\"${name}_REVISED\"");" >> $$.5
# The next line gives " Parameter_Name :Value". This format is deliberate in that we may want to
# use awk -F: to separate out the parameter ($1) and the value ($2). Remember Value could be a sentence with spaces!
echo " word[0] = P->$name;" >> $$.3
echo " if (F->Want_Header_Item[$key]) printf (\"%s %44s : ${fmt}%c\", F->NGDC_id, \"$name\", word, EOL);" >> $$.3
echo " for (i = 0; i < 2; i++) H[$key].ptr[i] = ${cast}${pre}P[i]->$name;" >> $$.7
else
echo " $REV = MGD77_Get_Param (C, F, "\"$name\"", ${cast}${pre}P[0]->$name, ${cast}${pre}P[1]->$name);" >> mgd77_functions.c
echo " MGD77_Put_Param (C, F, "\"$name\"", $length1, ${cast}${pre}P[0]->$name, $length2, ${cast}${pre}P[1]->$name, $REV);" >> $$.2
echo " (void) nc_del_att (F->nc_id, NC_GLOBAL, "\"${name}_REVISED\"");" >> $$.5
# The next line gives " Parameter_Name :Value". This format is deliberate in that we may want to
# use awk -F: to separate out the parameter ($1) and the value ($2). Remember Value could be a sentence with spaces!
echo " if (F->Want_Header_Item[$key]) printf (\"%s %44s : ${fmt}%c\", F->NGDC_id, \"$name\", P->$name, EOL);" >> $$.3
echo " for (i = 0; i < 2; i++) H[$key].ptr[i] = ${cast}${pre}P[i]->$name;" >> $$.7
fi
if [ $check = "Y" ]; then
echo " \"$name\" $L $rec $item TRUE FALSE NULL" >> $$.6
else
echo " \"$name\" $L $rec $item FALSE FALSE NULL" >> $$.6
fi
key=$(expr $key + 1)
last=$rec
done < $$.1
n_names=$(cat $$.6 | wc -l | awk '{printf "%d\n", $1}')
cat << EOF >> mgd77_functions.c
for (i = 0, F->revised = FALSE; !F->revised && i < MGD77_N_HEADER_PARAMS; i++) if (L[i].revised) F->revised = TRUE;
}
void MGD77_Write_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F, struct MGD77_HEADER_PARAMS **P)
{
/* Write the MGD77 header parameters as attributes of the netCDF-encoded data set */
struct MGD77_HEADER_LOOKUP *L;
L = MGD77_Header_Lookup;
EOF
cat $$.2 >> mgd77_functions.c
cat << EOF >> mgd77_functions.c
}
void MGD77_Dump_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F, struct MGD77_HEADER_PARAMS *P)
{
char word[2] = { '\0', '\0'}, EOL = '\n';
/* Write all the individual MGD77 header parameters to stdout */
EOF
cat $$.3 >> mgd77_functions.c
cat << EOF >> mgd77_functions.c
}
void MGD77_Reset_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F)
{
/* Remove the revised MGD77 header attributes so we return to the original values.
* Here we simply ignore return values since many of these are presumably unknown attributes.
* File is assumed to be in define mode. */
EOF
cat $$.5 >> mgd77_functions.c
cat << EOF >> mgd77_functions.c
(void) nc_del_att (F->nc_id, NC_GLOBAL, "E77");
}
GMT_LONG MGD77_Get_Param (struct GMT_CTRL *C, struct MGD77_CONTROL *F, char *name, char *value_orig, char *value_rev)
{ /* Get a single parameter: original if requested, otherwise check for revised value first */
GMT_LONG got_rev = FALSE;
if (!F->original) { /* Must look for revised attribute unless explicitly turned off [ e.g, mgd77convert -FC] */
char Att[64] = {""};
sprintf (Att, "%s_REVISED", name); /* Revised attributes have _REVISED at the end of their names */
if (nc_get_att_text (F->nc_id, NC_GLOBAL, Att, value_rev) == NC_NOERR) got_rev = TRUE; /* Found a revised attribute */
}
/* Next, we get the original value */
MGD77_nc_status (C, nc_get_att_text (F->nc_id, NC_GLOBAL, name, value_orig));
return (got_rev);
}
void MGD77_Put_Param (struct GMT_CTRL *C, struct MGD77_CONTROL *F, char *name, size_t length_orig, char *value_orig, size_t length_rev, char *value_revised, GMT_LONG revised)
{ /* Function assumes we are in define mode.
* Place a single parameter in one of several ways:
* revised == 2: Only write the revised attribute [This only happens in mgd77manage where we update a value via -Ae]
* revised == 1: Write both revised and original attribute; [e.g., mgd77manage -D needs this]
* revised == 0: Only write original attribute;
* If F->original is TRUE place a revised attribute name.
*/
if (revised == 2 || revised == 0) MGD77_nc_status (C, nc_put_att_text (F->nc_id, NC_GLOBAL, name, length_orig, value_orig));
if (revised) { /* Write revised attribute */
char Att[64] = {""};
sprintf (Att, "%s_REVISED", name); /* Revised attributes have _REVISED at the end of their names */
MGD77_nc_status (C, nc_put_att_text (F->nc_id, NC_GLOBAL, Att, length_rev, value_revised));
}
}
EOF
cat << EOF >> mgd77_functions.h
};
void MGD77_Write_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F, struct MGD77_HEADER_PARAMS **P);
GMT_LONG MGD77_Get_Param (struct GMT_CTRL *C, struct MGD77_CONTROL *F, char *name, char *value_orig, char *value_revised);
void MGD77_Put_Param (struct GMT_CTRL *C, struct MGD77_CONTROL *F, char *name, size_t length_orig, char *value_orig, size_t length_rev, char *value_revised, GMT_LONG revised);
void MGD77_Read_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F, struct MGD77_HEADER_PARAMS **P);
void MGD77_Dump_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F, struct MGD77_HEADER_PARAMS *P);
void MGD77_Reset_Header_Params (struct GMT_CTRL *C, struct MGD77_CONTROL *F);
void MGD77_Init_Ptr (struct GMT_CTRL *C, struct MGD77_HEADER_LOOKUP *H, struct MGD77_HEADER_PARAMS **P);
int MGD77_Param_Key (struct GMT_CTRL *C, GMT_LONG record, int item);
#define MGD77_N_HEADER_PARAMS $n_names
extern struct MGD77_HEADER_LOOKUP MGD77_Header_Lookup[];
#endif /* _MGD77_FUNCTIONS_H */
EOF
cat << EOF >> mgd77_functions.c
struct MGD77_HEADER_LOOKUP MGD77_Header_Lookup[MGD77_N_HEADER_PARAMS] = {
EOF
awk '{printf "\t{ %-46s, %3d, %2d, %2d, %5s, %5s, { %s, %s } },\n", $1, $2, $3, $4, $5, $6, $7, $7}' $$.6 >> mgd77_functions.c
cat << EOF >> mgd77_functions.c
};
void MGD77_Init_Ptr (struct GMT_CTRL *C, struct MGD77_HEADER_LOOKUP *H, struct MGD77_HEADER_PARAMS **P)
{ /* Assigns array of pointers to each individual parameter */
int i;
EOF
cat $$.7 >> mgd77_functions.c
cat << EOF >> mgd77_functions.c
}
EOF
rm -f $$.*
echo "mgd77netcdfhelper.sh: mgd77_functions.[ch] created"