home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d123456 / MOON20.ZIP / MOON.PAS < prev    next >
Pascal/Delphi Source File  |  2001-07-07  |  82KB  |  2,778 lines

  1. unit moon;
  2.  
  3.  {$i ah_def.inc }
  4.  
  5. { Copyright 1997-2001 Andreas H÷rstemeier            Version 2.0 2001-07-07 }
  6. { this component is public domain - please check the file moon.hlp for       }
  7. { more detailed info on usage and distributing                               }
  8.  
  9. { Algorithms taken from the book "Astronomical Algorithms" by Jean Meeus     }
  10.  
  11. (*$b-*)   { I may make use of the shortcut boolean eval }
  12.  
  13. (*@/// interface *)
  14. interface
  15.  
  16.  
  17. (*@/// uses *)
  18. uses
  19.   ah_math,
  20.   vsop,
  21.   sysutils;
  22. (*@\\\*)
  23.  
  24. type
  25.   TMoonPhase=(Newmoon,WaxingCrescrent,FirstQuarter,WaxingGibbous,
  26.               Fullmoon,WaningGibbous,LastQuarter,WaningCrescent);
  27.   TSeason=(Winter,Spring,Summer,Autumn);
  28.   TEclipse=(none, partial, noncentral, circular, circulartotal, total, halfshadow);
  29.   E_NoRiseSet=class(Exception);
  30.   E_OutOfAlgorithmRange=class(Exception);
  31.   TSolarTerm=(st_z2,st_j3,st_z3,st_j4,st_z4,st_j5,st_z5,st_j6,st_z6,
  32.               st_j7,st_z7,st_j8,st_z8,st_j9,st_z9,st_j10,st_z10,
  33.               st_j11,st_z11,st_j12,st_z12,st_j1,st_z1,st_j2);
  34.   TChineseZodiac=(ch_rat,ch_ox,ch_tiger,ch_rabbit,ch_dragon,ch_snake,
  35.                       ch_horse,ch_goat,ch_monkey,ch_chicken,ch_dog,ch_pig);
  36.   TChineseStem=(ch_jia,ch_yi,ch_bing,ch_ding,ch_wu,ch_ji,
  37.                     ch_geng,ch_xin,ch_ren,ch_gui);
  38.   (*@/// TChineseCycle= record *)
  39.   TChineseCycle=record
  40.     zodiac: TChineseZodiac;
  41.     stem:   TChineseStem;
  42.     end;
  43.   (*@\\\*)
  44.   (*@/// TChineseDate = record *)
  45.   TChineseDate = record
  46.     cycle: integer;
  47.     year: integer;
  48.     epoch_years: integer;
  49.     month: integer;
  50.     leap: boolean;
  51.     leapyear: boolean;
  52.     day: integer;
  53.     yearcycle: TChineseCycle;
  54.     daycycle: TChineseCycle;
  55.     monthcycle: TChineseCycle;
  56.     end;
  57.   (*@\\\*)
  58.  
  59. const
  60.   (* Date of calendar reformation - start of gregorian calendar *)
  61.   calendar_change_standard: extended = 2299160.5;
  62.   calendar_change_russia: extended = 2421638.5;
  63.   calendar_change_england: extended = 2361221.5;
  64.   calendar_change_sweden: extended = 2361389.5;
  65.   (*@/// Jewish_Month_Name:array[1..13] of string *)
  66.   Jewish_Month_Name:array[1..13] of string = (
  67.     'Nisan',
  68.     'Iyar',
  69.     'Sivan',
  70.     'Tammuz',
  71.     'Av',
  72.     'Elul',
  73.     'Tishri',
  74.     'Heshvan',
  75.     'Kislev',
  76.     'Tevet',
  77.     'Shevat',
  78.     'Adar',
  79.     'Adar 2'
  80.     );
  81.   (*@\\\*)
  82.  
  83.  
  84. { Calendar algorithms }
  85. function julian_date(date:TDateTime):extended;
  86. function delphi_date(juldat:extended):TDateTime;
  87. function EasterDate(year:integer):TDateTime;
  88. function EasterDateJulian(year:integer):TDateTime;
  89. function PesachDate(year:integer):TDateTime;
  90. procedure DecodeDateJewish(date: TDateTime; var year,month,day: word);
  91. function EncodeDateJewish(year,month,day: word):TDateTime;
  92. function WeekNumber(date:TDateTime):integer;
  93.  
  94. { Convert date to julian date and back }
  95. function Calc_Julian_date_julian(year,month,day:word):extended;
  96. function Calc_Julian_date_gregorian(year,month,day:word):extended;
  97. function Calc_Julian_date_switch(year,month,day:word; switch_date:extended):extended;
  98. function Calc_Julian_date(year,month,day:word):extended;
  99. procedure Calc_Calendar_date_julian(juldat:extended; var year,month,day:word);
  100. procedure Calc_Calendar_date_gregorian(juldat:extended; var year,month,day:word);
  101. procedure Calc_Calendar_date_switch(juldat:extended; var year,month,day:word; switch_date:extended);
  102. procedure Calc_Calendar_date(juldat:extended; var year,month,day:word);
  103.  
  104. { corrected TDateTime functions }
  105. function isleapyearcorrect(year:word):boolean;
  106. function EncodedateCorrect(year,month,day: word):TDateTime;
  107. procedure DecodedateCorrect(date:TDateTime; var year,month,day: word);
  108. procedure DecodetimeCorrect(date:TDateTime; var hour,min,sec,msec: word);
  109. function FalsifyTdateTime(date:TDateTime):TdateTime;
  110.  
  111. { Sun and Moon }
  112. function sun_distance(date:TDateTime): extended;
  113. function moon_distance(date:TDateTime): extended;
  114. function age_of_moon(date:TDateTime): extended;
  115.  
  116. function last_phase(date:TDateTime; phase:TMoonPhase):TDateTime;
  117. function next_phase(date:TDateTime; phase:TMoonPhase):TDateTime;
  118. function nearest_phase(date: TDateTime):TMoonPhase;
  119. function next_blue_moon(date: TDateTime):TDateTime;
  120. function is_blue_moon(lunation: integer):boolean;
  121.  
  122. function moon_phase_angle(date: TDateTime):extended;
  123. function current_phase(date:TDateTime):extended;
  124. function lunation(date:TDateTime):integer;
  125.  
  126. function sun_diameter(date:TDateTime):extended;
  127. function moon_diameter(date:TDateTime):extended;
  128.  
  129. function Sun_Rise(date:TDateTime; latitude, longitude:extended):TDateTime;
  130. function Sun_Set(date:TDateTime; latitude, longitude:extended):TDateTime;
  131. function Sun_Transit(date:TDateTime; latitude, longitude:extended):TDateTime;
  132. function Morning_Twilight_Civil(date:TDateTime; latitude, longitude:extended):TDateTime;
  133. function Evening_Twilight_Civil(date:TDateTime; latitude, longitude:extended):TDateTime;
  134. function Morning_Twilight_Nautical(date:TDateTime; latitude, longitude:extended):TDateTime;
  135. function Evening_Twilight_Nautical(date:TDateTime; latitude, longitude:extended):TDateTime;
  136. function Morning_Twilight_Astronomical(date:TDateTime; latitude, longitude:extended):TDateTime;
  137. function Evening_Twilight_Astronomical(date:TDateTime; latitude, longitude:extended):TDateTime;
  138. function Moon_Rise(date:TDateTime; latitude, longitude:extended):TDateTime;
  139. function Moon_Set(date:TDateTime; latitude, longitude:extended):TDateTime;
  140. function Moon_Transit(date:TDateTime; latitude, longitude:extended):TDateTime;
  141.  
  142. function nextperigee(date:TDateTime):TDateTime;
  143. function nextapogee(date:TDateTime):TDateTime;
  144. function nextperihel(date:TDateTime):TDateTime;
  145. function nextaphel(date:TDateTime):TDateTime;
  146.  
  147. function NextEclipse(var date:TDateTime; sun:boolean):TEclipse;
  148.  
  149. procedure Moon_Position_Horizontal(date:TdateTime; longitude,latitude: extended; var elevation,azimuth: extended);
  150. procedure Sun_Position_Horizontal(date:TdateTime; longitude,latitude: extended; var elevation,azimuth: extended);
  151.  
  152. { Further useful functions }
  153. function star_time(date:TDateTime):extended;
  154. function StartSeason(year: integer; season:TSeason):TDateTime;
  155. function CalcSolarTerm(year: integer; term: TSolarTerm):TDateTime;
  156.  
  157. { Chinese calendar }
  158. function ChineseNewYear(year: integer): TDateTime;
  159. function ChineseDate(date: TdateTime): TChineseDate;
  160. function EncodeDateChinese(date:TChineseDate):TDateTime;
  161. (*@\\\0000000301*)
  162. (*@/// implementation *)
  163. implementation
  164.  
  165. (*$undef low_accuracy *)
  166.  
  167. const
  168.   AU=149597869;             (* astronomical unit in km *)
  169.   mean_lunation=29.530589;  (* Mean length of a month *)
  170.   tropic_year=365.242190;   (* Tropic year length *)
  171.   earth_radius=6378.15;     (* Radius of the earth *)
  172.  
  173. (*$ifdef delphi_ge_3 *)
  174. var
  175. (*$else *)
  176. const
  177. (*$endif *)
  178.   (* Shortcuts to avoid calling Encodedate too often *)
  179.   datetime_2000_01_01: extended = 0;
  180.   datetime_1999_01_01: extended = 0;
  181.   datetime_chinese_epoch: extended = 0;
  182.   datetime_first_lunation: extended = 0;
  183.   julian_offset: extended = 0;
  184.   (* How broken is the TDateTime? *)
  185.   negative_dates_broken: boolean = false;
  186.   calendar_reform_supported: boolean = true;
  187.   julian_calendar_before_1582: boolean = true;
  188.  
  189. const
  190.   beijing_longitude = -(116+25/60);
  191.  
  192. type
  193.   (*@/// t_coord = record *)
  194.   t_coord = record
  195.     longitude, latitude, radius: extended; (* lambda, beta, R *)
  196.     rektaszension, declination: extended;  (* alpha, delta *)
  197.     parallax: extended;
  198.     elevation, azimuth: extended;          (* h, A *)
  199.     end;
  200.   (*@\\\*)
  201.   T_RiseSet=(_rise,_set,_transit,_rise_civil,_rise_nautical,_rise_astro,_set_civil,_set_nautical,_set_astro);
  202.   TJewishYearStyle=(ys_common_deficient,ys_common_regular,ys_common_complete,
  203.                     ys_leap_deficient,ys_leap_regular,ys_leap_complete);
  204. const
  205.   (*@/// Jewish_Month_length:array[1..13,TJewishYearStyle] of shortint *)
  206.   Jewish_Month_length:array[1..13,TJewishYearStyle] of word = (
  207.    ( 30,30,30,30,30,30),
  208.    ( 29,29,29,29,29,29),
  209.    ( 30,30,30,30,30,30),
  210.    ( 29,29,29,29,29,29),
  211.    ( 30,30,30,30,30,30),
  212.    ( 29,29,29,29,29,29),
  213.    ( 30,30,30,30,30,30),
  214.    ( 29,29,30,29,29,30),
  215.    ( 29,30,30,29,30,30),
  216.    ( 29,29,29,29,29,29),
  217.    ( 30,30,30,30,30,30),
  218.    ( 29,29,29,30,30,30),
  219.    (  0, 0, 0,29,29,29)
  220.    );
  221.   (*@\\\*)
  222.   (*@/// Jewish_Month_Name_short:array[1..13] of string *)
  223.   Jewish_Month_Name_short:array[1..13] of string = (
  224.     'Nis',
  225.     'Iya',
  226.     'Siv',
  227.     'Tam',
  228.     'Av' ,
  229.     'Elu',
  230.     'Tis',
  231.     'Hes',
  232.     'Kis',
  233.     'Tev',
  234.     'She',
  235.     'Ada',
  236.     'Ad2'
  237.     );
  238.   (*@\\\*)
  239.   Jewish_year_length:array[TJewishYearStyle] of integer = (353,354,355,383,384,385);
  240.  
  241. { Julian date }
  242. (*@/// function julian_date(date:TDateTime):extended; *)
  243. function julian_date(date:TDateTime):extended;
  244. begin
  245.   julian_date:=julian_offset+date
  246.   end;
  247. (*@\\\*)
  248. (*@/// function delphi_date(juldat:extended):TDateTime; *)
  249. function delphi_date(juldat:extended):TDateTime;
  250. begin
  251.   delphi_date:=juldat-julian_offset;
  252.   end;
  253. (*@\\\*)
  254. (*@/// function isleapyearcorrect(year:word):boolean; *)
  255. function isleapyearcorrect(year:word):boolean;
  256. begin
  257.   if year<=1582 then
  258.     result:=((year mod 4)=0)
  259.   else
  260.     result:=(((year mod 4)=0) and ((year mod 100)<>0)) or
  261.              ((year mod 400)=0);
  262.   end;
  263. (*@\\\*)
  264. (*@/// function Calc_Julian_date_julian(year,month,day:word):extended; *)
  265. function Calc_Julian_date_julian(year,month,day:word):extended;
  266. begin
  267.   if (year<1) or (year>9999) then
  268.     raise EConvertError.Create('Invalid year');
  269.   if month<3 then begin
  270.     month:=month+12;
  271.     year:=year-1;
  272.     end;
  273.   case month of
  274.     3,5,7,8,10,12,13: if (day<1) or (day>31) then EConvertError.Create('Invalid day');
  275.     4,6,9,11:         if (day<1) or (day>30) then EConvertError.Create('Invalid day');
  276.     14: case day of
  277.           1..28: ;
  278.           29: if (year+1) mod 4<>0 then EConvertError.Create('Invalid day');
  279.           else EConvertError.Create('Invalid day');
  280.           end;
  281.      else raise EConvertError.Create('Invalid month');
  282.      end;
  283.   result:=trunc(365.25*(year+4716))+trunc(30.6001*(month+1))+day-1524.5;
  284.   end;
  285. (*@\\\*)
  286. (*@/// function Calc_Julian_date_gregorian(year,month,day:word):extended; *)
  287. function Calc_Julian_date_gregorian(year,month,day:word):extended;
  288. var
  289.   a,b: longint;
  290. begin
  291.   if (year<1) or (year>9999) then
  292.     raise EConvertError.Create('Invalid year');
  293.   if month<3 then begin
  294.     month:=month+12;
  295.     year:=year-1;
  296.     end;
  297.   a:=year div 100;
  298.   case month of
  299.     3,5,7,8,10,12,13: if (day<1) or (day>31) then EConvertError.Create('Invalid day');
  300.     4,6,9,11:         if (day<1) or (day>30) then EConvertError.Create('Invalid day');
  301.     14: case day of
  302.           1..28: ;
  303.           29: if (((year mod 4)<>0) or ((year mod 100)=0)) and
  304.                  ((year mod 400)<>0) then EConvertError.Create('Invalid day');
  305.           else EConvertError.Create('Invalid day');
  306.           end;
  307.      else raise EConvertError.Create('Invalid month');
  308.      end;
  309.   b:=2-a+(a div 4);
  310.   result:=trunc(365.25*(year+4716))+trunc(30.6001*(month+1))+day+b-1524.5;
  311.   end;
  312. (*@\\\*)
  313. (*@/// function Calc_Julian_date_switch(year,month,day:word; switch_date:extended):extended; *)
  314. function Calc_Julian_date_switch(year,month,day:word; switch_date:extended):extended;
  315. begin
  316.   result:=Calc_Julian_date_julian(year,month,day);
  317.   if result>=switch_date then begin
  318.     result:=Calc_Julian_date_gregorian(year,month,day);
  319.     if result<switch_date then
  320.       raise EConvertError.Create('Date invalid due to calendar change');
  321.     end;
  322.   end;
  323. (*@\\\*)
  324. (*@/// function Calc_Julian_date(year,month,day:word):extended; *)
  325. function Calc_Julian_date(year,month,day:word):extended;
  326. begin
  327.   result:=Calc_Julian_date_switch(year,month,day,calendar_change_standard);
  328.   end;
  329. (*@\\\*)
  330. (*@/// procedure Calc_Calendar_date_julian(juldat:extended; var year,month,day:word); *)
  331. procedure Calc_Calendar_date_julian(juldat:extended; var year,month,day:word);
  332. var
  333.   z,a,b,c,d,e: longint;
  334. begin
  335.   if juldat<0 then
  336.     raise EConvertError.Create('Negative julian dates not supported');
  337.   juldat:=juldat+0.5;
  338.   z:=trunc(juldat);
  339.   a:=z;
  340.   b:=a+1524;
  341.   c:=trunc((b-122.1)/365.25);
  342.   d:=trunc(365.25*c);
  343.   e:=trunc((b-d)/30.6001);
  344.   day:=b-d-trunc(30.6001*e);
  345.   year:=c-4716;
  346.   if e<14 then
  347.     month:=e-1
  348.   else begin
  349.     month:=e-13;
  350.     year:=year+1;
  351.     end;
  352.   end;
  353. (*@\\\*)
  354. (*@/// procedure Calc_Calendar_date_gregorian(juldat:extended; var year,month,day:word); *)
  355. procedure Calc_Calendar_date_gregorian(juldat:extended; var year,month,day:word);
  356. var
  357.   alpha,z,a,b,c,d,e: longint;
  358. begin
  359.   if juldat<0 then
  360.     raise EConvertError.Create('Negative julian dates not supported');
  361.   juldat:=juldat+0.5;
  362.   z:=trunc(juldat);
  363.   alpha:=trunc((z-1867216.25)/36524.25);
  364.   a:=z+1+alpha-trunc(alpha/4);
  365.   b:=a+1524;
  366.   c:=trunc((b-122.1)/365.25);
  367.   d:=trunc(365.25*c);
  368.   e:=trunc((b-d)/30.6001);
  369.   day:=b-d-trunc(30.6001*e);
  370.   year:=c-4716;
  371.   if e<14 then
  372.     month:=e-1
  373.   else begin
  374.     month:=e-13;
  375.     year:=year+1;
  376.     end;
  377.   end;
  378. (*@\\\*)
  379. (*@/// procedure Calc_Calendar_date_switch(juldat:extended; var year,month,day:word; switch_date:extended); *)
  380. procedure Calc_Calendar_date_switch(juldat:extended; var year,month,day:word; switch_date:extended);
  381. begin
  382.   if juldat<0 then
  383.     raise EConvertError.Create('Negative julian dates not supported');
  384.   if juldat<switch_date then
  385.     Calc_Calendar_date_julian(juldat,year,month,day)
  386.   else
  387.     Calc_Calendar_date_gregorian(juldat,year,month,day);
  388.   end;
  389. (*@\\\*)
  390. (*@/// procedure Calc_Calendar_date(juldat:extended; var year,month,day:word); *)
  391. procedure Calc_Calendar_date(juldat:extended; var year,month,day:word);
  392. begin
  393.   Calc_Calendar_date_switch(juldat,year,month,day,calendar_change_standard);
  394.   end;
  395. (*@\\\*)
  396.  
  397. { TDateTime correction }
  398. (*@/// procedure check_TDatetime; *)
  399. (* Check how many bugs the TDateTime has compare to julian date *)
  400. procedure check_TDatetime;
  401. var
  402.   h,m,s,ms: word;
  403.   d1,d2: TDateTime;
  404. begin
  405.   (*$ifndef delphi_1 *)        { Delphi 1 did not allow negative values }
  406.   decodetime(-1.9,h,m,s,ms);
  407.   negative_dates_broken:=h>12;
  408.   (*$endif delphi_1 *)
  409.   d1:=EncodeDate(1582,10,15);
  410.   d2:=EncodeDate(1582,10,4);
  411.   calendar_reform_supported:=((d1-d2)=1);
  412.   d1:=EncodeDate(1500,3,1);
  413.   d2:=EncodeDate(1500,2,28);
  414.   julian_calendar_before_1582:=((d1-d2)=2);
  415.   end;
  416. (*@\\\0000001107*)
  417. (*@/// function EncodedateCorrect(year,month,day: word):TDateTime; *)
  418. function EncodedateCorrect(year,month,day: word):TDateTime;
  419. begin
  420.   result:=delphi_date(Calc_Julian_date(year,month,day));
  421.   end;
  422. (*@\\\*)
  423. (*@/// procedure DecodedateCorrect(date:TDateTime; var year,month,day: word); *)
  424. procedure DecodedateCorrect(date:TDateTime; var year,month,day: word);
  425. begin
  426.   Calc_Calendar_date(julian_date(date),year,month,day);
  427.   end;
  428. (*@\\\*)
  429. (*@/// procedure DecodetimeCorrect(date:TDateTime; var hour,min,sec,msec: word); *)
  430. procedure DecodetimeCorrect(date:TDateTime; var hour,min,sec,msec: word);
  431. begin
  432.   Decodetime(1+frac(date),hour,min,sec,msec);
  433.   end;
  434. (*@\\\*)
  435. (*@/// function FalsifyTdateTime(date:TDateTime):TdateTime; *)
  436. function FalsifyTdateTime(date:TDateTime):TdateTime;
  437. var
  438.   d,m,y: word;
  439. begin
  440.   DecodedateCorrect(date,d,m,y);
  441.   result:=Encodedate(d,m,y);
  442.   result:=result+frac(date);
  443.   if negative_dates_broken and (result<0) and (frac(result)<>0) then
  444.     result:=int(result)-(1-abs(frac(result)));
  445.   end;
  446. (*@\\\*)
  447.  
  448. { Calendar functions }
  449. (*@/// function WeekNumber(date:TDateTime):integer; *)
  450. function WeekNumber(date:TDateTime):integer;
  451. var
  452.   y,m,d: word;
  453.   h: integer;
  454.   FirstofJanuary,
  455.   FirstThursday,
  456.   FirstWeekStart: TDateTime;
  457. begin
  458.   DecodedateCorrect(date,y,m,d);
  459.   FirstofJanuary:=EncodedateCorrect(y,1,1);
  460.   h:=dayOfWeek(FirstofJanuary);
  461.   FirstThursday:=FirstofJanuary+((12-h) mod 7);
  462.   FirstWeekStart:=FirstThursday-3;
  463.   if trunc(date)<FirstWeekStart then
  464.     result:=WeekNumber(FirstofJanuary-1) (* 12-31 of previous year *)
  465.   else
  466.     result:=(round(trunc(date)-FirstWeekStart) div 7)+1;
  467.   end;
  468. (*@\\\*)
  469. (*@/// function EasterDateGregorian(year:integer):TDateTime; *)
  470. function EasterDateGregorian(year:integer):TDateTime;
  471. var
  472.   a,b,c,d,e,m,n,day,month: integer;
  473. begin
  474.   case year of
  475.     1583..1699:  begin  m:=22; n:=2;  end;
  476.     1700..1799:  begin  m:=23; n:=3;  end;
  477.     1800..1899:  begin  m:=23; n:=4;  end;
  478.     1900..2099:  begin  m:=24; n:=5;  end;
  479.     2100..2199:  begin  m:=24; n:=6;  end;
  480.     2200..2399:  begin  m:=25; n:=0;  end;
  481.     else raise E_OutOfAlgorithmRange.Create('Out of range of the algorithm');
  482.     end;
  483.   a:=year mod 19;
  484.   b:=year mod 4;
  485.   c:=year mod 7;
  486.   d:=(19*a+m) mod 30;
  487.   e:=(2*b+4*c+6*d+n) mod 7;
  488.   day:=(22+d+e);
  489.   if day<=31 then
  490.     month:=3
  491.   else begin
  492.     day:=(d+e-9);
  493.     month:=4;
  494.     end;
  495.   if (day=26) and (month=4) then  day:=19;
  496.   if (day=25) and (month=4) and (d=28) and (e=6) and (a>10) then  day:=18;
  497.   result:=EncodedateCorrect(year,month,day);
  498.   end;
  499. (*@\\\*)
  500. (*@/// function EasterDate(year:integer):TDateTime; *)
  501. function EasterDate(year:integer):TDateTime;
  502. begin
  503.   if year<1583 then
  504.     result:=EasterDateJulian(year)
  505.   else
  506.     result:=EasterDateGregorian(year);
  507.   end;
  508. (*@\\\*)
  509. (*@/// function EasterDateJulian(year:integer):TDateTime; *)
  510. function EasterDateJulian(year:integer):TDateTime;
  511. var
  512.   a,b,c,d,e,f,g: integer;
  513. begin
  514.   a:=year mod 4;
  515.   b:=year mod 7;
  516.   c:=year mod 19;
  517.   d:=(19*c+15) mod 30;
  518.   e:=(2*a+4*b-d+34) mod 7;
  519.   f:=(d+e+114) div 31;
  520.   g:=(d+e+114) mod 31;
  521.   result:=EncodedateCorrect(year,f,g+1);
  522.   end;
  523. (*@\\\*)
  524. (*@/// function PesachDate(year:integer):TDateTime; *)
  525. function PesachDate(year:integer):TDateTime;
  526. var
  527.   a,b,c,d,j,s: integer;
  528.   q,r: extended;
  529. begin
  530.   if year<359 then
  531.     raise E_OutOfAlgorithmRange.Create('Out of range of the algorithm');
  532.   c:=year div 100;
  533.   if year<1583 then
  534.     s:=0
  535.   else
  536.     s:=(3*c-5) div 4;
  537.   a:=(12*year+12) mod 19;
  538.   b:=year mod 4;
  539.   q:=-1.904412361576+1.554241796621*a+0.25*b-0.003177794022*year+s;
  540.   j:=(trunc(q)+3*year+5*b+2-s) mod 7;
  541.   r:=frac(q);
  542.   if false then
  543.   else if j in [2,4,6] then
  544.     d:=trunc(q)+23
  545.   else if (j=1) and (a>6) and (r>=0.632870370) then
  546.     d:=trunc(q)+24
  547.   else if (j=0) and (a>11) and (r>=0.897723765) then
  548.     d:=trunc(q)+23
  549.   else
  550.     d:=trunc(q)+22;
  551.  
  552.   if d>31 then
  553.     result:=EncodedateCorrect(year,4,d-31)
  554.   else
  555.     result:=EncodedateCorrect(year,3,d);
  556.   end;
  557. (*@\\\*)
  558. (*@/// function JewishYearStyle(year:word):TJewishYearStyle; *)
  559. function JewishYearStyle(year:word):TJewishYearStyle;
  560. var
  561.   i: TJewishYearStyle;
  562.   yearlength: integer;
  563. begin
  564.   yearlength:=round(pesachdate(year-3760)-pesachdate(year-3761));
  565.   result:=low(TJewishYearStyle);
  566.   for i:=low(TJewishYearStyle) to high(TJewishYearStyle) do
  567.     if yearlength=Jewish_year_length[i] then
  568.       result:=i;
  569.   end;
  570. (*@\\\*)
  571. (*@/// function EncodeDateJewish(year,month,day: word):TDateTime; *)
  572. function EncodeDateJewish(year,month,day: word):TDateTime;
  573. var
  574.   yearstyle: TJewishYearStyle;
  575.   offset,i: integer;
  576. begin
  577.   yearstyle:=JewishYearStyle(year);
  578.   if (month<1) or (month>13) then
  579.     raise EConvertError.Create('Invalid month');
  580.   if (month=13) and
  581.      (yearstyle in [ys_common_deficient,ys_common_regular,ys_common_complete]) then
  582.     raise EConvertError.Create('Invalid month');
  583.   if (day<1) or (day>Jewish_Month_length[month,yearstyle]) then
  584.     raise EConvertError.Create('Invalid day');
  585.   offset:=day-1;
  586.   (* count months from tishri *)
  587.   month:=(month+6) mod 13 +1;
  588.   for i:=1 to month-1 do
  589.     offset:=offset+Jewish_Month_length[(i+5) mod 13 +1,yearstyle];
  590.   result:=pesachdate(year-3761)+163+offset;
  591.   end;
  592. (*@\\\*)
  593. (*@/// procedure DecodeDateJewish(date: TDateTime; var year,month,day: word); *)
  594. procedure DecodeDateJewish(date: TDateTime; var year,month,day: word);
  595. var
  596.   year_g,month_g,day_g: word;
  597.   yearstyle: TJewishYearStyle;
  598.   tishri1: TDateTime;
  599. begin
  600.   DecodedateCorrect(date,year_g,month_g,day_g);
  601.   tishri1:=pesachdate(year_g)+163;
  602.   if tishri1>date then begin
  603.     tishri1:=pesachdate(year_g-1)+163;
  604.     year:=year_g+3760;
  605.     end
  606.   else
  607.     year:=year_g+3761;
  608.   yearstyle:=JewishYearStyle(year);
  609.   month:=7;
  610.   day:=round(date-tishri1+1);
  611.   while day>Jewish_Month_length[month,yearstyle] do begin
  612.     dec(day,Jewish_Month_length[month,yearstyle]);
  613.     month:=(month mod 13) +1;
  614.     end;
  615.   end;
  616. (*@\\\*)
  617.  
  618. { Misc }
  619. (*@/// procedure calc_epsilon_phi(date:TDateTime; var delta_phi,epsilon:extended); *)
  620. procedure calc_epsilon_phi(date:TDateTime; var delta_phi,epsilon:extended);
  621. (*$ifndef low_accuracy *)
  622. const
  623.   (*@/// arg_mul:array[0..30,0..4] of shortint = (..); *)
  624.   arg_mul:array[0..30,0..4] of shortint = (
  625.      ( 0, 0, 0, 0, 1),
  626.      (-2, 0, 0, 2, 2),
  627.      ( 0, 0, 0, 2, 2),
  628.      ( 0, 0, 0, 0, 2),
  629.      ( 0, 1, 0, 0, 0),
  630.      ( 0, 0, 1, 0, 0),
  631.      (-2, 1, 0, 2, 2),
  632.      ( 0, 0, 0, 2, 1),
  633.      ( 0, 0, 1, 2, 2),
  634.      (-2,-1, 0, 2, 2),
  635.      (-2, 0, 1, 0, 0),
  636.      (-2, 0, 0, 2, 1),
  637.      ( 0, 0,-1, 2, 2),
  638.      ( 2, 0, 0, 0, 0),
  639.      ( 0, 0, 1, 0, 1),
  640.      ( 2, 0,-1, 2, 2),
  641.      ( 0, 0,-1, 0, 1),
  642.      ( 0, 0, 1, 2, 1),
  643.      (-2, 0, 2, 0, 0),
  644.      ( 0, 0,-2, 2, 1),
  645.      ( 2, 0, 0, 2, 2),
  646.      ( 0, 0, 2, 2, 2),
  647.      ( 0, 0, 2, 0, 0),
  648.      (-2, 0, 1, 2, 2),
  649.      ( 0, 0, 0, 2, 0),
  650.      (-2, 0, 0, 2, 0),
  651.      ( 0, 0,-1, 2, 1),
  652.      ( 0, 2, 0, 0, 0),
  653.      ( 2, 0,-1, 0, 1),
  654.      (-2, 2, 0, 2, 2),
  655.      ( 0, 1, 0, 0, 1)
  656.                    );
  657.   (*@\\\*)
  658.   (*@/// arg_phi:array[0..30,0..1] of longint = (); *)
  659.   arg_phi:array[0..30,0..1] of longint = (
  660.      (-171996,-1742),
  661.      ( -13187,  -16),
  662.      (  -2274,   -2),
  663.      (   2062,    2),
  664.      (   1426,  -34),
  665.      (    712,    1),
  666.      (   -517,   12),
  667.      (   -386,   -4),
  668.      (   -301,    0),
  669.      (    217,   -5),
  670.      (   -158,    0),
  671.      (    129,    1),
  672.      (    123,    0),
  673.      (     63,    0),
  674.      (     63,    1),
  675.      (    -59,    0),
  676.      (    -58,   -1),
  677.      (    -51,    0),
  678.      (     48,    0),
  679.      (     46,    0),
  680.      (    -38,    0),
  681.      (    -31,    0),
  682.      (     29,    0),
  683.      (     29,    0),
  684.      (     26,    0),
  685.      (    -22,    0),
  686.      (     21,    0),
  687.      (     17,   -1),
  688.      (     16,    0),
  689.      (    -16,    1),
  690.      (    -15,    0)
  691.     );
  692.   (*@\\\*)
  693.   (*@/// arg_eps:array[0..30,0..1] of longint = (); *)
  694.   arg_eps:array[0..30,0..1] of longint = (
  695.      ( 92025,   89),
  696.      (  5736,  -31),
  697.      (   977,   -5),
  698.      (  -895,    5),
  699.      (    54,   -1),
  700.      (    -7,    0),
  701.      (   224,   -6),
  702.      (   200,    0),
  703.      (   129,   -1),
  704.      (   -95,    3),
  705.      (     0,    0),
  706.      (   -70,    0),
  707.      (   -53,    0),
  708.      (     0,    0),
  709.      (   -33,    0),
  710.      (    26,    0),
  711.      (    32,    0),
  712.      (    27,    0),
  713.      (     0,    0),
  714.      (   -24,    0),
  715.      (    16,    0),
  716.      (    13,    0),
  717.      (     0,    0),
  718.      (   -12,    0),
  719.      (     0,    0),
  720.      (     0,    0),
  721.      (   -10,    0),
  722.      (     0,    0),
  723.      (    -8,    0),
  724.      (     7,    0),
  725.      (     9,    0)
  726.     );
  727.   (*@\\\*)
  728. (*$endif *)
  729. var
  730.   t,omega: extended;
  731. (*$ifdef low_accuracy *)
  732.   l,ls: extended;
  733. (*$else *)
  734.   d,m,ms,f,s: extended;
  735.   i: integer;
  736. (*$endif *)
  737.   epsilon_0,delta_epsilon: extended;
  738. begin
  739.   t:=(julian_date(date)-2451545.0)/36525;
  740.  
  741.   (* longitude of rising knot *)
  742.   omega:=put_in_360(125.04452+(-1934.136261+(0.0020708+1/450000*t)*t)*t);
  743.  
  744. (*$ifdef low_accuracy *)
  745.   (*@/// delta_phi and delta_epsilon - low accuracy *)
  746.   (* mean longitude of sun (l) and moon (ls) *)
  747.   l:=280.4665+36000.7698*t;
  748.   ls:=218.3165+481267.8813*t;
  749.  
  750.   (* correction due to nutation *)
  751.   delta_epsilon:=9.20*cos_d(omega)+0.57*cos_d(2*l)+0.10*cos_d(2*ls)-0.09*cos_d(2*omega);
  752.  
  753.   (* longitude correction due to nutation *)
  754.   delta_phi:=(-17.20*sin_d(omega)-1.32*sin_d(2*l)-0.23*sin_d(2*ls)+0.21*sin_d(2*omega))/3600;
  755.   (*@\\\*)
  756. (*$else *)
  757.   (*@/// delta_phi and delta_epsilon - higher accuracy *)
  758.   (* mean elongation of moon to sun *)
  759.   d:=put_in_360(297.85036+(445267.111480+(-0.0019142+t/189474)*t)*t);
  760.  
  761.   (* mean anomaly of the sun *)
  762.   m:=put_in_360(357.52772+(35999.050340+(-0.0001603-t/300000)*t)*t);
  763.  
  764.   (* mean anomly of the moon *)
  765.   ms:=put_in_360(134.96298+(477198.867398+(0.0086972+t/56250)*t)*t);
  766.  
  767.   (* argument of the latitude of the moon *)
  768.   f:=put_in_360(93.27191+(483202.017538+(-0.0036825+t/327270)*t)*t);
  769.  
  770.   delta_phi:=0;
  771.   delta_epsilon:=0;
  772.  
  773.   for i:=0 to 30 do begin
  774.     s:= arg_mul[i,0]*d
  775.        +arg_mul[i,1]*m
  776.        +arg_mul[i,2]*ms
  777.        +arg_mul[i,3]*f
  778.        +arg_mul[i,4]*omega;
  779.     delta_phi:=delta_phi+(arg_phi[i,0]+arg_phi[i,1]*t*0.1)*sin_d(s);
  780.     delta_epsilon:=delta_epsilon+(arg_eps[i,0]+arg_eps[i,1]*t*0.1)*cos_d(s);
  781.     end;
  782.  
  783.   delta_phi:=delta_phi*0.0001/3600;
  784.   delta_epsilon:=delta_epsilon*0.0001/3600;
  785.   (*@\\\*)
  786. (*$endif *)
  787.  
  788.   (* angle of ecliptic *)
  789.   epsilon_0:=84381.448+(-46.8150+(-0.00059+0.001813*t)*t)*t;
  790.  
  791.   epsilon:=(epsilon_0+delta_epsilon)/3600;
  792.   end;
  793. (*@\\\0000000A0A*)
  794. (*@/// function star_time(date:TDateTime):extended;            // degrees *)
  795. function star_time(date:TDateTime):extended;
  796. var
  797.   jd, t: extended;
  798.   delta_phi, epsilon: extended;
  799. begin
  800.   jd:=julian_date(date);
  801.   t:=(jd-2451545.0)/36525;
  802.   calc_epsilon_phi(date,delta_phi,epsilon);
  803.   result:=put_in_360(280.46061837+360.98564736629*(jd-2451545.0)+
  804.                      t*t*(0.000387933-t/38710000)+
  805.                      delta_phi*cos_d(epsilon) );
  806.   end;
  807. (*@\\\*)
  808.  
  809. { Coordinate functions }
  810. (*@/// procedure calc_geocentric(var coord:t_coord; date:TDateTime); *)
  811. { Based upon Chapter 13 (12) and 22 (21) of Meeus }
  812.  
  813. procedure calc_geocentric(var coord:t_coord; date:TDateTime);
  814. var
  815.   epsilon: extended;
  816.   delta_phi: extended;
  817.   alpha,delta: extended;
  818. begin
  819.   calc_epsilon_phi(date,delta_phi,epsilon);
  820.   coord.longitude:=put_in_360(coord.longitude+delta_phi);
  821.  
  822.   (* geocentric coordinates *)
  823. {   alpha:=arctan2_d(cos_d(epsilon)*sin_d(o),cos_d(o)); }
  824. {   delta:=arcsin_d(sin_d(epsilon)*sin_d(o)); }
  825.   alpha:=arctan2_d( sin_d(coord.longitude)*cos_d(epsilon)
  826.                    -tan_d(coord.latitude)*sin_d(epsilon)
  827.                   ,cos_d(coord.longitude));
  828.   delta:=arcsin_d( sin_d(coord.latitude)*cos_d(epsilon)
  829.                   +cos_d(coord.latitude)*sin_d(epsilon)*sin_d(coord.longitude));
  830.  
  831.   coord.rektaszension:=alpha;
  832.   coord.declination:=delta;
  833.   end;
  834. (*@\\\0000000129*)
  835. (*@/// procedure calc_horizontal(var coord:t_coord; date:TDateTime longitude,latitude: extended); *)
  836. procedure calc_horizontal(var coord:t_coord; date:TDateTime; longitude,latitude: extended);
  837. var
  838.   h: extended;
  839. begin
  840.   h:=put_in_360(star_time(date)-coord.rektaszension-longitude);
  841.   coord.azimuth:=arctan2_d(sin_d(h),
  842.                            cos_d(h)*sin_d(latitude)-
  843.                            tan_d(coord.declination)*cos_d(latitude) );
  844.   coord.elevation:=arcsin_d(sin_d(latitude)*sin_d(coord.declination)+
  845.                             cos_d(latitude)*cos_d(coord.declination)*cos_d(h));
  846.   end;
  847. (*@\\\*)
  848.  
  849. (*@/// function sun_coordinate(date:TDateTime):t_coord; *)
  850. { Based upon Chapter 25 (24) of Meeus - low accurancy }
  851.  
  852. (*@/// function sun_coordinate_low(date:TDateTime):t_coord; *)
  853. function sun_coordinate_low(date:TDateTime):t_coord;
  854. var
  855.   t,e,m,c,nu: extended;
  856.   l0,o,omega,lambda: extended;
  857. begin
  858.   t:=(julian_date(date)-2451545.0)/36525;
  859.  
  860.   (* geometrical mean longitude of the sun *)
  861.   l0:=280.46645+(36000.76983+0.0003032*t)*t;
  862.  
  863.   (* excentricity of the earth orbit *)
  864.   e:=0.016708617+(-0.000042037-0.0000001236*t)*t;
  865.  
  866.   (* mean anomaly of the sun *)
  867.   m:=357.52910+(35999.05030-(0.0001559+0.00000048*t)*t)*t;
  868.  
  869.   (* mean point of sun *)
  870.   c:= (1.914600+(-0.004817-0.000014*t)*t)*sin_d(m)
  871.      +(0.019993-0.000101*t)*sin_d(2*m)
  872.      +0.000290*sin_d(3*m);
  873.  
  874.   (* true longitude of the sun *)
  875.   o:=put_in_360(l0+c);
  876.  
  877.   (* true anomaly of the sun *)
  878.   nu:=m+c;
  879.  
  880.   (* distance of the sun in km *)
  881.   result.radius:=(1.000001018*(1-e*e))/(1+e*cos_d(nu))*AU;
  882.  
  883.   (* apparent longitude of the sun *)
  884.   omega:=125.04452+(-1934.136261+(0.0020708+1/450000*t)*t)*t;
  885.   lambda:=put_in_360(o-0.00569-0.00478*sin_d(omega)
  886.                      -20.4898/3600/(result.radius/AU));
  887.  
  888.   result.longitude:=lambda;
  889.   result.latitude:=0;
  890.  
  891.   calc_geocentric(result,date);
  892.   end;
  893. (*@\\\*)
  894. (*@/// function sun_coordinate(date:TDateTime):t_coord; *)
  895. function sun_coordinate(date:TDateTime):t_coord;
  896. var
  897.   l,b,r: extended;
  898.   lambda,t: extended;
  899. begin
  900.   earth_coord(date,l,b,r);
  901.   (* convert earth coordinate to sun coordinate *)
  902.   l:=l+180;
  903.   b:=-b;
  904.   (* conversion to FK5 *)
  905.   t:=(julian_date(date)-2451545.0)/365250.0*10;
  906.   lambda:=l+(-1.397-0.00031*t)*t;
  907.   l:=l-0.09033/3600;
  908.   b:=b+0.03916/3600*(cos_d(lambda)-sin_d(lambda));
  909.   (* aberration *)
  910.   l:=l-20.4898/3600/r;
  911.   (* correction of nutation - is done inside calc_geocentric *)
  912. {   calc_epsilon_phi(date,delta_phi,epsilon); }
  913. {   l:=l+delta_phi; }
  914.   (* fill result and convert to geocentric *)
  915.   result.longitude:=put_in_360(l);
  916.   result.latitude:=b;
  917.   result.radius:=r*AU;
  918.   calc_geocentric(result,date);
  919.   end;
  920. (*@\\\*)
  921. (*@\\\0000000126*)
  922. (*@/// function moon_coordinate(date:TDateTime):t_coord; *)
  923. { Based upon Chapter 47 (45) of Meeus }
  924.  
  925. function moon_coordinate(date:TDateTime):t_coord;
  926. const
  927.   (*@/// arg_lr:array[0..59,0..3] of shortint = (..); *)
  928.   arg_lr:array[0..59,0..3] of shortint = (
  929.      ( 0, 0, 1, 0),
  930.      ( 2, 0,-1, 0),
  931.      ( 2, 0, 0, 0),
  932.      ( 0, 0, 2, 0),
  933.      ( 0, 1, 0, 0),
  934.      ( 0, 0, 0, 2),
  935.      ( 2, 0,-2, 0),
  936.      ( 2,-1,-1, 0),
  937.      ( 2, 0, 1, 0),
  938.      ( 2,-1, 0, 0),
  939.      ( 0, 1,-1, 0),
  940.      ( 1, 0, 0, 0),
  941.      ( 0, 1, 1, 0),
  942.      ( 2, 0, 0,-2),
  943.      ( 0, 0, 1, 2),
  944.      ( 0, 0, 1,-2),
  945.      ( 4, 0,-1, 0),
  946.      ( 0, 0, 3, 0),
  947.      ( 4, 0,-2, 0),
  948.      ( 2, 1,-1, 0),
  949.      ( 2, 1, 0, 0),
  950.      ( 1, 0,-1, 0),
  951.      ( 1, 1, 0, 0),
  952.      ( 2,-1, 1, 0),
  953.      ( 2, 0, 2, 0),
  954.      ( 4, 0, 0, 0),
  955.      ( 2, 0,-3, 0),
  956.      ( 0, 1,-2, 0),
  957.      ( 2, 0,-1, 2),
  958.      ( 2,-1,-2, 0),
  959.      ( 1, 0, 1, 0),
  960.      ( 2,-2, 0, 0),
  961.      ( 0, 1, 2, 0),
  962.      ( 0, 2, 0, 0),
  963.      ( 2,-2,-1, 0),
  964.      ( 2, 0, 1,-2),
  965.      ( 2, 0, 0, 2),
  966.      ( 4,-1,-1, 0),
  967.      ( 0, 0, 2, 2),
  968.      ( 3, 0,-1, 0),
  969.      ( 2, 1, 1, 0),
  970.      ( 4,-1,-2, 0),
  971.      ( 0, 2,-1, 0),
  972.      ( 2, 2,-1, 0),
  973.      ( 2, 1,-2, 0),
  974.      ( 2,-1, 0,-2),
  975.      ( 4, 0, 1, 0),
  976.      ( 0, 0, 4, 0),
  977.      ( 4,-1, 0, 0),
  978.      ( 1, 0,-2, 0),
  979.      ( 2, 1, 0,-2),
  980.      ( 0, 0, 2,-2),
  981.      ( 1, 1, 1, 0),
  982.      ( 3, 0,-2, 0),
  983.      ( 4, 0,-3, 0),
  984.      ( 2,-1, 2, 0),
  985.      ( 0, 2, 1, 0),
  986.      ( 1, 1,-1, 0),
  987.      ( 2, 0, 3, 0),
  988.      ( 2, 0,-1,-2)
  989.                    );
  990.   (*@\\\*)
  991.   (*@/// arg_b:array[0..59,0..3] of shortint = (); *)
  992.   arg_b:array[0..59,0..3] of shortint = (
  993.      ( 0, 0, 0, 1),
  994.      ( 0, 0, 1, 1),
  995.      ( 0, 0, 1,-1),
  996.      ( 2, 0, 0,-1),
  997.      ( 2, 0,-1, 1),
  998.      ( 2, 0,-1,-1),
  999.      ( 2, 0, 0, 1),
  1000.      ( 0, 0, 2, 1),
  1001.      ( 2, 0, 1,-1),
  1002.      ( 0, 0, 2,-1),  (* !!! Error in German Meeus *)
  1003.      ( 2,-1, 0,-1),
  1004.      ( 2, 0,-2,-1),
  1005.      ( 2, 0, 1, 1),
  1006.      ( 2, 1, 0,-1),
  1007.      ( 2,-1,-1, 1),
  1008.      ( 2,-1, 0, 1),
  1009.      ( 2,-1,-1,-1),
  1010.      ( 0, 1,-1,-1),
  1011.      ( 4, 0,-1,-1),
  1012.      ( 0, 1, 0, 1),
  1013.      ( 0, 0, 0, 3),
  1014.      ( 0, 1,-1, 1),
  1015.      ( 1, 0, 0, 1),
  1016.      ( 0, 1, 1, 1),
  1017.      ( 0, 1, 1,-1),
  1018.      ( 0, 1, 0,-1),
  1019.      ( 1, 0, 0,-1),
  1020.      ( 0, 0, 3, 1),
  1021.      ( 4, 0, 0,-1),
  1022.      ( 4, 0,-1, 1),
  1023.      ( 0, 0, 1,-3),
  1024.      ( 4, 0,-2, 1),
  1025.      ( 2, 0, 0,-3),
  1026.      ( 2, 0, 2,-1),
  1027.      ( 2,-1, 1,-1),
  1028.      ( 2, 0,-2, 1),
  1029.      ( 0, 0, 3,-1),
  1030.      ( 2, 0, 2, 1),
  1031.      ( 2, 0,-3,-1),
  1032.      ( 2, 1,-1, 1),
  1033.      ( 2, 1, 0, 1),
  1034.      ( 4, 0, 0, 1),
  1035.      ( 2,-1, 1, 1),
  1036.      ( 2,-2, 0,-1),
  1037.      ( 0, 0, 1, 3),
  1038.      ( 2, 1, 1,-1),
  1039.      ( 1, 1, 0,-1),
  1040.      ( 1, 1, 0, 1),
  1041.      ( 0, 1,-2,-1),
  1042.      ( 2, 1,-1,-1),
  1043.      ( 1, 0, 1, 1),
  1044.      ( 2,-1,-2,-1),
  1045.      ( 0, 1, 2, 1),
  1046.      ( 4, 0,-2,-1),
  1047.      ( 4,-1,-1,-1),
  1048.      ( 1, 0, 1,-1),
  1049.      ( 4, 0, 1,-1),
  1050.      ( 1, 0,-1,-1),
  1051.      ( 4,-1, 0,-1),
  1052.      ( 2,-2, 0, 1)
  1053.     );
  1054.   (*@\\\*)
  1055.   (*@/// sigma_r: array[0..59] of longint = (..); *)
  1056.   sigma_r: array[0..59] of longint = (
  1057.    -20905355,
  1058.     -3699111,
  1059.     -2955968,
  1060.      -569925,
  1061.        48888,
  1062.        -3149,
  1063.       246158,
  1064.      -152138,
  1065.      -170733,
  1066.      -204586,
  1067.      -129620,
  1068.       108743,
  1069.       104755,
  1070.        10321,
  1071.            0,
  1072.        79661,
  1073.       -34782,
  1074.       -23210,
  1075.       -21636,
  1076.        24208,
  1077.        30824,
  1078.        -8379,
  1079.       -16675,
  1080.       -12831,
  1081.       -10445,
  1082.       -11650,
  1083.        14403,
  1084.        -7003,
  1085.            0,
  1086.        10056,
  1087.         6322,
  1088.        -9884,
  1089.         5751,
  1090.            0,
  1091.        -4950,
  1092.         4130,
  1093.            0,
  1094.        -3958,
  1095.            0,
  1096.         3258,
  1097.         2616,
  1098.        -1897,
  1099.        -2117,
  1100.         2354,
  1101.            0,
  1102.            0,
  1103.        -1423,
  1104.        -1117,
  1105.        -1571,
  1106.        -1739,
  1107.            0,
  1108.        -4421,
  1109.            0,
  1110.            0,
  1111.            0,
  1112.            0,
  1113.         1165,
  1114.            0,
  1115.            0,
  1116.         8752
  1117.               );
  1118.   (*@\\\*)
  1119.   (*@/// sigma_l: array[0..59] of longint = (..); *)
  1120.   sigma_l: array[0..59] of longint = (
  1121.     6288774,
  1122.     1274027,
  1123.      658314,
  1124.      213618,
  1125.     -185116,
  1126.     -114332,
  1127.       58793,
  1128.       57066,
  1129.       53322,
  1130.       45758,
  1131.      -40923,
  1132.      -34720,
  1133.      -30383,
  1134.       15327,
  1135.      -12528,
  1136.       10980,
  1137.       10675,
  1138.       10034,
  1139.        8548,
  1140.       -7888,
  1141.       -6766,
  1142.       -5163,
  1143.        4987,
  1144.        4036,
  1145.        3994,
  1146.        3861,
  1147.        3665,
  1148.       -2689,
  1149.       -2602,
  1150.        2390,
  1151.       -2348,
  1152.        2236,
  1153.       -2120,
  1154.       -2069,
  1155.        2048,
  1156.       -1773,
  1157.       -1595,
  1158.        1215,
  1159.       -1110,
  1160.        -892,
  1161.        -810,
  1162.         759,
  1163.        -713,
  1164.        -700,
  1165.         691,
  1166.         596,
  1167.         549,
  1168.         537,
  1169.         520,
  1170.        -487,
  1171.        -399,
  1172.        -381,
  1173.         351,
  1174.        -340,
  1175.         330,
  1176.         327,
  1177.        -323,
  1178.         299,
  1179.         294,
  1180.           0
  1181.     );
  1182.   (*@\\\*)
  1183.   (*@/// sigma_b: array[0..59] of longint = (..); *)
  1184.   sigma_b: array[0..59] of longint = (
  1185.     5128122,
  1186.      280602,
  1187.      277693,
  1188.      173237,
  1189.       55413,
  1190.       46271,
  1191.       32573,
  1192.       17198,
  1193.        9266,
  1194.        8822,
  1195.        8216,
  1196.        4324,
  1197.        4200,
  1198.       -3359,
  1199.        2463,
  1200.        2211,
  1201.        2065,
  1202.       -1870,
  1203.        1828,
  1204.       -1794,
  1205.       -1749,
  1206.       -1565,
  1207.       -1491,
  1208.       -1475,
  1209.       -1410,
  1210.       -1344,
  1211.       -1335,
  1212.        1107,
  1213.        1021,
  1214.         833,
  1215.         777,
  1216.         671,
  1217.         607,
  1218.         596,
  1219.         491,
  1220.        -451,
  1221.         439,
  1222.         422,
  1223.         421,
  1224.        -366,
  1225.        -351,
  1226.         331,
  1227.         315,
  1228.         302,
  1229.        -283,
  1230.        -229,
  1231.         223,
  1232.         223,
  1233.        -220,
  1234.        -220,
  1235.        -185,
  1236.         181,
  1237.        -177,
  1238.         176,
  1239.         166,
  1240.        -164,
  1241.         132,
  1242.        -119,
  1243.         115,
  1244.         107
  1245.     );
  1246.   (*@\\\*)
  1247. var
  1248.   t,d,m,ms,f,e,ls : extended;
  1249.   sr,sl,sb,temp: extended;
  1250.   a1,a2,a3: extended;
  1251.   lambda,beta,delta: extended;
  1252.   i: integer;
  1253. begin
  1254.   t:=(julian_date(date)-2451545)/36525;
  1255.  
  1256.   (* mean elongation of the moon *)
  1257.   d:=297.8502042+(445267.1115168+(-0.0016300+(1/545868-1/113065000*t)*t)*t)*t;
  1258.  
  1259.   (* mean anomaly of the sun *)
  1260.   m:=357.5291092+(35999.0502909+(-0.0001536+1/24490000*t)*t)*t;
  1261.  
  1262.   (* mean anomaly of the moon *)
  1263.   ms:=134.9634114+(477198.8676313+(0.0089970+(1/69699-1/1471200*t)*t)*t)*t;
  1264.  
  1265.   (* argument of the longitude of the moon *)
  1266.   f:=93.2720993+(483202.0175273+(-0.0034029+(-1/3526000+1/863310000*t)*t)*t)*t;
  1267.  
  1268.   (* correction term due to excentricity of the earth orbit *)
  1269.   e:=1.0+(-0.002516-0.0000074*t)*t;
  1270.  
  1271.   (* mean longitude of the moon *)
  1272.   ls:=218.3164591+(481267.88134236+(-0.0013268+(1/538841-1/65194000*t)*t)*t)*t;
  1273.  
  1274.   (* arguments of correction terms *)
  1275.   a1:=119.75+131.849*t;
  1276.   a2:=53.09+479264.290*t;
  1277.   a3:=313.45+481266.484*t;
  1278.  
  1279.   (*@/// sr := Σ r_i cos(d,m,ms,f);   !!!  gives different value than in Meeus *)
  1280.   sr:=0;
  1281.   for i:=0 to 59 do begin
  1282.     temp:=sigma_r[i]*cos_d( arg_lr[i,0]*d
  1283.                            +arg_lr[i,1]*m
  1284.                            +arg_lr[i,2]*ms
  1285.                            +arg_lr[i,3]*f);
  1286.     if abs(arg_lr[i,1])=1 then temp:=temp*e;
  1287.     if abs(arg_lr[i,1])=2 then temp:=temp*e*e;
  1288.     sr:=sr+temp;
  1289.     end;
  1290.   (*@\\\*)
  1291.   (*@/// sl := Σ l_i sin(d,m,ms,f); *)
  1292.   sl:=0;
  1293.   for i:=0 to 59 do begin
  1294.     temp:=sigma_l[i]*sin_d( arg_lr[i,0]*d
  1295.                            +arg_lr[i,1]*m
  1296.                            +arg_lr[i,2]*ms
  1297.                            +arg_lr[i,3]*f);
  1298.     if abs(arg_lr[i,1])=1 then temp:=temp*e;
  1299.     if abs(arg_lr[i,1])=2 then temp:=temp*e*e;
  1300.     sl:=sl+temp;
  1301.     end;
  1302.  
  1303.   (* correction terms *)
  1304.   sl:=sl +3958*sin_d(a1)
  1305.          +1962*sin_d(ls-f)
  1306.           +318*sin_d(a2);
  1307.   (*@\\\*)
  1308.   (*@/// sb := Σ b_i sin(d,m,ms,f); *)
  1309.   sb:=0;
  1310.   for i:=0 to 59 do begin
  1311.     temp:=sigma_b[i]*sin_d( arg_b[i,0]*d
  1312.                            +arg_b[i,1]*m
  1313.                            +arg_b[i,2]*ms
  1314.                            +arg_b[i,3]*f);
  1315.     if abs(arg_b[i,1])=1 then temp:=temp*e;
  1316.     if abs(arg_b[i,1])=2 then temp:=temp*e*e;
  1317.     sb:=sb+temp;
  1318.     end;
  1319.  
  1320.   (* correction terms *)
  1321.   sb:=sb -2235*sin_d(ls)
  1322.           +382*sin_d(a3)
  1323.           +175*sin_d(a1-f)
  1324.           +175*sin_d(a1+f)
  1325.           +127*sin_d(ls-ms)
  1326.           -115*sin_d(ls+ms);
  1327.   (*@\\\*)
  1328.  
  1329.   lambda:=ls+sl/1000000;
  1330.   beta:=sb/1000000;
  1331.   delta:=385000.56+sr/1000;
  1332.  
  1333.   result.radius:=delta;
  1334.   result.longitude:=lambda;
  1335.   result.latitude:=beta;
  1336.  
  1337.   calc_geocentric(result,date);
  1338.   end;
  1339. (*@\\\000000011D*)
  1340.  
  1341. (*@/// procedure correct_position(var position:t_coord; date:TDateTime; ...); *)
  1342. { Based upon chapter 40 (39) of Meeus }
  1343.  
  1344. procedure correct_position(var position:t_coord; date:TDateTime;
  1345.                            latitude,longitude,height:extended);
  1346. var
  1347.   u,h,delta_alpha: extended;
  1348.   rho_sin, rho_cos: extended;
  1349. const
  1350.   b_a=0.99664719;
  1351. begin
  1352.   u:=arctan_d(b_a*b_a*tan_d(latitude));
  1353.   rho_sin:=b_a*sin_d(u)+height/6378140*sin_d(latitude);
  1354.   rho_cos:=cos_d(u)+height/6378140*cos_d(latitude);
  1355.  
  1356.   position.parallax:=arcsin_d(sin_d(8.794/3600)/(moon_distance(date)/AU));
  1357.   h:=star_time(date)-longitude-position.rektaszension;
  1358.   delta_alpha:=arctan_d(
  1359.                 (-rho_cos*sin_d(position.parallax)*sin_d(h))/
  1360.                 (cos_d(position.declination)-
  1361.                   rho_cos*sin_d(position.parallax)*cos_d(h)));
  1362.   position.rektaszension:=position.rektaszension+delta_alpha;
  1363.   position.declination:=arctan_d(
  1364.       (( sin_d(position.declination)
  1365.         -rho_sin*sin_d(position.parallax))*cos_d(delta_alpha))/
  1366.       ( cos_d(position.declination)
  1367.        -rho_cos*sin_d(position.parallax)*cos_d(h)));
  1368.   end;
  1369. (*@\\\000000011D*)
  1370.  
  1371. { Moon phases and age of the moon }
  1372. (*@/// procedure calc_phase_data(date:TDateTime; phase:TMoonPhase; var jde,kk,m,ms,f,o,e: extended); *)
  1373. { Based upon Chapter 49 (47) of Meeus }
  1374. { Both used for moon phases and moon and sun eclipses }
  1375.  
  1376. procedure calc_phase_data(date:TDateTime; phase:TMoonPhase; var jde,kk,m,ms,f,o,e: extended);
  1377. const
  1378.   phases = ord(high(TMoonPhase))+1;
  1379. var
  1380.   t: extended;
  1381.   k: longint;
  1382.   ts: extended;
  1383. begin
  1384.   k:=round((date-datetime_2000_01_01)/36525.0*1236.85);
  1385.   ts:=(date-datetime_2000_01_01)/36525.0;
  1386.   kk:=int(k)+ord(phase)/phases;
  1387.   t:=kk/1236.85;
  1388.   jde:=2451550.09765+29.530588853*kk
  1389.        +t*t*(0.0001337-t*(0.000000150-0.00000000073*t));
  1390.   m:=2.5534+29.10535669*kk-t*t*(0.0000218+0.00000011*t);
  1391.   ms:=201.5643+385.81693528*kk+t*t*(0.1017438+t*(0.00001239-t*0.000000058));
  1392.   f:= 160.7108+390.67050274*kk-t*t*(0.0016341+t*(0.00000227-t*0.000000011));
  1393.   o:=124.7746-1.56375580*kk+t*t*(0.0020691+t*0.00000215);
  1394.   e:=1-ts*(0.002516+ts*0.0000074);
  1395.   end;
  1396. (*@\\\0000000126*)
  1397. (*@/// function nextphase_approx(date: TDateTime; phase:TMoonphase):TDateTime; *)
  1398. function nextphase_approx(date: TDateTime; phase:TMoonphase):TDateTime;
  1399. const
  1400.   epsilon = 1E-7;
  1401.   phases = ord(high(TMoonPhase))+1;
  1402. var
  1403.   target_age: extended;
  1404.   h: extended;
  1405. begin
  1406.   target_age:=ord(phase)*mean_lunation/phases;
  1407.   result:=date;
  1408.   repeat
  1409.     h:=age_of_moon(result)-target_age;
  1410.     if h>mean_lunation/2 then
  1411.       h:=h-mean_lunation;
  1412.     result:=result-h;
  1413.     until abs(h)<epsilon;
  1414.   end;
  1415. (*@\\\*)
  1416. (*@/// function nextphase_49(date:TDateTime; phase:TMoonPhase):TDateTime; *)
  1417. { Based upon Chapter 49 (47) of Meeus }
  1418.  
  1419. function nextphase_49(date:TDateTime; phase:TMoonPhase):TDateTime;
  1420. var
  1421.   t: extended;
  1422.   kk: extended;
  1423.   jde: extended;
  1424.   m,ms,f,o,e: extended;
  1425.   korr,w,akorr: extended;
  1426.   a:array[1..14] of extended;
  1427. begin
  1428.   if not (phase in [Newmoon,FirstQuarter,Fullmoon,LastQuarter]) then
  1429.     raise E_OutOfAlgorithmRange.Create('Invalid TMoonPhase');
  1430.  
  1431.   calc_phase_data(date,phase,jde,kk,m,ms,f,o,e);
  1432. {   k:=round((date-datetime_2000_01_01)/36525.0*1236.85); }
  1433. {   ts:=(date-datetime_2000_01_01)/36525.0; }
  1434. {   kk:=int(k)+ord(phase)/4.0; }
  1435.   t:=kk/1236.85;
  1436. {   m:=2.5534+29.10535669*kk-t*t*(0.0000218+0.00000011*t); }
  1437. {   ms:=201.5643+385.81693528*kk+t*t*(0.1017438+t*(0.00001239-t*0.000000058)); }
  1438. {   f:= 160.7108+390.67050274*kk-t*t*(0.0016341+t*(0.00000227-t*0.000000011)); }
  1439. {   o:=124.7746-1.56375580*kk+t*t*(0.0020691+t*0.00000215); }
  1440. {   e:=1-ts*(0.002516+ts*0.0000074); }
  1441.   case phase of
  1442.     (*@/// Newmoon: *)
  1443.     Newmoon:  begin
  1444.       korr:= -0.40720*sin_d(ms)
  1445.              +0.17241*e*sin_d(m)
  1446.              +0.01608*sin_d(2*ms)
  1447.              +0.01039*sin_d(2*f)
  1448.              +0.00739*e*sin_d(ms-m)
  1449.              -0.00514*e*sin_d(ms+m)
  1450.              +0.00208*e*e*sin_d(2*m)
  1451.              -0.00111*sin_d(ms-2*f)
  1452.              -0.00057*sin_d(ms+2*f)
  1453.              +0.00056*e*sin_d(2*ms+m)
  1454.              -0.00042*sin_d(3*ms)
  1455.              +0.00042*e*sin_d(m+2*f)
  1456.              +0.00038*e*sin_d(m-2*f)
  1457.              -0.00024*e*sin_d(2*ms-m)
  1458.              -0.00017*sin_d(o)
  1459.              -0.00007*sin_d(ms+2*m)
  1460.              +0.00004*sin_d(2*ms-2*f)
  1461.              +0.00004*sin_d(3*m)
  1462.              +0.00003*sin_d(ms+m-2*f)
  1463.              +0.00003*sin_d(2*ms+2*f)
  1464.              -0.00003*sin_d(ms+m+2*f)
  1465.              +0.00003*sin_d(ms-m+2*f)
  1466.              -0.00002*sin_d(ms-m-2*f)
  1467.              -0.00002*sin_d(3*ms+m)
  1468.              +0.00002*sin_d(4*ms);
  1469.       end;
  1470.     (*@\\\*)
  1471.     (*@/// FirstQuarter,LastQuarter: *)
  1472.     FirstQuarter,LastQuarter:  begin
  1473.       korr:= -0.62801*sin_d(ms)
  1474.              +0.17172*e*sin_d(m)
  1475.              -0.01183*e*sin_d(ms+m)
  1476.              +0.00862*sin_d(2*ms)
  1477.              +0.00804*sin_d(2*f)
  1478.              +0.00454*e*sin_d(ms-m)
  1479.              +0.00204*e*e*sin_d(2*m)
  1480.              -0.00180*sin_d(ms-2*f)
  1481.              -0.00070*sin_d(ms+2*f)
  1482.              -0.00040*sin_d(3*ms)
  1483.              -0.00034*e*sin_d(2*ms-m)
  1484.              +0.00032*e*sin_d(m+2*f)
  1485.              +0.00032*e*sin_d(m-2*f)
  1486.              -0.00028*e*e*sin_d(ms+2*m)
  1487.              +0.00027*e*sin_d(2*ms+m)
  1488.              -0.00017*sin_d(o)
  1489.              -0.00005*sin_d(ms-m-2*f)
  1490.              +0.00004*sin_d(2*ms+2*f)
  1491.              -0.00004*sin_d(ms+m+2*f)
  1492.              +0.00004*sin_d(ms-2*m)
  1493.              +0.00003*sin_d(ms+m-2*f)
  1494.              +0.00003*sin_d(3*m)
  1495.              +0.00002*sin_d(2*ms-2*f)
  1496.              +0.00002*sin_d(ms-m+2*f)
  1497.              -0.00002*sin_d(3*ms+m);
  1498.       w:=0.00306-0.00038*e*cos_d(m)
  1499.                 +0.00026*cos_d(ms)
  1500.                 -0.00002*cos_d(ms-m)
  1501.                 +0.00002*cos_d(ms+m)
  1502.                 +0.00002*cos_d(2*f);
  1503.       if phase = FirstQuarter then begin
  1504.         korr:=korr+w;
  1505.         end
  1506.       else begin
  1507.         korr:=korr-w;
  1508.         end;
  1509.       end;
  1510.     (*@\\\*)
  1511.     (*@/// Fullmoon: *)
  1512.     Fullmoon:  begin
  1513.       korr:= -0.40614*sin_d(ms)
  1514.              +0.17302*e*sin_d(m)
  1515.              +0.01614*sin_d(2*ms)
  1516.              +0.01043*sin_d(2*f)
  1517.              +0.00734*e*sin_d(ms-m)
  1518.              -0.00515*e*sin_d(ms+m)
  1519.              +0.00209*e*e*sin_d(2*m)
  1520.              -0.00111*sin_d(ms-2*f)
  1521.              -0.00057*sin_d(ms+2*f)
  1522.              +0.00056*e*sin_d(2*ms+m)
  1523.              -0.00042*sin_d(3*ms)
  1524.              +0.00042*e*sin_d(m+2*f)
  1525.              +0.00038*e*sin_d(m-2*f)
  1526.              -0.00024*e*sin_d(2*ms-m)
  1527.              -0.00017*sin_d(o)
  1528.              -0.00007*sin_d(ms+2*m)
  1529.              +0.00004*sin_d(2*ms-2*f)
  1530.              +0.00004*sin_d(3*m)
  1531.              +0.00003*sin_d(ms+m-2*f)
  1532.              +0.00003*sin_d(2*ms+2*f)
  1533.              -0.00003*sin_d(ms+m+2*f)
  1534.              +0.00003*sin_d(ms-m+2*f)
  1535.              -0.00002*sin_d(ms-m-2*f)
  1536.              -0.00002*sin_d(3*ms+m)
  1537.              +0.00002*sin_d(4*ms);
  1538.       end;
  1539.     (*@\\\*)
  1540.     (*@/// else *)
  1541.     else
  1542.       korr:=0;   (* Delphi 2 shut up! *)
  1543.     (*@\\\*)
  1544.     end;
  1545.   (*@/// Additional Corrections due to planets *)
  1546.   a[1]:=299.77+0.107408*kk-0.009173*t*t;
  1547.   a[2]:=251.88+0.016321*kk;
  1548.   a[3]:=251.83+26.651886*kk;
  1549.   a[4]:=349.42+36.412478*kk;
  1550.   a[5]:= 84.66+18.206239*kk;
  1551.   a[6]:=141.74+53.303771*kk;
  1552.   a[7]:=207.14+2.453732*kk;
  1553.   a[8]:=154.84+7.306860*kk;
  1554.   a[9]:= 34.52+27.261239*kk;
  1555.   a[10]:=207.19+0.121824*kk;
  1556.   a[11]:=291.34+1.844379*kk;
  1557.   a[12]:=161.72+24.198154*kk;
  1558.   a[13]:=239.56+25.513099*kk;
  1559.   a[14]:=331.55+3.592518*kk;
  1560.   akorr:=   +0.000325*sin_d(a[1])
  1561.             +0.000165*sin_d(a[2])
  1562.             +0.000164*sin_d(a[3])
  1563.             +0.000126*sin_d(a[4])
  1564.             +0.000110*sin_d(a[5])
  1565.             +0.000062*sin_d(a[6])
  1566.             +0.000060*sin_d(a[7])
  1567.             +0.000056*sin_d(a[8])
  1568.             +0.000047*sin_d(a[9])
  1569.             +0.000042*sin_d(a[10])
  1570.             +0.000040*sin_d(a[11])
  1571.             +0.000037*sin_d(a[12])
  1572.             +0.000035*sin_d(a[13])
  1573.             +0.000023*sin_d(a[14]);
  1574.   korr:=korr+akorr;
  1575.   (*@\\\*)
  1576.   result:=delphi_date(jde+korr);
  1577.   end;
  1578. (*@\\\*)
  1579. (*@/// function nextphase(date:TDateTime; phase:TMoonPhase):TDateTime; *)
  1580. (* nextphase_approx has similar accuracy as nextphase_49 *)
  1581.  
  1582. function nextphase(date:TDateTime; phase:TMoonPhase):TDateTime;
  1583. begin
  1584.   case phase of
  1585.     Newmoon,FirstQuarter,Fullmoon,LastQuarter:
  1586.       result:=nextphase_49(date,phase);
  1587.     WaxingCrescrent,WaxingGibbous,WaningGibbous,WaningCrescent:
  1588.       result:=nextphase_approx(date,phase);
  1589.     else
  1590.       result:=0;   (* Delphi shut up *)
  1591.       end;
  1592.   end;
  1593. (*@\\\*)
  1594. (*@/// function last_phase(date:TDateTime; phase:TMoonPhase):TDateTime; *)
  1595. function last_phase(date:TDateTime; phase:TMoonPhase):TDateTime;
  1596. var
  1597.   temp_date: TDateTime;
  1598. begin
  1599.   temp_date:=date+28;
  1600.   result:=temp_date;
  1601.   while result>date do begin
  1602.     result:=nextphase(temp_date,phase);
  1603.     if result=0 then
  1604.       raise E_OutOfAlgorithmRange.Create('No TDateTime possible');
  1605.     temp_date:=temp_date-28;
  1606.     end;
  1607.   end;
  1608. (*@\\\*)
  1609. (*@/// function next_phase(date:TDateTime; phase:TMoonPhase):TDateTime; *)
  1610. function next_phase(date:TDateTime; phase:TMoonPhase):TDateTime;
  1611. var
  1612.   temp_date: TDateTime;
  1613. begin
  1614.   temp_date:=date-28;
  1615.   result:=temp_date;
  1616.   while result<date do begin
  1617.     result:=nextphase(temp_date,phase);
  1618.     if result=0 then
  1619.       raise E_OutOfAlgorithmRange.Create('No TDateTime possible');
  1620.     temp_date:=temp_date+28;
  1621.     end;
  1622.   end;
  1623. (*@\\\*)
  1624. (*@/// function nearest_phase(date: TDateTime):TMoonPhase; *)
  1625. function nearest_phase(date: TDateTime):TMoonPhase;
  1626. const
  1627.   phases = ord(high(TMoonPhase))+1;
  1628. begin
  1629.   result:=TMoonPhase(round(age_of_moon(date)/mean_lunation*phases) mod phases);
  1630.   end;
  1631. (*@\\\*)
  1632. (*@/// function next_blue_moon_bias(date: TDateTime; timezonebias:extended):TDateTime; *)
  1633. function next_blue_moon_bias(date: TDateTime; timezonebias:extended):TDateTime;
  1634. var
  1635.   h: TDateTime;
  1636.   y,m,d: word;
  1637.   y1,m1,d1: word;
  1638. begin
  1639.   h:=date-1+timezonebias;
  1640.   repeat
  1641.     h:=h+1;
  1642.     h:=next_phase(h,Fullmoon);
  1643.     DecodeDateCorrect(h-timezonebias,y,m,d);
  1644.     if d>27 then     (* only chance for a blue moon anyway *)
  1645.       DecodeDateCorrect(last_phase(h-5,FullMoon)-timezonebias,y1,m1,d1)
  1646.     else
  1647.       m1:=0;
  1648.     until m=m1;
  1649.   result:=h;
  1650.   end;
  1651. (*@\\\*)
  1652. (*@/// function next_blue_moon(date: TDateTime):TDateTime; *)
  1653. function next_blue_moon(date: TDateTime):TDateTime;
  1654. begin
  1655.   result:=next_blue_moon_bias(date,0);
  1656.   end;
  1657. (*@\\\*)
  1658. (*@/// function is_blue_moon(lunation: integer):boolean; *)
  1659. function is_blue_moon(lunation: integer):boolean;
  1660. var
  1661.   date: TDateTime;
  1662. begin
  1663.   date:=next_phase(datetime_first_lunation+(lunation-1)*mean_lunation-5,NewMoon);
  1664.   result:=((next_blue_moon(date)-date)<mean_lunation);
  1665.   end;
  1666. (*@\\\*)
  1667.  
  1668. (*@/// function moon_phase_angle(date: TDateTime):extended; *)
  1669. { Based upon Chapter 48 (46) of Meeus }
  1670.  
  1671. function moon_phase_angle(date: TDateTime):extended;
  1672. var
  1673.   sun_coord,moon_coord: t_coord;
  1674.   phi,i: extended;
  1675. begin
  1676.   sun_coord:=sun_coordinate(date);
  1677.   moon_coord:=moon_coordinate(date);
  1678.   phi:=arccos(cos_d(moon_coord.latitude)
  1679.              *cos_d(moon_coord.longitude-sun_coord.longitude));
  1680.   i:=arctan(sun_coord.radius*sin(phi)/
  1681.             (moon_coord.radius-sun_coord.radius*cos(phi)));
  1682.   if i<0 then  result:=i/pi*180+180
  1683.          else  result:=i/pi*180;
  1684.  
  1685.   if put_in_360(moon_coord.longitude-sun_coord.longitude)>180 then
  1686.     result:=-result;
  1687.  
  1688.   end;
  1689. (*@\\\000000011D*)
  1690. (*@/// function age_of_moon(date: TDateTime):extended; *)
  1691. function age_of_moon(date: TDateTime):extended;
  1692. var
  1693.   sun_coord,moon_coord: t_coord;
  1694. begin
  1695.   sun_coord:=sun_coordinate(date);
  1696.   moon_coord:=moon_coordinate(date);
  1697.   result:=put_in_360(moon_coord.longitude-sun_coord.longitude)/360*mean_lunation;
  1698.   end;
  1699. (*@\\\*)
  1700. (*@/// function current_phase(date:TDateTime):extended; *)
  1701. function current_phase(date:TDateTime):extended;
  1702. begin
  1703.   result:=(1+cos_d(moon_phase_angle(date)))/2;
  1704.   end;
  1705. (*@\\\*)
  1706.  
  1707. (*@/// function lunation(date:TDateTime):integer; *)
  1708. function lunation(date:TDateTime):integer;
  1709. begin
  1710.   result:=round((last_phase(date,NewMoon)-datetime_first_lunation)/mean_lunation)+1;
  1711.   end;
  1712. (*@\\\*)
  1713.  
  1714. { The distances }
  1715. (*@/// function sun_distance(date: TDateTime): extended;    // AU *)
  1716. function sun_distance(date: TDateTime): extended;
  1717. begin
  1718.   result:=sun_coordinate(date).radius/au;
  1719.   end;
  1720. (*@\\\*)
  1721. (*@/// function moon_distance(date: TDateTime): extended;   // km *)
  1722. function moon_distance(date: TDateTime): extended;
  1723. begin
  1724.   result:=moon_coordinate(date).radius;
  1725.   end;
  1726. (*@\\\*)
  1727.  
  1728. { The angular diameter (which is 0.5 of the subtent in moontool) }
  1729. (*@/// function sun_diameter(date:TDateTime):extended;     // angular seconds *)
  1730. function sun_diameter(date:TDateTime):extended;
  1731. begin
  1732.   result:=959.63/(sun_coordinate(date).radius/au)*2;
  1733.   end;
  1734. (*@\\\*)
  1735. (*@/// function moon_diameter(date:TDateTime):extended;    // angular seconds *)
  1736. function moon_diameter(date:TDateTime):extended;
  1737. begin
  1738.   result:=358473400/moon_coordinate(date).radius*2;
  1739.   end;
  1740. (*@\\\*)
  1741.  
  1742. { Perigee and Apogee }
  1743. (*@/// function nextXXXgee(date:TDateTime; apo: boolean):TDateTime; *)
  1744. { Based upon Chapter 50 (48) of Meeus }
  1745.  
  1746. function nextXXXgee(date:TDateTime; apo: boolean):TDateTime;
  1747. const
  1748.   (*@/// arg_apo:array[0..31,0..2] of shortint = (..); *)
  1749.   arg_apo:array[0..31,0..2] of shortint = (
  1750.      { D  F  M }
  1751.      ( 2, 0, 0),
  1752.      ( 4, 0, 0),
  1753.      ( 0, 0, 1),
  1754.      ( 2, 0,-1),
  1755.      ( 0, 2, 0),
  1756.      ( 1, 0, 0),
  1757.      ( 6, 0, 0),
  1758.      ( 4, 0,-1),
  1759.      ( 2, 2, 0),
  1760.      ( 1, 0, 1),
  1761.      ( 8, 0, 0),
  1762.      ( 6, 0,-1),
  1763.      ( 2,-2, 0),
  1764.      ( 2, 0,-2),
  1765.      ( 3, 0, 0),
  1766.      ( 4, 2, 0),
  1767.      ( 8, 0,-1),
  1768.      ( 4, 0,-2),
  1769.      (10, 0, 0),
  1770.      ( 3, 0, 1),
  1771.      ( 0, 0, 2),
  1772.      ( 2, 0, 1),
  1773.      ( 2, 0, 2),
  1774.      ( 6, 2, 0),
  1775.      ( 6, 0,-2),
  1776.      (10, 0,-1),
  1777.      ( 5, 0, 0),
  1778.      ( 4,-2, 0),
  1779.      ( 0, 2, 1),
  1780.      (12, 0, 0),
  1781.      ( 2, 2,-1),
  1782.      ( 1, 0,-1)
  1783.                );
  1784.   (*@\\\*)
  1785.   (*@/// arg_per:array[0..59,0..2] of shortint = (..); *)
  1786.   arg_per:array[0..59,0..2] of shortint = (
  1787.      { D  F  M }
  1788.      ( 2, 0, 0),
  1789.      ( 4, 0, 0),
  1790.      ( 6, 0, 0),
  1791.      ( 8, 0, 0),
  1792.      ( 2, 0,-1),
  1793.      ( 0, 0, 1),
  1794.      (10, 0, 0),
  1795.      ( 4, 0,-1),
  1796.      ( 6, 0,-1),
  1797.      (12, 0, 0),
  1798.      ( 1, 0, 0),
  1799.      ( 8, 0,-1),
  1800.      (14, 0, 0),
  1801.      ( 0, 2, 0),
  1802.      ( 3, 0, 0),
  1803.      (10, 0,-1),
  1804.      (16, 0, 0),
  1805.      (12, 0,-1),
  1806.      ( 5, 0, 0),
  1807.      ( 2, 2, 0),
  1808.      (18, 0, 0),
  1809.      (14, 0,-1),
  1810.      ( 7, 0, 0),
  1811.      ( 2, 1, 0),
  1812.      (20, 0, 0),
  1813.      ( 1, 0, 1),
  1814.      (16, 0,-1),
  1815.      ( 4, 0, 1),
  1816.      ( 2, 0,-2),
  1817.      ( 4, 0,-2),
  1818.      ( 6, 0,-2),
  1819.      (22, 0, 0),
  1820.      (18, 0,-1),
  1821.      ( 6, 0, 1),
  1822.      (11, 0, 0),
  1823.      ( 8, 0, 1),
  1824.      ( 4,-2, 0),
  1825.      ( 6, 2, 0),
  1826.      ( 3, 0, 1),
  1827.      ( 5, 0, 1),
  1828.      (13, 0, 0),
  1829.      (20, 0,-1),
  1830.      ( 3, 0, 2),
  1831.      ( 4, 2,-2),
  1832.      ( 1, 0, 2),
  1833.      (22, 0,-1),
  1834.      ( 0, 4, 0),
  1835.      ( 6,-2, 0),
  1836.      ( 2,-2, 1),
  1837.      ( 0, 0, 2),
  1838.      ( 0, 2,-1),
  1839.      ( 2, 4, 0),
  1840.      ( 0, 2,-2),
  1841.      ( 2,-2, 2),
  1842.      (24, 0, 0),
  1843.      ( 4,-4, 0),
  1844.      ( 9, 0, 0),
  1845.      ( 4, 2, 0),
  1846.      ( 2, 0, 2),
  1847.      ( 1, 0,-1)
  1848.                );
  1849.   (*@\\\*)
  1850.   (*@/// koe_apo:array[0..31,0..1] of longint = (..); *)
  1851.   koe_apo:array[0..31,0..1] of longint = (
  1852.      {    1   T }
  1853.      ( 4392,  0),
  1854.      (  684,  0),
  1855.      (  456,-11),
  1856.      (  426,-11),
  1857.      (  212,  0),
  1858.      ( -189,  0),
  1859.      (  144,  0),
  1860.      (  113,  0),
  1861.      (   47,  0),
  1862.      (   36,  0),
  1863.      (   35,  0),
  1864.      (   34,  0),
  1865.      (  -34,  0),
  1866.      (   22,  0),
  1867.      (  -17,  0),
  1868.      (   13,  0),
  1869.      (   11,  0),
  1870.      (   10,  0),
  1871.      (    9,  0),
  1872.      (    7,  0),
  1873.      (    6,  0),
  1874.      (    5,  0),
  1875.      (    5,  0),
  1876.      (    4,  0),
  1877.      (    4,  0),
  1878.      (    4,  0),
  1879.      (   -4,  0),
  1880.      (   -4,  0),
  1881.      (    3,  0),
  1882.      (    3,  0),
  1883.      (    3,  0),
  1884.      (   -3,  0)
  1885.                  );
  1886.   (*@\\\*)
  1887.   (*@/// koe_per:array[0..59,0..1] of longint = (..); *)
  1888.   koe_per:array[0..59,0..1] of longint = (
  1889.      {     1   T }
  1890.      (-16769,  0),
  1891.      (  4589,  0),
  1892.      ( -1856,  0),
  1893.      (   883,  0),
  1894.      (  -773, 19),
  1895.      (   502,-13),
  1896.      (  -460,  0),
  1897.      (   422,-11),
  1898.      (  -256,  0),
  1899.      (   253,  0),
  1900.      (   237,  0),
  1901.      (   162,  0),
  1902.      (  -145,  0),
  1903.      (   129,  0),
  1904.      (  -112,  0),
  1905.      (  -104,  0),
  1906.      (    86,  0),
  1907.      (    69,  0),
  1908.      (    66,  0),
  1909.      (   -53,  0),
  1910.      (   -52,  0),
  1911.      (   -46,  0),
  1912.      (   -41,  0),
  1913.      (    40,  0),
  1914.      (    32,  0),
  1915.      (   -32,  0),
  1916.      (    31,  0),
  1917.      (   -29,  0),
  1918.      (   -27,  0),
  1919.      (    24,  0),
  1920.      (   -21,  0),
  1921.      (   -21,  0),
  1922.      (   -21,  0),
  1923.      (    19,  0),
  1924.      (   -18,  0),
  1925.      (   -14,  0),
  1926.      (   -14,  0),
  1927.      (   -14,  0),
  1928.      (    14,  0),
  1929.      (   -14,  0),
  1930.      (    13,  0),
  1931.      (    13,  0),
  1932.      (    11,  0),
  1933.      (   -11,  0),
  1934.      (   -10,  0),
  1935.      (    -9,  0),
  1936.      (    -8,  0),
  1937.      (     8,  0),
  1938.      (     8,  0),
  1939.      (     7,  0),
  1940.      (     7,  0),
  1941.      (     7,  0),
  1942.      (    -6,  0),
  1943.      (    -6,  0),
  1944.      (     6,  0),
  1945.      (     5,  0),
  1946.      (    27,  0),
  1947.      (    27,  0),
  1948.      (     5,  0),
  1949.      (    -4,  0)
  1950.                  );
  1951.   (*@\\\*)
  1952. var
  1953.   k, jde, t: extended;
  1954.   d,m,f,v: extended;
  1955.   i: integer;
  1956. begin
  1957.   k:=round(((date-datetime_1999_01_01)/365.25-0.97)*13.2555);
  1958.   if apo then k:=k+0.5;
  1959.   t:=k/1325.55;
  1960.   jde:=2451534.6698+27.55454988*k+(-0.0006886+
  1961.        (-0.000001098+0.0000000052*t)*t)*t*t;
  1962.   d:=171.9179+335.9106046*k+(-0.0100250+(-0.00001156+0.000000055*t)*t)*t*t;
  1963.   m:=347.3477+27.1577721*k+(-0.0008323-0.0000010*t)*t*t;
  1964.   f:=316.6109+364.5287911*k+(-0.0125131-0.0000148*t)*t*t;
  1965.   v:=0;
  1966.   if apo then
  1967.     for i:=0 to 31 do
  1968.       v:=v+sin_d(arg_apo[i,0]*d+arg_apo[i,1]*f+arg_apo[i,2]*m)*
  1969.          (koe_apo[i,0]*0.0001+koe_apo[i,1]*0.00001*t)
  1970.   else
  1971.     for i:=0 to 59 do
  1972.       v:=v+sin_d(arg_per[i,0]*d+arg_per[i,1]*f+arg_per[i,2]*m)*
  1973.          (koe_per[i,0]*0.0001+koe_per[i,1]*0.00001*t);
  1974.   result:=delphi_date(jde+v);
  1975.   end;
  1976. (*@\\\000000011D*)
  1977. (*@/// function nextperigee(date:TDateTime):TDateTime; *)
  1978. function nextperigee(date:TDateTime):TDateTime;
  1979. var
  1980.   temp_date: TDateTime;
  1981. begin
  1982.   temp_date:=date-28;
  1983.   result:=temp_date;
  1984.   while result<date do begin
  1985.     result:=nextXXXgee(temp_date,false);
  1986.     temp_date:=temp_date+28;
  1987.     end;
  1988.   end;
  1989. (*@\\\*)
  1990. (*@/// function nextapogee(date:TDateTime):TDateTime; *)
  1991. function nextapogee(date:TDateTime):TDateTime;
  1992. var
  1993.   temp_date: TDateTime;
  1994. begin
  1995.   temp_date:=date-28;
  1996.   result:=temp_date;
  1997.   while result<date do begin
  1998.     result:=nextXXXgee(temp_date,true);
  1999.     temp_date:=temp_date+28;
  2000.     end;
  2001.   end;
  2002. (*@\\\*)
  2003. (*@/// function nextxxxhel(date: TDateTime; apo: boolean):TDateTime; *)
  2004. function nextxxxhel(date: TDateTime; apo: boolean):TDateTime;
  2005. var
  2006.   k, jde: extended;
  2007. begin
  2008.   k:=round(((date-datetime_2000_01_01)/365.25-0.01)*0.99997);
  2009.   if apo then k:=k+0.5;
  2010.   jde:=2451547.507+(365.2596358+0.0000000158*k)*k;
  2011.   result:=delphi_date(jde);
  2012.   end;
  2013. (*@\\\*)
  2014. (*@/// function nextperihel(date:TDateTime):TDateTime; *)
  2015. function nextperihel(date:TDateTime):TDateTime;
  2016. var
  2017.   temp_date: TDateTime;
  2018. begin
  2019.   temp_date:=date-365.25;
  2020.   result:=temp_date;
  2021.   while result<date do begin
  2022.     result:=nextXXXhel(temp_date,false);
  2023.     temp_date:=temp_date+365.25;
  2024.     end;
  2025.   end;
  2026. (*@\\\*)
  2027. (*@/// function nextaphel(date:TDateTime):TDateTime; *)
  2028. function nextaphel(date:TDateTime):TDateTime;
  2029. var
  2030.   temp_date: TDateTime;
  2031. begin
  2032.   temp_date:=date-365.25;
  2033.   result:=temp_date;
  2034.   while result<date do begin
  2035.     result:=nextXXXhel(temp_date,true);
  2036.     temp_date:=temp_date+365.25;
  2037.     end;
  2038.   end;
  2039. (*@\\\*)
  2040.  
  2041. { The seasons }
  2042. (*@/// function CalcSolarTerm(year: integer; term: TSolarTerm):TDateTime; *)
  2043. function CalcSolarTerm(year: integer; term: TSolarTerm):TDateTime;
  2044. (*@/// function dist(degree1,degree2:extended):extended; *)
  2045. function dist(degree1,degree2:extended):extended;
  2046. begin
  2047.   result:=put_in_360(degree2-degree1);
  2048.   if result>180 then
  2049.     result:=result-360;
  2050.   end;
  2051. (*@\\\*)
  2052. const
  2053.   epsilon = 3E-10;
  2054. var
  2055.   degree: extended;
  2056.   coord: T_coord;
  2057. begin
  2058.   degree:=15*ord(term);
  2059.   result:=tropic_year/24*ord(term)+31+28+21;  (* approximate date of term *)
  2060.   if result>365 then
  2061.     result:=result+encodedate(year-1,1,1)
  2062.   else
  2063.     result:=result+encodedate(year,1,1);
  2064.   coord:=sun_coordinate(result);
  2065.   while abs(dist(coord.longitude,degree))>epsilon do begin
  2066.     result:=result+58*sin_d(degree-coord.longitude);
  2067.     coord:=sun_coordinate(result);
  2068.     end;
  2069.   end;
  2070. (*@\\\*)
  2071. (*@/// function StartSeason(year: integer; season:TSeason):TDateTime; *)
  2072. (*$ifndef low_accuracy *)
  2073. function StartSeason(year: integer; season:TSeason):TDateTime;
  2074. begin
  2075.   result:=0;
  2076.   case season of
  2077.     spring: result:=CalcSolarTerm(year,st_z2);
  2078.     summer: result:=CalcSolarTerm(year,st_z5);
  2079.     autumn: result:=CalcSolarTerm(year,st_z8);
  2080.     winter: result:=CalcSolarTerm(year,st_z11);
  2081.     end;
  2082.   end;
  2083. (*$else *)
  2084.  
  2085. { Based upon chapter 27 (26) of Meeus }
  2086.  
  2087. function StartSeason(year: integer; season:TSeason):TDateTime;
  2088. var
  2089.   y: extended;
  2090.   jde0: extended;
  2091.   t, w, dl, s: extended;
  2092.   i: integer;
  2093. const
  2094.   (*@/// a: array[0..23] of integer = (..); *)
  2095.   a: array[0..23] of integer = (
  2096.     485, 203, 199, 182, 156, 136, 77, 74, 70, 58, 52, 50,
  2097.     45, 44, 29, 18, 17, 16, 14, 12, 12, 12, 9, 8 );
  2098.   (*@\\\*)
  2099.   (*@/// bc:array[0..23,1..2] of extended = (..); *)
  2100.   bc:array[0..23,1..2] of extended = (
  2101.      ( 324.96,   1934.136 ),
  2102.      ( 337.23,  32964.467 ),
  2103.      ( 342.08,     20.186 ),
  2104.      (  27.85, 445267.112 ),
  2105.      (  73.14,  45036.886 ),
  2106.      ( 171.52,  22518.443 ),
  2107.      ( 222.54,  65928.934 ),
  2108.      ( 296.72,   3034.906 ),
  2109.      ( 243.58,   9037.513 ),
  2110.      ( 119.81,  33718.147 ),
  2111.      ( 297.17,    150.678 ),
  2112.      (  21.02,   2281.226 ),
  2113.      ( 247.54,  29929.562 ),
  2114.      ( 325.15,  31555.956 ),
  2115.      (  60.93,   4443.417 ),
  2116.      ( 155.12,  67555.328 ),
  2117.      ( 288.79,   4562.452 ),
  2118.      ( 198.04,  62894.029 ),
  2119.      ( 199.76,  31436.921 ),
  2120.      (  95.39,  14577.848 ),
  2121.      ( 287.11,  31931.756 ),
  2122.      ( 320.81,  34777.259 ),
  2123.      ( 227.73,   1222.114 ),
  2124.      (  15.45,  16859.074 )
  2125.                              );
  2126.   (*@\\\*)
  2127. begin
  2128.   case year of
  2129.     (*@/// -1000..+999: *)
  2130.     -1000..+999: begin
  2131.       y:=year/1000;
  2132.       case season of
  2133.         spring: jde0:=1721139.29189+(365242.13740+( 0.06134+( 0.00111-0.00071*y)*y)*y)*y;
  2134.         summer: jde0:=1721233.25401+(365241.72562+(-0.05323+( 0.00907+0.00025*y)*y)*y)*y;
  2135.         autumn: jde0:=1721325.70455+(365242.49558+(-0.11677+(-0.00297+0.00074*y)*y)*y)*y;
  2136.         winter: jde0:=1721414.39987+(365242.88257+(-0.00769+(-0.00933-0.00006*y)*y)*y)*y;
  2137.         else    jde0:=0;   (* this can't happen *)
  2138.         end;
  2139.       end;
  2140.     (*@\\\*)
  2141.     (*@/// +1000..+3000: *)
  2142.     +1000..+3000: begin
  2143.       y:=(year-2000)/1000;
  2144.       case season of
  2145.         spring: jde0:=2451623.80984+(365242.37404+( 0.05169+(-0.00411-0.00057*y)*y)*y)*y;
  2146.         summer: jde0:=2451716.56767+(365241.62603+( 0.00325+( 0.00888-0.00030*y)*y)*y)*y;
  2147.         autumn: jde0:=2451810.21715+(365242.01767+(-0.11575+( 0.00337+0.00078*y)*y)*y)*y;
  2148.         winter: jde0:=2451900.05952+(365242.74049+(-0.06223+(-0.00823+0.00032*y)*y)*y)*y;
  2149.         else    jde0:=0;   (* this can't happen *)
  2150.         end;
  2151.       end;
  2152.     (*@\\\*)
  2153.     else raise E_OutOfAlgorithmRange.Create('Out of range of the algorithm');
  2154.     end;
  2155.   t:=(jde0-2451545.0)/36525;
  2156.   w:=35999.373*t-2.47;
  2157.   dl:=1+0.0334*cos_d(w)+0.0007*cos_d(2*w);
  2158.   (*@/// s := Σ a cos(b+c*t) *)
  2159.   s:=0;
  2160.   for i:=0 to 23 do
  2161.     s:=s+a[i]*cos_d(bc[i,1]+bc[i,2]*t);
  2162.   (*@\\\*)
  2163.   result:=delphi_date(jde0+(0.00001*s)/dl);
  2164.   end;
  2165. (*$endif *)
  2166. (*@\\\*)
  2167. (*@/// function MajorSolarTerm(month: integer):TSolarTerm; *)
  2168. function MajorSolarTerm(month: integer):TSolarTerm;
  2169. var
  2170.   count: integer;
  2171. begin
  2172.   count:=(month-2)*2 + ord(st_z1);
  2173.   result:=TSolarTerm(count mod 24);
  2174.   end;
  2175. (*@\\\*)
  2176. (*@/// function MajorSolarTermAfter(date: TDateTime):TDateTime; *)
  2177. function MajorSolarTermAfter(date: TDateTime):TDateTime;
  2178. var
  2179.   y,m,d: word;
  2180. begin
  2181.   DecodeDateCorrect(date,y,m,d);
  2182.   repeat
  2183.     result:=CalcSolarTerm(y,MajorSolarTerm(m));
  2184.     inc(m);
  2185.     if m>12 then begin
  2186.       inc(y);
  2187.       m:=1;
  2188.       end;
  2189.   until result>=date;
  2190.   end;
  2191. (*@\\\*)
  2192. (*@/// function MajorSolarTermBefore(date: TDateTime):TDateTime; *)
  2193. function MajorSolarTermBefore(date: TDateTime):TDateTime;
  2194. var
  2195.   y,m,d: word;
  2196. begin
  2197.   DecodeDateCorrect(date,y,m,d);
  2198.   repeat
  2199.     result:=CalcSolarTerm(y,MajorSolarTerm(m));
  2200.     dec(m);
  2201.     if m<1 then begin
  2202.       dec(y);
  2203.       m:=12;
  2204.       end;
  2205.   until result<date;
  2206.   end;
  2207. (*@\\\*)
  2208.  
  2209.  
  2210. { Rising and setting of moon and sun }
  2211. (*@/// function Calc_Set_Rise(date:TDateTime; latitude, longitude:extended; *)
  2212. { Based upon chapter 15 (14) of Meeus }
  2213.  
  2214. function Calc_Set_Rise(date:TDateTime; latitude, longitude:extended;
  2215.                        sun: boolean; kind: T_RiseSet):TDateTime;
  2216. var
  2217.   h: Extended;
  2218.   pos1, pos2, pos3: t_coord;
  2219.   h0, theta0, cos_h0, cap_h0: extended;
  2220.   m0,m1,m2: extended;
  2221. (*@/// function interpolation(y1,y2,y3,n: extended):extended; *)
  2222. function interpolation(y1,y2,y3,n: extended):extended;
  2223. var
  2224.   a,b,c: extended;
  2225. begin
  2226.   a:=y2-y1;
  2227.   b:=y3-y2;
  2228.   if a>100 then  a:=a-360;
  2229.   if a<-100 then  a:=a+360;
  2230.   if b>100 then  b:=b-360;
  2231.   if b<-100 then  b:=b+360;
  2232.   c:=b-a;
  2233.   result:=y2+0.5*n*(a+b+n*c);
  2234.   end;
  2235. (*@\\\*)
  2236. (*@/// function correction(m:extended; kind:integer):extended; *)
  2237. function correction(m:extended; kind:integer):extended;
  2238. var
  2239.   alpha,delta,h, height: extended;
  2240. begin
  2241.   alpha:=interpolation(pos1.rektaszension,
  2242.                        pos2.rektaszension,
  2243.                        pos3.rektaszension,
  2244.                        m);
  2245.   delta:=interpolation(pos1.declination,
  2246.                        pos2.declination,
  2247.                        pos3.declination,
  2248.                        m);
  2249.   h:=put_in_360((theta0+360.985647*m)-longitude-alpha);
  2250.   if h>180 then h:=h-360;
  2251.  
  2252.   height:=arcsin_d(sin_d(latitude)*sin_d(delta)
  2253.                    +cos_d(latitude)*cos_d(delta)*cos_d(h));
  2254.  
  2255.   case kind of
  2256.     0:   result:=-h/360;
  2257.     1,2: result:=(height-h0)/(360*cos_d(delta)*cos_d(latitude)*sin_d(h));
  2258.     else result:=0;   (* this cannot happen *)
  2259.     end;
  2260.   end;
  2261. (*@\\\*)
  2262. const
  2263.   sun_diameter = 0.8333;
  2264.   civil_twilight_elevation = -6.0;
  2265.   nautical_twilight_elevation = -12.0;
  2266.   astronomical_twilight_elevation = -18.0;
  2267. begin
  2268.   case kind of
  2269.     _rise, _set: begin
  2270.       if sun then
  2271.         h0:=-sun_diameter
  2272.       else begin
  2273.         pos1:=moon_coordinate(date);
  2274.         correct_position(pos1,date,latitude,longitude,0);
  2275.         h0:=0.7275*pos1.parallax-34/60;
  2276.         end;
  2277.       end;
  2278.     _rise_civil,
  2279.     _set_civil:   h0:=civil_twilight_elevation;
  2280.     _rise_nautical,
  2281.     _set_nautical: h0:=nautical_twilight_elevation;
  2282.     _rise_astro,
  2283.     _set_astro:   h0:=astronomical_twilight_elevation;
  2284.     else          h0:=0;  (* don't care for _transit *)
  2285.     end;
  2286.  
  2287.   h:=int(date);
  2288.   theta0:=star_time(h);
  2289.   if sun then begin
  2290.     pos1:=sun_coordinate(h-1);
  2291.     pos2:=sun_coordinate(h);
  2292.     pos3:=sun_coordinate(h+1);
  2293.     end
  2294.   else begin
  2295.     pos1:=moon_coordinate(h-1);
  2296.     correct_position(pos1,h-1,latitude,longitude,0);
  2297.     pos2:=moon_coordinate(h);
  2298.     correct_position(pos2,h,latitude,longitude,0);
  2299.     pos3:=moon_coordinate(h+1);
  2300.     correct_position(pos3,h+1,latitude,longitude,0);
  2301.     end;
  2302.  
  2303.   cos_h0:=(sin_d(h0)-sin_d(latitude)*sin_d(pos2.declination))/
  2304.           (cos_d(latitude)*cos_d(pos2.declination));
  2305.   if (cos_h0<-1) or (cos_h0>1) then
  2306.     raise E_NoRiseSet.Create('No rises or sets calculable');
  2307.   cap_h0:=arccos_d(cos_h0);
  2308.  
  2309.   m0:=(pos2.rektaszension+longitude-theta0)/360;
  2310.   m1:=m0-cap_h0/360;
  2311.   m2:=m0+cap_h0/360;
  2312.  
  2313.   m0:=frac(m0);
  2314.   if m0<0 then m0:=m0+1;
  2315.   m1:=frac(m1);
  2316.   if m1<0 then m1:=m1+1;
  2317.   m2:=frac(m2);
  2318.   if m2<0 then m2:=m2+1;
  2319.  
  2320.   m0:=m0+correction(m0,0);
  2321.   m1:=m1+correction(m1,1);
  2322.   m2:=m2+correction(m2,2);
  2323.  
  2324.   case kind of
  2325.     _rise,
  2326.     _rise_civil,
  2327.     _rise_nautical,
  2328.     _rise_astro:    result:=h+m1;
  2329.     _set,
  2330.     _set_civil,
  2331.     _set_nautical,
  2332.     _set_astro:     result:=h+m2;
  2333.     _transit: result:=h+m0;
  2334.     else      result:=0;    (* this can't happen *)
  2335.     end;
  2336.  
  2337.   end;
  2338. (*@\\\000000011D*)
  2339.  
  2340. (*@/// function Sun_Rise(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2341. function Sun_Rise(date:TDateTime; latitude, longitude:extended):TDateTime;
  2342. begin
  2343.   result:=Calc_Set_Rise(date,latitude,longitude,true,_rise);
  2344.   end;
  2345. (*@\\\*)
  2346. (*@/// function Sun_Set(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2347. function Sun_Set(date:TDateTime; latitude, longitude:extended):TDateTime;
  2348. begin
  2349.   result:=Calc_Set_Rise(date,latitude,longitude,true,_set);
  2350.   end;
  2351. (*@\\\*)
  2352. (*@/// function Sun_Transit(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2353. function Sun_Transit(date:TDateTime; latitude, longitude:extended):TDateTime;
  2354. begin
  2355.   result:=Calc_Set_Rise(date,latitude,longitude,true,_transit);
  2356.   end;
  2357. (*@\\\*)
  2358. (*@/// function Moon_Rise(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2359. function Moon_Rise(date:TDateTime; latitude, longitude:extended):TDateTime;
  2360. begin
  2361.   result:=Calc_Set_Rise(date,latitude,longitude,false,_rise);
  2362.   end;
  2363. (*@\\\*)
  2364. (*@/// function Moon_Set(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2365. function Moon_Set(date:TDateTime; latitude, longitude:extended):TDateTime;
  2366. begin
  2367.   result:=Calc_Set_Rise(date,latitude,longitude,false,_set);
  2368.   end;
  2369. (*@\\\*)
  2370. (*@/// function Moon_Transit(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2371. function Moon_Transit(date:TDateTime; latitude, longitude:extended):TDateTime;
  2372. begin
  2373.   result:=Calc_Set_Rise(date,latitude,longitude,false,_transit);
  2374.   end;
  2375. (*@\\\*)
  2376.  
  2377. (*@/// function Morning_Twilight_Civil(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2378. function Morning_Twilight_Civil(date:TDateTime; latitude, longitude:extended):TDateTime;
  2379. begin
  2380.   result:=Calc_Set_Rise(date,latitude,longitude,true,_rise_civil);
  2381.   end;
  2382. (*@\\\*)
  2383. (*@/// function Evening_Twilight_Civil(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2384. function Evening_Twilight_Civil(date:TDateTime; latitude, longitude:extended):TDateTime;
  2385. begin
  2386.   result:=Calc_Set_Rise(date,latitude,longitude,true,_set_civil);
  2387.   end;
  2388. (*@\\\*)
  2389. (*@/// function Morning_Twilight_Nautical(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2390. function Morning_Twilight_Nautical(date:TDateTime; latitude, longitude:extended):TDateTime;
  2391. begin
  2392.   result:=Calc_Set_Rise(date,latitude,longitude,true,_rise_nautical);
  2393.   end;
  2394. (*@\\\*)
  2395. (*@/// function Evening_Twilight_Nautical(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2396. function Evening_Twilight_Nautical(date:TDateTime; latitude, longitude:extended):TDateTime;
  2397. begin
  2398.   result:=Calc_Set_Rise(date,latitude,longitude,true,_set_nautical);
  2399.   end;
  2400. (*@\\\*)
  2401. (*@/// function Morning_Twilight_Astronomical(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2402. function Morning_Twilight_Astronomical(date:TDateTime; latitude, longitude:extended):TDateTime;
  2403. begin
  2404.   result:=Calc_Set_Rise(date,latitude,longitude,true,_rise_astro);
  2405.   end;
  2406. (*@\\\*)
  2407. (*@/// function Evening_Twilight_Astronomical(date:TDateTime; latitude, longitude:extended):TDateTime; *)
  2408. function Evening_Twilight_Astronomical(date:TDateTime; latitude, longitude:extended):TDateTime;
  2409. begin
  2410.   result:=Calc_Set_Rise(date,latitude,longitude,true,_set_astro);
  2411.   end;
  2412. (*@\\\*)
  2413.  
  2414.  
  2415. { Checking for eclipses }
  2416. (*@/// function Eclipse(var date:TDateTime; sun:boolean):TEclipse; *)
  2417. function Eclipse(var date:TDateTime; sun:boolean):TEclipse;
  2418. var
  2419.   jde,kk,m,ms,f,o,e: extended;
  2420.   t,f1,a1: extended;
  2421.   p,q,w,gamma,u: extended;
  2422. begin
  2423.   if sun then
  2424.     calc_phase_data(date,NewMoon,jde,kk,m,ms,f,o,e)
  2425.   else
  2426.     calc_phase_data(date,FullMoon,jde,kk,m,ms,f,o,e);
  2427.   t:=kk/1236.85;
  2428.   if abs(sin_d(f))>0.36 then
  2429.     result:=none
  2430.   (*@/// else *)
  2431.   else begin
  2432.     f1:=f-0.02665*sin_d(o);
  2433.     a1:=299.77+0.107408*kk-0.009173*t*t;
  2434.     if sun then
  2435.       jde:=jde - 0.4075     * sin_d(ms)
  2436.                + 0.1721 * e * sin_d(m)
  2437.     else
  2438.       jde:=jde - 0.4065     * sin_d(ms)
  2439.                + 0.1727 * e * sin_d(m);
  2440.     jde:=jde   + 0.0161     * sin_d(2*ms)
  2441.                - 0.0097     * sin_d(2*f1)
  2442.                + 0.0073 * e * sin_d(ms-m)
  2443.                - 0.0050 * e * sin_d(ms+m)
  2444.                - 0.0023     * sin_d(ms-2*f1)
  2445.                + 0.0021 * e * sin_d(2*m)
  2446.                + 0.0012     * sin_d(ms+2*f1)
  2447.                + 0.0006 * e * sin_d(2*ms+m)
  2448.                - 0.0004     * sin_d(3*ms)
  2449.                - 0.0003 * e * sin_d(m+2*f1)
  2450.                + 0.0003     * sin_d(a1)
  2451.                - 0.0002 * e * sin_d(m-2*f1)
  2452.                - 0.0002 * e * sin_d(2*ms-m)
  2453.                - 0.0002     * sin_d(o);
  2454.     p:=        + 0.2070 * e * sin_d(m)
  2455.                + 0.0024 * e * sin_d(2*m)
  2456.                - 0.0392     * sin_d(ms)
  2457.                + 0.0116     * sin_d(2*ms)
  2458.                - 0.0073 * e * sin_d(ms+m)
  2459.                + 0.0067 * e * sin_d(ms-m)
  2460.                + 0.0118     * sin_d(2*f1);
  2461.     q:=        + 5.2207
  2462.                - 0.0048 * e * cos_d(m)
  2463.                + 0.0020 * e * cos_d(2*m)
  2464.                - 0.3299     * cos_d(ms)
  2465.                - 0.0060 * e * cos_d(ms+m)
  2466.                + 0.0041 * e * cos_d(ms-m);
  2467.     w:=abs(cos_d(f1));
  2468.     gamma:=(p*cos_d(f1)+q*sin_d(f1))*(1-0.0048*w);
  2469.     u:= + 0.0059
  2470.         + 0.0046 * e * cos_d(m)
  2471.         - 0.0182     * cos_d(ms)
  2472.         + 0.0004     * cos_d(2*ms)
  2473.         - 0.0005     * cos_d(m+ms);
  2474.     (*@/// if sun then *)
  2475.     if sun then begin
  2476.       if abs(gamma)<0.9972 then begin
  2477.         if u<0 then
  2478.           result:=total
  2479.         else if u>0.0047 then
  2480.           result:=circular
  2481.         else if u<0.00464*sqrt(1-gamma*gamma) then
  2482.           result:=circulartotal
  2483.         else
  2484.           result:=circular;
  2485.         end
  2486.       else if abs(gamma)>1.5433+u then
  2487.         result:=none
  2488.       else if abs(gamma)<0.9972+abs(u) then
  2489.         result:=noncentral
  2490.       else
  2491.         result:=partial;
  2492.       end
  2493.     (*@\\\*)
  2494.     (*@/// else *)
  2495.     else begin
  2496.       if (1.0128 - u - abs(gamma)) / 0.5450 > 0 then
  2497.         result:=total
  2498.       else if (1.5573 + u - abs(gamma)) / 0.5450 > 0 then
  2499.         result:=halfshadow
  2500.       else
  2501.         result:=none;
  2502.       end;
  2503.     (*@\\\*)
  2504.     end;
  2505.   (*@\\\*)
  2506.   date:=delphi_date(jde);
  2507.   end;
  2508. (*@\\\*)
  2509. (*@/// function NextEclipse(var date:TDateTime; sun:boolean):TEclipse; *)
  2510. function NextEclipse(var date:TDateTime; sun:boolean):TEclipse;
  2511. var
  2512.   temp_date: TDateTime;
  2513. begin
  2514.   result:=none;    (* just to make Delphi 2/3 shut up, not needed really *)
  2515.   temp_date:=date-28*2;
  2516.   while temp_date<date do begin
  2517.     temp_date:=temp_date+28;
  2518.     result:=Eclipse(temp_date,sun);
  2519.     end;
  2520.   date:=temp_date;
  2521.   end;
  2522. (*@\\\*)
  2523.  
  2524. { Horizontal positions }
  2525. (*@/// procedure Moon_Position_Horizontal(date:TdateTime; longitude,latitude: extended; var elevation,azimuth: extended); *)
  2526. procedure Moon_Position_Horizontal(date:TdateTime; longitude,latitude: extended; var elevation,azimuth: extended);
  2527. var
  2528.   pos1: T_Coord;
  2529. begin
  2530.   pos1:=moon_coordinate(date);
  2531.   calc_horizontal(pos1,date,longitude,latitude);
  2532.   end;
  2533. (*@\\\*)
  2534. (*@/// procedure Sun_Position_Horizontal(date:TdateTime; longitude,latitude: extended; var elevation,azimuth: extended); *)
  2535. procedure Sun_Position_Horizontal(date:TdateTime; longitude,latitude: extended; var elevation,azimuth: extended);
  2536. var
  2537.   pos1: T_Coord;
  2538. begin
  2539.   pos1:=sun_coordinate(date);
  2540.   calc_horizontal(pos1,date,longitude,latitude);
  2541.   end;
  2542. (*@\\\*)
  2543.  
  2544. { Chinese calendar }
  2545. (*@/// function UTCtoChina(date: TdateTime):TDateTime; *)
  2546. function UTCtoChina(date: TdateTime):TDateTime;
  2547. var
  2548.   y,m,d: word;
  2549. begin
  2550.   decodedatecorrect(date,y,m,d);
  2551.   if y<1929 then
  2552.     result:=date-beijing_longitude/360
  2553.   else
  2554.     result:=date+120/360;
  2555.   end;
  2556. (*@\\\*)
  2557. (*@/// function ChinaToUTC(date: TdateTime):TDateTime; *)
  2558. function ChinaToUTC(date: TdateTime):TDateTime;
  2559. var
  2560.   y,m,d: word;
  2561. begin
  2562.   decodedatecorrect(date,y,m,d);
  2563.   if y<1929 then
  2564.     result:=date+beijing_longitude/360
  2565.   else
  2566.     result:=date-120/360;
  2567.   end;
  2568. (*@\\\*)
  2569. (*@/// function ChinaNewMoonBefore(date:TDateTime):TdateTime; *)
  2570. function ChinaNewMoonBefore(date:TDateTime):TdateTime;
  2571. begin
  2572.   result:=Trunc(UTCtoChina(last_phase(ChinaToUTC(date),Newmoon)));
  2573.   end;
  2574. (*@\\\*)
  2575. (*@/// function ChinaNewMoonAfter(date:TDateTime):TdateTime; *)
  2576. function ChinaNewMoonAfter(date:TDateTime):TdateTime;
  2577. begin
  2578.   result:=Trunc(UTCtoChina(next_phase(ChinaToUTC(date),Newmoon)));
  2579.   end;
  2580. (*@\\\*)
  2581. (*@/// function ChinaSolarTermAfter(date:TDateTime):TdateTime; *)
  2582. function ChinaSolarTermAfter(date:TDateTime):TdateTime;
  2583. begin
  2584.   result:=Trunc(UTCtoChina(MajorSolarTermAfter(ChinaToUTC(date))));
  2585.   end;
  2586. (*@\\\*)
  2587. (*@/// function ChinaSolarTermBefore(date:TDateTime):TdateTime; *)
  2588. function ChinaSolarTermBefore(date:TDateTime):TdateTime;
  2589. begin
  2590.   result:=Trunc(UTCtoChina(MajorSolarTermBefore(ChinaToUTC(date))));
  2591.   end;
  2592. (*@\\\*)
  2593. (*@/// function hasnomajorsolarterm(date:TDateTime):boolean; *)
  2594. function hasnomajorsolarterm(date:TDateTime):boolean;
  2595. var
  2596.   term_before_date,
  2597.   term_before_next_month,
  2598.   next_month: TDateTime;
  2599. begin
  2600.   term_before_date:=ChinaSolarTermBefore(date);
  2601.   next_month:=ChinaNewMoonAfter(date+2);
  2602.   term_before_next_month:=ChinaSolarTermBefore(next_month);
  2603.   result:=term_before_date=term_before_next_month;
  2604.   end;
  2605. (*@\\\*)
  2606. (*@/// function haspriorleapmonth(date1,date2: TdateTime):boolean; *)
  2607. function haspriorleapmonth(date1,date2: TdateTime):boolean;
  2608. { Recursive }
  2609. begin
  2610.   if (date2>date1) then
  2611.     result:=haspriorleapmonth(date1,ChinaNewMoonBefore(date2-1))
  2612.   else
  2613.     result:=false;
  2614.   if not result then
  2615.     result:=hasnomajorsolarterm(date2);
  2616.   end;
  2617. (*@\\\*)
  2618.  
  2619. (*@/// function ChineseDate(date: TdateTime): TChineseDate; *)
  2620. function ChineseDate(date: TdateTime): TChineseDate;
  2621. var
  2622.   s1,s2,s3: TDateTime;
  2623.   m0,m1,m2: TdateTime;
  2624.   y,m,d: word;
  2625.   daycycle, monthcycle: integer;
  2626. begin
  2627.   Decodedatecorrect(date,y,m,d);
  2628.   date:=trunc(date);
  2629.   (* Winter solstices (Z12) around the date *)
  2630.   s1:=ChinaSolarTermAfter(encodedatecorrect(y-1,12,15));
  2631.   s2:=ChinaSolarTermAfter(encodedatecorrect(y  ,12,15));
  2632.   s3:=ChinaSolarTermAfter(encodedatecorrect(y+1,12,15));
  2633.   (* Start of Months around winter solstices *)
  2634.   if (s1<=date) and (date<s2) then begin
  2635.     m1:=ChinaNewMoonAfter(s1+1);
  2636.     m2:=ChinaNewMoonBefore(s2+1);
  2637.     end
  2638.   else begin
  2639.     m1:=ChinaNewMoonAfter(s2+1);
  2640.     m2:=ChinaNewMoonBefore(s3+1);
  2641.     end;
  2642.   (* Start of current month *)
  2643.   m0:=ChinaNewMoonBefore(date+1);
  2644.   result.leapyear:=round((m2-m1)/mean_lunation)=12;
  2645.   result.month:=round((m0-m1)/mean_lunation);
  2646.   result.leap:=result.leapyear and hasnomajorsolarterm(m0) and
  2647.                (not haspriorleapmonth(m1,ChinaNewMoonBefore(m0)));
  2648.   if result.leapyear and (haspriorleapmonth(m1,m0) or result.leap) then
  2649.     result.month:=result.month-1;
  2650.   result.month:=adjusted_mod(result.month,12);
  2651.   result.day:=round(date-m0+1);
  2652.   result.epoch_years:=y+2636;
  2653.   if (result.month<11) or (date>EncodedateCorrect(y,7,1)) then
  2654.     inc(result.epoch_years);
  2655.   result.cycle:=((result.epoch_years-1) div 60)+1;
  2656.   result.year:=adjusted_mod(result.epoch_years,60);
  2657.   result.yearcycle.zodiac:=TChineseZodiac((result.year-1) mod 12);
  2658.   result.yearcycle.stem:=TChineseStem((result.year-1) mod 10);
  2659.   (* 2000-1-7 = daycycle jia-zi *)
  2660.   daycycle:=adjusted_mod(round(trunc(date)-datetime_2000_01_01-6),60);
  2661.   result.daycycle.zodiac:=TChineseZodiac(daycycle mod 12);
  2662.   result.daycycle.stem:=TChineseStem(daycycle mod 10);
  2663.   (* 1998-12-19 = monthcycle jia-zi *)
  2664.   monthcycle:=adjusted_mod(1+round((m0-datetime_1999_01_01-13)/mean_lunation),60);
  2665.   result.monthcycle.zodiac:=TChineseZodiac(monthcycle mod 12);
  2666.   result.monthcycle.stem:=TChineseStem(monthcycle mod 10);
  2667.   end;
  2668. (*@\\\*)
  2669. (*@/// function EncodeDateChinese(date:TChineseDate):TDateTime; *)
  2670. function EncodeDateChinese(date:TChineseDate):TDateTime;
  2671. var
  2672.   y: integer;
  2673.   newyear, month_begin: TdateTime;
  2674.   chinese: TChineseDate;
  2675. begin
  2676.   y:=60*(date.cycle-1)+(date.year-1)-2636;
  2677.   newyear:=ChineseNewYear(y);
  2678.   month_begin:=ChinaNewMoonAfter(newyear+29*(date.month-1));
  2679.   chinese:=ChineseDate(month_begin);
  2680.   if (chinese.month=date.month) and (chinese.leap=date.leap) then
  2681.     result:=month_begin+date.day-1
  2682.   else
  2683.     result:=ChinaNewMoonAfter(month_begin+5)+date.day-1;
  2684.   (* check if the input date was valid *)
  2685.   chinese:=ChineseDate(result);
  2686.   if (chinese.day<>date.day) or (chinese.month<>date.month) or
  2687.      (chinese.leap<>date.leap) or (chinese.year<>date.year) or
  2688.      (chinese.cycle<>date.cycle) then
  2689.     raise EConvertError.Create('Invalid chinese date');
  2690.   end;
  2691. (*@\\\*)
  2692. (*@/// function ChineseNewYear(year: integer): TDateTime; *)
  2693. function ChineseNewYear(year: integer): TDateTime;
  2694. var
  2695.   s1,s2: TDateTime;
  2696.   m1,m2,m11: TdateTime;
  2697. begin
  2698.   (* Winter solstices (Z12) around the January 1st of the year *)
  2699.   s1:=ChinaSolarTermAfter(encodedatecorrect(year-1,12,15));
  2700.   s2:=ChinaSolarTermAfter(encodedatecorrect(year  ,12,15));
  2701.   m1:=ChinaNewMoonAfter(s1+1);
  2702.   m2:=ChinaNewMoonAfter(m1+4);
  2703.   m11:=ChinaNewMoonBefore(s2+1);
  2704.   if (round((m11-m1)/mean_lunation)=12) and
  2705.      (hasnomajorsolarterm(m1) or hasnomajorsolarterm(m2)) then
  2706.     result:=ChinaNewMoonAfter(m2+1)
  2707.   else
  2708.     result:=m2;
  2709.   end;
  2710. (*@\\\*)
  2711.  
  2712. (*$ifdef zero *)
  2713. (*@/// Jupiter coordinates and moons - currently under construction *)
  2714. type
  2715.   (*@/// t_jupiter_coord=record *)
  2716.   t_jupiter_coord=record
  2717.     d, v, m, n, j, a, b, k, rr, r, delta, psi, deke: extended;
  2718.     end;
  2719.   (*@\\\*)
  2720.   t_jupiter_moon=array[0..3,0..1] of extended;
  2721.  
  2722. (*@/// function Jupiter_ephem_phys(date:TdateTime):t_jupiter_coord; *)
  2723. { Based upon chapter 44 (42) of Meeus }
  2724.  
  2725. function Jupiter_ephem_phys(date:TdateTime):t_jupiter_coord;
  2726. var
  2727.   d, t, t1, a0, d0, w1, w2: extended;
  2728.   l, b, r, l0, b0, r0: extended;
  2729.   x,y,z,delta: extended;
  2730.   epsilon_0: extended;
  2731. begin
  2732.   d:=(julian_date(date)-2433282.5);
  2733.   t1:=d/36525;
  2734.   t:=(julian_date(date)-2451545)/36525;
  2735.   a0:=268.0+0.1061*t1;
  2736.   d0:=64.5-0.0164*t1;
  2737.   w1:=put_in_360(17.710+877.90003539*d);
  2738.   w2:=put_in_360(16.838+870.27003539*d);
  2739.   earth_coord(date,l0,b0,r0);
  2740.   jupiter_coord(date,l,b,r);
  2741.   x:=r*cos_d(b)*cos_d(l)-r0*cos_d(l0);
  2742.   y:=r*cos_d(b)*sin_d(l)-r0*sin_d(l0);
  2743.   z:=r*sin_d(b)-r0*sin_d(b0);
  2744.   delta:=sqrt(x*x+y*y+z*z);
  2745.   l:=l-0.012990*delta/r/r;
  2746.   x:=r*cos_d(b)*cos_d(l)-r0*cos_d(l0);
  2747.   y:=r*cos_d(b)*sin_d(l)-r0*sin_d(l0);
  2748.   z:=r*sin_d(b)-r0*sin_d(b0);
  2749.   delta:=sqrt(x*x+y*y+z*z);
  2750.   epsilon_0:=84381.448+(-46.8150+(-0.00059+0.001813*t)*t)*t;
  2751.   end;
  2752. (*@\\\000000011D*)
  2753. (*@/// function Jupiter_moon(date:TdateTime):t_jupiter_moon; *)
  2754. { Based upon chapter 43 of Meeus }
  2755.  
  2756. function Jupiter_moon(date:TdateTime):t_jupiter_moon;
  2757. begin
  2758.   end;
  2759. (*@\\\*)
  2760. (*@\\\0000000501*)
  2761. (*$endif *)
  2762. (*@\\\000000A70C*)
  2763. (*@/// initialization *)
  2764. (*$ifdef delphi_1 *)
  2765. begin
  2766. (*$else *)
  2767. initialization
  2768. (*$endif *)
  2769.   julian_offset:=2451544.5-EncodeDate(2000,1,1);
  2770.   datetime_2000_01_01:=EncodedateCorrect(2000,1,1);
  2771.   datetime_1999_01_01:=EncodedateCorrect(1999,1,1);
  2772.   datetime_first_lunation:=EncodeDate(1923,1,17);
  2773.   check_TDatetime;
  2774. (*@\\\*)
  2775. (*$ifdef delphi_ge_2 *) (*$warnings off *) (*$endif *)
  2776. end.
  2777. (*@\\\003F001101001101001001001101000F01000011000F01*)
  2778.