-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_func.com
More file actions
executable file
·1665 lines (1535 loc) · 56.7 KB
/
map_func.com
File metadata and controls
executable file
·1665 lines (1535 loc) · 56.7 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/tcsh -f
#
# perform some c function on an electron density map
# using the "float_func.c" program -James Holton 12-31-22
#
#
# defaults
set mapfile1 = signal.map
set mapfile2 = noise.map
set func = divide
set param = ""
set outfile = output.map
set tempfile = ${CCP4_SCR}/map_func$$
set logfile = /dev/null
set tempfile = tempfile_mapfunc_
#set logfile = /dev/tty
set logfile = debuglog.log
#echo -n "" >! $logfile
set i = 0
set user_maps = 0
while( $i < $#argv )
@ i = ( $i + 1 )
set arg = "$argv[$i]"
if( "$arg" =~ *.map ) then
@ user_maps = ( $user_maps + 1 )
if($user_maps == 1) then
set mapfile1 = "$arg"
set mapfile2 = ""
endif
if($user_maps == 2) set mapfile2 = "$arg"
if($user_maps == 3) set outfile = "$arg"
endif
set test = `echo $arg | awk -F "=" '/^func/{print $2}'`
if( "$test" != "" ) set func = "$arg"
set test = `echo $arg | awk -F "=" '/^func/{print $2}'`
if( "$arg" =~ "-func" && $i < $#argv ) then
@ i = ( $i + 1 )
set func = "$argv[$i]"
endif
if( "$arg" =~ "-param" && $i < $#argv ) then
@ i = ( $i + 1 )
set param = "-param $argv[$i]"
endif
if( ( "$arg" =~ "-output" || "$arg" =~ "-outfile" ) && $i < $#argv ) then
@ i = ( $i + 1 )
set outfile = "$argv[$i]"
endif
end
if("$*" == "" || "$*" =~ "-h"* ) then
cat << EOF
usage $0 signal.map [noise.map] -param 1 -func divide [output.map]
where:
signal.map is a CCP4 format electron density map
noise.map is an optional second map for binary functions
param a value to serve as the second map, or a third argument to a ternary function
func one of:
sqrt, cbrt, ceil, floor, fabs,
sin, asin, sinh, asinh,
cos, acos, cosh, acosh,
tan, atan, tanh, atanh,
erf, erfc, exp, log, log10,
j0, j1, jn, y0, y1, yn, gamma, lgamma,
pow, erfpow where erfpow is erf(rho)^n
norm the normal distribution
urand, grand, lrand, prand, erand, trand for uniform, gaussian, lorentz, poisson, exponential or triangle-random values
set = for the output map to have all one value
add, subtract, multiply, divide, inverse, negate,
maximum, minimum, nanzero,
thresh output 1 or 0 if first map is greather than second
maxradius avgradius
EOF
exit 9
endif
# synonyms
if("$func" == "mult") set func = "multiply"
if("$func" == "sub") set func = "subtract"
echo "selected $func function with parameters: $param"
if("$func" == "add" || "$func" == "subtract" || "$func" == "multiply" || "$func" == "divide" || "$func" == "max" || "$func" == "min" || "$func" == "pow") then
# two maps are expected?
else
if($user_maps == 2) then
set outfile = "$mapfile2"
set mapfile2 = ""
endif
endif
# use local copy if its there
if(-x ./float_func) then
set path = ( . $path )
endif
if(! -e "$mapfile1") then
set BAD = "$mapfile1 does not exist."
goto exit
endif
echo go | mapdump mapin $mapfile1 | tee ${tempfile}mapdump.log |\
awk '/Grid sampling on x, y, z/{gx=$8;gy=$9;gz=$10}\
/Maximum density /{max=$NF}\
/Cell dimensions /{xs=$4/gx;ys=$5/gy;zs=$6/gz}\
/Number of columns, rows, sections/{nc=$7;nr=$8;ns=$9}\
END{print xs,ys,zs,nc,nr,ns,max}' >! ${tempfile}mapstuff.txt
echo "$mapfile1 :"
egrep dens ${tempfile}mapdump.log
if(-e "$mapfile2") then
echo "$mapfile2 :"
echo go | mapdump mapin $mapfile2 | egrep dens
else
if("$mapfile2" != "") then
echo "WARNING: $mapfile2 does not exist."
endif
endif
set xsize = `awk '{print $4}' ${tempfile}mapstuff.txt`
set ysize = `awk '{print $5}' ${tempfile}mapstuff.txt`
set zsize = `awk '{print $6}' ${tempfile}mapstuff.txt`
set voxels = `awk '{print $4*$5*$6}' ${tempfile}mapstuff.txt`
set size = `echo $voxels | awk '{print 4*$1}'`
set head = `ls -l $mapfile1 | awk -v size=$size '{print $5-size}'`
set skip = `echo $head | awk '{print $1+1}'`
rm -f ${tempfile}mapstuff.txt
rm -f ${tempfile}mapdump.log
if("$param" == "-param voxels") then
echo "$voxels voxels"
set power = `echo "$voxels" | awk '{printf("%d",$1)}'`
set param = "-param $power"
endif
echo "DEBUG: $param"
# now perform the function on the map data
func:
rm -f ${outfile} ${tempfile}output.bin
head -c $head $mapfile1 >! ${tempfile}temp.map
tail -c +$skip $mapfile1 >! ${tempfile}input1.bin
if(-e "$mapfile2") then
tail -c +$skip $mapfile2 >! ${tempfile}input2.bin
set map2 = ${tempfile}input2.bin
else
set map2 = ""
endif
ls -l ${tempfile}input1.bin $map2 >& $logfile
echo "float_func ${tempfile}input1.bin $map2 ${tempfile}output.bin -func $func $param -xsize $xsize -ysize $ysize -zsize $zsize" >> $logfile
float_func ${tempfile}input1.bin $map2 -output ${tempfile}output.bin -func $func $param -xsize $xsize -ysize $ysize -zsize $zsize >> $logfile
if(-e ${tempfile}output.bin) then
echo "success! "
od -w4 --read-bytes=64 -f ${tempfile}input1.bin >! ${tempfile}before.txt
od -w4 --read-bytes=64 -f ${tempfile}output.bin >! ${tempfile}after.txt
cat ${tempfile}before.txt ${tempfile}after.txt |\
awk '! bef[$1]{bef[$1]=$2;next} {print bef[$1],"->",$2}'
cat ${tempfile}output.bin >> ${tempfile}temp.map
echo "scale factor 1 0" | mapmask mapin ${tempfile}temp.map mapout $outfile >> $logfile
rm -f ${tempfile}output.bin
set map2 = ""
if(-e "$mapfile2") set map2 = ",$mapfile2"
set pparm = `echo $param | awk '{print $2}'`
if("$pparm" != "" ) set pparm = ",$pparm"
echo "$outfile = ${func}(${mapfile1}${map2}${pparm}) "
else
echo "ack! "
if(! -e float_func.c) goto compile_float_func
set BAD = "unable to run float_func"
goto exit
endif
rm -f ${tempfile}temp.map
rm -f ${tempfile}input1.bin
rm -f ${tempfile}input2.bin
rm -f ${tempfile}output.bin
echo "$outfile :"
echo "go" | mapdump mapin $outfile | egrep density
exit:
if($?BAD) then
echo "ERROR: $BAD"
exit 9
endif
exit
#############################################################################
#############################################################################
compile_float_func:
echo "attempting to generate float_func utility ..."
cat << EOF >! float_func.c
/* apply a C function to each value in a raw "float" input file -James Holton 3-6-23
example:
gcc -O -O -o float_func float_func.c -lm
./float_func -func erf snr.bin occ.bin
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
char *infile1name = NULL;
char *infile2name = NULL;
FILE *infile1 = NULL;
FILE *infile2 = NULL;
char *outfilename = "output.bin\\0";
FILE *outfile = NULL;
typedef enum { UNKNOWN, SQRT, CBRT, CEIL, FLOOR, FABS,
SIGN,
SIN, ASIN, SINH, ASINH,
COS, ACOS, COSH, ACOSH,
TAN, ATAN, TANH, ATANH,
ERF, ERFC, EXP, SAFEXP, LOG, LOG10,
J0, J1, JN, Y0, Y1, YN, GAMMA, LGAMMA,
POW, ERFPOW,
NORM,
URAND, GRAND, LRAND, PRAND, ERAND, TRAND,
SET,
ADD, SUBTRACT, MULTIPLY, DIVIDE, INVERSE, NEGATE,
MAXIMUM, MINIMUM, NANZERO,
THRESH,
FFT, INVFFT, REALFFT, INVREALFFT, AB2PHIF, PHIF2AB,
SWAB2, SWAB4,
ODD, EVEN, ODDEVEN, EVENODD,
REVERSE, STRIDE, STITCH, FLIP, FLOP, FLIPFLOP,
MAXPOOL, AVGBOX,
MAXRADIUS, AVGRADIUS } func_name;
/* for byte swapping */
typedef union {
unsigned char byte[4];
unsigned short int word[2];
float floaty;
} FOURBYTES;
FOURBYTES swapper,swappee;
/* random deviate with Poisson distribution */
float poidev(float xm, long *idum);
/* random deviate with Gaussian distribution */
float gaussdev(long *idum);
/* random deviate with Lorentzian distribution */
float lorentzdev(long *idum);
/* random deviate with triangle-shaped distribution */
float triangledev(long *idum);
/* random deviate with exponential distribution (>0) */
float expdev(long *idum);
/* random deviate with uniform distribution */
float ran1(long *idum);
/* FFT function */
float *Fourier(float *data, unsigned long length, int direction);
long seed;
int debug = 0;
int main(int argc, char** argv)
{
int n,i,j,k,pixels,outpixels;
int xsize=0,ysize=0,zsize=0,x,y,z;
int xosize=0,yosize=0,zosize=0,xo,yo,zo;
int xostart=0,yostart=0,zostart=0;
float *outimage;
float *inimage1;
float *inimage2;
int *count;
char *headerstuff;
int header=0,outheader=0;
func_name func = UNKNOWN;
float param=1.0;
int parami;
int user_param = 0, map_param = 0;
float sum,sumd,sumsq,sumdsq,avg,rms,rmsd,min,max;
int ignore_values=0,valid_pixels=0;
float ignore_value[70000];
unsigned short int *invalid_pixel;
float phi,F,a,b;
/* check argument list */
for(i=1; i<argc; ++i)
{
if(strlen(argv[i]) > 4)
{
if(strstr(argv[i]+strlen(argv[i])-4,".bin") || strstr(argv[i]+strlen(argv[i])-4,".map"))
{
printf("filename: %s\\n",argv[i]);
if(infile1name == NULL){
infile1name = argv[i];
}
else
{
if(infile2name == NULL){
infile2name = argv[i];
}
else
{
outfilename = argv[i];
}
}
}
}
if(argv[i][0] == '-')
{
/* option specified */
if(strstr(argv[i], "-debug"))
{
debug = 1;
continue;
}
if(strstr(argv[i], "-func") && (argc >= (i+1)))
{
func = UNKNOWN;
if(strstr(argv[i+1],"sqrt")) func = SQRT;
if(strstr(argv[i+1],"cbrt")) func = CBRT;
if(strstr(argv[i+1],"ceil")) func = CEIL;
if(strstr(argv[i+1],"floor")) func = FLOOR;
if(strstr(argv[i+1],"abs")) func = FABS;
if(strstr(argv[i+1],"fabs")) func = FABS;
if(strstr(argv[i+1],"sign")) func = SIGN;
if(strstr(argv[i+1],"sin")) func = SIN;
if(strstr(argv[i+1],"asin")) func = ASIN;
if(strstr(argv[i+1],"sinh")) func = SINH;
if(strstr(argv[i+1],"asinh")) func = ASINH;
if(strstr(argv[i+1],"cos")) func = COS;
if(strstr(argv[i+1],"acos")) func = ACOS;
if(strstr(argv[i+1],"cosh")) func = COSH;
if(strstr(argv[i+1],"acosh")) func = ACOSH;
if(strstr(argv[i+1],"tan")) func = TAN;
if(strstr(argv[i+1],"atan")) func = ATAN;
if(strstr(argv[i+1],"tanh")) func = SINH;
if(strstr(argv[i+1],"atanh")) func = ATANH;
if(strstr(argv[i+1],"erf")) func = ERF;
if(strstr(argv[i+1],"erfc")) func = ERFC;
if(strstr(argv[i+1],"pow")) func = POW;
if(strstr(argv[i+1],"erfpow")) func = ERFPOW;
if(strstr(argv[i+1],"exp")) func = EXP;
if(strstr(argv[i+1],"safexp")) func = SAFEXP;
if(strstr(argv[i+1],"log")) func = LOG;
if(strstr(argv[i+1],"log10")) func = LOG10;
if(strstr(argv[i+1],"j0")) func = J0;
if(strstr(argv[i+1],"j1")) func = J1;
if(strstr(argv[i+1],"jn")) func = JN;
if(strstr(argv[i+1],"y0")) func = Y0;
if(strstr(argv[i+1],"y1")) func = Y1;
if(strstr(argv[i+1],"yn")) func = YN;
if(strstr(argv[i+1],"gamma")) func = GAMMA;
if(strstr(argv[i+1],"lgamma")) func = LGAMMA;
if(strstr(argv[i+1],"norm")) func = NORM;
if(strstr(argv[i+1],"urand")) func = URAND;
if(strstr(argv[i+1],"grand")) func = GRAND;
if(strstr(argv[i+1],"lrand")) func = LRAND;
if(strstr(argv[i+1],"prand")) func = PRAND;
if(strstr(argv[i+1],"erand")) func = ERAND;
if(strstr(argv[i+1],"trand")) func = TRAND;
if(strstr(argv[i+1],"zero")) { func = SET; param = 0.0; user_param=1;};
if(strstr(argv[i+1],"one")) { func = SET; param = 1.0; user_param=1;};
if(strstr(argv[i+1],"unity")) { func = SET; param = 1.0; user_param=1;};
if(strstr(argv[i+1],"set")) func = SET;
if(strstr(argv[i+1],"const")) func = SET;
if(strstr(argv[i+1],"constant")) func = SET;
if(strstr(argv[i+1],"add")) func = ADD;
if(strstr(argv[i+1],"subtract")) func = SUBTRACT;
if(strstr(argv[i+1],"multiply")) func = MULTIPLY;
if(strstr(argv[i+1],"mult")) func = MULTIPLY;
if(strstr(argv[i+1],"divide")) func = DIVIDE;
if(strstr(argv[i+1],"div")) func = DIVIDE;
if(strstr(argv[i+1],"inverse")) func = INVERSE;
if(strstr(argv[i+1],"inv")) func = INVERSE;
if(strstr(argv[i+1],"negative")) func = NEGATE;
if(strstr(argv[i+1],"negate")) func = NEGATE;
if(strstr(argv[i+1],"max")) func = MAXIMUM;
if(strstr(argv[i+1],"maximum")) func = MAXIMUM;
if(strstr(argv[i+1],"min")) func = MINIMUM;
if(strstr(argv[i+1],"minimum")) func = MINIMUM;
if(strstr(argv[i+1],"nanzero")) func = NANZERO;
if(strstr(argv[i+1],"thresh")) func = THRESH;
if(strstr(argv[i+1],"fft")) func = FFT;
if(strstr(argv[i+1],"invfft")) func = INVFFT;
if(strstr(argv[i+1],"realfft")) func = REALFFT;
if(strstr(argv[i+1],"invrealfft")) func = INVREALFFT;
if(strstr(argv[i+1],"ab2phif")) func = AB2PHIF;
if(strstr(argv[i+1],"phif2ab")) func = PHIF2AB;
if(strstr(argv[i+1],"swab")) func = SWAB4;
if(strstr(argv[i+1],"swab2")) func = SWAB2;
if(strstr(argv[i+1],"swap")) func = SWAB4;
if(strstr(argv[i+1],"swap2")) func = SWAB2;
if(strstr(argv[i+1],"odd")) func = ODD;
if(strstr(argv[i+1],"even")) func = EVEN;
if(strstr(argv[i+1],"oddeven")) func = ODDEVEN;
if(strstr(argv[i+1],"evenodd")) func = EVENODD;
if(strstr(argv[i+1],"reverse")) func = REVERSE;
if(strstr(argv[i+1],"stride")) func = STRIDE;
if(strstr(argv[i+1],"stitch")) func = STITCH;
if(strstr(argv[i+1],"flip")) func = FLIP;
if(strstr(argv[i+1],"flop")) func = FLOP;
if(strstr(argv[i+1],"flipflop")) func = FLIPFLOP;
if(strstr(argv[i+1],"maxpool")) func = MAXPOOL;
if(strstr(argv[i+1],"avgbox")) func = AVGBOX;
if(strstr(argv[i+1],"maxradius")) func = MAXRADIUS;
if(strstr(argv[i+1],"avgradius")) func = AVGRADIUS;
if(strstr(argv[i+1],"+")) func = ADD;
if(strstr(argv[i+1],"-")) func = SUBTRACT;
if(strstr(argv[i+1],"*")) func = MULTIPLY;
if(strstr(argv[i+1],"/")) func = DIVIDE;
if(strstr(argv[i+1],"1/")) {func = INVERSE ; param = 1.0; user_param=1;};
if(strstr(argv[i+1],"1-")) {func = NEGATE ; param = 1.0; user_param=1;};
if(func == UNKNOWN) printf("WARNING: unknown function: %s\\n",argv[i+1]);
}
if(strstr(argv[i], "-param") && (argc >= (i+1)))
{
param = atof(argv[i+1]);
user_param=1;
}
if(strstr(argv[i], "-xsize") && (argc >= (i+1)))
{
xsize = atoi(argv[i+1]);
}
if(strstr(argv[i], "-ysize") && (argc >= (i+1)))
{
ysize = atoi(argv[i+1]);
}
if(strstr(argv[i], "-zsize") && (argc >= (i+1)))
{
zsize = atoi(argv[i+1]);
}
if(strstr(argv[i], "-ignore") && (argc >= (i+1)))
{
++ignore_values;
ignore_value[ignore_values] = atof(argv[i+1]);
}
if(strstr(argv[i], "-seed") && (argc >= (i+1)))
{
seed = -atoi(argv[i+1]);
}
if(strstr(argv[i], "-box") || strstr(argv[i], "-roi"))
{
if(argc >= (i+2))
{
xostart = atol(argv[i+1]);
xosize = atol(argv[i+2]);
xosize -= xostart;
}
if(argc >= (i+4))
{
yostart = atol(argv[i+3]);
yosize = atol(argv[i+4]);
yosize -= yostart;
}
if(argc >= (i+6))
{
zostart = atol(argv[i+5]);
zosize = atol(argv[i+6]);
zosize -= zostart;
}
}
if((strstr(argv[i], "-output") || strstr(argv[i], "-outfile")) && (argc >= (i+1)))
{
outfilename = argv[i+1];
++i;
continue;
}
if((strstr(argv[i], "-input1") || strstr(argv[i], "-infile1")) && (argc >= (i+1)))
{
infile1name = argv[i+1];
++i;
continue;
}
if((strstr(argv[i], "-input2") || strstr(argv[i], "-infile2")) && (argc >= (i+1)))
{
infile2name = argv[i+1];
++i;
continue;
}
if(strstr(argv[i], "-header") && (argc >= (i+1)))
{
header = outheader = atof(argv[i+1]);
}
if(strstr(argv[i], "-outheader") && (argc >= (i+1)))
{
outheader = atof(argv[i+1]);
}
++i;
}
}
if(infile1name != NULL){
if( strstr(infile1name,"-") && strlen(infile1name) == 1 )
{
infile1 = stdin;
}
else {
infile1 = fopen(infile1name,"rb");
}
}
if(infile1 == NULL){
printf("ERROR: unable to open %s as input 1\\n", infile1name);
perror("");
}
if(infile1 == NULL || func == UNKNOWN){
printf("usage: float_func file.bin [file2.bin] [outfile.bin] -func jn -param 1.0 -ignore 0\\n");
printf("options:\\n");
printf("\\tfile.bin\\t binary file containing the arguments to the desired function\\n");
printf("\\tfile2.bin\\t binary file containing second argument to the desired function\\n");
printf("\\t-func\\t may be one of:\\n");
printf("\\t sqrt cbrt ceil floor abs sign\\n");
printf("\\t zero one set (one output value)\\n");
printf("\\t add subtract multiply divide inverse negate maximum minimum \\n");
printf("\\t thresh (1-or-0 threshold)\\n");
printf("\\t nanzero (set all NaN and Inf values to zero)\\n");
printf("\\t sin asin sinh asinh cos acos cosh acosh tan atan tanh atanh (trigonometry)\\n");
printf("\\t pow erf erfpow norm erfc (power and error functions)\\n");
printf("\\t exp log log10 (natural or base-10 log)\\n");
printf("\\t safexp (safe exponential, wont over/underflow)\\n");
printf("\\t j0 j1 jn y0 y1 yn gamma lgamma (Bessel and gamma functions)\\n");
printf("\\t urand grand lrand prand erand trand (uniform gaussian lorentzian poisson exponential or triangle randomness)\\n");
printf("\\t fft invfft realfft invrealfft (complex or real FFT - base 2)\\n");
printf("\\t ab2phif phif2ab (cartesian to amplitude-phase conversion)\\n");
printf("\\t swab4 swab2 (swap bytes)\\n");
printf("\\t odd even oddeven evenodd (extract or interleave values)\\n");
printf("\\t reverse (re-order 4-byte floats in file)\\n");
printf("\\t stride (skip blocks of size param)\\n");
printf("\\t stitch (interleave blocks from different files)\\n");
printf("\\t flip (reverse data in interleaved blocks)\\n");
printf("\\t flop (reverse data as interleaved blocks)\\n");
printf("\\t flipflop (reverse x and y data with -xsize or -param xsize)\\n");
printf("\\t maxpool (maximum value within box with -param edge pixels)\\n");
printf("\\t avgbox (average value of box with -param edge pixels)\\n");
printf("\\t maxradius (maximum value within radius given by -param pixels, no down-sampling)\\n");
printf("\\t avgradius (local average within radius given by -param pixels, no down-sampling)\\n");
printf("\\t-param\\t second value for add, subtract, set, etc. or parameter needed by the function (such as jn)\\n");
printf("\\t-xsize\\t fast dimension when treating data as 2D array\\n");
printf("\\t-ysize\\t 2nd fastest dimension when treating data as 2D or 3D array\\n");
printf("\\t-zsize\\t slowest dimension when treating data as 3D array\\n");
printf("\\t-seed\\t seed for random number functions\\n");
printf("\\t-ignore\\t value found in either input file will pass through\\n");
printf("\\t-header \\tnumber of bytes to ignore in header of each file\\n");
printf("\\t-outheader \\tnumber of bytes of the header to pass on to output file\\n");
printf("\\toutput.bin\\t output binary file containing the results to the desired function\\n");
exit(9);
}
printf("selected function: ");
switch(func){
case AB2PHIF: printf("AB2PHIF\\n"); break;
case PHIF2AB: printf("PHIF2AB\\n"); break;
case ACOS: printf("ACOS\\n"); break;
case ACOSH: printf("ACOSH\\n"); break;
case ADD: printf("ADD\\n"); break;
case ASIN: printf("ASIN\\n"); break;
case ASINH: printf("ASINH\\n"); break;
case ATAN: printf("ATAN\\n"); break;
case ATANH: printf("ATANH\\n"); break;
case CBRT: printf("CBRT\\n"); break;
case CEIL: printf("CEIL\\n"); break;
case COS: printf("COS\\n"); break;
case COSH: printf("COSH\\n"); break;
case DIVIDE: printf("DIVIDE\\n"); break;
case ERAND: printf("ERAND\\n"); break;
case ERF: printf("ERF\\n"); break;
case ERFC: printf("ERFC\\n"); break;
case ERFPOW: printf("ERFPOW\\n"); break;
case EVEN: printf("EVEN\\n"); break;
case EXP: printf("EXP\\n"); break;
case SAFEXP: printf("SAFEXP\\n"); break;
case FABS: printf("FABS\\n"); break;
case SIGN: printf("SIGN\\n"); break;
case FFT: printf("FFT\\n"); break;
case INVFFT: printf("INVFFT\\n"); break;
case REALFFT: printf("REALFFT\\n"); break;
case INVREALFFT: printf("INVREALFFT\\n"); break;
case FLOOR: printf("FLOOR\\n"); break;
case GAMMA: printf("GAMMA\\n"); break;
case GRAND: printf("GRAND\\n"); break;
case INVERSE: printf("INVERSE\\n"); break;
case J0: printf("J0\\n"); break;
case J1: printf("J1\\n"); break;
case JN: printf("JN\\n"); break;
case LGAMMA: printf("LGAMMA\\n"); break;
case LOG: printf("LOG\\n"); break;
case LOG10: printf("LOG10\\n"); break;
case LRAND: printf("LRAND\\n"); break;
case MAXIMUM: printf("MAXIMUM\\n"); break;
case MINIMUM: printf("MINIMUM\\n"); break;
case MULTIPLY: printf("MULTIPLY\\n"); break;
case NANZERO: printf("NANZERO\\n"); break;
case NEGATE: printf("NEGATE\\n"); break;
case NORM: printf("NORM\\n"); break;
case ODD: printf("ODD\\n"); break;
case ODDEVEN: printf("ODDEVEN\\n"); break;
case EVENODD: printf("EVENODD\\n"); break;
case REVERSE: printf("REVERSE\\n"); break;
case STRIDE: printf("STRIDE\\n"); break;
case STITCH: printf("STITCH\\n"); break;
case FLIP: printf("FLIP\\n"); break;
case FLOP: printf("FLOP\\n"); break;
case FLIPFLOP: printf("FLIPFLOP\\n"); break;
case MAXPOOL: printf("MAXPOOL\\n"); break;
case AVGBOX: printf("AVGBOX\\n"); break;
case POW: printf("POW\\n"); break;
case MAXRADIUS: printf("MAXRADIUS %f\\n",param); break;
case AVGRADIUS: printf("AVGRADIUS %f\\n",param); break;
case PRAND: printf("PRAND\\n"); break;
case SET: printf("SET\\n"); break;
case SIN: printf("SIN\\n"); break;
case SINH: printf("SINH\\n"); break;
case SQRT: printf("SQRT\\n"); break;
case SUBTRACT: printf("SUBTRACT\\n"); break;
case SWAB2: printf("SWAB2\\n"); break;
case SWAB4: printf("SWAB4\\n"); break;
case TAN: printf("TAN\\n"); break;
case TANH: printf("TANH\\n"); break;
case THRESH: printf("THRESH\\n"); break;
case TRAND: printf("TRAND\\n"); break;
case UNKNOWN: printf("UNKNOWN\\n"); break;
case URAND: printf("URAND\\n"); break;
case Y0: printf("Y0\\n"); break;
case Y1: printf("Y1\\n"); break;
case YN: printf("YN\\n"); break;
}
/*
awk '/func = / && \$NF !~ /}/{print substr(\$NF,1,length(\$NF)-1);}' ~/Develop/src/float_stuff/float_func.c |\\
sort -u | awk '{ print "\\tcase "\$1": printf(\\""\$1"\\\\n\\"); break;"}'
*/
for(k=1;k<=ignore_values;++k)
{
printf("ignoring value %g in both files\\n",ignore_value[k]);
}
/* load first float-image */
printf("header = %d bytes\\n",header);
if(header)
{
headerstuff = calloc(header+10,1);
rewind(infile1);
fread(headerstuff,header,1,infile1);
}
fseek(infile1,0,SEEK_END);
n = ftell(infile1)-header;
inimage1 = calloc(n+10,1);
invalid_pixel = calloc(n+10,1);
printf("reading %u floats from %s\\n",n/sizeof(float),infile1name);
fseek(infile1,header,SEEK_SET);
fread(inimage1,n,1,infile1);
fclose(infile1);
/* assume file is all pixels */
pixels = outpixels = n/sizeof(float);
/* handy integer version of param */
parami = param;
/* apply defaults to array dimensions */
if(xsize <= 0 && ( func == FLIP || func == FLOP || func == FLIPFLOP ) ) xsize = abs(parami);
if(xsize <= 0) xsize = 1;
if(ysize <= 0) ysize = pixels/xsize;
if(ysize <= 0) ysize = 1;
zsize = pixels/xsize/ysize;
if(zsize <= 0) zsize = 1;
printf("array dimensions: x %d y %d z %d\\n",xsize,ysize,zsize);
/* if the function takes two args ... */
if( func == ADD || func == SUBTRACT || func == MULTIPLY || func == DIVIDE || func == POW ||
func == MAXIMUM || func == MINIMUM || func == THRESH ||
func == EVENODD || func == ODDEVEN ||
func == STITCH ) {
if(infile2name == NULL || user_param )
{
/* use the "param" as the second value */
map_param = 0;
}
else
{
map_param = 1;
}
}
else
{
if(infile2name != NULL)
{
/* second filename must be the output file */
outfilename = infile2name;
infile2name = NULL;
}
}
/* open second file, if it was specified */
if(infile2name != NULL){
infile2 = fopen(infile2name,"r");
if(infile2 == NULL){
printf("ERROR: unable to open %s as input 2\\n", infile2name);
perror("");
exit(9);
}
inimage2 = calloc(n,1);
fseek(infile2,header,SEEK_SET);
fread(inimage2,n,1,infile2);
fclose(infile2);
}
outfile = fopen(outfilename,"wb");
if(outfile == NULL)
{
printf("ERROR: unable to open %s for output\\n", outfilename);
perror("");
exit(9);
}
printf("input1 is: %s\\n",infile1name);
if(map_param)
{
printf("input2 is: %s\\n",infile2name);
}else{
printf("input2 is: %g\\n",param);
}
printf("output to: %s\\n",outfilename);
/* just to keep track */
outimage = NULL;
count = NULL;
if( func == FFT || func == INVFFT || func == REALFFT )
{
/* must be a power of two */
valid_pixels = (int) ceil(pow(2.0,floor(log(pixels)/log(2))));
if(valid_pixels != pixels){
pixels = outpixels = valid_pixels;
printf("truncating data to %d floats\\n",pixels);
}
valid_pixels = 0;
}
if( func == INVREALFFT )
{
/* must be a power of two plus 2 */
valid_pixels = 2+(int) ceil(pow(2.0,floor(log(pixels-2)/log(2))));
if(valid_pixels != pixels){
pixels = outpixels = valid_pixels;
printf("truncating data to %d floats\\n",pixels);
}
valid_pixels = 0;
}
if( func == FFT ){
/* assume input array is complex numbers (even=real, odd=imag) */
/* fortranish, so zeroeth item will not be touched */
outimage = Fourier(inimage1-1, pixels/2, 1);
/* correct for fortranish */
++outimage;
/* correct coefficients so that they back-transform on the same scale */
for(j=0;j<outpixels;++j)
{
outimage[j]/=(outpixels/2);
}
}
if( func == INVFFT ){
/* assume input array is complex numbers (even=real, odd=imag) */
/* fortranish, so zeroeth item will not be touched */
outimage = Fourier(inimage1-1, pixels/2, -1);
/* correct for fortranish */
++outimage;
}
if( func == REALFFT ){
/* need to convert to complex numbers */
outimage = calloc(pixels+2,2*sizeof(float));
for(i=0;i<pixels;++i)
{
j = 2*i;
outimage[j] = inimage1[i]/(pixels);
}
/* output will be twice the size */
/* fortranish, so zeroeth item will not be touched */
outimage = Fourier(outimage-1, pixels, 1);
/* correct for fortranish */
++outimage;
/* but we are only interested in first half (plus one more for the Nyquist frequency) */
pixels=pixels+2;
}
if( func == INVREALFFT ){
pixels-=2;
outimage = calloc(pixels,2*sizeof(float));
/* compensate for zero-value negative index coefficients by doubling non-trivial ones */
outimage[0]=inimage1[0];
outimage[pixels]=inimage1[pixels];
for(j=1;j<pixels;++j)
{
outimage[j] = 2*inimage1[i];
}
/* fortranish, so zeroeth item will not be touched */
outimage = Fourier(outimage-1, pixels, -1);
/* correct for fortranish */
++outimage;
/* but we are only interested in even numbered ones (real part) */
for(j=0;j<pixels;++j)
{
outimage[j] = outimage[2*j];
}
}
if( func == ODD || func == EVEN ){
/* output will be half the size */
outpixels = pixels = pixels/2;
}
if( func == ODDEVEN || func == EVENODD ){
/* interleaving values, so output will be twice the size */
outpixels = 2*pixels;
outimage = calloc(outpixels,sizeof(float));
}
if( func == AB2PHIF || func == PHIF2AB ){
/* values of interest are two floats */
outimage = calloc(pixels,sizeof(float));
pixels = pixels/2;
/* we will restore true float-count before output */
}
if( func == STITCH ){
/* output file is twice the size */
outpixels = 2*pixels;
outimage = calloc(outpixels,sizeof(float));
}
if( func == MAXPOOL || func == AVGBOX ){
/* param is the down-sampling box size */
if(xosize <= 0 ) xosize = (int) xsize/param;
if(yosize <= 0 ) yosize = (int) ysize/param;
if(zosize <= 0 ) zosize = (int) zsize/param;
if(xosize <= 0 ) xosize = 1;
if(yosize <= 0 ) yosize = 1;
if(zosize <= 0 ) zosize = 1;
/* output file is smaller */
outpixels = xosize*yosize*zosize;
outimage = calloc(outpixels,sizeof(float));
count = calloc(outpixels,sizeof(int));
}
if( func == MAXRADIUS || func == AVGRADIUS ){
/* param is the neighbor radius */
if(xosize <= 0 ) xosize = (int) xsize;
if(yosize <= 0 ) yosize = (int) ysize;
if(zosize <= 0 ) zosize = (int) zsize;
if(xosize <= 0 ) xosize = 1;
if(yosize <= 0 ) yosize = 1;
if(zosize <= 0 ) zosize = 1;
/* output file is smaller */
outpixels = xosize*yosize*zosize;
outimage = calloc(outpixels,sizeof(float));
count = calloc(outpixels,sizeof(int));
}
/* make sure these get set */
if(xosize <= 0 ) xosize = (int) xsize;
if(yosize <= 0 ) yosize = (int) ysize;
if(zosize <= 0 ) zosize = (int) zsize;
if(xosize <= 0 ) xosize = 1;
if(yosize <= 0 ) yosize = 1;
if(zosize <= 0 ) zosize = 1;
printf("output array dimensions: x %d y %d z %d\\n",xosize,yosize,zosize);
outpixels = xosize*yosize*zosize;
/* see if we need to allocate memory for output image */
if( outimage == NULL ) {
printf("allocating %d pixels\\n",pixels);
outimage = calloc(pixels+10,sizeof(float));
}
/* i is input j is output */
j=0;
for(i=0;i<pixels;++i)
{
j=i;
if(debug) printf("inimage1[%d] = %f\\n",i,inimage1[i]);
/* skip any invalid values, propagate to output */
for(k=1;k<=ignore_values;++k)
{
if(inimage1[i]==ignore_value[k]){
outimage[j] = ignore_value[k];
++invalid_pixel[j];
/* no need to check others */
break;
}
if(infile2name == NULL) continue;
if(inimage2[i]==ignore_value[k]){
outimage[j] = ignore_value[k];
++invalid_pixel[j];
/* no need to check others */
break;
}
}
/* skip all calcs for this one */
if(invalid_pixel[j]) continue;
if( func == AB2PHIF ){
j=i*2;
a = inimage1[j];
b = inimage1[j+1];
F = sqrtf(a*a+b*b);
phi = atan2(b,a);
outimage[j] = F;
outimage[j+1] = phi;
}
if( func == PHIF2AB ){
j=i*2;
F = inimage1[j];
phi = inimage1[j+1];
a = F*cos(phi);
b = F*sin(phi);
outimage[j] = a;
outimage[j+1] = b;
}
if( func == SQRT ){
outimage[j] = sqrtf(inimage1[i]);
}
if( func == CBRT ){
outimage[j] = cbrtf(inimage1[i]);
}
if( func == CEIL ){
outimage[j] = ceilf(inimage1[i]);
}
if( func == FLOOR ){
outimage[j] = floorf(inimage1[i]);
}
if( func == FABS ){
outimage[j] = fabsf(inimage1[i]);
}
if( func == SIGN ){
outimage[j] = 2.0*(inimage1[i] >= 0.0)-1.0;
}
if( func == SIN ){
outimage[j] = sinf(inimage1[i]);
}
if( func == ASIN ){
outimage[j] = asinf(inimage1[i]);
}
if( func == SINH ){
outimage[j] = sinhf(inimage1[i]);
}
if( func == ASINH ){
outimage[j] = asinhf(inimage1[i]);
}
if( func == COS ){
outimage[j] = cosf(inimage1[i]);
}
if( func == ACOS ){
outimage[j] = acosf(inimage1[i]);
}
if( func == COSH ){
outimage[j] = coshf(inimage1[i]);
}
if( func == ACOSH ){
outimage[j] = acoshf(inimage1[i]);
}
if( func == TAN ){
outimage[j] = tanf(inimage1[i]);
}
if( func == ATAN ){
outimage[j] = atanf(inimage1[i]);
}
if( func == TANH ){
outimage[j] = tanhf(inimage1[i]);
}
if( func == ATANH ){
outimage[j] = atanhf(inimage1[i]);
}
if( func == ERF ){
outimage[j] = erff(inimage1[i]);
}
if( func == ERFC ){
outimage[j] = erfcf(inimage1[i]);
}
if( func == EXP ){
outimage[j] = expf(inimage1[i]);
}
if( func == SAFEXP ){
float v;
v = inimage1[i];
if(v > 3.4e38) v = 3.4e38;
if(v < -3.4e38) v = -3.4e38;
outimage[j] = expf(v);
}
if( func == LOG ){
outimage[j] = logf(inimage1[i]);
}
if( func == LOG10 ){