#!/bin/bash

procs=4

### First let me download and unpacking Axel\'s files for rtcontrib lesson

printf "\nDownloading Axels rtcontrib lesson files...\n"
curl -O http://www.jaloxa.eu/resources/radiance/documentation/docs/rtcontrib_lesson.tgz
tar -xf rtcontrib_lesson.tgz
mv rtcontrib_lesson 5p_tutorial_examples

cd 5p_tutorial_examples

mkdir bsdf hourlypics hourlypics_dir hourlypics_ds hourlyresult \
	matrices octs viewpics viewpics_dir viewpics_ds
	
### Let me download the weather data file for you
printf "\nDownloading weather data...\n"
curl -O http://apps1.eere.energy.gov/buildings/energyplus/weatherdata/4_north_and_central_america_wmo_region_4/1_usa/USA_CA_Oakland.Intl.AP.724930_TMY3.zip
tar -xf USA_CA_Oakland.Intl.AP.724930_TMY3.zip
rm USA_CA_Oakland.Intl.AP.724930_TMY3.ddy 
rm USA_CA_Oakland.Intl.AP.724930_TMY3.stat
rm USA_CA_Oakland.Intl.AP.724930_TMY3.zip 
mv USA_CA_Oakland.Intl.AP.724930_TMY3.epw skies/.

# grabbing reinsrc.cal from CVS
printf "\nDownloading reinsrc.cal...\n"
curl -O http://radiance-online.org/cgi-bin/viewcvs.cgi/ray/src/cal/cal/reinsrc.cal

### Section 6.1 - Model adjustments for five-phase simulation
printf "\nMaking model adjustments...\n"
cat <<EOF > objects/testroom_Swall.rad
!genbox wall_mat wall_s_bottom 4 .3 1 |xform -t 0 -.3 0
!genbox wall_mat wall_s_top 4 .3 .5 |xform -t 0 -.3 2
!genbox wall_mat wall_s_west .5 .3 1 |xform -t 0 -.3 1
!genbox wall_mat wall_s_west .5 .3 1 |xform -t 3.5 -.3 1
EOF

cat <<EOF > objects/testroom.rad
!genbox floor_mat floor 4.6 6.6 .3 |xform -t -.3 -.3 -.3
!genbox ceiling_mat ceiling 4.6 6.6 .3 |xform -t -.3 -.3 2.5

!genbox wall_mat wall_w .3 6.6 2.5 |xform -t -.3 -.3 0
!genbox wall_mat wall_e .3 6.6 2.5 |xform -t 4 -.3 0
!genbox wall_mat wall_n 4 .3 2.5 |xform -t 0 6 0
EOF

cat <<EOF > objects/glazing.rad
void glass glazing
0
0
3 0.73 0.73 0.73

glazing polygon glass
0
0
12    0.5  -0.25   1
        0.5  -0.25   2
        3.5  -0.25   2
        3.5  -0.25   1
EOF

cat <<EOF > objects/venetianblind.rad
void plastic blindslat
0
0
5 .65 .65 .65 .01 0 

!genblinds blindslat blinds .05 3 1 25 0 +r .15 | xform -rz -90 -t 0.5 -.15 1 
EOF

cat <<EOF > objects/viewmtxsurf.rad
void glow viewsurf
0
0
4 1 1 1 0

viewsurf polygon inside
0
0
12    0.5   0   1
        0.5   0   2
        3.5   0   2
        3.5   0   1
EOF

cat <<EOF > objects/daymtxsurf.rad
void plastic daymtxsurf
0
0
5 0 0 0 0 0

daymtxsurf polygon outside
0
0
12    0.5   -0.3   1
        0.5   -0.3   2
        3.5   -0.3   2
        3.5   -0.3  1
EOF

cat <<EOF > objects/glazing_bsdf.rad
void BSDF BSDFproxy
6 0.30 bsdf/fullwindow_t45.xml 0 0 1 .
0
0

BSDFproxy polygon inside
0
0
12    0.5   0   1
        0.5   0   2
        3.5   0   2
        3.5   0   1
EOF

cat <<EOF >> materials/testroom.mat
void plastic black
0
0
5 0 0 0 0 0
EOF


### Section 6.2 - Creating BSDFs
xform -rz 180 -rx -90 -t 4 0 0 objects/glazing.rad objects/venetianblind.rad materials/testroom.mat \
	objects/testroom_Swall.rad > bsdf/fullwindow.rad

### I am going to save you the trouble of generating the High-Res BSDF
#genBSDF -n $procs +f +b -geom meter -dim 0.5 3.5 1 2 -.3 0 -t4 5 \
#	bsdf/fullwindow.rad > bsdf/fullwindow_t45.xml
printf "\nDownloading tensor tree BSDF...\n"
curl -o bsdf/fullwindow_t45.xml http://www.radiance-online.org/learning/tutorials/fivephasetutorialfiles/fullwindow_t45.xml

### I will also save you the trouble with the low-res BSDF
#bsdf2klems bsdf/fullwindow_t45.xml > bsdf/fullwindow_klems.xml
#genBSDF -n $procs +f +b -geom meter -dim 0.5 3.5 1 2 -.3 0 \
#	bsdf/fullwindow.rad > bsdf/fullwindow_klems.xml
printf "\nDownloading klems BSDF...\n"
curl -o bsdf/fullwindow.xml http://www.radiance-online.org/learning/tutorials/fivephasetutorialfiles/fullwindow.xml


### Section 6.3 - View matrix (V)
printf "\nDoing 5-phase simulation for sensor points...\n"
oconv materials/testroom.mat objects/daymtxsurf.rad objects/ground.rad objects/testroom.rad \
	objects/testroom_Swall.rad objects/viewmtxsurf.rad skies/sky_white.rad \
	objects/viewmtxsurf.rad objects/daymtxsurf.rad > octs/model_3ph.oct
printf "\nView Matrix\n"
time rcontrib < data/photocells.pts -n $procs -f klems_int.cal -b kbinS -bn Nkbins -m viewsurf \
	-I+ -ab 10 -ad 65536 -lw 1.52e-5 octs/model_3ph.oct > matrices/viewmatrix.vmx


### Section 6.4 - Direct view matrix (Vd)
printf "\nDirect View Matrix\n"
time rcontrib < data/photocells.pts -n $procs -f klems_int.cal -b kbinS -bn Nkbins -m viewsurf \
	-I+ -ab 1 -ad 65536 -lw 1.52e-5 octs/model_3ph.oct > matrices/viewmatrix_direct.vmx

### Section 6.5 - Daylight matrix (D)
printf "\nDaylight Matrix\n"
time genklemsamp -c 1000 -vd 0 -1 0 objects/daymtxsurf.rad | \
	rcontrib -n $procs -c 1000 -ab 2 -ad 1024 -e MF:1 -f reinhart.cal -b rbin -bn Nrbins \
		-m sky_glow octs/model_3ph.oct > matrices/daylightmatrix.dmx

### Section 6.6 - Direct daylight matrix (Dd)
printf "\nDirect Daylight Matrix\n"
xform -m black objects/testroom_Swall.rad objects/testroom.rad objects/ground.rad \
	objects/venetianblind.rad | oconv materials/testroom.mat - objects/viewmtxsurf.rad \
	objects/daymtxsurf.rad skies/sky_white.rad > octs/model_allblack.oct

time genklemsamp -c 1000 -vd 0 -1 0 objects/daymtxsurf.rad | \
	rcontrib -n $procs -c 1000 -ab 0 -e MF:1 -f reinhart.cal -b rbin -bn Nrbins \
		-m sky_glow octs/model_allblack.oct > matrices/daylightmatrix_direct.dmx

### Section 6.7 - Direct sun coefficient matrix (Cds)
printf "\nDirect Sun Coefficient Matrix\n"
echo void light solar 0 0 3 1e6 1e6 1e6 > skies/suns.rad
cnt 5185  |  rcalc -e MF:6 -f reinsrc.cal -e Rbin=recno \
	-o 'solar source sun 0 0 4 ${ Dx } ${ Dy } ${ Dz } 0.533' >> skies/suns.rad

xform -m black objects/testroom_Swall.rad objects/testroom.rad objects/ground.rad \
	| oconv materials/testroom.mat - objects/glazing.rad objects/venetianblind.rad \
		objects/glazing_bsdf.rad skies/suns.rad > octs/model_suns.oct

time rcontrib < data/photocells.pts -n $procs -I -ab 1 -ad 65536 -lw 1.52e-5 -dc 1 -dt 0 -dj 0 -st 1 -ss 0 -faf  \
	-e MF:6 -f reinhart.cal -b rbin -bn Nrbins -m solar \
	octs/model_suns.oct > matrices/directsun.dsmx

### Section 6.8 - Putting it all together
printf "\nCombining it all\n"
epw2wea skies/USA_CA_Oakland.Intl.AP.724930_TMY3.epw skies/OakLand.wea
gendaymtx -of skies/OakLand.wea > matrices/OakLand.smx
gendaymtx -of -d skies/OakLand.wea > matrices/OakLand_direct.smx
gendaymtx -5 -d -m 6 -of skies/OakLand.wea > matrices/OakLand_direct_m6.smx

time dctimestep -n 8760 -if matrices/viewmatrix.vmx bsdf/fullwindow.xml  \
	matrices/daylightmatrix.dmx matrices/OakLand.smx | \
	rcollate -h -oc 1 > i_3ph.txt

time dctimestep -n 8760 -if  matrices/viewmatrix_direct.vmx bsdf/fullwindow.xml  \
	matrices/daylightmatrix_direct.dmx matrices/OakLand_direct.smx | \
	rcollate -h -oc 1 > i_ds3ph.txt

time dctimestep -n 8760 -if matrices/directsun.dsmx matrices/OakLand_direct_m6.smx | \
	rcollate -h -oc 1 > i_ds5ph.txt

rlam i_3ph.txt i_ds3ph.txt i_ds5ph.txt | \
	rcalc -e 'r=$1-$4+$7;g=$2-$5+$8;b=$3-$6+$9' \
	-e '$1=179*(.265*r+.670*g+.065*b)' | \
	rcollate -h -fa1 -ic 8760 -t > illum.txt

printf "\nAll done with example 1!  look at illum.txt for the result.\n"


printf "\n\n\nDoing 5-phase simulation for rendering...\n"
### 7.1	View matrix (V)
printf "\nView Matrix...\n"
time vwrays -vf views/back.vf  -ff -x 500 -y 500 \
	| rcontrib `vwrays -vf views/back.vf  -x 500 -y 500 -d` -n $procs -ffc -fo -o viewpics/back_%03d.hdr \
		-f klems_int.cal -b kbinS -bn Nkbins -m viewsurf \
		-ab 10 -ad 65536 -lw 1.52e-5 octs/model_3ph.oct

### 7.2	Direct view matrix (Vd)	
printf "\nDirect View Matrix...\n"	
time vwrays -vf views/back.vf  -ff -x 500 -y 500 \
	| rcontrib `vwrays -vf views/back.vf  -x 500 -y 500 -d` -n $procs -ffc -fo -o viewpics_dir/back_%03d.hdr \
		-f klems_int.cal -b kbinS -bn Nkbins -m viewsurf \
		-ab 1 -ad 65536 -lw 1.52e-5 octs/model_3ph.oct

### 7.5	Direct sun coefficient matrix (Cds)
printf "\nDirect Coefficient Matrix...\n"
ulimit -n 9999
xform -m black objects/testroom_Swall.rad objects/testroom.rad objects/ground.rad | \
	oconv materials/testroom.mat - objects/venetianblind.rad > octs/model_nosuns.oct
time vwrays -c 4 -pj 1 -fa -vf views/back.vf -x 500 -y 500 | \
	rtrace -h- -opn -faa -ab 0 octs/model_nosuns.oct | \
	rcontrib `vwrays -vf views/back.vf -x 500 -y 500 -d` -n $procs -fac -fo -o viewpics_ds/back_%04d.hdr \
	-e MF:6 -f reinhart.cal -b rbin -bn Nrbins -m solar -c 4 -I -ab 1 -ad 100 -dt 0 -dc 1 -lw 1e-2 octs/model_suns.oct

oconv materials/testroom.mat objects/testroom_Swall.rad objects/testroom.rad \
	objects/ground.rad objects/glazing.rad objects/venetianblind.rad \
	objects/daymtxsurf.rad > octs/model_material.oct
time rpict -x 1000 -y 1000 -vf views/back.vf -ps 1 -pj 1 -av 0.31831 0.31831 0.31831 -aa 0 \
	octs/model_material.oct | pfilt -x /2 -y /2 -1 -e 1 > materialmap.hdr  

### 7.6	Putting it all together
printf "\nPutting it together...\n"
time dctimestep -n 8760 -if -o hourlypics/back_%04d.hdr viewpics/back_%03d.hdr \
	bsdf/fullwindow.xml matrices/daylightmatrix.dmx matrices/OakLand.smx 

time dctimestep -n 8760 -if -o hourlypics_dir/back_%04d.hdr viewpics_dir/back_%03d.hdr \
	bsdf/fullwindow.xml matrices/daylightmatrix_direct.dmx matrices/OakLand_direct.smx 

time dctimestep -n 8760 -if -o hourlypics_ds/back_%04d.hdr viewpics_ds/back_%04d.hdr \
	matrices/OakLand_direct_m6.smx

time for t in {1..8760} 
do ts=`printf %04d $t `
pcomb -e 'lo=li(1)-li(2)+li(3)*li(4)' -o hourlypics/back_${ts}.hdr -o hourlypics_dir/back_${ts}.hdr \
	-o hourlypics_ds/back_${ts}.hdr -o materialmap.hdr > hourlyresult/back_${ts}.hdr 
done
printf "\nAll Done with 5-phase Rendering sim\n"
pcond hourlyresult/back_0015.hdr | ximage &

