 {~ Times and azimuth of Sun passing a given angular height ~}

 { Has to be converted to the appropriate code page;
   the original one is 'Kamenicky'
  -- this is just because of a character denoting a degree}

PROGRAM Nights; {by J.Hollan}
(*  Copyright (C) 1999 Jan Hollan

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*)

uses Solar_ut, angles_o, str_num, Params,
     Un_Str_M {instead of Unit_str by Blaise Computing};

const
h1=cl+
'Ends and starts of nights and further data on Sun motion in the sky:'+cl+
                                          cl;
h2=
'Nights dDD[.MM[.YY]]  N#  I#  UT#  h#  L<longitude> F<latitude>'+cl+
'      W[<output file>]'+cl;
h3=cl+
'                                                       default values:'+cl+
' d :  starting date                                    today'   +cl+
' N :  how many further dates are to be computed        2'       +cl ;
h4=
' I :  at what interval                                 1 day'      +cl+
' UT:  difference of the used time from the UTC         1 Nov.-March, else 2'+cl+
' h :  height of Sun (/ 1 degree) as a night limit     -6 (citizen night)'+cl;
h5=
' L :  geographical longitude of the place / 1 degree   '
 +MyLocation_longitude_string+'  ('
  +MyLocation_Name+')'+cl+
' F :               latitude                            '
 +MyLocation_latitude_string+'  ('
  +MyLocation_Name+')'+cl+
' W :  write to file as well                            Nights.txk'+cl;
h6=cl+
' If no parameter is given, the programme shows the next three days'+cl+
'  for '+MyLocation_Name+' and the output goes just to the screen.'+cl+
' The parameters may begin as usually with / or -.'+cl+cl;
h7=
'( (C) Jan Hollan, N.Copernicus Observatory and Planetarium in Brno, 1999;'+cl+
' subject to the GNU General Public License, http://www.gnu.org/copyleft;'+cl+
' source code available at http://astro.sci.muni.cz/pub/hollan/programmes)'+cl;


expl_col1=cl2+
'Meaning of remaining columns:'+cl+
' Twilight    is an interval to/from h=-0.8ø'+cl+
' Declination of Sun is given for the moment of local mean noon,'+cl;
expl_col2=
' >Mean       says how much is the Sun ahead of the mean time,'+cl+
' Night       is the time the Sun travels below h,'+cl+
' A-W         is departure of solar azimuth from the west when at h afternoon.'+cl2;


STD=24./365.2422;

VAR
 T_twi, Morning, Evening, twi,  Sun_Before_mean_Time, Night, aW : real;
 JD_inc, j, yy2,mm2: word;
 out: text;
 Day,twistr: string[7];
 tyy: string[5];
 tmm: string[3];
 whystr,aux_str:string; sets:boolean;
const
 a_Morning : real =0;
 a_Evening: real =0;
 a_twi  : real =0;
 a_Decl : real =0;
 a_SEC  : real =0;
 a_Night  : real =0;
 a_AW   : real =0;
 Next   : word= 2;
 count  : word =0;
 count_twi  : word =0;
 count_line : word =0;
 Each   : word=1;
 W_file: boolean = false;
 Out_file: string = 'Nights.txk';

procedure Help;
begin
writeln(h1,h2,h3,h4,h5,h6,h7);
halt
end;


BEGIN
{ Datum:=__DateStr(YYYY,MM,DD2);
  ND:=DD2;
  UT:=11;}
  h:=-6;

  if paramcount>0 then
  for j:= 1 to paramcount do
   begin

    Par:=__cvtstr(paramstr(j),_To_UpCase_Str);
    if (Par[1]='/') or (Par[1]='-') then
          Par:=copy(Par,2,length(par)-1);
    case Par[1] of
    '?','-': Help; {- added to respect unix --help even without the help}
     'D': if not par_date then help;
     'N': Next:=trunc(par2r(Par));
     'I': Each:=trunc(par2r(Par));
     'U': if not Par_UT then help;
     'H': h:=par2r(Par);
     'L': if not Par_Lambda then help;
     'F': if not Par_Fi then help;
     'W': begin
           W_file:=true;
           if length(Par)>1 then
             Out_file:= copy(Par,2,length(Par)-1);
          end;
    end; {of case}
   end; {of for}

  if W_file then
   begin
    assign(out, Out_file);
    rewrite(out)
   end;

  YY2:=0; MM2:=0;
  UT:=12-rad_to_deg(Lambda)/15;     {approximate local noon}
  ND:=DD;
  if UT<0  then dec(ND);
  if UT>24 then inc(ND);
  UT:=Bas_int(UT,24);
  SID_time_loc(SM,JD,UT,Lambda);


  write(cl,
'The centre of Sun is at a place with coordinates [Lambda=',
          rad_to_deg(Lambda):4:1,'ø, Phi=',rad_to_deg(Fi):3:1,'ø]',cl,
' in an angular height of h =',h:4:1,'ø',cl,
' in the following morning and evening moments (given in UTC+',t_UT:2:0,'h):',cl,
                                                                         cl,
' year-month-day  morning evening twilight decl.  >mean  night   A-W',cl,
'                       h:min     / 1 min   /1ø   /1min   /1h    /1ø',cl);


if W_file then
begin
  write(out,cl,
'Output of Night (a programme written in April 1994 by Jan Hollan,',cl,
'                 N. Copernicus Observatory and Planetarium in Brno).',cl,cl,

'The centre of Sun is at a place with coordinates [Lambda=',
          rad_to_deg(Lambda):4:1,'ø, Phi=',rad_to_deg(Fi):3:1,'ø]',cl,
' in an angular height of h =',h:4:1,'ø',cl,
' in the following morning and evening moments (given in UTC+',t_UT:2:0,'h):',cl,
                                                                         cl,
' year-month-day  morning evening twilight decl.  >mean  night   A-W',cl,
'                       h:min     / 1 min   /1ø   /1min   /1h    /1ø',cl);
end;

  JD:=JD-Each;
  for JD_inc:= 0 to Next do if JD_inc mod Each = 0 then
   begin
    inc(count_line);
    JD:=JD+Each;
    Ecl_to_Equ(Sun_Long(JD), RA, Decl);
    Sun_Before_mean_Time:=  SM + JD_inc*STD - RA/15 ;
    Sun_Before_mean_Time:=Bas_Int(Sun_Before_mean_Time,24);
    if Sun_Before_mean_Time > 12 then
     Sun_Before_mean_Time:= Sun_Before_mean_Time - 24;
    Day:=DoW(JD,t_UT,_English);
    if (MM<>MM2) or (Each>335) then
     begin
      MM2:=MM;
      str(MM:2,tmm);
      tmm:=tmm+'-';
      if YYYY>YY2 then
       begin
        YY2:=YYYY;
        str(YYYY:4,tyy);
        tyy:=tyy+'-'
       end
     end;
    if not h2t(h,Decl,A,T_twi,whystr) then
     whystr:=' The Sun centre is '+whystr+' than the given height'
    else
     begin
      inc(count);
      whystr:='';
      Morning:= UT+t_UT -T_twi-Sun_Before_mean_Time;
      Evening:=UT+t_UT +T_twi-Sun_Before_mean_Time;
      sets:=h2t(-0.8,Decl,A,twi,aux_str);
      if sets then
       begin
        twi:=(t_twi-twi)*60;
        str(twi:7:1,twistr);
        inc(count_twi);
       end
      else twistr:='       ';
      Night:=24-(T_twi*2);
      aW:=A-90;
      a_Morning:=a_Morning+Morning;
      a_Evening:=a_Evening+Evening;
      if sets then a_twi:=a_twi+twi;
      a_Decl:=a_Decl+Decl;
      a_SEC:=a_SEC+Sun_Before_mean_Time*60;
      a_Night:=a_Night+Night;
      a_AW:=a_aw+aw;
      writeln(
  tyy,tmm,ND:2,' ',Day,
  Trunc(Morning):3,':',Round(Frac(Morning)*60):2,Trunc(Evening):4,':',Round(Frac(Evening)*60):2,
  twistr,
  Decl:8:2,  Sun_Before_mean_Time*60:8:1, Night:7:2, aw:7:1
       );
      if count_line mod 3 =0 then writeln;

      if W_file then
       begin
        writeln(out,
  tyy,tmm,ND:2,' ',Day,
  Trunc(Morning):3,':',Round(Frac(Morning)*60):2,Trunc(Evening):4,':',Round(Frac(Evening)*60):2,
  twistr,
  Decl:8:2,  Sun_Before_mean_Time*60:8:1, Night:7:2, aw:7:1
       );
        if count_line mod 3 =0 then writeln(out)
       end;
     end;
    tmm:='   '; tyy:='     ';
    if whystr<>'' then
     begin
      writeln(tyy,tmm,ND:2,' ',Day,whystr);
      if count_line mod 3 =0 then writeln;

      if W_file then
       begin
        writeln(out,tyy,tmm,ND:2,' ',Day,whystr);
        if count_line mod 3 =0 then writeln(out);
       end;
     end;
   end;

  if count<2 then begin if W_file then close(out); halt; end;
  a_Morning:=a_Morning/count;
  a_Evening:=a_Evening/count;
  if count_twi>0 then
   str(a_twi/count_twi:7:1,twistr)
  else
   twistr:='       ';
  a_Decl:=a_Decl/count;
  a_SEC:=a_SEC/count;
  a_Night:=a_Night/count;
  a_AW:=a_aw/count;
  if count>5 then
   write(cl,
' year-month-day  morning evening twilight decl.  >Mean "night"  A-W',cl,
'                       h:min     / 1 min   /1ø   /1min   /1h    /1ø',cl,
                                                                      cl);
  write(
'Average for',Next+1:4,' days'+cl+
'from',count:4,' values is',
Trunc(a_Morning):3,':',Round(Frac(a_Morning)*60):2,
Trunc(a_Evening):4,':',Round(Frac(a_Evening)*60):2,
twistr,
a_Decl:8:2,  a_sec:8:1, a_Night:7:2, a_aw:7:1,
expl_col1,expl_col2);

if W_file then
begin
  write(out,cl,
' year-month-day  morning evening twilight decl.  > EuT "night"  A-W',cl,
'                       h:min     / 1 min   /1ø   /1min   /1h    /1ø',cl,
                                                                      cl,
'Average for',Next+1:4,' days'+cl+
'from',count:4,' values is',
Trunc(a_Morning):3,':',Round(Frac(a_Morning)*60):2,
Trunc(a_Evening):4,':',Round(Frac(a_Evening)*60):2,
a_twi:7:1,
a_Decl:8:2,  a_sec:8:1, a_Night:7:2, a_aw:7:1,
expl_col1,expl_col2);
  close(out);
end;

END.
