56{
57 GetPot cl(argc, argv);
58
59
60 if (!cl.search("--input"))
61 {
62 std::cerr << "No --input argument found!" << std::endl;
64 }
65 const char * meshname = cl.next("");
66
67 if (!cl.search("--oldid"))
68 {
69 std::cerr << "No --oldid argument found!" << std::endl;
71 }
72 long oldid = cl.next(0);
73
74 if (!cl.search("--newid"))
75 {
76 std::cerr << "No --newid argument found!" << std::endl;
78 }
79 long newid = cl.next(0);
80
81 unsigned char flags = 0;
82 if (cl.search("--nodesetonly"))
84 if (cl.search("--sidesetonly"))
85 flags |= SIDES;
86 if (cl.search("--blockonly"))
87 flags |= BLOCKS;
88 if (cl.search("--dim"))
89 flags |= EXODUS_DIM;
90
91
92 if (!flags)
93 flags =
NODES | SIDES | BLOCKS;
94
95
96 if (flags != NODES &&
97 flags != SIDES &&
98 flags != BLOCKS &&
99 flags != EXODUS_DIM &&
100 flags != (NODES | SIDES | BLOCKS))
101 {
102 std::cerr << "Only one of the following options may be active [--nodesetonly | --sidesetonly | --blockonly | --dim]!" << std::endl;
104 }
105
106
107 std::string var_name, dim_name;
109 int nc_id, var_id, dim_id;
110 size_t dim_len;
111
112 status = nc_open (meshname, NC_WRITE, &nc_id);
113 if (status != NC_NOERR)
handle_error(status,
"Error while opening file.");
114
115 for (unsigned char mask = 8; mask; mask/=2)
116 {
117
118 switch (flags & mask)
119 {
120 case BLOCKS:
121 dim_name = DIM_NUM_EL_BLK;
122 var_name = VAR_ID_EL_BLK;
123 break;
124 case SIDES:
125 dim_name = DIM_NUM_SS;
126 var_name = VAR_SS_IDS;
127 break;
129 dim_name = DIM_NUM_NS;
130 var_name = VAR_NS_IDS;
131 break;
132 case EXODUS_DIM:
133 dim_name = DIM_NUM_DIM;
134
135 break;
136 default:
137
138 continue;
139 }
140
141
142 status = nc_inq_dimid (nc_id, dim_name.c_str(), &dim_id);
143 if (status != NC_NOERR)
handle_error(status,
"Error while inquiring about a dimension's ID.");
144
145 status = nc_inq_dimlen (nc_id, dim_id, &dim_len);
146 if (status != NC_NOERR)
handle_error(status,
"Error while inquiring about a dimension's length.");
147
148 if ((flags & mask) != EXODUS_DIM)
149 {
150
151 std::vector<long> var_vals(dim_len);
152
153 status = nc_inq_varid (nc_id, var_name.c_str(), &var_id);
154 if (status != NC_NOERR)
handle_error(status,
"Error while inquiring about a variable's ID.");
155
156 status = nc_get_var_long (nc_id, var_id, var_vals.data());
157 if (status != NC_NOERR)
handle_error(status,
"Error while retrieving a variable's values.");
158
159
160 for (unsigned int i=0; i<dim_len; ++i)
161 if (var_vals[i] == oldid)
162 var_vals[i] = newid;
163
164
165 status = nc_put_var_long (nc_id, var_id, var_vals.data());
166 if (status != NC_NOERR)
handle_error(status,
"Error while writing a variable's values.");
167 }
168
169
170 else
171 {
172
173 if (dim_len == (size_t)oldid)
174 {
175
176
177
178
179
181 if (status != NC_NOERR)
handle_error(status,
"Error while putting file into define mode.");
182
183
184
185
186
187
188
189
190 std::string random_dim_name;
192 random_dim_name = std::string("ignored_num_dim_") + random_dim_name;
193
194
195 status = nc_rename_dim(nc_id, dim_id, random_dim_name.c_str());
196 if (status != NC_NOERR)
handle_error(status,
"Error while trying to rename num_dim.");
197
198
199 int dummy=0;
200 status = nc_def_dim (nc_id, dim_name.c_str(), newid, &dummy);
201 if (status != NC_NOERR)
handle_error(status,
"Error while trying to define num_dim.");
202
203
204 status = nc_enddef(nc_id);
205 if (status != NC_NOERR)
handle_error(status,
"Error while leaving define mode.");
206 }
207 }
208 }
209
210
212
213 return (status != NC_NOERR);
214}
void gen_random_string(std::string &s, const int len)
void handle_error(int error, std::string message)
void usage_error(const char *progname)