program SCSProject;



var
      A, Hmax ,Hmin, L, Cn : real;
      wName : String;
      Tc, Tp, Qp, wi, D,
      y , De, TC1, TC2, TC3,
      Tl, S : real;

      fif : TextFile;

      tcSelector : integer;
      p : Array [1..6] of real;
      const tpArr :array [1..33] of real = (0.0,
      0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
      1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
      2.2, 2.4, 2.6, 2.8, 3.0, 3.2, 3.4, 3.6, 3.8, 4.0,
      4.5, 5.0);
      const qpArr :array [1..33] of real = (0.0,
      0.03, 0.1, 0.19, 0.31, 0.47, 0.66, 0.82, 0.93,0.99,
      1.0, 0.99, 0.93, 0.86, 0.78, 0.68, 0.56, 0.46, 0.39,
      0.33, 0.28, 0.207, 0.147, 0.107, 0.077, 0.055, 0.04,
      0.029, 0.021, 0.015, 0.011, 0.005, 0.0);

      procedure Create();

      function clcQp():real;
      function clcQMax(Pi : real):real;
      function clcRe(Pi : real):real;
      function clcV(Pi : real): real;
      function clcCoef(Pi : real): real;
      procedure piPrinter();
      procedure tablePrinter();
      procedure calculation();
      procedure writeToFile(fileName: String);

      procedure getInput();
      procedure PrintValues();
      procedure fileManager();


procedure calculation;
begin
  getInput();
  clcQp();
end;

function clcCoef(Pi: real): real;
begin
  clcCoef := (clcRe(Pi) / Pi) * 100;
end;

function clcQMax(Pi: real): real;
begin
  clcQMax := Qp * clcRe(Pi);
end;

function clcQp: Real;
begin
  // clc Wi value
  Wi := (Hmax - Hmin) / (10 * L);
  // clc Y value
  y := (Hmax - Hmin) / (1000 * L);
  // clc De value
  De := sqrt(4 * A / 3.1416);
  // clc S value
  S := (1000 / Cn) - 10;
  // clc Tc1 value
  TC1 := (0.666 * (L / De)) * power(((A * A) / Wi) , 0.2);
  // clc Tc2 value
  TC2 := 0.0003 * (power((L * 1000) , 0.77) * power(y , -0.385));
  // clc Tc3 value
  TC3 := 0.057 * power(L , 0.8) * power(S + 1 , 0.7) / power(y , 0.5);
  // clc Tc value for TC (1 , 2 , 3)
  writeln(format('1.  TC1= %5.2f', [TC1]));
  writeln(format('2.  TC2= %5.2f', [TC2]));
  writeln(format('3.  TC3= %5.2f', [TC3]));
  write('Select TC from 1..3: ');
  readln(tcSelector);
  case tcSelector of
      1 : Tc := TC1;
      2 : Tc := TC2;
      3 : Tc := TC3
    else
      Tc := 0;
  end;
  // clc Tl value
  Tl := 0.6 * Tc;
  // clc D value
  D := 0.13 * Tc;
  // clc Tp value
  Tp := (D / 2) + Tl;
  // clc Qp value
  Qp := (0.2083 * A) / Tp;
  clcQp := Qp;
end;

function clcRe(Pi: real): real;
var
  reV: real;
begin
  reV := power(Pi - (0.2 * S * 25.4) , 2.0) / (Pi + (0.8 * S * 25.4));
  if (pi < (0.2 * S * 25.4)) then
    clcRe := 0
  else
    clcRe := reV;
end;

function clcV(Pi: real): real;
begin
  clcV := A * clcRe(Pi);
end;

procedure Create;
begin
  writeln('************************** S.C.S Caculator **************************');
  writeln('Version: 1.00 feb-2010');
  writeln('Programming: Ahmad daneshvar (ahmad.daneshvar@gmail.com)');
  writeln('*********************************************************************');
  writeln;
end;

procedure fileManager;
var
  sel: String;
begin
  writeln;
  writeln('>> To exit just press enter key');
  writeln('>> To save data to file enter, file name');
  writeln('>> To select watershed name as file name enter "*"');
  readln(sel);
  sel := trim(sel);
  if sel = '' then
    halt;
  if sel = '*' then
    sel := wName;
  writeToFile(sel);
  writeln('File successful saved as name "' + sel + '.txt". Enter key to exit...');
  readln;
  halt;
end;

procedure getInput;
var
  ind : integer;
begin
  write('Enter watershed name: ');
  readln(wName);
  write('Enter A value: ');
  readln(A);
  write('Enter Hmax: ');
  readln(Hmax);
  write('Enter Hmin: ');
  readln(Hmin);
  write('Enter L value: ');
  readln(L);
  write('Enter Cn value: ');
  readln(Cn);

  writeln('Now you must enter six value for P:');
  for ind := 1 to 6 do
  begin
    write('P[' + inttostr(ind) + ']: ');
    readln(P[ind]);
  end;

end;

procedure piPrinter;
var
  ind: Integer;
begin
  writeln;
  writeln('==========================[ Calculation ]============================');
  writeln;
  writeln('----------+---------+---------+---------+---------+---------+--------');
  writeln('TR(year)  |    2    |    5    |    10   |    25   |    50   |  100');
  writeln('----------+---------+---------+---------+---------+---------+--------');
  // print QMax valus;
  write(format('Qmax cms%2s',[' ']));
  for ind := 1 to 6 do
    write(format('| %7.2f ',[clcQMax(p[ind])]));
  writeln;
  // print V/1000 valus;
  write(format('V/1000 m3%1s',[' ']));
  for ind := 1 to 6 do
    write(format('| %7.2f ',[clcV(p[ind])]));
  writeln;
  // print RE valus;
  write(format('RE mm%5s',[' ']));
  for ind := 1 to 6 do
    write(format('| %7.2f ',[clcRe(p[ind])]));
  writeln;
  // print P valus;
  write(format('P mm%6s',[' ']));
  for ind := 1 to 6 do
    write(format('| %7.2f ',[p[ind]]));
  writeln;
  // print Coef valus;
  write(format('Coef %%%4s',[' ']));
  for ind := 1 to 6 do
    write(format('| %7.2f ',[clcCoef(p[ind])]));
  writeln;
  writeln('----------+---------+---------+---------+---------+---------+--------');
  writeln;
end;

procedure PrintValues;
begin
  writeln;
  writeln('Watershed name: ' + wName);
  writeln;
  writeln('============================[ Summery ]==============================');
  writeln(format('Watershed area:%15s A   = %5.2f km2',[' ', A]));
  writeln(format('Maximum hight:%16s Hmax= %5.2f m',[' ', Hmax]));
  writeln(format('Minimum hight:%16s Hmin= %5.2f m',[' ', Hmin]));
  writeln(format('Hydraulic length:%13s L   = %5.2f km',[' ', L]));
  writeln(format('Curve number:%17s CN  = %5.2f km2',[' ', Cn]));
  writeln(format('Time of consenteration:%7s TC  = %5.2f hrs',[' ', TC]));
  writeln(format('Time of peak discharge:%7s TP  = %5.2f hrs',[' ', TP]));
  writeln(format('Peak discharge for U.H:%7s Qp  = %5.2f cms',[' ', Qp]));
  piPrinter();
  tablePrinter();
end;

procedure tablePrinter;
var
  ind: Integer;
  Qre : real;
begin
  writeln('=======================[ Flood hydrograph ]==========================');
  writeln;
  writeln('-------+--------+--------+--------+--------+--------+--------+-------');
  writeln('t(hrs) |  U.H   |    2   |    5   |   10   |   25   |   50   |  100');
  writeln('-------+--------+--------+--------+--------+--------+--------+-------');
  for ind := 1 to 33 do
  begin
    Qre := Qp * qpArr[ind];
    writeln(format('%6.2f | %6.2f | %6.2f | %6.2f | %6.2f | %6.2f | %6.2f | %6.2f',
    [Tp * tpArr[ind], Qre, clcRe(p[1]) * Qre, clcRe(p[2]) * Qre,
    clcRe(p[3]) * Qre, clcRe(p[4]) * Qre, clcRe(p[5]) * Qre, clcRe(p[6]) * Qre]));
  end;
  writeln('-------+--------+--------+--------+--------+--------+--------+-------');
end;

procedure writeToFile(fileName: String);
var
  ind : Integer;
  Qre : real;
begin
  assign(fif , fileName + '.txt');
  reWrite(fif);
  // C: Part 1 ********************************************
  writeln(fif, 'Calculation hydrograph by S.C.S method');
  writeln(fif);
  writeln(fif, 'Watershed name: ' + wName);
  writeln(fif, '============================[ Summery ]==============================');
  writeln(fif, format('Watershed area:%15s A   = %5.2f km2',[' ', A]));
  writeln(fif, format('Maximum hight:%16s Hmax= %5.2f m',[' ', Hmax]));
  writeln(fif, format('Minimum hight:%16s Hmin= %5.2f m',[' ', Hmin]));
  writeln(fif, format('Hydraulic length:%13s L   = %5.2f km',[' ', L]));
  writeln(fif, format('Curve number:%17s CN  = %5.2f km2',[' ', Cn]));
  writeln(fif, format('Time of consenteration:%7s TC  = %5.2f hrs',[' ', TC]));
  writeln(fif, format('Time of peak discharge:%7s TP  = %5.2f hrs',[' ', TP]));
  writeln(fif, format('Peak discharge for U.H:%7s Qp  = %5.2f cms',[' ', Qp]));

  // C: Part 2 *********************************************
  writeln(fif);
  writeln(fif, '==========================[ Calculation ]============================');
  writeln(fif);
  writeln(fif, '----------+---------+---------+---------+---------+---------+--------');
  writeln(fif, 'TR(year)  |    2    |    5    |    10   |    25   |    50   |  100');
  writeln(fif, '----------+---------+---------+---------+---------+---------+--------');
  // print QMax valus;
  write(fif, format('Qmax cms%2s',[' ']));
  for ind := 1 to 6 do
    write(fif, format('| %7.2f ',[clcQMax(p[ind])]));
  writeln(fif);
  // print V/1000 valus;
  write(fif, format('V/1000 m3%1s',[' ']));
  for ind := 1 to 6 do
    write(fif, format('| %7.2f ',[clcV(p[ind])]));
  writeln(fif);
  // print RE valus;
  write(fif, format('RE mm%5s',[' ']));
  for ind := 1 to 6 do
    write(fif, format('| %7.2f ',[clcRe(p[ind])]));
  writeln(fif);
  // print P valus;
  write(fif, format('P mm%6s',[' ']));
  for ind := 1 to 6 do
    write(fif, format('| %7.2f ',[p[ind]]));
  writeln(fif);
  // print Coef valus;
  write(fif, format('Coef %%%4s',[' ']));
  for ind := 1 to 6 do
    write(fif, format('| %7.2f ',[clcCoef(p[ind])]));
  writeln(fif);
  writeln(fif, '----------+---------+---------+---------+---------+---------+--------');
  writeln(fif);

  // C: part 3 **************************************************************
  writeln(fif, '=======================[ Flood hydrograph ]==========================');
  writeln(fif);
  writeln(fif, '-------+--------+--------+--------+--------+--------+--------+-------');
  writeln(fif, 't(hrs) |  U.H   |    2   |    5   |   10   |   25   |   50   |  100');
  writeln(fif, '-------+--------+--------+--------+--------+--------+--------+-------');
  for ind := 1 to 33 do
  begin
    Qre := Qp * qpArr[ind];
    writeln(fif, format('%6.2f | %6.2f | %6.2f | %6.2f | %6.2f | %6.2f | %6.2f | %6.2f',
    [Tp * tpArr[ind], Qre, clcRe(p[1]) * Qre, clcRe(p[2]) * Qre,
    clcRe(p[3]) * Qre, clcRe(p[4]) * Qre, clcRe(p[5]) * Qre, clcRe(p[6]) * Qre]));
  end;
  writeln(fif, '-------+--------+--------+--------+--------+--------+--------+-------');
  close(fif);
end;

BEGIN
  Create;
  calculation();
  PrintValues();
  fileManager()
END.