# Convert cfield histogram information (neuron activities) into receptive fields
# $1 name of cfield information file
# $2 threshold for 0/1 rounding
cat $1 | sort -t' ' -k3 -rn | sort -ns | awk '{if(old!=$1) print ""; old=$1; print}END{print""}' | awk '{if(NF==0) {i=1; while(i in frq) {printf " " int(100*frq[i]/cnt+.5)/100; frq[i++]=0.} print ""; cnt=0} else if(NF==2) {printf $0} else {cnt+=$NF; for(i=1;i<=length($2);i++)frq[i]+=$NF*substr($2,i,1)}}' | tr ' ' '\t' > $1.p1 && awk -vS=$2 'BEGIN{if(S<=0) S=.2}{printf $1 "\t"; for(i=3;i<=NF;i++) {if(index($i,".")>0){n=$i+0;$i=(n<S)?"0":((n>1.-S)?"1":"*")}printf $i} print "\t" $2}' $1.p1 | sort -k2 > $1.p2
