Skip to content

Commit 5ba0410

Browse files
various awk, Bash, SCM scheme and R files to calculate and plot scale theoretical reflectance
1 parent 60ea6fa commit 5ba0410

10 files changed

+951
-0
lines changed

Aglayeropt.sh

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
## BASH shell code to perform parameter sweeps by varying one layer parameter at a time and calculate the corresponding normal reflectance for a 3-layer scale ultrastructure.
2+
## Author: Vinodkumar Saranathan (https://github.com/evolphotonics, August 2021)
3+
## Usage: sh Aglayeropt.sh
4+
5+
#!/bin/bash
6+
7+
## Comment in/out the relevant layer thickness parameters for each scale type, as needed. Currently, they are set for varying the air gap layer thickness from 0 to 2 um while keeping upper and lower lamina thickness constant, for a hind-wing silver scale.
8+
9+
## upper lamina
10+
#ul=(56) ##FW Ag
11+
#ul=(64) ##FW Gland Ag
12+
#ul=(76) ##HW Gray
13+
ul=(83) ##HW Ag
14+
#ul=(120) ##HW Coupling
15+
#midul="$(seq -s ' ' 0.001 5 501)" ## varying ul from 0 to 300 nm
16+
17+
## air gap/lumen layer
18+
#gap=(1044) ##FW Ag
19+
#gap=(1420) ##FW Gland AG
20+
#gap=(819) ##HW Gray
21+
#gap=(1069) ##HW AG
22+
#gap=(1223) ##HW Coupling
23+
midgap="$(seq -s ' ' 0.001 20 2001)" ## varying air gap from 0 to 2000 nm
24+
25+
#"$(seq -s ' ' 1 2 200)"
26+
#CV= #(0.08) (0.14) (0.07) ##FW Ag
27+
#CV= #(0.13) (0.10) (0.12) ##FW Gland Ag
28+
#CV= #(0.23) (0.14) (0.13) ##HW Gray
29+
CV=(0.17) #(0.11) (0.17) (0.14) ##HW Ag
30+
#CV= #(0.17) (0.28) (0.24) ##HW Coupling
31+
32+
## lower lamina
33+
#ll=(69) ##FW Ag
34+
#ll=(81) ##FW Gland Ag
35+
#ll=(83) ##HW Gray
36+
ll=(67) ##HW Ag
37+
#ll=(105) ##HW Coupling
38+
#midll="$(seq -s ' ' 0.001 5 501)" ## varying ul from 0 to 500 nm
39+
40+
N=(100) ##No of samples
41+
count=1 ## counter start
42+
extension=txt ## for output file name
43+
suffix=e-9 ## for nanometer
44+
45+
runfs () {
46+
local i=$1
47+
local j=$2
48+
local k=$3
49+
50+
## increment and run Freesnell by passing the clparams as cl-arguments
51+
clparamul=$(echo $i$suffix) #"$i"
52+
clparamgap=$(echo $j$suffix) #"$j"
53+
clparamll=$(echo $k$suffix) #"$k"
54+
55+
## append direct output to dev null for speed
56+
/usr/local/bin/scm -v -f BanySilver.scm $clparamul $clparamgap $clparamll $i-$j-$k.$extension > /dev/null 2>&1
57+
58+
## run verbosely by uncommenting the next line
59+
#echo "done with step $count";
60+
}
61+
62+
63+
#for m in ${midul[@]}; do
64+
for m in ${midgap[@]}; do
65+
#for m in ${midll[@]}; do
66+
67+
## We use the well known trick to average 3 uniform distributions to get a nearly normal distribution (See http://www.johndcook.com/blog/2009/02/12/sums-of-uniform-random-values/)
68+
69+
## generate the normally distributed upper lamina values to average over
70+
#ul=$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CV"'*'"$m"'+'"$m"')}'; done)
71+
72+
## generate the normally distributed air gap values to average over
73+
gap=$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CV"'*'"$m"'+'"$m"')}'; done)
74+
75+
## generate the normally distributed lower lamina values to average over
76+
#ll=$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CV"'*'"$m"'+'"$m"')}'; done)
77+
78+
79+
## changing params simultaneously
80+
for i in ${ul[@]}; do for j in ${gap[@]}; do for k in ${ll[@]}; do
81+
82+
num_procs=100 ## number of concurrent jobs
83+
num_jobs="\j" ## The prompt escape for number of jobs currently running
84+
while (( ${num_jobs@P} >= num_procs )); do
85+
wait ## wait before starting any more jobs
86+
done
87+
88+
## run each loop iteration in parallel
89+
runfs "$i" "$j" "$k" "$count" &
90+
91+
## increment counter
92+
count=$((count+1))
93+
94+
done;
95+
done;
96+
done;
97+
98+
99+
num_jobs="\j" ## The prompt escape for number of jobs currently running
100+
while (( ${num_jobs@P} > 0 )); do
101+
# echo "waiting for job" ${num_jobs@P}
102+
wait; ## for jobs to finish
103+
done
104+
## First calculate average spectra
105+
awk -v avgfile=$m".dat" -f avg.awk *.txt
106+
107+
## Then calculate standard deviation of spectra
108+
#awk -v avgfile=$m".dat" -f sd.awk *.txt
109+
110+
## cleanup
111+
rm *.txt
112+
done;
113+
114+
115+
## Post processing: calculate the mean broadband reflectivity for each thickness parameter value and write it to a file for plotting
116+
ls *.dat | while read filename ; do awk '{sum+=$2} END { print FILENAME"\t"sum/NR}' $filename ; done > Aglayeropt_avg.dat
117+
118+
exit 0

Agreflmodelcalc.sh

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
## BASH shell code to calculate the theoretical normal reflectivity of a 3-layer silver scale ultrastructure. The scale layer thickness parameters can be varied one at a time, any two or all three simultaneously.
2+
## Author: Vinodkumar Saranathan (https://github.com/evolphotonics, August-October 2021)
3+
## Usage: sh Agreflmodelcalc.sh midul midgap midll CVul CVgap CVll [N]
4+
5+
#!/bin/bash
6+
7+
## Instead of hard coding, the scale thickness parameters for each scale type are passed as command line arguments (see usage)
8+
9+
## upper lamina
10+
#ll=(56) ##FW Ag
11+
#ul=(64) ##FW Gland Ag
12+
#ul=(76) ##HW Gray
13+
#ul=(83) ##HW Ag
14+
#ul=(120) ##HW Coupling
15+
#midul="$(seq -s ' ' 0.001 5 501)" ## varying ul from 0 to 300 nm
16+
midul=$1
17+
18+
## air gap/lumen layer
19+
#gap=(1044) ##FW Ag
20+
#gap=(1420) ##FW Gland AG
21+
#gap=(819) ##HW Gray
22+
#gap=(1069) ##HW AG
23+
#gap=(1223) ##HW Coupling
24+
#midgap="$(seq -s ' ' 0.001 20 2001)" ## varying air gap from 0 to 2000 nm
25+
midgap=$2
26+
27+
## lower lamina
28+
#ll=(69) ##FW Ag
29+
#ll=(81) ##FW Gland Ag
30+
#ll=(83) ##HW Gray
31+
#ll=(67) ##HW Ag
32+
#ll=(105) ##HW Coupling
33+
#midll="$(seq -s ' ' 0.001 5 501)" ## varying ul from 0 to 500 nm
34+
midll=$3
35+
36+
## Coefficient of Variation
37+
#CV= #(0.08) (0.14) (0.07) ##FW Ag
38+
#CV= #(0.13) (0.10) (0.12) ##FW Gland Ag
39+
#CV= #(0.23) (0.14) (0.13) ##HW Gray
40+
#CV= #(0.11) (0.17) (0.14) ##HW Ag
41+
#CV= #(0.17) (0.28) (0.24) ##HW Coupling
42+
CVul=$4
43+
CVgap=$5
44+
CVll=$6
45+
46+
## Sample size
47+
if [ $7 -ge 0 ];
48+
then
49+
N=$7
50+
else
51+
N=200
52+
fi
53+
54+
count=1 ## counter start
55+
extension=txt ## for output file name
56+
suffix=e-9 ## for nanometer
57+
58+
runfs () {
59+
local i=$1
60+
local j=$2
61+
local k=$3
62+
63+
64+
## increment and run Freesnell by passing the clparams as cl-arguments
65+
clparamul=$(echo $i$suffix) #"$i"
66+
clparamgap=$(echo $j$suffix) #"$j"
67+
clparamll=$(echo $k$suffix) #"$k"
68+
69+
## append direct output to dev null for speed
70+
/usr/local/bin/scm -v -f BanySilver.scm $clparamul $clparamgap $clparamll $i-$j-$k.$extension > /dev/null 2>&1
71+
72+
## run verbosely by uncommenting the next line
73+
#echo "done with step $count";
74+
}
75+
76+
## We use the well known trick to average 3 uniform distributions to get a nearly normal distribution (See http://www.johndcook.com/blog/2009/02/12/sums-of-uniform-random-values/)
77+
78+
## generate the normally distributed upper lamina values to average over
79+
#ul=$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CVul"'*'"$m"'+'"$m"')}'; done)
80+
if (( $(echo "$CVul > 0" |bc -l) )); ## Do only if the upper lamina layer thickness varies
81+
then
82+
uavg=0 ## initialize
83+
tol=5 ## tolerance of distribution mean from desired value, in nm
84+
while (( $(echo "$midul $uavg $tol" | awk '{print (sqrt(($1 - $2)^2) > $3)}') )) ## we shouldn't need this loop, but just in case
85+
do
86+
echo "$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CVul"'*'"$midul"'+'"$midul"')}'; done)" > ul
87+
uavg="$(awk '{sum+=$0} END { print sum/NR}' ul)"
88+
done;
89+
ul="$(cat ul)"
90+
else
91+
ul=$midul
92+
fi
93+
94+
## generate the normally distributed air gap values to average over
95+
#gap=$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CVgap"'*'"$m"'+'"$m"')}'; done)
96+
if (( $(echo "$CVgap > 0" |bc -l) )); ## Do only if the air gap layer thickness varies
97+
then
98+
gavg=0 ## initialize
99+
tol=5 ## tolerance of distribution mean from desired value, in nm
100+
while (( $(echo "$midgap $gavg $tol" | awk '{print (sqrt(($1 - $2)^2) > $3)}') )) ## we shouldn't need this loop, but just in case
101+
do
102+
echo "$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CVgap"'*'"$midgap"'+'"$midgap"')}'; done)" > gap
103+
gavg="$(awk '{sum+=$0} END { print sum/NR}' gap)"
104+
done;
105+
gap="$(cat gap)"
106+
else
107+
gap=$midgap
108+
fi
109+
110+
## generate the normally distributed lower lamina values to average over
111+
#ll=$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CVll"'*'"$m"'+'"$m"')}'; done)
112+
if (( $(echo "$CVll > 0" |bc -l) )); ## Do only if the lower lamina layer thickness varies
113+
then
114+
uavg=0 ## initialize
115+
tol=5 ## tolerance of distribution mean from desired value, in nm
116+
while (( $(echo "$midll $uavg $tol" | awk '{print (sqrt(($1 - $2)^2) > $3)}') )) ## we shouldn't need this loop, but just in case
117+
do
118+
echo "$(for i in $(seq 1 $N) ; do awk -v seed=$RANDOM 'BEGIN{srand(seed); rdm=(((rand()*2)-1)+((rand()*2)-1)+((rand()*2)-1)); print (rdm*'"$CVll"'*'"$midll"'+'"$midll"')}'; done)" > ll
119+
uavg="$(awk '{sum+=$0} END { print sum/NR}' ll)"
120+
done;
121+
ll="$(cat ll)"
122+
else
123+
ll=$midll
124+
fi
125+
126+
127+
## changing parameters simultaneously - if any of them are constant, then the corresponding for loop is only executed once
128+
for i in ${ul[@]}; do for j in ${gap[@]}; do for k in ${ll[@]}; do
129+
130+
num_procs=100 ## number of concurrent jobs -- adjust according to your computer hardware specs
131+
num_jobs="\j" ## The prompt escape for number of jobs currently running
132+
while (( ${num_jobs@P} >= num_procs )); do
133+
wait ## wait before starting any more jobs
134+
done
135+
136+
## run each loop iteration in parallel
137+
runfs "$i" "$j" "$k" "$count" &
138+
139+
## increment counter
140+
count=$((count+1))
141+
142+
done;
143+
done;
144+
done;
145+
146+
147+
num_jobs="\j" ## The prompt escape for number of jobs currently running
148+
while (( ${num_jobs@P} > 0 )); do
149+
# echo "waiting for job" ${num_jobs@P}
150+
wait; ## for jobs to finish
151+
done
152+
153+
## First calculate average spectra
154+
awk -v avgfile="avg.dat" -f avg.awk *.txt
155+
156+
## Then calculate standard deviation of spectra
157+
awk -v avgfile="avg.dat" -f sd.awk *.txt
158+
159+
## post processing/cleanup
160+
rm *.txt
161+
rm avg.dat
162+
mv avgwsd.dat $midul"_"$midgap"_"$midll"_"$CVul"_"$CVgap"_"$CVll"_"$N"_avg.dat" ## a more meaningful and unique name for the result
163+
164+
exit 0

Agreflmodelcalc_FIBSEMlineROI.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
## BASH shell code to calculate the theoretical normal reflectivity of a 3-layer silver scale ultrastructure. The layer thicknesses are specified by the line ROI measurements from a scale FIB-SEM image in FIJI, passed here as a tab-delimited text file.
2+
## Author: Vinodkumar Saranathan (https://github.com/evolphotonics, October 2021)
3+
## Usage: sh Agreflmodelcalc_FIBSEMlineROI.sh FWSilver_lineROImeasurements.txt
4+
5+
#!/bin/bash
6+
7+
infile=$1 ## command line parameter
8+
9+
## In order to perform hierarchical modeling, where 1, 2 or all 3 parameters are varied, just set the other parameter(s) to a constant value below
10+
11+
## upper lamina
12+
#ul=(56) ##FW Ag
13+
#ul=(64) ##FW Gland Ag
14+
#ul=(76) ##HW Gray
15+
#ul=(83) ##HW Ag
16+
#ul=(120) ##HW Coupling
17+
18+
## air gap/lumen layer
19+
#gap=(1044) ##FW Ag
20+
#gap=(1420) ##FW Gland AG
21+
#gap=(819) ##HW Gray
22+
#gap=(1069) ##HW AG
23+
#gap=(1223) ##HW Coupling
24+
25+
## lower lamina
26+
#ll=(69) ##FW Ag
27+
#ll=(81) ##FW Gland Ag
28+
#ll=(83) ##HW Gray
29+
#ll=(67) ##HW Ag
30+
#ll=(105) ##HW Coupling
31+
32+
## Coefficient of Variation
33+
#CV= #(0.08) (0.14) (0.07) ##FW Ag
34+
#CV= #(0.13) (0.10) (0.12) ##FW Gland Ag
35+
#CV= #(0.23) (0.14) (0.13) ##HW Gray
36+
#CV= #(0.11) (0.17) (0.14) ##HW Ag
37+
#CV= #(0.17) (0.28) (0.24) ##HW Coupling
38+
39+
count=1 ## counter start
40+
extension=txt ## for output file name
41+
suffix=e-9 ## for nanometer
42+
43+
runfs () {
44+
local i=$1
45+
local j=$2
46+
local k=$3
47+
local fname=$4
48+
49+
## increment and run Freesnell by passing the clparams as cl-arguments
50+
clparamul=$(echo $i$suffix) #"$i"
51+
clparamgap=$(echo $j$suffix) #"$j"
52+
clparamll=$(echo $k$suffix) #"$k"
53+
54+
## append direct output to dev null for speed
55+
/usr/local/bin/scm -v -f BanySilver.scm $clparamul $clparamgap $clparamll $fname > /dev/null 2>&1
56+
57+
## run verbosely by uncommenting the next line
58+
#echo "done with step $count";
59+
}
60+
61+
62+
## code to read in the measured line ROI layer parameters, line by line from input file, and run Free Snell
63+
while read line
64+
do
65+
## comment out any 1 or 2 of the following three lines and set the corresponding parameters to a constant value above, in order to create hierarchical models
66+
ul=(`echo $line | awk -F ' ' '{print $1}'`)
67+
gap=(`echo $line | awk -F ' ' '{print $2}'`)
68+
ll=(`echo $line | awk -F ' ' '{print $3}' | sed 's/\r$//'`) ## strip carriage return for the last column, otherwise bad things happen
69+
70+
num_procs=100 ## number of concurrent jobs -- adjust according to your computer hardware specs
71+
num_jobs="\j" ## The prompt escape for number of jobs currently running
72+
while (( ${num_jobs@P} >= num_procs )); do
73+
# echo "waiting for job" ${num_jobs@P}
74+
wait ## wait before starting any more jobs
75+
done
76+
77+
## run each loop iteration in parallel
78+
runfs "$ul" "$gap" "$ll" "$count" &
79+
80+
## increment counter
81+
count=$((count+1))
82+
done<$infile
83+
84+
exit 0

0 commit comments

Comments
 (0)