if [ $# == 4 ]
then

RA=$1
DEC=$2
NAME=$3
DATE=`date +%d.%m.%Y`
SOMMERZEIT=$4     # 1 or 0

#calc Julian day
a=`echo "(14 - \`date +%m\`)/12" | bc`
y=`echo "\`date +%Y\` + 4800 - $a" | bc`
m=`echo "\`date +%m\` + 12*$a - 3" | bc`
JDN=`echo "\`date +%d\` + (153*$m+2)/5 + 365*$y + $y/4 - $y/100 + $y/400 - 32045" | bc`
JDN00GMT=`echo $JDN+0.5 | bc -l` #tomorrow

#### all this goes to skychart.gpl ##########
cat > skychart.gpl <<EOL
reset

#Funktionen definieren
fmod(x,y) = x - int(x / y) * y

#Plot anpassen
set xrange [-21600:21600]
set yrange [0:90]
set title "$DATE"
set xlabel "Local Time [h]"
set ylabel "Elevation [°]"
set grid xtics mxtics

set arrow 1 from -21600,41.810 to 21600,41.81 nohead lc 4
set label "AM=1.5" at 17000,43.5 font ",10"
set arrow 2 from -21600,50.285 to 21600,50.285 nohead lc 4
set label "AM=1.3" at 17000,52 font ",10"
set arrow 3 from -21600,65.380 to 21600,65.38 nohead lc 4
set label "AM=1.1" at 17000,67 font ",10"


set xdata time
#set timefmt "%S"
set format x "%H"


set term pdfcairo
set output "skychart.pdf"

#Koordinaten vom WST
LONG = 11.5*pi/180			# 11.5°
LONG_h = 11.5*24/360
LAT = (48./360.+11./(360.*60.))*2*pi	# 48.1833°
LAT_d = 48.1833


RA = $1
DEC_deg = $2
DEC = DEC_deg/360.*2*pi
#LMST berechnen
JD_tonight00GMT = $JDN00GMT
JD(x) = JD_tonight00GMT+(x-1.-$SOMMERZEIT)/24.
d(x) = JD(x) - 2451545.
GMST_h(x) = 18.697374558 + 24.06570982441908*d(x)
GMST_h_mod24(x) = fmod(GMST_h(x),24.)
LMST_h(x) = GMST_h_mod24(x) + LONG_h

#Stundenwinkel und Elongation berechnen
LHA_h(x)=LMST_h(x)-RA
LHA(x)=LHA_h(x)/24.*2*pi
alt(x)=180./pi*asin((sin(LAT)*sin(DEC)+cos(LAT)*cos(DEC)*cos(LHA(x/3600.))))

labelxpos = -LHA_h(0)*3600.
while (labelxpos >= 21600){
  labelxpos=labelxpos-86400
}
while (labelxpos <= -21600){
  labelxpos=labelxpos+86400
}
minimum=0
if (labelxpos >= 21600){
  labelxpos=labelxpos-43200
  minimum=1
}

if (DEC_deg < 48.) {
  labelypos=DEC_deg+90-LAT_d+2
}else{
  labelypos=-DEC_deg+90+LAT_d+2
}
 
if (minimum == 1){
  labelypos=labelypos-90.
}

if (DEC_deg < 48.) {
  set label center "$NAME" at labelxpos,labelypos font ",10"
}else{
  set label center "$NAME" at labelxpos,labelypos font ",10"
}

plot alt(x) lc 3 notitle

set output
exit
EOL

gnuplot ./skychart.gpl

mv skychart.pdf $SCRIPTS/
acroread $SCRIPTS/skychart.pdf &

else
  echo "usage: skychart.sh [RA(h)] [DEC(deg)] [OBJECTNAME] [DAYLIGHT_SAVING (1/0)]"
  echo " e.g.: skychart.sh  15.28   7.02       A2052        1"
fi
