28 #include "ext_cmdline.h" 54 if( strchr( sp,
':' ) ){
55 char *colptr = strchr( sp,
':' );
57 sscanf( colptr+1,
"%d%n", &inc, &consumed );
58 if( !consumed || consumed!=strlen(colptr+1) ) {
59 fprintf( stderr,
"Illegal range specified: %s\n", sp );
65 if( !strcmp(
"*", sp ) ) {
67 for(
int i=0; i<
max; i+=inc )
72 if( strchr( sp,
'-' ) ){
75 int nr = sscanf( sp,
"%d-%s", &s, fs );
77 fprintf( stderr,
"Illegal range specified: %s\n", sp );
82 if( !strcmp(fs,
"*") )
86 sscanf( fs,
"%d%n", &f, &consumed );
87 if( consumed != strlen(fs) ) {
88 fprintf( stderr,
"Illegal range specified: %s\n", sp );
94 fprintf( stderr,
"Illegal range specified: %s\n", sp );
97 for(
int i=s; i<=f; i+=inc )
103 sscanf( sp,
"%d", &num );
105 if( !sscanf( sp,
"%d", &num ) || num<0 ) {
106 fprintf( stderr,
"Illegal node number specified in list\n" );
109 nodes.push_back( num );
118 char *lc = strdup( list );
119 char *sp = strtok( lc,
"," );
120 while( sp != NULL ) {
124 sp = strtok( NULL,
"," );
133 FILE *in = fopen( file,
"r" );
135 fprintf( stderr,
"cannot open node list file\n" );
140 while( fgets( buf,
BUFSIZE, in ) != NULL ) {
157 outfile(
string specified, enum_format format,
int s=-1 )
160 return specified.c_str();
164 case format_arg_binary:
165 case format_arg_asciiTm:
166 case format_arg_ascii:
167 case format_arg_ascii_1cpL:
168 case format_arg_ascii_1pL:
169 case format_arg_ascii_1pLn:
172 case format_arg_dat_t:
183 sprintf( paddedS,
"_%05d", s );
188 if( specified.length()>ext.length() &&
189 specified.substr(specified.length()-ext.length()) == ext )
190 specified = specified.substr(0,specified.length()-ext.length());
192 return specified + slice + ext;
209 enum_format of,
int t0,
int t1,
int stride,
bool explode,
float scale)
213 if( !strcmp(ofname,
"-") )
215 else if( !explode ) {
216 string out_fname =
outfile(ofname, of);
217 out = fopen(out_fname.c_str(),
"w");
219 fprintf(stderr,
"Can't open output file '%s'\n", out_fname.c_str());
224 if (t1 >= h->
t()) t1 = h->
t()-1;
225 int tend = t1==-1? h->
t()-1 : t1;
227 hout.
x( nodes.size() );
230 hout.
t( explode ? 1 : (tend-t0)/stride+1 );
243 if( of==format_arg_IGB ) {
245 if( !explode ) hout.
write();
247 const bool lessnodes = nodes.size() != (size_t) h->
x();
249 std::vector<float> slice_cropped(lessnodes ? nodes.size() * hout.
num_components() : 0);
250 float * slice_ptr = slice.data();
251 float *output_slice = lessnodes ? slice_cropped.data() : slice_ptr;
253 if( of==format_arg_dat_t )
254 fprintf( out,
"%d\n", (tend-t0)/stride+1 );
255 else if( of==format_arg_ascii_1pLn )
256 fprintf( out,
"%d\n",
int(nodes.size()) );
258 for(
int t=t0; t<=tend; t+=stride ) {
260 if( explode && out!=stdout ) {
261 string out_fname =
outfile(ofname, of, t);
262 out = fopen(out_fname.c_str(),
"w");
263 if( of==format_arg_IGB ) {
269 if( of==format_arg_dat_t )
270 fprintf(out,
"%d\n",
int(nodes.size()));
275 if( of== format_arg_asciiTm )
276 fprintf( out,
"%f", h->
org_t()+h->
inc_t()*t );
280 for(
const auto p : nodes)
284 std::for_each(output_slice, output_slice + nodes.size() * hout.
num_components(), [scale](
float & c){ c *= scale; });
287 case format_arg_binary:
290 case format_arg_asciiTm:
293 for(
size_t i = 0; i < nodes.size(); i++)
295 fprintf(out,
" %g", output_slice[cnt++]);
298 case format_arg_ascii_1cpL:
301 for(
size_t i = 0; i < nodes.size(); i++)
303 fprintf(out,
" %g\n", output_slice[cnt++]);
306 case format_arg_ascii_1pL:
307 case format_arg_ascii_1pLn:
310 for(
size_t i = 0; i < nodes.size(); i++) {
312 fprintf(out,
" %g", output_slice[cnt++]);
317 case format_arg_ascii:
320 for(
size_t i=0; i < nodes.size(); i++)
322 fprintf(out,
"%g ", output_slice[cnt++]);
325 case format_arg_dat_t:
328 for(
size_t i=0; i < nodes.size(); i++)
330 fprintf(out,
"%g\n", output_slice[cnt++]);
334 if( of==format_arg_ascii || of==format_arg_asciiTm )
335 fprintf( out,
"\n" );
336 if( explode && out!=stdout )
340 if( !explode && out != stdout )
348 gengetopt_args_info args;
351 if (cmdline_parser (argc, argv, &args) != 0)
355 gzFile in = ( !args.inputs_num || !strcmp(args.inputs[0],
"-") ) ?
356 gzdopen( dup(0),
"r" ):
357 gzopen( args.inputs[0],
"r" );
359 cerr <<
"File not found: " << args.inputs[0] << endl;
365 if( args.explode_flag && args.format_arg==format_arg_dat_t ) {
366 cerr <<
"Cannot explode into dat_t format" << endl;
371 if( args.list_given )
373 else if( args.input_file_given )
378 for(
auto p : nodes )
380 cerr <<
"Error: out of range nodes requested" << endl;
384 int f0=args.f0_arg, f1=args.f1_arg;
386 f0 = std::round((args.t0_arg-header.
org_t())/header.
inc_t());
387 if( f0<0 || f0>=header.
t() ) {
388 cerr <<
"Illegal ending frame specified: "<<f0<<
" not in [0,"<<header.
t()<<
")"<< endl;
392 f1 = args.t1_arg==-1 ? header.
t()-1 : std::round((args.t1_arg-header.
org_t())/header.
inc_t());
393 if( f1>=0 && f1<f0 ) {
394 cerr <<
"Illegal ending frame specified: "<<f1<<
" not in ["<<f0<<
","<<header.
t()<<
")"<< endl;
398 output(nodes, &header, args.output_file_arg,args.format_arg,
399 f0, f1, args.df_arg, args.explode_flag, args.scale_arg );
401 cmdline_parser_free(&args);
constexpr T max(T a, T b)