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:
179 fprintf(stderr,
"Unknown format '%d'\n", format);
185 snprintf(paddedS,
sizeof paddedS,
"_%05d", s);
190 if( specified.length()>ext.length() &&
191 specified.substr(specified.length()-ext.length()) == ext )
192 specified = specified.substr(0,specified.length()-ext.length());
194 return specified + slice + ext;
211 enum_format of,
int t0,
int t1,
int stride,
bool explode,
float scale)
215 if( !strcmp(ofname,
"-") )
217 else if( !explode ) {
218 string out_fname =
outfile(ofname, of);
219 out = fopen(out_fname.c_str(),
"w");
221 fprintf(stderr,
"Can't open output file '%s'\n", out_fname.c_str());
226 if (t1 >= h->
t()) t1 = h->
t()-1;
227 int tend = t1==-1? h->
t()-1 : t1;
229 hout.
x( nodes.size() );
232 hout.
t( explode ? 1 : (tend-t0)/stride+1 );
245 if( of==format_arg_IGB ) {
247 if( !explode ) hout.
write();
249 const bool lessnodes = nodes.size() != (size_t) h->
x();
251 std::vector<float> slice_cropped(lessnodes ? nodes.size() * hout.
num_components() : 0);
252 float * slice_ptr = slice.data();
253 float *output_slice = lessnodes ? slice_cropped.data() : slice_ptr;
255 if( of==format_arg_dat_t )
256 fprintf( out,
"%d\n", (tend-t0)/stride+1 );
257 else if( of==format_arg_ascii_1pLn )
258 fprintf( out,
"%d\n",
int(nodes.size()) );
260 for(
int t=t0; t<=tend; t+=stride ) {
262 if( explode && out!=stdout ) {
263 string out_fname =
outfile(ofname, of, t);
264 out = fopen(out_fname.c_str(),
"w");
265 if( of==format_arg_IGB ) {
271 if( of==format_arg_dat_t )
272 fprintf(out,
"%d\n",
int(nodes.size()));
277 if( of== format_arg_asciiTm )
278 fprintf( out,
"%f", h->
org_t()+h->
inc_t()*t );
282 for(
const auto p : nodes)
286 std::for_each(output_slice, output_slice + nodes.size() * hout.
num_components(), [scale](
float & c){ c *= scale; });
289 case format_arg_binary:
292 case format_arg_asciiTm:
295 for(
size_t i = 0; i < nodes.size(); i++)
297 fprintf(out,
" %g", output_slice[cnt++]);
300 case format_arg_ascii_1cpL:
303 for(
size_t i = 0; i < nodes.size(); i++)
305 fprintf(out,
" %g\n", output_slice[cnt++]);
308 case format_arg_ascii_1pL:
309 case format_arg_ascii_1pLn:
312 for(
size_t i = 0; i < nodes.size(); i++) {
314 fprintf(out,
" %g", output_slice[cnt++]);
319 case format_arg_ascii:
322 for(
size_t i=0; i < nodes.size(); i++)
324 fprintf(out,
"%g ", output_slice[cnt++]);
327 case format_arg_dat_t:
330 for(
size_t i=0; i < nodes.size(); i++)
332 fprintf(out,
"%g\n", output_slice[cnt++]);
336 fprintf(stderr,
"Unknown format '%d'\n", of);
338 if( of==format_arg_ascii || of==format_arg_asciiTm )
339 fprintf( out,
"\n" );
340 if( explode && out!=stdout )
344 if( !explode && out != stdout )
352 gengetopt_args_info args;
355 if (cmdline_parser (argc, argv, &args) != 0)
359 gzFile in = ( !args.inputs_num || !strcmp(args.inputs[0],
"-") ) ?
360 gzdopen( dup(0),
"r" ):
361 gzopen( args.inputs[0],
"r" );
363 cerr <<
"File not found: " << args.inputs[0] << endl;
369 if( args.explode_flag && args.format_arg==format_arg_dat_t ) {
370 cerr <<
"Cannot explode into dat_t format" << endl;
375 if( args.list_given )
377 else if( args.input_file_given )
382 for(
auto p : nodes )
384 cerr <<
"Error: out of range nodes requested" << endl;
388 int f0=args.f0_arg, f1=args.f1_arg;
390 f0 = std::round((args.t0_arg-header.
org_t())/header.
inc_t());
391 if( f0<0 || f0>=header.
t() ) {
392 cerr <<
"Illegal ending frame specified: "<<f0<<
" not in [0,"<<header.
t()<<
")"<< endl;
396 f1 = args.t1_arg==-1 ? header.
t()-1 : std::round((args.t1_arg-header.
org_t())/header.
inc_t());
397 if( f1>=0 && f1<f0 ) {
398 cerr <<
"Illegal ending frame specified: "<<f1<<
" not in ["<<f0<<
","<<header.
t()<<
")"<< endl;
402 output(nodes, &header, args.output_file_arg,args.format_arg,
403 f0, f1, args.df_arg, args.explode_flag, args.scale_arg );
405 cmdline_parser_free(&args);
constexpr T max(T a, T b)