program moon; { Mondpositionen berechnen }

const
   {$I b:math_con }

type
   DATUM = record
              jahr:integer;
              monat:1..12;
              tag:1..31;
           end;

   ZEIT = record
             stunde:0..23;
             minute,sekunde:0..59;
          end;

   JUL = record
            ganz:long_integer;
            fract:real;
         end;

var start,stop:JUL;
    date:DATUM;
    lokal:ZEIT;
    ra,de,r,f,f1:real;
    i,increment,h,h1,h2:integer;
    printer:boolean;
    c:char;

   {$I b:math_sub }

procedure juldat (var julian:JUL; gregorian:DATUM; uhrzeit:ZEIT;
                  flag:boolean);
external;

procedure gregdat(var gregorian:DATUM; var uhrzeit:ZEIT; julian:JUL;
                  flag:boolean);
external;

procedure lun_pos (var ra,de,r:real; julian:JUL);
external;

procedure par_ein(var start,stop:JUL; var incr:integer; var printer:boolean);
var lokal:ZEIT;
    date:DATUM;
    greg:boolean;
    c:char;
    t:real;
begin
   writeln(chr(27),'E< CALCULATE LUNAR POSITION >');
   writeln;
   writeln('  Enter parameters:');
   write('> Enter start: year : '); readln(date.jahr);
   write('>              month: '); readln(date.monat);
   write('>              day  : '); readln(date.tag);
   write('>              gregorian calendar (y/n): '); read(c);
   writeln;
   if c = 'n' then greg := false else greg := true;
   lokal.stunde := 0; lokal.minute := 0; lokal.sekunde := 0;
   juldat(start,date,lokal,greg);
   write('> Enter stop : year : '); readln(date.jahr);
   write('>              month: '); readln(date.monat);
   write('>              day  : '); readln(date.tag);
   write('>              gregorian calendar (y/n): '); read(c);
   writeln;
   if c = 'n' then greg := false else greg := true;
   juldat(stop,date,lokal,greg);
   write('> Enter UT   : hour  : '); readln(lokal.stunde);
   write('>              minute: '); readln(lokal.minute);
   write('>              second: '); readln(lokal.sekunde);
   t := (lokal.stunde * 3600.0 + lokal.minute * 60.0 + lokal.sekunde) /
        86400.0;
   start.fract := start.fract + t;
   stop.fract := stop.fract + t;
   if start.fract < 0.0 then
   begin
      start.fract := start.fract + 1.0; start.ganz := start.ganz - 1;
      stop.fract := stop.fract + 1.0; stop.ganz := stop.ganz - 1
   end;
   writeln;
   write('> Enter increment: '); readln(incr);
   writeln;
   write('> Output to screen or printer (s/p): '); read(c);
   if c = 'p' then printer := true else printer := false;
end;

begin
   par_ein(start,stop,increment,printer);
   gregdat(date,lokal,start,true); i := 0;
   if printer then rewrite(output,'LST:');
   while start.ganz <= stop.ganz do
   begin
      if i = 0 then
      begin
         if not printer then write(chr(27),'E');
         writeln('       Lunar positions of at ',lokal.stunde:2,'h',
         lokal.minute:2,'m',lokal.sekunde:2,'s UT for equinoxe 1950.0');
         writeln;
         writeln('          Date        RA         De         r/Erdradien');
         writeln('       ------------------------------------------------')
      end;
      lun_pos(ra,de,r,start);
      ra := deg(ra) / 15.0; de := deg(de);
      h := trunc(ra); f := (ra - h) * 60;
      h2 := sign(de); de := abs(de); h1 := h2 * trunc(de);
      f1 := (de - abs(h1)) * 60.0;
      gregdat(date,lokal,start,true);
      write('       ');
      writeln(date.jahr:4,date.monat:3,date.tag:3,'  ',h:3,f:5:1,'   ',
              h1:3,f1:5:1,'   ',r:7:4);
      i := i + 1;
      start.ganz := start.ganz + increment;
      if not printer then
         if i = 15 then
         begin
            writeln; writeln('                     PRESS ANY KEY TO CONTINUE');
            read(c);
            i := 0
         end
   end;
   read(c)
end.

