unit uedf2xlt;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,
  uauxiliares;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure procesar( archi: string );
var
  ent, sal: textfile;
  r: string;
  v: double;
  archi_sal: string;
  i, ires: integer;
  anio, semana, estadoh, estadov: integer;

  buffsal: string;
  muda: string;

  ih: integer;

begin

  // busco el último punto
  ires:= 0;
  for i:= length( archi ) downto 1 do
  begin
    if archi[i]='.' then
    begin
      ires:= i;
      break;
    end;
  end;

  if ires > 0 then
    archi_sal:= copy( archi, 1, ires )+'xlt'
  else
    archi_sal:= archi+'.xlt';

  assignfile( ent, archi );
  reset( ent );
  assignfile( sal, archi_sal );
  rewrite( sal );

  while not eof( ent ) do
  begin
    readln( ent, r );
    r:= trim ( r );
    if (pos('ANNEE', r )=1) or ( pos('AÑO', r )= 1) then
    begin
      readln( ent, r );
      anio:= nextint( r );
    end
    else if (pos('PAS DE TEMPS', r) = 1 )
      or ((pos('Semana', r) = 1 )and (pos('Semanas', r) = 0 )) then
    begin
      if (pos('PAS DE TEMPS', r) = 1 ) then
      begin
        muda:= nextpal( r );
        muda:= nextpal( r );
      end;
      muda:= nextpal( r );
      muda:= nextpal( r );
      if muda <> '' then
      begin
        semana:= nextint( r );
        buffsal:= IntToStr(anio)+#9+IntToStr(semana);
        readln( ent, r );  // linea en blanco
        for ih := 1 to 5 do
        begin
          readln( ent, r );
          if (pos('CLASSES', r ) = 0) and ( pos('Clase', r ) = 0)  then
            raise Exception.Create('Error, se esperaba CLASSES' );
          if pos('CLASSES', r )<>0 then
          begin
            muda:= nextpal( r );
            muda:= nextpal( r );
            muda:= nextpal( r );
          end;
          muda:= nextpal( r );

          estadoh:= nextint( r );
          if estadoh <> ih then
            raise Exception.Create('Se desfasó el estado H, estadoh:'+IntToStr(estadoh) );

          readln( ent, r );
          for I := 1 to 7 do
          begin
            v:= nextfloat( r )*1.0E6;
            buffsal:= buffsal+#9+FloatToStrF(v, ffFixed, 12, 1 );
          end;
          readln( ent, r );
          for I := 1 to 1 do
          begin
            v:= nextfloat( r )*1.0E6;
            buffsal:= buffsal+#9+FloatToStrF(v, ffFixed, 12, 1 );
          end;

        end;
        writeln( sal, buffsal );
      end;
    end;

  end;
  closefile( sal );
  closefile( ent );

end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin

    procesar( OpenDialog1.Filename );
  end;
// procesar( 'C:\simsee\corridas\InfluCVR_EstudioConUTE\EDF_cBase_SInMinimosTEcnicos\vaguaOP000159.d' );
// procesar( 'C:\simsee\corridas\InfluCVR_EstudioConUTE\EDF_c1_CasoBase\vaguaOP000159.d' );

end;

end.
