#!/bin/bash

## script to produce plots of output reconstructions using Gnuplot
## script adapted for the new expected FACET bunch FWHM

output="/home/tovey/sp/output"		#  output directory


#  do for all beam parameters
for beam in facet ; do

	#  do for all electron bunch profiles
        for profile in single background double ; do

                case "${profile}" in
                        single)
                                amplist=1		#  amplitude ratios
				fwhm2ps=0		#  FWHM peak2 [ps]
				pos2list=0		#  positions of peak2 [ps]
                                ;;
			background)
				amplist="2 10"
				fwhm2ps=2
				pos2list="-1 -0.8 -0.6 -0.4 -0.2 0" 
				;;
			double)
				amplist=2
				fwhm2ps=0.02
				pos2list=-0.04
				;;
                        *)
                                echo "ERROR: the specified electron bunch profile is not defined!"
				exit 1
                esac
# hello
		for pos2 in ${pos2list} ; do

			#  do for all profile relative amplitudes
	                for amp in ${amplist} ; do
	
				ampratio=$( echo "1 ${amp}" | awk '{print $1/$2}' )
				

				#  do for all fwhm spike profiles
	                        for fwhm in 100 110 120 130 140 150 ; do
				
					fwhm1ps=$( echo "${fwhm} 1000" | awk '{print $1/$2}' )

					#  do for matching on high frequency in Cauchy.c
                       		        for h in h l ; do

						#  do for both infinite and finite width grating calculations
	                                        for f in i f ; do
						
							mkdir -p ${output}/${beam}/${profile}${pos2}/amp${amp}/${fwhm}/out/${h}/${f}/graphs
							cd ${output}/${beam}/${profile}${pos2}/amp${amp}/${fwhm}/out/${h}/${f}/graphs
							
							#  Gnuplot plotting file
							outplot="${beam}_${profile}${pos2}_${amp}_${fwhm}_${h}_${f}.plot"

							#  output comments for parameters
							echo "#  ${beam} ${profile}${pos2} amp${amp} ${fwhm}fs ${h} ${f}" > ${outplot}
						
							#  theoretical bunch profile plot
							case "${profile}" in
								single)
									formula="s=(0.5*${fwhm1ps})/sqrt(2*log(2))\nf(x)=exp(-(x*x)/(2*s*s))"
									pedge=$( echo "${fwhm} 1000" | awk '{print $1/$2}' )
									nedge="-${pedge}"
									;;

								background)
									formula="s=(0.5*${fwhm1ps})/sqrt(2*log(2))\nt=(0.5*${fwhm2ps})/sqrt(2*log(2))\nf(x)=(exp(-(x*x)/(2*s*s))+${ampratio}*exp(-((x-${pos2})*(x-${pos2}))/(2*t*t)))/(1+${ampratio}*exp(-(${pos2}*${pos2})/(2*t*t)))"
									pedge=3
									nedge="-5"
									;;

								double)
									formula="s=(0.5*${fwhm1ps})/sqrt(2*log(2))\nt=(0.5*${fwhm2ps})/sqrt(2*log(2))\nf(x)=(exp(-(x*x)/(2*s*s))+${ampratio}*exp(-((x-${pos2})*(x-${pos2}))/(2*t*t)))/(1+${ampratio}*exp(-(${pos2}*${pos2})/(2*t*t)))"
									pedge=0.1
									nedge="-0.1"
									;;
								*)
									echo "EEROR: output unknown for the specified electron bunch profile!"
									exit 1
							esac

							echo -e -n "set terminal png size 1024,768 xffffff\nset output '${output}/${beam}/${profile}${pos2}/amp${amp}/${fwhm}/out/${h}/${f}/graphs/output.png'\nset title 'KK Reconstruction for: ${beam} ${profile}${pos2} amp${amp} ${fwhm}fs ${h} ${f}'\nset xlabel 'Time [ps]'\nset ylabel 'Amplitude'\nset xtic auto\nset ytic auto\nset autoscale\n${formula}\nplot f(x) title 'Theoretical: ${fwhm}fs'" >> ${outplot}	


							#  do for all grating combinations
        	                                        for grating1 in 15 50 250 500 ; do
						
								case "${grating1}" in
									5)
										#  only reconstruct for the grating combination 15.85.500um for the background profile, where the separation is not 1ps
										if [ "${profile}" == 'background' ] && [ "${pos2}" != '-1' ] ; then 
											continue
										fi
	
        	        	                                                gr2range="85"
                	        	                                        gr3range="1500"
                        	        	                                ;;
                                	        	                15)
                                        	        	                gr2range="85"
                                                	        	        gr3range="500"
										#  only reconstruct for the grating combination 15.85.500um for the background profile, where the separation is not 1ps
										if [ "${profile}" == 'background' ] && [ "${pos2}" != '-1' ] ; then
											gr2range=85
											gr3range=500
										fi
	                                                                        ;;
									50)
										gr2range="250"
										gr3range="500"
										;;
									250)
										gr2range="500"
										gr3range="1000"
										;;
									500)
										gr2range="1000"
										gr3range="1500"
										;;
        	                                                       	*)
                	                                                        echo "ERROR: the first grating does not have a defined second and third grating group!"
										exit 1
									esac
	
		
                	                                        for grating2 in ${gr2range} ; do
	
        	                                                        for grating3 in ${gr3range} ; do


									echo -e -n ", '${output}/${beam}/${profile}${pos2}/amp${amp}/${fwhm}/out/${h}/${f}/cauchy/${grating1}.${grating2}.${grating3}/${beam}_${profile}${pos2}_${amp}_${fwhm}_${h}_${f}_${grating1}.${grating2}.${grating3}_${pedge}_${nedge}.dat' title '${beam} ${profile}${pos2} amp${amp} ${fwhm} ${h} ${f} ${grating1}.${grating2}.${grating3}um'" >> ${outplot}	

									done

                                                                done
                                                        done
                                                
							if [ "${profile}" == 'background' ] ; then
						
							#  zoom output	
							echo -e "\nset output '${output}/${beam}/${profile}${pos2}/amp${amp}/${fwhm}/out/${h}/${f}/graphs/output_zoom.png'\nset xr [-0.4:0.4]\nreplot" >> ${outplot}
                                       			fi
							#pwd 
							gnuplot ${output}/${beam}/${profile}${pos2}/amp${amp}/${fwhm}/out/${h}/${f}/graphs/${outplot}
						done
					done
                                done
                        done
                done
        done
done

