#!/bin/bash 
# Usage: bash ./Extract_LPRad_data.bash year month
#
# ex:  bash ./Extract_LPRad_data.bash 2016 02
#
# couvre-feu
curfewLocal=0
#minuit en GMT
# 6 novembre a 0h EDT -> Heure normale EST  GMT=local +5
# 12 mars a 0h EST -> heure avancee EDT     GMT=local +4
echo "JulianDay  Mode  Min   Max    Avg   Var      Date    GMTime   LocalDate  LocalTime CurfewFlag CloudFlag CloudIndex MoonFlag MoonAlt SunFlag SunAlt Filename" > IR-$1-$2.txt
echo "JulianDay  Mode  Min   Max    Avg   Var      Date    GMTime   LocalDate  LocalTime CurfewFlag CloudFlag CloudIndex MoonFlag MoonAlt SunFlag SunAlt Filename" > DS-$1-$2.txt
echo "JulianDay  Mode  Min   Max    Avg   Var      Date    GMTime   LocalDate  LocalTime CurfewFlag CloudFlag CloudIndex MoonFlag MoonAlt SunFlag SunAlt Filename" > CO-$1-$2.txt
echo "JulianDay  Mode  Min   Max    Avg   Var      Date    GMTime   LocalDate  LocalTime CurfewFlag CloudFlag CloudIndex MoonFlag MoonAlt SunFlag SunAlt Filename" > HA-$1-$2.txt
echo "JulianDay  Mode  Min   Max    Avg   Var      Date    GMTime   LocalDate  LocalTime CurfewFlag CloudFlag CloudIndex MoonFlag MoonAlt SunFlag SunAlt Filename" > CL-$1-$2.txt

list=`find -name '*.fits' | grep $1"/"$2`
echo $list "toto"
for i in $list
do if [ `echo $i | grep "/"` ]
   then echo $i | sed s'/\// /g' | sed s'/_/ /g' | sed s'/\.fits/ /g' | sed s'/:/ /g' | sed s'/\. / /g' | sed s'/-/ /g' > toto
        read bidon bidon bidon bidon bidon filter bidon bidon bidon bidon bidon yy mm dd  h m s bidon < toto
#        echo $filter $yy $mm $dd $h $m $s $i
#
#       find julian day
#
        echo "/bin/date --date='"$yy-$mm-$dd" "$h:$m:$s"' +%s" > jj.tmp
        julian=`bash ./jj.tmp`
        if [ $((julian)) -ge 1262322000 ]
        then 
             if [ ! `grep 65535 $i` ] ; then 
#               jour julien de changement vers EST
                echo "/bin/date --date='"$yy"-11-06 00:00:00' +%s" > jj.tmp
                julianEST=`bash ./jj.tmp`
#               jour julien de changement vers EDT
                echo "/bin/date --date='"$yy"-03-12 00:00:00' +%s" > jj.tmp
                julianEDT=`bash ./jj.tmp`
                if [ $julian -lt $julianEDT ] ; then
                   dGMT=5
                else
                   if [ $julian -lt $julianEST ] ; then
                      dGMT=4
                   else
                      dGMT=5
                   fi
                fi
                curfew=curfewLocal+dGMT
# jour julien du curfew
                echo "/bin/date --date='"$yy-$mm-$dd" "$curfewLocal":00:00' +%s" > jj.tmp
                julianCur=`bash ./jj.tmp`
                let julianCur=julianCur+dGMT*3600
# calcul de l'heure locale
                gmt=$yy"/"$mm"/"$dd" "$h":"$m":"$s
                localtime=$(date --date="TZ=\"America/Montreal\" $gmt"z" " +"%H:%M:%S" )
                localdate=$(date --date="TZ=\"America/Montreal\" $gmt"z" " +"%Y/%m/%d" )
                localhour=$(date --date="TZ=\"America/Montreal\" $gmt"z" " +"%H" )
                localhour=${localhour#0}
                if [ $julian -ge $julianCur ] ; then
                     curflag="a_midnight"
                else 
                     curflag="b_midnight"
                fi


#                  julien depuis 1er janvier 2011 0h TU
                   JJ=`echo "scale=6; ("$julian"-18000)/86400.0+2440587.5-2455562.5" | bc`
#                  verifier si la lune est presente
                   python ./moonalt.py $yy $mm $dd $h $m $s > moonalt.tmp
                   read moonup bidon moonalt bidon < moonalt.tmp
                   if [ $moonup -eq 0 ] ; then
                      moon="wo-moon"
                   else
                      moon="w-moon"
                   fi
                   python ./sunalt.py $yy $mm $dd $h $m $s > sunalt.tmp
                   read sun bidon sunalt bidon < sunalt.tmp
                   if [ $sun -eq 0 ] ; then
                      sun="night"
                   else
                      sun="twilight"
                   fi
#
#                  verifier si le ciel semble dégagé
#
                   bash ./histo.bash $i > stats.tmp
                   read mode min max moy var toto < stats.tmp

                   let "indexe=(max-moy)/moy"
                   index=`echo "scale=3;("$max"-"$moy")/"$moy | bc`
#
#                  index=1 -> all data , index=8 -> clear sky only
#
                   if [ $indexe -ge 8 ]
                   then sky="clear"
                   else
                        sky="cloudy?"
                   fi   
#                
#                  assurer un bon S/B value > 255
#
                if [ ${sunalt:0:1} == "-" ] ; then
                   if [ $filter = "IR" ] ; then
                      if [ $mode -gt 255 ] ; then 
                      echo $JJ $mode $min $max $moy $var $yy"/"$mm"/"$dd" "$h":"$m":"$s $localdate $localtime $curflag $sky $index $moon $moonalt $sun $sunalt $i >> IR-$1-$2.txt
                      fi
                   fi
                   if [ $filter = "DeepSky" ] ; then
                      if [ $mode -gt 255 ] ; then 
                         echo $JJ $mode $min $max $moy $var $yy"/"$mm"/"$dd" "$h":"$m":"$s $localdate $localtime $curflag $sky $index $moon $moonalt $sun $sunalt $i >> DS-$1-$2.txt
                      fi
                   fi
                   if [ $filter = "Comet" ] ; then
                      if [ $mode -gt 255 ] ; then 
                         echo $JJ $mode $min $max $moy $var $yy"/"$mm"/"$dd" "$h":"$m":"$s $localdate $localtime $curflag $sky $index $moon $moonalt $sun $sunalt $i >> CO-$1-$2.txt
                      fi
                   fi
                   if [ $filter = "Halpha" ] ; then
                      if [ $mode -gt 255 ] ; then 
                         echo $JJ $mode $min $max $moy $var $yy"/"$mm"/"$dd" "$h":"$m":"$s $localdate $localtime $curflag $sky $index $moon $moonalt $sun $sunalt $i >> HA-$1-$2.txt
                      fi
                   fi
                   if [ $filter = "Clear" ] ; then
                      if [ $mode -gt 255 ] ; then 
                         echo $JJ $mode $min $max $moy $var $yy"/"$mm"/"$dd" "$h":"$m":"$s $localdate $localtime $curflag $sky $index $moon $moonalt $sun $sunalt $i >> CL-$1-$2.txt
                      fi
                   fi
                fi
             fi
        fi
   fi
done  
