unit Inputdat;

interface

uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls, Buttons,
  StdCtrls, ExtCtrls, DBCtrls, DB, DBTables, Mask,SysUtils, Dialogs;


type
  TEditDatTerr = class(TForm)
    OKBtn: TBitBtn;
    Bevel1: TBevel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    ComboBox1: TComboBox;
    Ano: TMaskEdit;
    LongI: TMaskEdit;
    LatI: TMaskEdit;
    DepthI: TMaskEdit;
    MagI: TMaskEdit;
    MaxI: TMaskEdit;
    Label11: TLabel;
    Mes: TMaskEdit;
    Label12: TLabel;
    Dia: TMaskEdit;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    LongM: TMaskEdit;
    LatM: TMaskEdit;
    profm: TMaskEdit;
    MagM: TMaskEdit;
    Label9: TLabel;
    Label17: TLabel;
    Hour: TMaskEdit;
    Minutes: TMaskEdit;
    Seconds: TMaskEdit;
    Label8: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    coefa: TMaskEdit;
    coefc: TMaskEdit;
    coefb: TMaskEdit;
    coefd: TMaskEdit;
    azim: TMaskEdit;
    ratioab: TMaskEdit;
    ComboBox2: TComboBox;
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure OKBtnClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


type
 TEfecto = array[1..8] of integer;
 TEfectoNatura = array[1..19] of integer;
 TClaseVul = array[1..6,1..5] of integer;

const
  inicial: TEfecto =(0,0,0,0,0,0,0,0);
  initNatura: TEfectoNatura = (0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,0);
  initClaseVul: TClaseVul = ((0,0,0,0,0),(0,0,0,0,0),(0,0,0,0,0),
                          (0,0,0,0,0),(0,0,0,0,0),(0,0,0,0,0));


 procedure getfirstcad(var cad:string; var firstcad:string);
 procedure removespace(var cad:string);
 procedure fillspace(var cad:string);

 type
    Myplace=record
       code:string[40];
       Longitud,Latitud:real;
       intensidad:real;
       efecto:TEfecto;
       natura:TEfectoNatura;
       ClaseVul:TClaseVul;
    end;

 type
    places=record
       Name:string[40];
       Longitud,Latitud:real;
    end;

type
  TDatosTerr = record
               ano:word;
               mes,dia:integer;
               seconds:    Real;
               hour,minutes:integer;
               Longitud:Real;
               Latitud: Real;
               H:       Real;
               Magnitud:Real;
               Long_M:Real;
               Lat_M:Real;
               H_M:real;
               Mag_M:Real;
               intensidad:integer;
               CodZona: String[20];
               a_b:real;
               a,b,c,d:real;
               azimut:integer;
               Direccion:string[1];
               existe:Boolean;
               end;

var
  nsite:integer; 
  EditDatTerr: TEditDatTerr;
  DatosTerr: TDatosTerr;
  syspath,Camino,scountry:string;
  filemap,masterlist,selectedlist:string[50];
  SelSite:array[1..1000] of MyPlace;
  EfecHuman: array[1..1000] of MyPlace;
  InpSite:array[1..50] of Places;
  listplaces:array[1..10000] of Places;
  MaxSelSite,NinputSite:integer;
  namepost,sdir:string;
  indexsite,indeximage:integer;
  ImageList:array[1..100] of string;
  NumberImage:integer;
  closewin:boolean;


implementation

{$R *.DFM}

procedure removespace(var cad:string);
var
i:integer;
begin
i:=length(cad);
while (cad[i]=' ') and (i>0) do
 i:=i-1;
if i>0 then
cad:=copy(cad,1,i)
else
 cad:='0';
end;

procedure fillspace(var cad:string);
var
i:integer;
begin
i:=length(cad);
while (i>0) and (cad[i]<>'-') do
 begin
 if cad[i]=' ' then cad[i]:='0';
 i:=i-1;
 end;
end;

procedure getfirstcad(var cad:string; var firstcad:string);
var
i:integer;
cad1:string;
 begin
  i:=1;
    if length(cad)>0 then
    while (i<=length(cad)) and (cad[i]=' ') or (ord(cad[i])=9) do
      i:=i+1;
        cad1:=Copy(cad,i,length(cad)-i+1);
        i:=1;
     if length(cad1)>0 then
      while (i<=length(cad1)) and (cad1[i]<>' ') and (ord(cad1[i])<>9) do
         i:=i+1;
        firstcad:=Copy(cad1,1,i-1);
        cad := Copy(cad1,i,length(cad1)-i+1);
  end;

procedure TEditDatTerr.FormCreate(Sender: TObject);
var
mes,dia:word;
n,i,cod,j,k:integer;
cad,S,name,cad1:string;
F1:TextFile;
begin
closewin:=false;
DecodeDate(Date,DatosTerr.ano,mes,dia);
DatosTerr.mes:=mes;
DatosTerr.dia:=dia;
DatosTerr.direccion:='A';
DatosTerr.azimut:=0;
DatosTerr.a := 1.5;
DatosTerr.b := -2.63;
DatosTerr.c := -0.0087;
DatosTerr.d := 2.5;
DatosTerr.A_B := 1;
if ParamCount>=1 then
 begin
    i:=1;
    n:=0;
    getdir(0,sdir);
    AssignFile(F1, ParamStr(1));
      {$I-}
    Reset(F1);
  {$I+}
    if IOResult <> 0 then
      MessageDlg('File '+ ParamStr(1) +' not found', mtWarning, [mbOk], 0)
     else
    begin 
    while not Eof(F1) do
      begin
        Readln(F1, cad);
        if ((length(cad)<80) or (cad[80]='1') or
           (cad[80]=' ')) and (i=1) then
        begin
         S := Copy(cad, 2, 4);	{year}
         val(S,DatosTerr.ano,cod);
         S := Copy(cad, 7, 2); {month}
         val(S,DatosTerr.mes,cod);
         S := Copy(cad, 9, 2); {day}
         val(S,DatosTerr.dia,cod);
         S := Copy(cad, 12, 2); {Hour}
         val(S,DatosTerr.Hour,cod);
         S := Copy(cad, 14, 2); {Minutes}
         val(S,DatosTerr.minutes,cod);
         S := Copy(cad, 17, 4);	{Seconds}
         val(S,DatosTerr.seconds,cod);
         S := Copy(cad, 24, 7);	{latitude}
         val(S,DatosTerr.latitud,cod);
         S := Copy(cad, 31, 8); {longitud}
         val(S,DatosTerr.longitud,cod);
         S := Copy(cad,39, 5); {depth}
         val(S,DatosTerr.H,cod);
         S := Copy(cad, 57, 3); {mag}
         val(S,DatosTerr.magnitud,cod);
        end else
        if cad[80]='2' then
        begin
         S := Copy(cad, 34, 6); {Macro latitude}
         val(S,DatosTerr.lat_M,cod);
         S := Copy(cad, 41, 7); {Macro longitud}
         val(S,DatosTerr.long_M,cod);
         S := Copy(cad,28, 2); {Intensity}
         val(S,DatosTerr.Intensidad,cod);
         S := Copy(cad, 49, 3); {macro mag}
         val(S,DatosTerr.mag_M,cod);
        end else
        if cad[79]='M' then
        begin
          DatosTerr.CodZona:=copy(cad,6,8);
          DatosTerr.direccion:=copy(cad,15,1);
          S := Copy(cad,20, 4);
          val(S,DatosTerr.A_B,cod);
          S := Copy(cad,28, 3);
          val(S,DatosTerr.azimut,cod);
          S := Copy(cad,34, 4);
          Val(S,DatosTerr.a,cod);
          S := Copy(cad,41, 5);
          val(S,DatosTerr.b,cod);
          S := Copy(cad,49, 7);
          val(S,DatosTerr.c,cod);
          S := Copy(cad,59, 3);
          val(S,DatosTerr.d,cod);
          S := Copy(cad,63, 5);
          val(S,DatosTerr.H_M,cod);
        end else
        if cad[79]='S' then
        begin
         n:=n+1;
        SelSite[n].code:=copy(cad,6,40);
        S := Copy(cad,13+34, 7);
        val(S,SelSite[n].latitud,cod);
        S := Copy(cad,21+34,8);
        val(S,SelSite[n].longitud,cod);
        S := Copy(cad,30+34, 4);
        val(S,SelSite[n].intensidad,cod);
       end else
      if cad[79]='O' then
        begin
        for j:=1 to 8 do
         SelSite[n].efecto[j]:=ord(cad[12+j])-48;
        for j:=1 to 19 do
         SelSite[n].Natura[j]:=ord(cad[12+j+8])-48;
        for k:=1 to 6 do
         for j:=1 to 5 do
          SelSite[n].ClaseVul[k,j]:=ord(cad[12+j+5*(k-1)+27])-48;
          DatosTerr.existe:=True;
       end else
       if cad[79]='P' then
        begin
         NumberImage:=NumberImage+1;
         cad1:=copy(cad,6,72);
         getfirstcad(cad1,ImageList[NumberImage]);
        end;
        i:=i+1
      end;
      if (datosterr.long_m=0) and (datosterr.lat_m=0) then
      begin
       datosterr.long_m:=datosterr.longitud;
       datosterr.lat_m:=datosterr.latitud;
      end;
      if (datosterr.mag_m = 0) then
       datosterr.mag_m := datosterr.magnitud; 
      Maxselsite:=n;
      for i:=1 to MaxSelSite do
       begin
        EfecHuman[i].code := SelSite[i].code;
        EfecHuman[i].latitud := SelSite[i].latitud;
        EfecHuman[i].longitud := SelSite[i].longitud;
        EfecHuman[i].efecto := SelSite[i].efecto;
        EfecHuman[i].natura := SelSite[i].natura;
        EfecHuman[i].ClaseVul := SelSite[i].ClaseVul;
        EfecHuman[i].intensidad := SelSite[i].intensidad;
       end;
    CloseFile(F1);
  end;
  end;
   S := syspath+'dat\isomodel.par';
  AssignFile(F1, S);
  {$I-}
    Reset(F1);
  {$I+}
    if IOResult <> 0 then
      MessageDlg('File '+ S +' not found', mtWarning, [mbOk], 0)
     else
     begin
      Readln(F1, cad);
    while not Eof(F1) do
      begin
        Readln(F1, cad);
        getfirstcad(cad,name);
        ComboBox1.items.add(name);
      end;
      CloseFile(F1);
      end;
    end;

procedure TEditDatTerr.OKBtnClick(Sender: TObject);
var
cad:string;
cod:integer;
begin
  cad:=DepthI.Text;
  val(cad,DatosTerr.H,cod);
  cad:=MagI.Text;
  val(cad,DatosTerr.Magnitud,cod);
  cad:=ProfM.Text;
  val(cad,DatosTerr.H_M,cod);
  cad:=MagM.Text;
  val(cad,DatosTerr.Mag_M,cod);
  cad:=MaxI.Text;
  val(cad,DatosTerr.intensidad,cod);
  cad:=LongI.Text;
  val(cad,DatosTerr.Longitud,cod);
  cad:=LongM.Text;
  val(cad,DatosTerr.Long_M,cod);
  cad:=LatI.Text;
  val(cad,DatosTerr.Latitud,cod);
  cad:=LatM.Text;
  val(cad,DatosTerr.Lat_M,cod);
  cad:=ano.Text;
  fillspace(cad);
  val(cad,DatosTerr.ano,cod);
  cad:=mes.text;
  fillspace(cad);
  val(cad,DatosTerr.mes,cod);
  cad:=dia.text;
  fillspace(cad);
  val(cad,DatosTerr.dia,cod);
  cad:=Hour.Text;
  fillspace(cad);
  val(cad,DatosTerr.hour,cod);
  cad:=Minutes.Text;
  fillspace(cad);
  val(cad,DatosTerr.minutes,cod);
  cad:=Seconds.Text;
  fillspace(cad);
  val(cad,DatosTerr.Seconds,cod);
  DatosTerr.CodZona:=ComboBox1.Text;
  DatosTerr.Direccion:=ComboBox2.Text;
  cad:=azim.text;
  val(cad,DatosTerr.azimut,cod);
  cad:=ratioab.text;
  val(cad,DatosTerr.a_b,cod);
  cad:=coefa.text;
  val(cad,DatosTerr.A,cod);
  cad:=coefb.text;
  val(cad,DatosTerr.B,cod);
  cad:=coefc.text;
  val(cad,DatosTerr.C,cod);
  cad:=coefd.text;
  val(cad,DatosTerr.D,cod);
  close;
  end;

procedure TEditDatTerr.FormActivate(Sender: TObject);
var
cad:string;
begin
 ano.Text:=IntToStr(DatosTerr.ano);
 mes.Text:=IntToStr(DatosTerr.mes);
 dia.Text:=IntToStr(DatosTerr.dia);
 Hour.Text:=IntToStr(DatosTerr.hour);
 Minutes.Text:=IntToStr(DatosTerr.minutes);
 Str(DatosTerr.Seconds:5:2,cad);
 Seconds.Text:=cad;
 Str(DatosTerr.Longitud:8:3,cad);
 LongI.Text:=cad;
 Str(DatosTerr.Latitud:7:3,cad);
 LatI.Text:=cad;
 Str(DatosTerr.H:5:1,cad);
 DepthI.Text:=cad;
 Str(DatosTerr.Magnitud:3:1,cad);
 MagI.Text:=cad;
 Str(DatosTerr.Long_M:7:2,cad);
 LongM.Text:=cad;
 Str(DatosTerr.Lat_M:6:2,cad);
 LatM.Text:=cad;
 Str(DatosTerr.H_M:5:1,cad);
 ProfM.Text:=cad;
 Str(DatosTerr.Mag_M:3:1,cad);
 MagM.Text:=cad;
 MaxI.Text:=IntToStr(DatosTerr.intensidad);
 ComboBox1.Text:=DatosTerr.CodZona;
 ComboBox2.Text:=DatosTerr.Direccion;
 Str(DatosTerr.azimut:3,cad);
 azim.text:=cad;
 Str(DatosTerr.a_b:4:2,cad);
 ratioab.text:=cad;
 Str(DatosTerr.A:6:4,cad);
 coefa.text:=cad;
 Str(DatosTerr.B:6:3,cad);
 coefb.text:=cad;
 Str(DatosTerr.C:7:4,cad);
 coefc.text:=cad;
 Str(DatosTerr.D:6:4,cad);
 coefd.text:=cad;
end;

procedure TEditDatTerr.ComboBox1Change(Sender: TObject);
var
code:integer;
F1:TextFile;
name,cad1,cad,dp,filename:string;
ca,cb,cc,cd,ratio:real;
azimuth:integer;
begin
filename := syspath+'dat\isomodel.par';
 AssignFile(F1, filename);
 {$I-}
    Reset(F1);
 {$I+}
    if IOResult <> 0 then
      MessageDlg('File '+filename+' not found', mtWarning, [mbOk], 0)
     else
     begin
      Readln(F1, cad);
    while not Eof(F1) do
      begin
        Readln(F1, cad);
        getfirstcad(cad,name);
        getfirstcad(cad,cad1);
        val(cad1,ca,code);
        getfirstcad(cad,cad1);
        val(cad1,cb,code);
        getfirstcad(cad,cad1);
        val(cad1,cc,code);
        getfirstcad(cad,cad1);
        val(cad1,cd,code);
        getfirstcad(cad,cad1);
        val(cad1,ratio,code);
        getfirstcad(cad,cad1);
        val(cad1,azimuth,code);
        getfirstcad(cad,dp);
   if name=ComboBox1.Text then
    begin
     Str(azimuth:3,cad);
     azim.text:=cad;
     Str(ratio:4:2,cad);
     ratioab.text:=cad;
     Str(ca:6:4,cad);
     coefa.text:=cad;
     Str(cb:6:3,cad);
     coefb.text:=cad;
     Str(cc:7:4,cad);
     coefc.text:=cad;
     Str(cd:6:4,cad);
     coefd.text:=cad;
     ComboBox2.Text:=dp;
     break;
    end;
  end;
  CloseFile(F1);
  end;
end;

procedure TEditDatTerr.BitBtn1Click(Sender: TObject);
begin
close;
end;

end.
