MODULE C_CONSTS
PUBLIC
   REAL,    PARAMETER :: pi = 3.14159265,tpi=6.2831853,d2r=.01745329
   REAL,    PARAMETER :: e = 2.7182818284590455
   REAL,    PARAMETER :: expmin=-20.
   REAL,    PARAMETER :: tpiinv = 1. / tpi   
   REAL,    PARAMETER :: max15bit=32768.,max8bit=256.,max7bit=128.
   REAL,    PARAMETER :: max14bit=16384.
   INTEGER, PARAMETER :: i15bit=32768,i8bit=256,i7bit=128,i14bit=16384
   REAL,    PARAMETER :: g =  9.81          !gravity       : SI  (m/s^2)
   REAL,    PARAMETER :: rhow =  1026.      !water density : SI  (kg/m^3)
   REAL,    PARAMETER :: rhoa =  1.293      !air density (0 C): SI  (kg/m^3)
   REAL,    PARAMETER :: kappa = 0.4        !Von Karman's constant
   REAL,    PARAMETER :: nu  = 3.E-6        !Salt water kinematic viscosity
END MODULE C_CONSTS
!*********************************************************************
 !  -convert big_endian
PROGRAM make_buoylists
! this programs reads the buoy list compiled by ECMWF (J. Bidlot) and 
! generates the proper WW3 lines to be included in ww3_shel.inp or ww3_multi.inp
!*********************************************************************
USE C_CONSTS
USE C_CALDAT
USE C_DISPER
   IMPLICIT NONE

   INTEGER     I,IB,IE,J,II,i1,i2,Ntime,idp1,idp2,id1,id2,im1,im2,nread,nfiles
   INTEGER     np,npar,PIND,IFILE,found,IF02MAX
   INTEGER     iy,im,im0,id,ih,ih2,imin,isec,icode,icode2,icode3,iostat,finished
   INTEGER     iyo,imo,ido,iho
   INTEGER(KIND=4) Jdays,Jdayend,Jdayfile,Jday,Jdayo
   REAL, DIMENSION(:),   ALLOCATABLE :: lat,lon,depth,Hs,fp,f02,f0m1,U10,Udir,Curr,Currdir
   CHARACTER(120) path,filename,filei,name,string1,string2
   CHARACTER(120), DIMENSION(:),     ALLOCATABLE :: filenames
   CHARACTER(20) , DIMENSION(:),     ALLOCATABLE ::  buoyname
   CHARACTER(15) buoyname1
   CHARACTER(20) buoyname2
   CHARACTER(5) buoyshort,buoyshort2
   CHARACTER(5), DIMENSION(580) :: buoyshort_ECMWF
   REAL , DIMENSION(580) :: XE,YE
   INTEGER, DIMENSION(0:580):: PERIOD_TYPE

   INTEGER nECMWF
   CHARACTER space
   REAL m1,m2,a1p,a2p,b1p,b2p,Ep,a1tot,b1tot,FOUT
   REAL dtheta,latdeg,londeg,latmin,lonmin
   INTEGER nfband,nabin,ip,nchar
   INTEGER   NX,NY
   REAL    DX,DY,X0,Y0,XMAX,YMAX

   REAL Timezone_data,Time_zone_output
   
   LOGICAL         :: FILEW = .TRUE.
   CHARACTER*10, PARAMETER:: VERBPT = 'III  1.03 '
   CHARACTER*32, PARAMETER::                             &
                        IDSTR = 'WAVEWATCH III BOUNDARY DATA FILE'

   REAL , DIMENSION(:),   ALLOCATABLE :: XBPO,YBPO
   REAL , DIMENSION(:,:),   ALLOCATABLE :: RDBPO
   INTEGER, DIMENSION(:,:),   ALLOCATABLE :: IPBPO
   
   Timezone_data=0.

   path=''
  
   nfiles=3
!
! Reads ECMWF buoy list to check positions and get period type
!
   OPEN(101,FILE='ECMWF_buoylist.txt',status='old')
   ICODE=0
   I=0
   DO WHILE (icode.EQ.0) 
      I=I+1
      !READ(101,'(A5,F8.3,X,F8.3,X,I1)',iostat=ICODE)  &
      READ(101,*,iostat=ICODE)  &
      buoyshort_ECMWF(I), YE(I),   XE(I),  PERIOD_TYPE(I) 
   ENDDO
   CLOSE(101)
   nECMWF=I-1
   WRITE(6,*) 'ECMWF buoy list read, number of buoys:',nECMWF
   PERIOD_TYPE(0)=0
!
! loops on buoys
!
   OPEN(102,FILE='buoylist_fromECMWF.txt',status='unknown')
   DO IB=1,nECMWF
      WRITE(102,'(A5)') buoyshort_ECMWF(IB)
   ENDDO 
   CLOSE(102)
    
    OPEN(102,FILE='output_points_1.txt',status='unknown')
    OPEN(103,FILE='buoylist_GLOBAL1',status='unknown')
    DO I=1,nECMWF
            WRITE(102,'(2F9.3,X,A5)') XE(I), YE(I), buoyshort_ECMWF(I)
            WRITE(103,'(A5)') buoyshort_ECMWF(I)
      ENDDO 
   CLOSE(102)
   CLOSE(103)

    OPEN(102,FILE='all_buoys.grgp',status='unknown')
        WRITE(102,*)  '  1   Number of grid points'
        WRITE(102,*)  '  1  23.0386 187.4708   36  11.2343  -75 -44.5732'
        WRITE(102,*)  nECMWF
        DO I=1,nECMWF
   	  latdeg=FLOOR(YE(I))
	  IF (YE(I).LT.0) latdeg=latdeg+1
	  latmin=(YE(I)-latdeg)*60.
   	  londeg=FLOOR(XE(I))
	  IF (XE(I).LT.0) londeg=londeg+1
	  lonmin=(XE(I)-londeg)*60.
	  WRITE(102,'(A,4F9.2,A,A5)') '1  26.4636 192.0925  ', latdeg, latmin, londeg, lonmin,' 1 0.8  6  2  0 1.0 ', buoyshort_ECMWF(I)
      ENDDO 
   CLOSE(102)
   CLOSE(103)


    NX=269 
    NY=197
    DX=0.5
    DY=0.5 
    X0=-98.0 
    Y0=-28.0 
    
    XMAX=X0+DX*(NX-1)
    YMAX=Y0+DY*(NY-1)
    OPEN(102,FILE='z_shelmidATN2',status='unknown')
    OPEN(103,FILE='buoylist_ATN2',status='unknown')
    DO I=1,nECMWF
      IF (XE(I).GE.(X0+DX).AND.XE(I).LE.(XMAX-DX) &
        .AND.YE(I).GE.(Y0+DY).AND.YE(I).LE.(YMAX-DY)) THEN 
            IF (buoyshort_ECMWF(I).EQ.'62095') THEN 
               XE(I)=-15.92467
               YE(I)=53.05633
               ENDIF
            IF (buoyshort_ECMWF(I).EQ.'TFSTD') THEN 
               XE(I)=-23.8 
               YE(I)=66.7
               ENDIF 
            WRITE(102,'(2F9.3,X,A5)') XE(I), YE(I), buoyshort_ECMWF(I)
            WRITE(103,'(A5)') buoyshort_ECMWF(I)
        END IF
      ENDDO 
   CLOSE(102)
   CLOSE(103)

    NX=151
    NY=91
    DX=0.1
    DY=0.1 
    X0=-11.0
    Y0=43.0 
    
    XMAX=X0+DX*(NX-1)
    YMAX=Y0+DY*(NY-1)
    OPEN(102,FILE='z_shelmidGAS2',status='unknown')
    OPEN(103,FILE='buoylist_GAS2',status='unknown')
    DO I=1,nECMWF
      IF (XE(I).GE.(X0+DX).AND.XE(I).LE.(XMAX-DX) &
        .AND.YE(I).GE.(Y0+DY).AND.YE(I).LE.(YMAX-DY)) THEN 
            WRITE(102,'(2F9.3,X,A5)') XE(I), YE(I), buoyshort_ECMWF(I)
            WRITE(103,'(A5)') buoyshort_ECMWF(I)
        END IF
      ENDDO 
   CLOSE(102)
   CLOSE(103)

    NX=426
    NY=161
    DX=0.1
    DY=0.1 
    X0=-6.0
    Y0=30.0 
    
    XMAX=X0+DX*(NX-1)
    YMAX=Y0+DY*(NY-1)
    OPEN(102,FILE='z_shelmidMED2',status='unknown')
    OPEN(103,FILE='buoylist_MED2',status='unknown')
    DO I=1,nECMWF
      IF (XE(I).GE.(X0+DX).AND.XE(I).LE.(XMAX-DX) &
        .AND.YE(I).GE.(Y0+DY).AND.YE(I).LE.(YMAX-DY)) THEN 
            WRITE(102,'(2F9.3,X,A5)') XE(I), YE(I), buoyshort_ECMWF(I)
            WRITE(103,'(A5)') buoyshort_ECMWF(I)
        END IF
      ENDDO 
   CLOSE(102)
   CLOSE(103)


END
  
