# set averaging window in days (dt)
dt=28.
nn=0
nda=0
echo "#Jday(day)  meanClear  LightPollution minClear minDeepSky" > timeserie
rm -f LP.txt
rm -f V.txt
rm -f DS.txt
list=`du -ax | grep "\.phot"`
for i in $list
do if [ `echo $i | grep "/"` ]
   then echo $i | sed s'/\// /g' | sed s'/_/ /g' | sed s'/\.phot/ /g' | sed s'/:/ /g' | sed s'/\. / /g' > toto
   read bidon yy mm dd bidon h m s < toto
   echo "/bin/date --date='"$yy-$mm-$dd" "$h:$m:$s"' +%s" > jj
   julian=`bash ./jj`
   if [ $julian -ge 1262322000 ]
   then if [ ! `grep 65535 $i` ]
        then 
#            julien depuis 1er janvier 2011 0h TU
             JJ=`echo "scale=6; ("$julian"-18000)/86400.0+2440587.5-2455562.5" | bc`
#            echo $i $yy $mm $dd $h $m $s $julian

#            verifier si le ciel semble dégagé
             grep -4 Clear $i | tail -1 > min.tmp
             grep -5 Clear $i | tail -1 > max.tmp
             grep -3 Clear $i | tail -1 | sed 's/\./ /g'> moy.tmp
             read toto toto toto min  bidon <min.tmp
             read toto toto toto max  bidon <max.tmp
             read toto toto toto moy toto   <moy.tmp
             if [ $min -ne 0 ]
             then let 'index=(max-moy)/(moy-min)'
                  if [ $index -ge 1 ]
                  then 
#
                       grep -3 IR $i | tail -1 | sed 's/\./ /g' > IR
                       grep -3 DeepSky $i | tail -1| sed 's/\./ /g' > DS
                       grep -3 Comet $i | tail -1| sed 's/\./ /g' > CO
                       grep -3 Halpha $i | tail -1 | sed 's/\./ /g'> HA
                       grep -3 Clear $i | tail -1 | sed 's/\./ /g'> CL
                       read bidon bidon bidon a bidon < IR
                       read bidon bidon bidon b  bidon < DS
                       read bidon bidon bidon c  bidon < CO
                       read bidon bidon bidon d  bidon < HA
                       read bidon bidon bidon e  bidon < CL
# assurer un bon S/B
                       if [ $b -gt 255 ]
                       then 
                       if [ $e -gt 255 ]
                       then
# limiter les nuages de type sirus
                            if [ $b -lt $e ]
                            then 
#                            LP=`echo "scale=10; ("$e"-1.03*"$b")*(0.000000275/1577)" | bc`
                            LP=$a                            

                            echo $JJ $LP $a $b $yy"-"$mm"-"$dd" "$h":"$m":"$s >> timeserie 
                            if [ $LP ]  
                            then if [ ! `echo $LP | grep "-"` ]
                                 then echo $JJ $LP >> LP.txt
                                 else
                                      let nn=nn+1
                                 fi
                            fi
                            echo $JJ $a >> IR.txt
                            echo $JJ $e >> CL.txt
                            echo $JJ $b >> DS.txt
                            let nda=nda+1
                            fi
                       fi
                       fi
                      echo $JJ $LP $i 
                  fi
             fi
        fi
   fi
   fi
done  
echo "Number of rejected negative points=" $nn
# moyennage
echo $dt > moyenne.in
echo $nda >> moyenne.in
cat LP.txt >> moyenne.in
./moyenne
mv moyenne.out LPmoy_$dt
cp moyenne.in minimum.in
./minimum
mv minimum.out LPmin_$dt
