program moc

   implicit none
   
   integer, parameter                  :: ND=116, HH=4, NVARp=16, NVARo=2, PER=21
   integer                             :: i, j, nv, a, b, itemp, ith=6
   integer, dimension(NVARp)           :: icorrel

   real                                :: mediax, mediay, varx, vary, rth=0.4
   real                                :: covarxy, temp
   real, dimension(ND)                 :: x, y, ivobs
   real, dimension(NVARp)              :: correlxy, correlord
   real, dimension(ND, NVARp)          :: prev
   real, dimension(ND, NVARo)          :: obs

   character(len=5), dimension(NVARp)  :: varp
   character(len=5), dimension(NVARo)  :: varo
   
   

!  Input data: previsao -----------------------------------------------   
   open(10, file='../datain/previsao.txt')

   do j=1, NVARp-1
      read(10, *) varp(j), (prev(i, j), i=1, ND)
      icorrel(j)=j
   enddo
   prev(:, NVARp)=sqrt(prev(:, 1)**2 + prev(:, 2)**2)
   varp(NVARp)="intv"
   icorrel(NVARp)=NVARp
  
   
!  observacao: -------------------------------------------------------   
   open(11, file='../datain/obs.txt')
   
   do j=1, NVARo
      read(11, *) varo(j), (obs(i, j), i=1, ND)
   enddo
   ivobs=sqrt(obs(:, 1)**2 + obs(:, 2)**2)
      

!--------------------------------------------------------------------------------------------
!  Esquema de armazenamento das variaveis:
!  
!  prev(ND, NVARp) ::  ND    :: numero de horarios dos dias corridos
!                          NVARp :: numero de variaveis: 1->10u, 2->10v, ..., 15->ultimavar
!
!--------------------------------------------------------------------------------------------


!--------------------------------------------------------------------------------------------
!  1a tentativa: calcular u e v separadamente 
!  2a          : calcular o vento u;v
!XX3a          : calcular para os horarios separados: 00, 00, ...  deu ruim (correl baixa)
!  4a          : calcular para os horarios corridos: 00, 06, 12, ...


!  X :: vars independentes y(x)=x1...  previsoes
!  Y :: var dependente                 obs 
!--------------------------------------------------------------------------------------------

  correlxy=0.0
  do nv=1, NVARp

     y=ivobs
     x=prev(:, nv)



!     -------------------------
!     media:   
!
      mediax = sum(x)/ND
      mediay = sum(y)/ND





!     -------------------------
!     variancia:   
!
      varx = sum((x - mediax) * (x - mediax))
      vary = sum((y - mediay) * (y - mediay))




!     -------------------------
!     covariancia:   
!
      covarxy = sum((x - mediax) * (y - mediay))




!     -------------------------
!     correlacao:   
!
      correlxy(nv)=covarxy/sqrt(varx)/sqrt(vary)
   

!      open(20, file='../dataout/serie.bin', access='direct', form='unformatted', status='unknown', recl=2*4)
!
!      do i=1, ND
!         print*, i, y(i), x(i)
!         write(20, rec=i) prev(i, 1), obs(i, 1)
!      enddo
   
   
   enddo   
   
      print*,''
   
!  Ordenando o vetor de correlacoes:
   correlord=abs(correlxy)
   
!   do i=1, NVARp
!      print*, i, correlxy(i), correlord(i), icorrel(i)
!   enddo

   do a=1, NVARp
      do b=a, NVARp
         if (correlord(a) <= correlord(b))then
            temp=correlord(a)
            itemp=icorrel(a)

            correlord(a)=correlord(b)
            icorrel(a)=icorrel(b)

            correlord(b)=temp
            icorrel(b)=itemp
         endif
      enddo
   enddo 
  
   do i=1, NVARp
      print*, i, correlxy(i), correlord(i), icorrel(i), varp(icorrel(i))
   enddo

      
   

end program moc
