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

 { Has to be converted to the appropriate code page;
   the original one is 'Kamenicky'}

PROGRAM Noci; {in Czech, by J.Hollan, twilights, nights etc.}
uses Solar_ut, angles_o, str_num, Params,
     Un_Str_M {instead of Unit_str by Blaise Computing};
const
hlptxt1=cl+
'Okamžiky konce a začátku noci a další údaje o putování Slunce po nebi:'+cl;
hlptxt2=
'Noci /dDD[.MM[.YY]]  /N#  /I#  /UT#  /h#  /L<zem.délka> /F<zem.šířka>'+cl+
'     /W[<výst.soubor>]';
hlptxt3=
'                                                     implicitní hodnoty:'+cl+
' d :  počáteční datum                                  dnešní'   +cl+
' N :  kolik dalších dní se má počítat                  4'        ;
hlptxt4=
' I :  s jakým krokem                                   1 den'      +cl+
' UT:  ochylka občanského času od světového v hodinách  1 - listopad až březen,'+cl;
h45=
'                                                       2 - duben až říjen'+cl+
' h :  úhlová výška Slunce (ve stupních) jako mez noci  -6, tzv. občanská noc';
hlptxt5=
' L :  zeměpisná délka stanoviště ve stupních           '
 +MyLocation_longitude_string+'  ('
  +MyLocation_Name+')'+cl+
' F :  zeměpisná šířka  -      "       -                '
 +MyLocation_latitude_string+'  ('
  +MyLocation_Name+')'+cl+
' W :  má se krom na obrazovku psát do souboru          Noci.txk';
hlptxt6=cl+
' Není-li udán žádný parametr, program spočte následující tři dny'+cl+
'  (pro polohu místa: '+MyLocation_Name+') a výsledek jde jen na obrazovku.'+cl+
' / před identifikátory parametrů lze vynechat nebo místo něj psát -.'+cl
 +cl;
h7=
'( (C) Jan Hollan, Hvězdárna a planetárium M. Koperníka v Brně, 1999;'+cl+
' pod licencí GNU General Public License, http://www.gnu.org/copyleft;'+cl+
' zdrojový text viz http://astro.sci.muni.cz/pub/hollan/programmes)';

expl_col1=cl2+
'  Význam dalších sloupců:'+cl+
'SOUMRAK   je doba do/od h=-0.8°,'+cl+
'DEKLinace Slunce platí pro místní střední poledne,'+cl;
expl_col2=
'>stř.     říká, o kolik Slunce předbíhá,'+cl+
'"NOC"     je trvání noci,'+cl+
'A-W       je rozdíl azimutu Slunce (při průchodu danou úhlovou výškou) a západu.';

STD=24./365.2422;

VAR
 T_twi, Rano, Vecer, twi,  S_BT, Noc, aW : real;
 JD_inc, j, yy2,mm2: word;
 out: text;
 Den,twistr: string[7];
 tyy: string[5];
 tmm: string[3];
 whystr,aux_str:string; sets:boolean;
const
 a_rano : real =0;
 a_vecer: real =0;
 a_twi  : real =0;
 a_Decl : real =0;
 a_SEC  : real =0;
 a_Noc  : 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;
 OuFile: string = 'Noci.txk';

procedure Help;
begin
{lowvideo;}
writeln(hlptxt1);
{highvideo;}
writeln(hlptxt2);
{lowvideo;}
writeln(hlptxt3);
writeln(hlptxt4,h45);
writeln(hlptxt5);
writeln(hlptxt6,h7);
halt
end;

function par_Rnum(s:string):real; {the first character (switch) is skipped}
var j:integer; aux:real;
begin
  val(copy(s,2,length(s)-1),aux,j);
  if j>0 then
   begin
    writeln('"',aux,'" není číslo - ',j,'-tý znak je špatně.');
    halt
   end;
  par_Rnum:=aux
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;
     'D': if not par_date then help;
     'N': Next:=trunc(par_Rnum(Par));
     'I': Each:=trunc(par_Rnum(Par));
     'U': if not Par_UT then help;
     'H': h:=par_Rnum(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
             OuFile:= copy(Par,2,length(Par)-1);
          end;
    end; {of case}
   end; {of for}

  if W_file then
   begin
    assign(out, OuFile);
    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);

  writeln(
'Střed Slunce je v místě o souřadnicích [Lambda=',
                    rad_to_deg(Lambda):4:1,'°, Fí=',rad_to_deg(Fi):3:1,'°]');
  writeln(
' ve směru  h =',h:4:1,'° šikmo dolů');
  writeln(
' v následující okamžiky ráno a večer (další sloupce viz vysvětlivky dole)');
  writeln(
'                    času UT+',t_UT:2:0,'h:',cl);
  writeln(
' rok-měsíc-den      ráno  večer  soumrak  dekl.  >stř.  "noc"   A-W');
  writeln(
'                       h:min     / 1 min   /1°   /1min   /1h    /1°');

if W_file then
begin
  writeln(out,
'Výstup z programu Noci, který napsal v dubnu 1994  Jan Hollan,');
  writeln(out,
'                        Hvězdárna a planetárium Mikuláše Koperníka v Brně');
  writeln(out);
  writeln(out,
'Střed Slunce je v místě o souřadnicích [Lambda=',
                    rad_to_deg(Lambda):4:1,'°, Fí=',rad_to_deg(Fi):3:1,'°]');
  writeln(out,
' ve směru  h =',h:4:1,'° šikmo dolů');
  writeln(out,
' v následující okamžiky ráno a večer (další sloupce viz vysvětlivky dole)');
  writeln(out,
'                    času UT+',t_UT:2:0,'h:',cl);
  writeln(out);
  writeln(out,
' rok-měsíc-den      ráno  večer  soumrak  dekl.  >stř.  "noc"   A-W');
  writeln(out,
'                       h:min     / 1 min   /1°   /1min   /1h    /1°');
  writeln(out)
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);
    S_BT:=  SM + JD_inc*STD - RA/15 ;
    S_BT:=Bas_Int(S_BT,24);
    if s_BT > 12 then S_BT:= s_BT - 24;
    Den:=DoW(JD,t_UT,_Czech);
    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:=' Střed Slunce je "'+whystr+'" než daná úhlová výška'
    else
     begin
      inc(count);
      Rano:= UT+t_UT -T_twi-S_BT;
      Vecer:=UT+t_UT +T_twi-S_BT;
      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:='       ';
      Noc:=24-(T_twi*2);
      aW:=A-90;
      a_rano:=a_rano+Rano;
      a_vecer:=a_vecer+vecer;
      if sets then a_twi:=a_twi+twi;
      a_Decl:=a_Decl+Decl;
      a_SEC:=a_SEC+S_BT*60;
      a_Noc:=a_Noc+Noc;
      a_AW:=a_aw+aw;
      writeln(
  tyy,tmm,ND:2,' ',Den,
  Trunc(Rano):3,':',Round(Frac(rano)*60):2,Trunc(Vecer):4,':',Round(Frac(Vecer)*60):2,
  twistr,
  Decl:8:2,  S_BT*60:8:1, Noc:7:2, aw:7:1
       );
      if count_line mod 3 =0 then writeln;

      if W_file then
       begin
        writeln(out,
  tyy,tmm,ND:2,' ',Den,
  Trunc(Rano):3,':',Round(Frac(rano)*60):2,Trunc(Vecer):4,':',Round(Frac(Vecer)*60):2,
  twistr,
  Decl:8:2,  S_BT*60:8:1, Noc:7:2, aw:7:1
       );
       if count_line mod 3 =0 then writeln(out)
       end;
     end;
    tmm:='   '; tyy:='     ';
    if whystr<>'' then writeln(tyy,tmm,ND:2,' ',Den,whystr);
   end;

  if count<2 then begin if W_file then close(out); halt; end;
  a_rano:=a_rano/count;
  a_vecer:=a_vecer/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_Noc:=a_Noc/count;
  a_AW:=a_aw/count;
  if count>5 then
   writeln(
' rok-měsíc-den      ráno  večer  soumrak  dekl.  >stř.  "noc"   A-W',cl,
'                       h:min     / 1 min   /1°   /1min   /1h    /1°');
  writeln(
'Průměr za',Next+1:4,' dní');
writeln(
'  z',count:4,' hodnot je ',
Trunc(a_Rano):3,':',Round(Frac(a_rano)*60):2,
Trunc(a_Vecer):4,':',Round(Frac(a_Vecer)*60):2,
twistr,
a_Decl:8:2,  a_sec:8:1, a_Noc:7:2, a_aw:7:1,
expl_col1,expl_col2);


if W_file then
begin
  writeln(out);
  writeln(out,
' rok-měsíc-den      ráno  večer  soumrak  dekl.  >stř.  "noc"   A-W');
  writeln(out,
'                       h:min     / 1 min   /1°   /1min   /1h    /1°');
  writeln(out,'Průměr za',Next+1:4,' dní');
writeln(out,
'  z',count:4,' hodnot je ',
Trunc(a_Rano):3,':',Round(Frac(a_rano)*60):2,
Trunc(a_Vecer):4,':',Round(Frac(a_Vecer)*60):2,
a_twi:7:1,
a_Decl:8:2,  a_sec:8:1, a_Noc:7:2, a_aw:7:1,
expl_col1,expl_col2);
  close(out);
end;

END.
