#!/bin/csh

#creates a 2 hour movie (hour option) using all frames in the 2 hour window
#creates a day movie (day option) using frame in first 10 sec of each minute

#lower case locations like bflat or wbbw
set location=$1
set location = ${location}_cam
set day=`date -u +%Y%m%d`
echo $day 
set in="/uufs/chpc.utah.edu/common/home/horel-group/archive/${day}/$location"
#need to change to your directory!
set out = "/uufs/chpc.utah.edu/common/home/u0035056/public_html/atmos_5340/scripts"
#foreach length ( hour day )
#just hour
foreach length ( hour )
#cd $in
rm -f frame*.jpg
set count = 0
if ( $length == hour ) then
set files = `find $in -type f -mmin -30 -size +1k | sort`
else 
set files = `find $in -type f -mtime -1 -size +1k |  sort`
endif
echo $files
foreach file ( $files )
#echo $file $count
if ( $length == hour ) then
set counts = `printf "%04d" $count`
ln -s $file frame_${counts}.jpg
@ count = $count + 1
else
# get only one per minute
set sec = `echo $file | cut -c15-16 | sed 's/^0*//'`
echo $file $count $sec
if ( $sec < 40 && $sec > 29 ) then
set counts = `printf "%04d" $count`
ln -s $file frame_${counts}.jpg
@ count = $count + 1
endif
endif
end
rm -f ${out}/${location}_${length}.mov
ffmpeg -i frame_%04d.jpg -qmin 6 -qmax 6  ${out}/${location}_${length}.mov
if ( $length == "day" ) then
if ( -e ${out}/${location}_${length}.mov ) then
cp ${out}/${location}_${length}.mov ${out}/${location}_${day}.mov
endif
endif
rm -f frame*.jpg
end
