#include "w3macros.h"
!/ ------------------------------------------------------------------- /
      PROGRAM W3OUNP
!/
!/                  +-----------------------------------+
!/                  | WAVEWATCH III           NOAA/NCEP |
!/                  |           F. Ardhuin              |
!/                  |           M. Accensi              |
!/                  |                        FORTRAN 90 |
!/                  | Last update :         18-Jun-2014 |
!/                  +-----------------------------------+
!/
!/    14-Jan-1999 : Final FORTRAN 77                    ( version 1.18 )
!/    21-Jan-2000 : Upgrade to FORTRAN 90               ( version 2.00 )
!/    14-Feb-2000 : Exact nonlinear interactions        ( version 2.01 )
!/    09-Jan-2001 : U* bug fix in tabular output        ( version 2.05 )
!/    25-Jan-2001 : Flat grid version                   ( version 2.06 )
!/    02-Feb-2001 : Xnl version 3.0                     ( version 2.07 )
!/    11-Jun-2001 : Clean up                            ( version 2.11 )
!/    11-Oct-2001 : Clean up, X*, Y* in tables          ( version 2.14 )
!/    13-Nov-2002 : Add stress vector                   ( version 3.00 )
!/    27-Nov-2002 : First version of VDIA and MDIA      ( version 3.01 )
!/    24-Dec-2004 : Multiple grid version.              ( version 3.06 )
!/    17-Apr-2006 : Filter for directional spread.      ( version 3.09 )
!/    23-Jun-2006 : Linear input added.                 ( version 3.09 )
!/    28-Jun-2006 : Adding file name preamble.          ( version 3.09 )
!/    03-Jul-2006 : Separate flux modules.              ( version 3.09 )
!/    28-Oct-2006 : Add partitioning option.            ( version 3.10 )
!/    24-Mar-2007 : Add pars for entire spectrum.       ( version 3.11 )
!/    25-Apr-2007 : Battjes-Janssen Sdb added.          ( version 3.11 )
!/                  (J. H. Alves)
!/    08-Aug-2007 : Creation of buoy log file added     ( version 3.12 )
!/                  (switch O14 -- A. Chawla)
!/    09-Oct-2007 : WAM 4+ Sin and Sds added.           ( version 3.13 )
!/                  (F. Ardhuin)
!/    09-Oct-2007 : Experimental Sbs (BS1) added.       ( version 3.13 )
!/                  (F. Ardhuin)
!/    09-Apr-2008 : Adding an additional output for     ( version 3.12 )
!/                  WMO standard (A. Chawla)
!/    29-Apr-2008 : Adjust format partition output.     ( version 3.14 )
!/    29-May-2009 : Preparing distribution version.     ( version 3.14 )
!/    30-Oct-2009 : Implement run-time grid selection.  ( version 3.14 )
!/                  (W. E. Rogers & T. J. Campbell, NRL)
!/    24-Mar-2011 : Adaptation to NetCDF (M. Accensi)   ( version 4.04 )
!/    16-Jul-2011 : NC3 / NC4 switch     (M. Accensi)   ( version 4.05 )
!/    14-Mar-2013 : Writing optimization (M. Accensi)   ( version 4.09 )
!/    04-Jun-2014 : Correct bug TOGETHER (M. Accensi)   ( version 5.00 )
!/    04-Jun-2014 : Update use of date   (M. Accensi)   ( version 5.00 )
!/    13-Jun-2014 : Dimension order opt. (M. Accensi)   ( version 5.00 )
!/    18-Jun-2014 : add mpi implementat. (M. Accensi)   ( version 5.00 )
!/    27-Aug-2015 : Sice add as additional output       ( version 5.10 )
!/                  (in source terms)
!/
!/    Copyright 2009 National Weather Service (NWS),
!/       National Oceanic and Atmospheric Administration.  All rights
!/       reserved.  WAVEWATCH III is a trademark of the NWS. 
!/       No unauthorized use without permission.
!/
!  1. Purpose :
!
!     Post-processing of point output.
!
!  2. Method :
!
!     Data is read from the grid output file out_pnt.ww3 (raw data)
!     and from the file ww3_ounp.inp ( NDSI, output requests ).
!     Model definition and raw data files are read using WAVEWATCH III
!     subroutines.
!
!
!  3. Parameters :
!
!  4. Subroutines used :
!
!      Name      Type  Module   Description
!     ----------------------------------------------------------------
!      W3NMOD    Subr. W3GDATMD Set number of model.
!      W3SETG    Subr.   Id.    Point to selected model.
!      W3NDAT    Subr. W3WDATMD Set number of model for wave data.
!      W3SETW    Subr.   Id.    Point to selected model for wave data.
!      W3NAUX    Subr. W3ADATMD Set number of model for aux data.
!      W3SETA    Subr.   Id.    Point to selected model for aux data.
!      W3NOUT    Subr. W3ODATMD Set number of model for output.
!      W3SETO    Subr.   Id.    Point to selected model for output.
!      ITRACE    Subr. W3SERVMD Subroutine tracing initialization.
!      STRACE    Subr.   Id.    Subroutine tracing.
!      NEXTLN    Subr.   Id.    Get next line from input filw
!      EXTCDE    Subr.   Id.    Abort program as graceful as possible.
!      STME21    Subr. W3TIMEMD Convert time to string.
!      TICK21    Subr.   Id.    Advance time.
!      DSEC21    Func.   Id.    Difference between times.
!      W3IOGR    Subr. W3IOGRMD Reading/writing model definition file.
!      W3IOPO    Subr. W3IOPOMD Reading/writing raw point output file.
!      W3EXNC    Subr. Internal Execute point output.
!     ----------------------------------------------------------------
!
!  5. Called by :
!
!     None, stand-alone program.
!
!  6. Error messages :
!
!     Checks on input, checks in W3IOxx.
!
!  7. Remarks :
!
!     - Tables written to file 'tabNN.ww3', where NN is the
!       unit umber (NDSTAB).
!     - Transfer file written to ww3.yymmddhh.spc with multiple
!       spectra and times in file. yymmddhh relates to first
!       output (NDSTAB).
!     - !/IC1 !/IC2 !/IC3 are not included in dissipation term
!       FIXME: ICE is a dummy variable at the moment
!              Include ice parameters in point output file out_pnt.ww3
!              Ice coupling to SIN, SDS and SIC similar to w3srcemd.ftn
!
!  8. Structure :
!
!     See source code.
!
!     TOUT is the time defined in the input file
!     TIME is the time read from the out_pnt.ww3 file
!     DTREQ is the stride used for the time steps
!     at the beginning, if TOUT is after TIME, the program will read 
!     out_pnt.ww3 DTREQ by DTREQ until TIME is equal to TOUT
!     /!\ if DTREQ is too big, it's possible to never have TIME=TOUT /!\
!
!     PASTDATE is the date of the last time step
!     DATE is the date of the current time step
!     IOUT is the counter of time iteration of a same file
!
!     MFL is the number of stations processed in the 'time' loop
!     NOPTS is the total number of stations defined in out_pnt.ww3
!     NFL is the number of bunch of MFL stations to loop on to 
!     process all the NOPTS stations
!     NREQ is the number of valid stations to process, unvalid stations
!     are duplicata or stations not specified in the input file
!
!  9. Switches :
!
!       !/S    Enable subroutine tracing.
!
!       !/NCO  NCEP NCO modifications for operational implementation.
!
!       !/O14  Buoy log file generation.
!
! 10. Source code :
!
!/ ------------------------------------------------------------------- /
      USE CONSTANTS
!/
!     USE W3GDATMD, ONLY: W3NMOD, W3SETG
      USE W3WDATMD, ONLY: W3SETW, W3NDAT
!/NL1      USE W3ADATMD, ONLY: W3SETA, W3NAUX
      USE W3ODATMD, ONLY: W3SETO, W3NOUT
      USE W3ODATMD, ONLY: IAPROC, NAPROC, NAPERR, NAPOUT
      USE W3IOGRMD, ONLY: W3IOGR
      USE W3IOPOMD, ONLY: W3IOPO
      USE W3SERVMD, ONLY : ITRACE, NEXTLN, EXTCDE
!/S      USE W3SERVMD, ONLY : STRACE
      USE W3TIMEMD, ONLY: STME21, TICK21, DSEC21, julday
!/
      USE W3GDATMD
      USE W3WDATMD, ONLY: TIME
      USE W3ODATMD, ONLY: NDSE, NDST, NDSO, NOPTS, PTLOC, PTNME,     &
                          DPO, WAO, WDO, ASO, CAO, CDO, SPCO, FNMPRE,&
                          IPASS => IPASS2, ICEFO, ICEO, ICEHO
!/SETUP      USE W3ODATMD, ONLY: ZET_SETO
!
!/O14      USE W3ODATMD, ONLY: GRDID
!
      USE NETCDF
!
      IMPLICIT NONE
!
!/MPI      INCLUDE "mpif.h"
!/
!/ ------------------------------------------------------------------- /
!/ Local parameters
!/
!/MPI      INTEGER                 :: IERR_MPI
      INTEGER                 :: NDSI, NDSM, NDSOP,  NDSTRC, NTRACE,  &
                                 IERR, I, TOUT(2), NOUT, TDUM(2),     &
                                 NREQ, ITYPE, OTYPE, IPOINT,          &
                                 IOTEST, ITH, IOUT, J, DIMXP,         &
                                 icode, strl, strl2, FLWW3, NBFILEOUT,&
                                 S5, S3, NBSTATION, NCTYPE,   &
                                 NCFLUSH, NFL, MFL, IFL, NREQL, NOUTL,&
                                 TOUTL(2), NDSEN, one, two
      INTEGER                 :: ISCALE = 0
!/O14      INTEGER                 :: NDBO
!/S      INTEGER, SAVE           :: IENT   = 0
!/NCO      INTEGER                 :: NDSTAB
      REAL                    :: DTREQ, SCALE1, SCALE2, DTEST
      REAL                    :: M2KM
      REAL, ALLOCATABLE       :: XPART(:,:)
      INTEGER, ALLOCATABLE    :: INDREQ(:), INDREQTMP(:)
      LOGICAL                 :: FLFORM, FLSRCE(7)
      LOGICAL, ALLOCATABLE    :: FLREQ(:)
      LOGICAL                 :: TOGETHER, ORDER

      CHARACTER(16)           :: DATE, PASTDATE
      CHARACTER(30)           :: FILEPREFIX
      CHARACTER               :: COMSTR*1, IDTIME*23, IDDDAY*11,      &
                                 NCNAME*128, FILETIME*16,   &
                                 attname*120,attval*120

      CHARACTER*25            :: IDSRCE(7)

      REAL(kind=4)            :: DTHD,RTH0
      REAL(kind=4),DIMENSION(:),ALLOCATABLE    :: THD
      integer(kind=4)         :: iyc,imc,idc,ihc,iminc,isc
      CHARACTER*20            :: FORMAT1
      CHARACTER(8)            :: EXT

!ITYPE
      INTEGER,ALLOCATABLE     :: ncid(:)
      integer                 :: iret
      character(128),ALLOCATABLE   :: ncfile(:)
      integer                 :: dimid(7), dimln(5), varid(28)
      character               :: globalAtt*60, startdate*30, stopdate*30

!/
!/ ------------------------------------------------------------------- /
!/
      DATA IDSRCE / 'Spectrum                 ' ,                     &
                    'Wind-wave interactions   ' ,                     &
                    'Nonlinear interactions   ' ,                     &
                    'Dissipation              ' ,                     &
                    'Wave-bottom interactions ' ,                     &
                    'Wave-ice interactions    ' ,                     &
                    'Sum of selected sources  ' /
      FLSRCE = .FALSE.
!
!/NCO/!     CALL W3TAGB('WAVESPEC',1998,0007,0050,'NP21   ')
!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! 1.a  IO set-up.
!
      CALL W3NMOD ( 1, 6, 6 )
      CALL W3SETG ( 1, 6, 6 )
      CALL W3NDAT (    6, 6 )
      CALL W3SETW ( 1, 6, 6 )
!/NL1      CALL W3NAUX (    6, 6 )
!/NL1      CALL W3SETA ( 1, 6, 6 )
      CALL W3NOUT (    6, 6 )
      CALL W3SETO ( 1, 6, 6 )
!
      NDSI   = 10
      NDSM   = 20
      NDSOP  = 20
!
      NDSTRC =  6
      NTRACE = 10
      CALL ITRACE ( NDSTRC, NTRACE )
!
!/S      CALL STRACE (IENT, 'W3OUNP')
!
!/NCO/!
!/NCO/! Redo according to NCO
!/NCO/!
!/NCO      NDSI   = 11
!/NCO      NDSO   =  6
!/NCO      NDSE   = NDSO
!/NCO      NDST   = NDSO
!/NCO      NDSM   = 12
!/NCO      NDSOP  = 13
!/O14      NDBO   = 14
!/NCO      NDSTRC = NDSO
!
!
! 1.b MPP initializations
!
!/SHRD      NAPROC = 1
!/SHRD      IAPROC = 1
!
!/MPI      CALL MPI_INIT      ( IERR_MPI )
!/MPI      CALL MPI_COMM_SIZE ( MPI_COMM_WORLD, NAPROC, IERR_MPI )
!/MPI      CALL MPI_COMM_RANK ( MPI_COMM_WORLD, IAPROC, IERR_MPI )
!/MPI      IAPROC = IAPROC + 1  ! this is to have IAPROC between 1 and NAPROC
!
      IF ( IAPROC .EQ. NAPERR ) THEN
          NDSEN  = NDSE
        ELSE
          NDSEN  = -1
        END IF
!
      IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,900)
!
      J = LEN_TRIM(FNMPRE)
      OPEN (NDSI,FILE=FNMPRE(:J)//'ww3_ounp.inp',STATUS='OLD',        &
            ERR=800,IOSTAT=IERR)
      READ (NDSI,'(A)',END=801,ERR=802) COMSTR
      IF (COMSTR.EQ.' ') COMSTR = '$'
      IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,901) COMSTR
!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! 2.  Read model definition file.
!
      CALL W3IOGR ( 'READ', NDSM )
      IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,920) GNAME
!
      IF ( FLAGLL ) THEN
        M2KM = 1.
      ELSE
        M2KM = 1.E-3
      END IF
!
      DIMXP  = ((NK+1)/2) * ((NTH-1)/2)
      ALLOCATE ( XPART(6,0:DIMXP) )
!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! 3.  Read general data and first fields from file
!
      CALL W3IOPO ( 'READ', NDSOP, IOTEST )
!
      IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,930)
      DO I=1, NOPTS
        IF ( FLAGLL ) THEN
          IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,931) PTNME(I), M2KM*PTLOC(1,I), M2KM*PTLOC(2,I)
        ELSE
          IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,932) PTNME(I), M2KM*PTLOC(1,I), M2KM*PTLOC(2,I)
        END IF
      END DO
!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! 4.  Read requests from input file.
!

! 4.1 Gets output times
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      READ (NDSI,*,END=801,ERR=802) TOUT, DTREQ, NOUT
      DTREQ  = MAX ( 0. , DTREQ )
      IF ( DTREQ.EQ.0 ) NOUT = 1
      NOUT   = MAX ( 1 , NOUT )
      NOUTL = NOUT
      TOUTL = TOUT
      CALL STME21 ( TOUT , IDTIME )
      IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,940) IDTIME
      TDUM   = 0
      CALL TICK21 ( TDUM , DTREQ )
      CALL STME21 ( TDUM , IDTIME )
      IF ( DTREQ .GE. 86400. ) THEN
        WRITE (IDDDAY,'(I10,1X)') INT(DTREQ/86400.)
      ELSE
        IDDDAY = '           '
      END IF
      IDTIME(1:11) = IDDDAY
      IDTIME(21:23) = '   '
      IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,941) IDTIME, NOUT


! 4.2 Selects first time FILETIME between out_pnt.ww3 and ww3_ounp.inp
      IF (TOUT(1).GT.TIME(1) .OR. (TOUT(1).EQ.TIME(1) .AND. TOUT(2).GT.TIME(2))) THEN
        WRITE(DATE,'(I8.8,I6.6)') TOUT(1), TOUT(2)
      ELSE
        WRITE(DATE,'(I8.8,I6.6)') TIME(1), TIME(2)
      END IF
      WRITE(FILETIME,'(8A)') DATE(1:4), DATE(5:6), DATE(7:8), 'T', DATE(9:10), 'Z'


! 4.3 Loops on TIME from out_pnt file to reach the first time PASTDATE
      DTEST  = DSEC21 ( TIME , TOUT )
      DO WHILE (DTEST.NE.0) 
        DTEST  = DSEC21 ( TIME , TOUT )
        IF ( DTEST .GT. 0. ) THEN
          CALL W3IOPO ( 'READ', NDSOP, IOTEST )
          IF ( IOTEST .EQ. -1 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,949)
            GOTO 888
          END IF
          CYCLE
        END IF
        IF ( DTEST .LT. 0. ) THEN
          CALL TICK21 ( TOUT , DTREQ )
          CYCLE
        END IF
      END DO
      WRITE(PASTDATE,'(I8.8,I6.6)') TIME(1), TIME(2) 


! 4.4 Gets the list of output points
      ALLOCATE ( FLREQ(NOPTS) )
      ALLOCATE ( INDREQTMP(NOPTS) )
      FLREQ = .FALSE.
      NREQ   = 0
      ALLOCATE (ncfile(NOPTS))
      ALLOCATE (ncid(NOPTS))
      NBSTATION = 1
!
      DO I=1, NOPTS
        ! reads point index
        CALL NEXTLN ( COMSTR , NDSI , NDSE )
        READ (NDSI,*,END=801,ERR=802) IPOINT
        ! last index
        IF (IPOINT .LT. 0) THEN
          IF (I.EQ.1) THEN
            FLREQ = .TRUE.
            NREQ = NOPTS
            INDREQTMP=(/(J,J=1,NREQ)/) 
          END IF
          EXIT
        END IF
        ! existing index in out_pnt.ww3   
        IF ( (IPOINT .GT. 0) .AND. (IPOINT .LE. NOPTS) ) THEN
          IF ( .NOT. FLREQ(IPOINT) ) THEN 
            NREQ = NREQ + 1
            INDREQTMP(NREQ)=IPOINT
            END IF
          FLREQ(IPOINT) = .TRUE.
        END IF
        ! read the 'end of list' if nopts reached before it
        IF ( (IPOINT .GT. 0) .AND. (NREQ .EQ. NOPTS) ) THEN
          CALL NEXTLN ( COMSTR , NDSI , NDSE )
          READ (NDSI,*,END=801,ERR=802) IPOINT
        END IF
      END DO
      ! check if last point index is -1
      IF (IPOINT .NE. -1) THEN
        WRITE (NDSE,1007)
        CALL EXTCDE ( 47 )
      END IF
!
      ALLOCATE ( INDREQ(NREQ) )
      INDREQ(:)=INDREQTMP(1:NREQ)
      DEALLOCATE(INDREQTMP)


! 4.5 Gets the file prefix 
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      FILEPREFIX= 'ww3.'
      READ (NDSI,*,END=801,ERR=802) FILEPREFIX


! 4.6 Gets the date 
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      ! S3 defines the number of characters in the date for the filename
      ! S3=4-> YYYY, S3=6 -> YYYYMM, S3=10 -> YYYYMMDDHH
      READ (NDSI,*,END=801,ERR=802) S3
!
      ! Setups min and max date format
      IF (S3.LT.4) S3=4 
      IF (S3.GT.10) S3=10 
!
      ! Defines the format of FILETIME as ISO8601 convention
      S5=S3-8
      ! if S3=>YYYYMMDDHH then filetime='YYYYMMDDTHHMMSSZ'
      IF (S3.EQ.10) THEN 
        WRITE(FORMAT1,'(A,I1,A,I1,A)') '(I8.8,A1,I',S5,'.',S5,',A1)'
        WRITE (FILETIME,FORMAT1) TIME(1), 'T', &
               FLOOR(REAL(TIME(2))/NINT(10.**(6-S5))), 'Z'
      ! if S3=>YYYYMMDD then filetime='YYYYMMDD'
      ELSE IF (S3.EQ.8) THEN
        WRITE(FORMAT1,'(A,I1,A,I1,A)') '(I',S3,'.',S3,')'
        WRITE (FILETIME,FORMAT1) TIME(1)
      ! if S3=>YYYYMM then filetime='YYYYMM'
      ! or S3=>YYYY then filetime='YYYY'
      ELSE 
        WRITE(FORMAT1,'(A,I1,A,I1,A)') '(I',S3,'.',S3,')'
        WRITE (FILETIME,FORMAT1) FLOOR(REAL(TIME(1))/NINT(10.**(8-S3)))
      END IF
 

! 4.7 Gets the output types
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      READ (NDSI,*,END=801,ERR=802) NCTYPE
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      READ (NDSI,*,END=801,ERR=802) TOGETHER, MFL 
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      READ (NDSI,*,END=801,ERR=802) ITYPE
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      READ (NDSI,*,END=801,ERR=802) FLWW3
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      READ (NDSI,*,END=801,ERR=802) ORDER
!
      ! order time,station
      IF (ORDER) THEN
        one=1
        two=2
      ! order station,time
      ELSE
        one=2
        two=1
      END IF
!
      IF ((NCTYPE.EQ.3) .AND. (.NOT.ORDER)) GOTO 803 
      IF ((NCTYPE.EQ.4) .AND. INDEX(NF90_INQ_LIBVERS(),'"3.').NE.0) GOTO 804


! 4.8 Converts direction unit in degree
      ALLOCATE(THD(NTH))
      DTHD=360./NTH
      RTH0=TH(1)/DTH
      DO ITH=1, NTH
        THD(ITH)=DTHD*(RTH0+REAL(ITH-1))
      END DO
!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! 5.  Now creates files
!     If too many (memory problem) then makes several reads 
!


! 5.1 Defines number of files/stations per file NFL
      IF (TOGETHER) THEN 
        NFL=1
      ELSE
        NFL=1+NOPTS/MFL
      END IF


! 5.2 Creates filename listing
      CALL NEXTLN ( COMSTR , NDSI , NDSE )
      IF (ITYPE .EQ. 1) READ (NDSI,*,END=801,ERR=802) OTYPE, SCALE1, SCALE2
      IF (ITYPE .EQ. 2) READ (NDSI,*,END=801,ERR=802) OTYPE
      IF (ITYPE .EQ. 3) READ (NDSI,*,END=801,ERR=802) OTYPE, SCALE1, SCALE2, FLSRCE, ISCALE
      WRITE(EXT,'(A)') ''
      IF ((ITYPE .EQ. 1) .AND. (OTYPE.EQ.2)) WRITE(EXT,'(A)') '_tab.nc'
      IF ((ITYPE .EQ. 1) .AND. (OTYPE.EQ.3)) WRITE(EXT,'(A)') '_spec.nc'
      IF ((ITYPE .EQ. 1) .AND. (OTYPE.EQ.4)) WRITE(EXT,'(A)') '_tab.nc'
      IF  (ITYPE .EQ. 2)                     WRITE(EXT,'(A)') '_tab.nc'
      IF ((ITYPE .EQ. 3) .AND. (OTYPE.EQ.2)) WRITE(EXT,'(A)') '_tab.nc'
      IF ((ITYPE .EQ. 3) .AND. (OTYPE.EQ.3)) WRITE(EXT,'(A)') '_tab.nc'
      IF ((ITYPE .EQ. 3) .AND. (OTYPE.EQ.4)) WRITE(EXT,'(A)') '_src.nc'
      ! checks if extension exists
      IF (LEN_TRIM(EXT).EQ.0) THEN
        WRITE (NDSE,1006)
        CALL EXTCDE ( 46 )
      END IF

! 5.3 Redefines netCDF type 
!/NC4      IF((NCTYPE.EQ.4).AND.(.NOT.TOGETHER).AND.(NFL.GT.300).AND.(NREQ.GT.9000))  THEN
!/NC4        WRITE(NDSO,'(A)') ' WARNING : Files will be generated in netCDF3 with nf90_share mode'
!/NC4        WRITE(NDSO,'(A)') ' WARNING : this is due to nf90_sync memory problem with netCDF4 library'
!/NC4        WRITE(NDSO,'(A)') ' WARNING : to convert in netCDF4, use ncks -h -a -4 -L 9 file.nc3 file.nc4'
!/NC4        WRITE(NDSO,'(A)') ' WARNING : or use option "Points in same file" with value TRUE in .inp file'
!/NC4        WRITE(NDSO,'(A)') ' WARNING : or limit the output points list to less than 300'
!/NC4        NCTYPE=3
!/NC4      END IF



! 5.4 Defines periodic flushing of buffer (only available for netCDF3)
      NCFLUSH=FLOOR(15E7/(FLOAT(NK)*FLOAT(NTH)*FLOAT(NREQ)/NFL))
      IF (NCTYPE.EQ.3.AND.NREQ.GT.10.AND.(.NOT.TOGETHER)) WRITE(NDSO,5940) NCFLUSH


! 5.5 Removes the duplicata if "one file per station" mode
      IF (.NOT.TOGETHER) THEN
        ! defines a file name per station (NOT TOGETHER)
        DO I=1,NOPTS
          IF (FLREQ(I)) THEN
            J = LEN_TRIM(FNMPRE)
            WRITE (NCNAME, '(5A)') trim(FILEPREFIX), trim(PTNME(I)),'_', trim(FILETIME), trim(EXT)
            WRITE(ncfile(I),'(2A)') trim(FNMPRE(:J)), trim(NCNAME)  ! filename
            IF( SUM(index(ncfile(:),ncfile(I))).GT.1 ) THEN
              FLREQ(I)=.FALSE.
              WRITE(NDSO,5950) trim(PTNME(I))
              CYCLE
            END IF
          END IF  ! FLREQ(I)
        END DO  ! I=1,NOPTS    
      END IF  ! .NOT.TOGETHER


! 5.6 Loops on bunch of stations NFL
      DO IFL=IAPROC,NFL,NAPROC
!
        ! new file, so the time counter is initialized
    560 CONTINUE
        IOUT=0


! 5.6.1 Redefines the filetime when it's a new date defined by the date division S3
        ! if S3=>YYYYMMDDHH then filetime='YYYYMMDDTHHMMSSZ'
        IF (S3.EQ.10) THEN 
          WRITE(FORMAT1,'(A,I1,A,I1,A)') '(I8.8,A1,I',S5,'.',S5,',A1)'
          WRITE (FILETIME,FORMAT1) TIME(1), 'T', &
                 NINT(REAL(TIME(2))/NINT(10.**(6-S5))), 'Z'
        ! if S3=>YYYYMMDD then filetime='YYYYMMDD'
        ELSE IF (S3.EQ.8) THEN
          WRITE(FORMAT1,'(A,I1,A,I1,A)') '(I',S3,'.',S3,')'
          WRITE (FILETIME,FORMAT1) TIME(1)
        ! if S3=>YYYYMM then filetime='YYYYMM'
        ! or S3=>YYYY then filetime='YYYY'
        ELSE 
          WRITE(FORMAT1,'(A,I1,A,I1,A)') '(I',S3,'.',S3,')'
          WRITE (FILETIME,FORMAT1) NINT(REAL(TIME(1))/NINT(10.**(8-S3)))
        END IF


! 5.6.2 Defines the file names
        ! defines unique file name (TOGETHER)
        IF (TOGETHER) THEN
          WRITE (NCNAME, '(3A)') trim(FILEPREFIX), trim(FILETIME), trim(EXT)
          !IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1947) trim(NCNAME)
          J = LEN_TRIM(FNMPRE)
          WRITE(ncfile(1),'(2A)') trim(FNMPRE(:J)), trim(NCNAME)  ! filename
        ELSE
          ! defines a file name per station (NOT TOGETHER)
          DO I=1,NOPTS
            IF (FLREQ(I)) THEN
              WRITE (NCNAME, '(5A)') trim(FILEPREFIX), trim(PTNME(I)),'_', trim(FILETIME), trim(EXT)
              !IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1947) trim(NCNAME)
              J = LEN_TRIM(FNMPRE)
              WRITE(ncfile(I),'(2A)') trim(FNMPRE(:J)), trim(NCNAME)  ! filename
            END IF  ! FLREQ(I)
          END DO  ! I=1,NOPTS    
        END IF  ! TOGETHER


! 5.6.3 Defines number of stations and files to create
        ! together
        IF (TOGETHER) THEN
          NBFILEOUT = 1
          NBSTATION = NREQ
          NREQL=NBFILEOUT
        ! not together
        ELSE
          NBFILEOUT=MIN(MFL,NOPTS-(IFL-1)*MFL)
          NBSTATION = 1
          NREQL=0
          DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
            IF ( FLREQ(I) ) THEN
              NREQL = NREQL + 1
            END IF
          END DO
        END IF
        ! cycle if no file to create
        IF (NREQL.EQ.0) CYCLE


! 5.6.4 Creates netcdf file

! ... ITYPE = 1
        IF (ITYPE .EQ. 1) THEN
          IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,942) ITYPE, '1-D and/or 2-D spectra, pass #',IFL

! ... OTYPE = 1
          IF (OTYPE .EQ. 1) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'print plots'
            IF ( SCALE1 .LT. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1940) '1-D'
            ELSE IF ( SCALE1 .EQ. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1941) '1-D'
            ELSE
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1942) '1-D', SCALE1
            END IF
            IF ( SCALE2 .LT. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1940) '2-D'
            ELSE IF ( SCALE2 .EQ. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1941) '2-D'
            ELSE
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1942) '2-D', SCALE2
            END IF

! ... OTYPE = 2
          ELSE IF ( OTYPE .EQ. 2 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'Table of 1-D spectral data'
            DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
              IF (FLREQ(I) .OR. TOGETHER) THEN
             ! Create the netCDF file
                dimln(1)=NF90_UNLIMITED    ! time
                dimln(2)=NBSTATION         ! station
                dimln(3)=16                ! string station name length
                dimln(4)=NK                ! freq
                CALL W3CRNC(ITYPE,OTYPE,NCTYPE,ncfile(I),ncid(I),dimid,dimln,varid,one,two)
              END IF
            END DO

! ... OTYPE = 3
          ELSE IF ( OTYPE .EQ. 3 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'Transfer file'
            DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
              IF (FLREQ(I) .OR. TOGETHER) THEN
              ! Create the netCDF file
                dimln(1)=NF90_UNLIMITED  !time
                dimln(2)=NBSTATION ! station
                dimln(3)=16 ! string station name length
                dimln(4)=NK ! freq
                dimln(5)=NTH ! dir
                CALL W3CRNC(ITYPE,OTYPE,NCTYPE,ncfile(I),ncid(I),dimid,dimln,varid,one,two)
              END IF
            END DO

! ... OTYPE = 4
          ELSE IF ( OTYPE .EQ. 4 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'Partitioning of spectra'
            DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
              IF (FLREQ(I) .OR. TOGETHER) THEN
                ! Create the netCDF file
                dimln(1)=NF90_UNLIMITED  !time
                dimln(2)=NBSTATION ! station
                dimln(3)=16    ! string station name length
                dimln(4)=DIMXP ! npart
                CALL W3CRNC(ITYPE,OTYPE,NCTYPE,ncfile(I),ncid(I),dimid,dimln,varid,one,two)
              END IF
            END DO
          ELSE
            WRITE (NDSE,1011) OTYPE
            CALL EXTCDE ( 10 )
          END IF



! ... ITYPE = 2
        ELSE IF (ITYPE .EQ. 2) THEN
          IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,942) ITYPE, 'Table of mean wave parameters'
          DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
            IF (FLREQ(I) .OR. TOGETHER) THEN
              ! Create the netCDF file
              dimln(1)=NF90_UNLIMITED  !time
              dimln(2)=NBSTATION ! station
              dimln(3)=16    ! string station name length
              CALL W3CRNC(ITYPE,OTYPE,NCTYPE,ncfile(I),ncid(I),dimid,dimln,varid,one,two)
            END IF
          END DO

! ... OTYPE = 1
          IF ( OTYPE .EQ. 1 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,2940) 'depth, current and wind', NCNAME

! ... OTYPE = 2
          ELSE IF ( OTYPE .EQ. 2 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,2940) 'Mean wave parameters', NCNAME

! ... OTYPE = 3
          ELSE IF ( OTYPE .EQ. 3 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,2940) 'Nondimensional parameters (U*)', NCNAME

! ... OTYPE = 4
          ELSE IF ( OTYPE .EQ. 4 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,2940) 'Nondimensional parameters (U10)', NCNAME

! ... OTYPE = 5
          ELSE IF ( OTYPE .EQ. 5 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,2940) 'Validation parameters', NCNAME

! ... OTYPE = 6
          ELSE IF ( OTYPE .EQ. 6 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,2940) 'WMO standard mean parameters', NCNAME
! ... OTYPE = ILLEGAL
          ELSE
            WRITE (NDSE,1011) OTYPE
            CALL EXTCDE ( 30 )
          END IF
!
          DO I=1,6
            IF ( FLSRCE(I) .AND. IAPROC .EQ. NAPOUT ) WRITE (NDSO,3940) IDSRCE(I)
          END DO
          IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,*) ' '


! ... ITYPE = 3
        ELSE IF (ITYPE .EQ. 3) THEN
          IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,942) ITYPE, 'Source terms'
!/NCO         NDSTAB = 51
          ISCALE = MAX ( 0 , MIN ( 5 , ISCALE ) )

! ... OTYPE = 1
          IF ( OTYPE .EQ. 1 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'Print plots'
            IF ( SCALE1 .LT. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1940) '1-D'
            ELSE IF ( SCALE1 .EQ. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1941) '1-D'
            ELSE
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1942) '1-D', SCALE1
            END IF
            IF ( SCALE2 .LT. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1940) '2-D'
            ELSE IF ( SCALE2 .EQ. 0.  ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1941) '2-D'
            ELSE
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,1942) '2-D', SCALE2
            END IF

! ... OTYPE = 2  
! or  OTYPE = 3
          ELSE IF (( OTYPE .EQ. 2 ) .OR. ( OTYPE .EQ. 3 )) THEN
            IF ( ISCALE .LE. 2) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'Tables as a function of freq.'
            ELSE
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'Tables as a function of f/fp.'
            END IF
            IF ( MOD(ISCALE,3) .EQ. 1 ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,944) '(nondimensional based on U10)'
            ELSE IF ( MOD(ISCALE,3) .EQ. 2) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,944) '(nondimensional based on U*)'
            END IF

            DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
              IF (FLREQ(I) .OR. TOGETHER) THEN
                ! Create the netCDF file
                dimln(1)=NF90_UNLIMITED  !time
                dimln(2)=NBSTATION ! station
                dimln(3)=16    ! string station name length
                dimln(4)=NK ! freq
                CALL W3CRNC(ITYPE,OTYPE,NCTYPE,ncfile(I),ncid(I),dimid,dimln,varid,one,two)
              END IF
            END DO

! ... OTYPE = 4
          ELSE IF ( OTYPE .EQ. 4 ) THEN
            IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,943) 'Transfer file'
            DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
              IF (FLREQ(I) .OR. TOGETHER) THEN
                ! Create the netCDF file
                dimln(1)=NF90_UNLIMITED  !time
                dimln(2)=NBSTATION ! station
                dimln(3)=16    ! string station name length
                dimln(4)=NK ! freq
                dimln(5)=NTH ! dir
                CALL W3CRNC(ITYPE,OTYPE,NCTYPE,ncfile(I),ncid(I),dimid,dimln,varid,one,two,FLSRCE)
              END IF
            END DO

! ... OTYPE = ILLEGAL
          ELSE
            WRITE (NDSE,1011) OTYPE
            CALL EXTCDE ( 20 )
          END IF


! ... ITYPE = ILLEGAL
        ELSE
          WRITE (NDSE,1010) ITYPE
          CALL EXTCDE ( 1 )
        END IF


! 5.6.5 Output of output points
        IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,950) NREQ
        ! together
        IF (TOGETHER) THEN
          DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBSTATION
            IF (FLREQ(I)) THEN
              IF ( FLAGLL ) THEN
                IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,951) PTNME(I), M2KM*PTLOC(1,I),   &
                                           M2KM*PTLOC(2,I)
              ELSE
                IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,953) PTNME(I), M2KM*PTLOC(1,I),   &
                                           M2KM*PTLOC(2,I)
              END IF
            END IF
          END DO
        ! not together
        ELSE
          DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
            IF (FLREQ(I)) THEN
              IF ( FLAGLL ) THEN
                IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,951) PTNME(I), M2KM*PTLOC(1,I),   &
                                           M2KM*PTLOC(2,I)
              ELSE
                IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,953) PTNME(I), M2KM*PTLOC(1,I),   &
                                           M2KM*PTLOC(2,I)
              END IF
            END IF
          END DO
        END IF
!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! 6.  Time management.
!
!/IC1      WRITE(NDSO,3960)
!/IC2      WRITE(NDSO,3960)
!/IC3      WRITE(NDSO,3960)
!

        iyc=TIME(1)/10000
        imc=(TIME(1)-iyc*10000)/100
        idc=TIME(1)-iyc*10000-100*imc
        ihc=TIME(2)/10000
        iminc=(TIME(2)-ihc*10000)/100
        isc=TIME(2)-ihc*10000-100*iminc
        WRITE(startdate,'(I4.4,A,4(I2.2,A),I2.2)') iyc,'-',imc,'-',idc,' ',ihc,':',iminc,':',isc
        ! loops on TIME from out_pnt.ww3 till not reach TOUT from inp file
        DO
          DTEST = DSEC21 ( TIME , TOUT )
          IF ( DTEST .GT. 0. ) THEN
            ! reads TIME from out_pnt.ww3
            CALL W3IOPO ( 'READ', NDSOP, IOTEST )
            IF ( IOTEST .EQ. -1 ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,949)
              GOTO 700
            END IF
            CYCLE
          END IF
          IF ( DTEST .LT. 0. ) THEN
            CALL TICK21 ( TOUT , DTREQ )
            CYCLE
          END IF
          ! increment the time counter IOUT
          IOUT = IOUT + 1
          CALL STME21 ( TOUT , IDTIME )
          WRITE(DATE,'(I8.8,I6.6)') TOUT(1), TOUT(2) 


! 6.1 Creates a new file if it is a new date defined by the date division S3
          IF ( (IOUT.GT.1) .AND. (INDEX(PASTDATE(1:S3),DATE(1:S3)).EQ.0) ) THEN
            WRITE(NDSO,954) trim(DATE(1:S3))
            ! decrements timesteps already processed
            NOUT=NOUT-(IOUT-1)
            GOTO 700
          END IF


! 6.2 Writes out a progress message
          IF (NREQ.GT.10.OR.NBFILEOUT.GT.10) WRITE(NDSO,955) TIME,    &
                                             NBFILEOUT, IOUT, NOUT, IFL
          J=0

! 6.3 Calls subroutine w3exnc for each file
          DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
            IF (FLREQ(I) .OR. TOGETHER) THEN
              ! together
              IF ( TOGETHER ) THEN 
                CALL W3EXNC(I,ncid(I),NREQ,INDREQ,ORDER)
              ! not together
              ELSE 
                J=J+1
                CALL W3EXNC(I,ncid(I),1,(/ I /),ORDER)
                ! flush buffer (only available in netcdf3)
                IF (MOD(IOUT,NCFLUSH).EQ.0) THEN
                  iret=NF90_SYNC(ncid(I))
                END IF
              END IF ! TOGETHER
            END IF ! (FLREQ(I) .OR. TOGETHER)
          END DO ! I=1+ ...
!
          WRITE(PASTDATE,'(I8.8,I6.6)') TOUT(1), TOUT(2) 
          CALL TICK21 ( TOUT , DTREQ )
          IF ( IOUT .GE. NOUT ) GOTO 700
!
        END DO
!
        GOTO 888


!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! 7. Finalize file
!
    700 CONTINUE
!
        iyc=TIME(1)/10000
        imc=(TIME(1)-iyc*10000)/100
        idc=TIME(1)-iyc*10000-100*imc
        ihc=TIME(2)/10000
        iminc=(TIME(2)-ihc*10000)/100
        isc=TIME(2)-ihc*10000-100*iminc
        WRITE(stopdate,'(I4.4,A,4(I2.2,A),I2.2)') &
              iyc,'-',imc,'-',idc,' ', ihc,':',iminc,':',isc 


! 7.1 Writes the global attributes to netCDF file
        DO I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT
          IF ( FLREQ(I) .OR. TOGETHER ) THEN
            iret=nf90_redef(ncid(I))
            call check_err(iret)
            IF (FLWW3.EQ.0)                                      &
              OPEN(unit=994,file='NC_globatt.inp',status='old',iostat=icode)
            rewind(994)
            IF (ICODE.EQ.0) THEN
              DO WHILE (icode.EQ.0)
                read(994,'(a)',iostat=icode) attname
                read(994,'(a)',iostat=icode) attval
                IF (ICODE.EQ.0) THEN
                  strl=LEN_TRIM(attname)
                  strl2=LEN_TRIM(attval)
                  iret=nf90_put_att(ncid(I),NF90_GLOBAL,attname(1:strl),attval(1:strl2))
                  call check_err(iret)
                END IF
              END DO
            END IF
            close(994)
!            
            write(globalAtt,'(A)') TRIM(ncfile(I))
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'product_name' ,globalAtt(3:))
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'area',TRIM(GNAME))
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'data_type','OCO spectra 2D')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'format_version','1.1')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'southernmost_latitude','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'northernmost_latitude','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'latitude_resolution','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'westernmost_longitude','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'easternmost_longitude','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'longitude_resolution','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'minimum_altitude','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'maximum_altitude','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'altitude_resolution','n/a')
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'start_date',startdate)
            iret=nf90_put_att(ncid(I),NF90_GLOBAL,'stop_date',stopdate)
            IF (DTREQ.EQ.3600)  THEN
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','hourly')
            ELSE IF (DTREQ.EQ.7200)  THEN
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','2-hourly')
            ELSE IF (DTREQ.EQ.10800)  THEN
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','3-hourly')
            ELSE IF (DTREQ.EQ.21600)  THEN
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','6-hourly')
            ELSE IF (DTREQ.EQ.32400)  THEN
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','9-hourly')
            ELSE IF (DTREQ.EQ.43200)  THEN
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','12-hourly')
            ELSE IF (DTREQ.EQ.86400)  THEN
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','daily')
            ELSE  
              iret=nf90_put_att(ncid(I),NF90_GLOBAL,'field_type','n/a')
            END IF
!
            ! Close netCDF file
            iret=nf90_enddef(ncid(I))
            call check_err(iret)
            iret=nf90_close(ncid(I))
            call check_err(iret)
!
          END IF ! FLREQ(I) .OR. TOGETHER
        END DO ! I=1+(IFL-1)*MFL,(IFL-1)*MFL+NBFILEOUT


! 7.2 Goes back to the start of the loop with the same points
      ! but with a new date defined by the date division S3
        IF ( (IOUT.GT.1) .AND. (INDEX(PASTDATE(1:S3),DATE(1:S3)).EQ.0) ) THEN
          GOTO 560
        END IF


! 7.3 Reinitiazes TIME (close open out_pnt.ww3) and TOUT to process a new bunch of stations
        CLOSE(NDSOP) ! closes binary file out_pnt*
        IPASS = 0   ! resets time counter for binary file out_pnt*
        CALL W3IOPO ( 'READ', NDSOP, IOTEST )
!/T       WRITE(NDSE,*) 'out_pnt* closed and reopened'
        TOUT=TOUTL
        NOUT=NOUTL


! 7.4 Loops on TIME till it is equal to TOUT
        DTEST  = DSEC21 ( TIME , TOUT )
        DO WHILE (DTEST.NE.0) 
          DTEST  = DSEC21 ( TIME , TOUT )
          IF ( DTEST .GT. 0. ) THEN
            CALL W3IOPO ( 'READ', NDSOP, IOTEST )
            IF ( IOTEST .EQ. -1 ) THEN
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,949)
              GOTO 700
            END IF
            CYCLE
          END IF
          IF ( DTEST .LT. 0. ) THEN
            CALL TICK21 ( TOUT , DTREQ )
            CYCLE
          END IF
        END DO
!
      END DO ! IFL=1,NFL
!
      GOTO 888


!
! Escape locations read errors :
!
  800 CONTINUE
      WRITE (NDSE,1000) IERR
      CALL EXTCDE ( 40 )
!
  801 CONTINUE
      WRITE (NDSE,1001)
      CALL EXTCDE ( 41 )
!
  802 CONTINUE
      WRITE (NDSE,1002) IERR
      CALL EXTCDE ( 42 )
!
  803 CONTINUE
      WRITE (NDSE,1003) 
      CALL EXTCDE ( 43 )
!
  804 CONTINUE
      WRITE (NDSE,1004) NF90_INQ_LIBVERS()
      CALL EXTCDE ( 44 )
!
!/O14  805 CONTINUE
!/O14      WRITE (NDSE,1005) IERR
!/O14      CALL EXTCDE ( 45 )
!
!
  888 CONTINUE
!
      IF(ALLOCATED(THD)) DEALLOCATE(THD)
      IF(ALLOCATED(ncid)) DEALLOCATE(ncid)
      IF(ALLOCATED(ncfile)) DEALLOCATE(ncfile)
      IF(ALLOCATED(INDREQ)) DEALLOCATE(INDREQ)
!
      IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,999)
!/MPI      CALL MPI_FINALIZE  ( IERR_MPI )
!
!/NCO/!     CALL W3TAGE('WAVESPEC')
!
! Formats
!
  900 FORMAT (/15X,'    *** WAVEWATCH III Point output post.***    '/ &
               15X,'==============================================='/)
  901 FORMAT ( '  Comment character is ''',A,''''/)
!
  920 FORMAT ( '  Grid name : ',A/)
!
  930 FORMAT ( '  Points in file : '/                                 &
               ' ------------------------------------')
  931 FORMAT ( '      ',A,2F10.2)
  932 FORMAT ( '      ',A,2(F8.1,'E3'))
!
  940 FORMAT (/'  Output time data : '/                               &
               ' --------------------------------------------------'/ &
               '      First time         : ',A)
  941 FORMAT ( '      Interval           : ',A/                       &
               '      Number of requests : ',I4)
  942 FORMAT (/'  Output type ',I2,' :'/                              &
               ' --------------------------------------------------'/ &
               '      ',A, I3 /)
  943 FORMAT ( '      Subtype   : ',A)
  944 FORMAT ( '                  ',A)
!/O14   945 FORMAT ( '      ',I5,3X,A,2F10.2,3X,A)
  948 FORMAT ( '      Data for ',A)
  949 FORMAT (/'      End of file reached '/)
!
  950 FORMAT (/'  Requested output for ',I6,' points : '/             &
               ' --------------------------------------------------')
  951 FORMAT ( '      ',A,2F10.2)
  953 FORMAT ( '      ',A,2(F8.1,'E3'))
  952 FORMAT (/'  Output times :'/                                    &
               ' --------------------------------------------------')
  954 FORMAT (/'      New time step      : ',A)
  955 FORMAT ( '  Processing time : ', 2I8, ' in ', I4, 'files. Step '&
               I4, 'out of ', I4, 'pass ', I4)
!
 1940 FORMAT ( '      ',A,' print plots not requested.')
 1941 FORMAT ( '      ',A,' print plots normalized.')
 1942 FORMAT ( '      Scale factor ',A,' spectrum : ',E10.3)
 1943 FORMAT ( '      File name : ',A,' (',A,')')
 1944 FORMAT ('''',A,'''',1X,3I6,1X,'''',A,'''')
 1945 FORMAT (8E10.3)
 1946 FORMAT (7E11.3)
 1947 FORMAT ( '      File name : ',A)
!
 2940 FORMAT ( '      Table output : ',A/                             &
               '      File name    : ',A)
!
 3940 FORMAT ( '                        ',A)
 3944 FORMAT ('''',A,'''',1X,3I6,6L2)
 3945 FORMAT (8E10.3)
 3946 FORMAT (7E11.3)
 5940 FORMAT ( '      Buffer will be flushed every ',I6,' steps.')
 5950 FORMAT ( '      Point already exists, it will be skipped : ', A)
!
  960 FORMAT (//'  Output for ',A/                                    &
               ' --------------------------------------------------')
!
  999 FORMAT (/'  End of program '/                                   &
               ' ========================================='/          &
               '         WAVEWATCH III Point output '/)
!
 1000 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/               &
               '     ERROR IN OPENING INPUT FILE'/                    &
               '     IOSTAT =',I5/)
!
 1001 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/               &
               '     PREMATURE END OF INPUT FILE'/)
!
 1002 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/               &
               '     ERROR IN READING FROM INPUT FILE'/               &
               '     IOSTAT =',I5/)
!
 1003 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/               &
               '     NCTYPE=3 IS INCOMPATIBLE WITH'/                  &
               '     THE OPTIMIZED DIMENSION ORDER'/)
!
 1004 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/               &
               '     NCTYPE=4 IS INCOMPATIBLE WITH'/                  &
               '     NETCDF LIBRARY USED :',A/)
!
!/O14 1005 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/          &
!/O14               '     ERROR IN OPENING BUOY LOG FILE'/            &
!/O14               '     IOSTAT =',I5/)
!
 1006 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/              &
               '     ITYPE AND OTYPE COMBINATION NOT RECOGNIZED'/)
!
 1007 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/              &
               '     ERROR IN READING FROM INPUT FILE'/              &
               '     LAST POINT INDEX IS NOT -1'/                    &
               '     OR TOO MANY POINT INDEXES DEFINED'/)
!
 1010 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/               &
               '     ILLEGAL TYPE, ITYPE =',I4/)
!
 1011 FORMAT (/' *** WAVEWATCH III ERROR IN W3OUNP : '/               &
               '     ILLEGAL TYPE, OTYPE =',I4/)
!
!/IC1 3960 FORMAT (/' *** WAVEWATCH-III WARNING IN W3OUNP :'/         &
!/IC1               '     Ice source terms !/IC1 skipped'/            &
!/IC1               '     in dissipation term.'/)
!/IC2 3960 FORMAT (/' *** WAVEWATCH-III WARNING IN W3OUNP :'/         &
!/IC2               '     Ice source terms !/IC2 skipped'/            &
!/IC2               '     in dissipation term.'/)
!/IC3 3960 FORMAT (/' *** WAVEWATCH-III WARNING IN W3OUNP :'/         &
!/IC3               '     Ice source terms !/IC3 skipped'/            &
!/IC3               '     in dissipation term.'/)
!/
!/ Internal subroutine W3EXNC ---------------------------------------- /
!/
      CONTAINS
!/ ------------------------------------------------------------------- /
      SUBROUTINE W3EXNC(I,ncid,NREQ,INDREQ,ORDER)
!/
!/                  +-----------------------------------+
!/                  | WAVEWATCH III           NOAA/NCEP |
!/                  |            F. Ardhuin             |
!/                  |            M. Accensi             |
!/                  |                        FORTRAN 90 |
!/                  | Last update :         14-Mar-2013 |
!/                  +-----------------------------------+
!/
!/    01-Apr-2011 : Creation                            ( version 3.14 )
!/    14-Mar-2013 : Optimization and cleanup            ( version 4.10 )
!/
!  1. Purpose :
!
!     Perform actual point output.
!
!  3. Parameters :
!
!  4. Subroutines used :
!
!      Name      Type  Module   Description
!     ----------------------------------------------------------------
!      W3SPRn    Subr. W3SRCnMD Mean wave parameters for use in
!                               source terms.
!      W3FLXn    Subr. W3FLXnMD Flux/stress computation.
!      W3SLNn    Subr. W3SLNnMD Linear input.
!      W3SINn    Subr. W3SRCnMD Input source term.
!      W3SDSn    Subr. W3SRCnMD Whitecapping source term
!      W3SNLn    Subr. W3SNLnMD Nonlinear interactions.
!      W3SBTn    Subr. W3SBTnMD Bottom friction source term.
!      W3SDBn    Subr. W3SBTnMD depth induced breaking source term.
!      W3STRn    Subr. W3STRnMD Triad interaction source term.
!      W3SBSn    Subr. W3SBSnMD Bottom scattering source term.
!      W3SXXn    Subr. W3SXXnMD Unclassified source term.
!      W3PART    Sunr. W3PARTMD Spectral partitioning routine.
!      STRACE    Subr. W3SERVMD Subroutine tracing.
!      STME21    Subr. W3TIMEMD Convert time to string.
!      PRT1DS    Subr. W3ARRYMD Print plot of 1-D spectrum.
!      PRT1DM    Subr.   Id.    Print plot of several 1-D spectra.
!      PRT2DS    Subr.   Id.    Print plot of 2-D spectrum.
!      WAVNU1    Subr. W3DISPMD Solve dispersion relation.
!     ----------------------------------------------------------------
!
!  5. Called by :
!
!     Main program in which it is contained,
!
!  6. Error messages :
!
!     None.
!
!  7. Remarks :
!
!     - Spectra are relative frequency energy spectra.
!     - Note that arrays CX and CY of the main program now contain
!       the absolute sea water speed and direction respectively.
!
!  8. Structure :
!
!     See source code.
!
!  9. Switches :
!
!       !/S      Enable subroutine tracing.
!       !/T      Enable test output.
!
!       !/FLXx   Flux/stress computation.
!       !/LNx    Linear input package
!       !/STx    Source term package
!       !/NLx    Nonlinear interaction package
!       !/BTx    Bottom friction package
!       !/ICx    S_ice source term package
!       !/DBx    depth-induced breaking package
!       !/TRx    Triad interaction package
!       !/BSx    Bottom scattering package
!       !/XXx    Arbitrary adittional source term package
!
!       !/STAB2  Stability correction for !/ST2
!
! 10. Source code :
!
!/ ------------------------------------------------------------------- /
!/FLX1      USE W3FLX1MD
!/FLX2      USE W3FLX2MD
!/FLX3      USE W3FLX3MD
!/FLX4      USE W3FLX4MD
!/LN1      USE W3SLN1MD
!/LNX      USE W3SLNXMD
!/ST1      USE W3SRC1MD
!/ST2      USE W3SRC2MD
!/ST3      USE W3SRC3MD
!/ST4      USE W3SRC4MD, ONLY : W3SPR4, W3SIN4, W3SDS4
!/ST6      USE W3SRC6MD
!/ST6      USE W3SWLDMD, ONLY : W3SWL6
!/ST6      USE W3GDATMD, ONLY : SWL6S6
!/STX      USE W3SRCXMD
!/NL1      USE W3SNL1MD
!/NL2      USE W3SNL2MD
!/NL3      USE W3SNL3MD
!/NL4      USE W3SNL4MD
!/NLX      USE W3SNLXMD
!/BT1      USE W3SBT1MD
!/BT4      USE W3SBT4MD
!/BT8      USE W3SBT8MD
!/BT9      USE W3SBT9MD
!/BTX      USE W3SBTXMD
!/DB1      USE W3SDB1MD
!/DBX      USE W3SDBXMD
!/TRX      USE W3STRXMD
!/BS1      USE W3SBS1MD
!/BSX      USE W3SBSXMD
!/IS2      USE W3SIS2MD
!/XXX      USE W3SXXXMD
      USE W3PARTMD, ONLY: W3PART
      USE W3DISPMD, ONLY: WAVNU1, LIU_FORWARD_DISPERSION
      USE W3GDATMD, ONLY: IICEDISP
!/
      USE W3ARRYMD, ONLY: PRT1DS, PRT2DS, PRT1DM
      USE W3DISPMD, ONLY: NAR1D, DFAC, N1MAX, ECG1, EWN1, DSIE
      USE NETCDF
!/IG1       USE W3GIG1MD, ONLY: W3ADDIG
!/IG1       USE W3CANOMD, ONLY: W3ADD2NDORDER

      IMPLICIT NONE

!/
!/ ------------------------------------------------------------------- /

      INTEGER, INTENT(IN)       :: I, NCID, NREQ, INDREQ(NREQ)
      LOGICAL, INTENT(IN)       :: ORDER



!/ Local parameters
!/
      INTEGER                   :: J, J1, I1, I2, ISP, IKM, IKL, IKH, ITH,  &
                                   IK, ITT, NPART, IX, IY, ISEA
!/S      INTEGER, SAVE           :: IENT   = 0
      REAL                      :: depth, SQRTH, CDIR, SIX, R1, R2,     &
                                   UDIR, UDIRR, UABS, XL, XH, XL2, XH2, &
                                   ET, EWN, ETR, ETX, ETY, EBND, EBX,   &
                                   EBY, HSIG, WLEN, TMEAN, THMEAN,      &
                                   THSPRD, EMAX, EL, EH, DENOM, FP, THP,&
                                   SPP, CD, USTAR, FACTOR, UNORM, ESTAR,&
                                   FPSTAR, FACF, FACE, FACS, HMAT, WNA, &
                                   XYZ, AGE1, AFR, AGE2, FACT, XSTAR,   &
                                   YSTAR, FHIGH, ZWND, Z0, USTD, EMEAN, &
                                   FMEAN, WNMEAN, UDIRCA, CHARN, M2KM
      REAL                      :: ICETHICK, ICECON, WN_R(NK),CG_ICE(NK), &
                                   ALPHA_LIU(NK), R(NK)
!/IS2       REAL                     :: ICEF, ICEDMAX                              
      REAL                      :: UDIR1(NREQ), CDIR1(NREQ)
      LOGICAL                   :: LASTSTATION=.FALSE.
      LOGICAL                   :: SHORT=.TRUE.
!/ST1      REAL                    :: AMAX, FH1, FH2
!/ST2      REAL                    :: AMAX, ALPHA(NK), FPI
!/ST3      REAL                    :: AMAX, FMEANS, FMEANWS, TAUWX, TAUWY, &
!/ST3                                 TAUWNX, TAUWNY
!/ST4      REAL                    :: AMAX, FMEANS, FMEANWS, TAUWX, TAUWY, &
!/ST4                                 TAUWNX, TAUWNY, FMEAN1, WHITECAP(1:4)
!/ST6      REAL                    :: AMAX, TAUWX, TAUWY, TAUWNX, TAUWNY
!/BS1      REAL                    :: TAUSCX, TAUSCY
!/BT4      REAL                    :: D50, PSIC, BEDFORM(3), TAUBBL(2)
           REAL                    :: ICE
!/STAB2      REAL                    :: STAB0, STAB,  COR1, COR2, ASFAC,     &
!/STAB2                                 THARG1, THARG2
      REAL, SAVE                :: HSMIN  = 0.05
      REAL                      :: WN(NK), CG(NK)
      REAL                      :: E3(NTH,NK,NREQ)
      REAL                      :: E(NK,NTH), E1(NK), APM(NK),          &
                                   THBND(NK), SPBND(NK), A(NTH,NK),     &
                                   WN2(NTH,NK)
      REAL                      :: STT(NK,NTH), SWN(NK,NTH), SNL(NK,NTH),&
                                   SDS(NK,NTH), SBT(NK,NTH), SIS(NK,NTH)
      REAL                      :: XIN(NTH,NK), XNL(NTH,NK), XTR(NTH,NK),&
                                   XDS(NTH,NK), XDB(NTH,NK), XBT(NTH,NK),&
                                   XBS(NTH,NK), XXX(NTH,NK), DIA(NTH,NK),&
                                   XLN(NTH,NK), XWL(NTH,NK), XIS(NTH,NK), DIA2(NTH,NK)
      REAL                      :: SIN1(NK), SNL1(NK), SDS1(NK),         &
                                   SBT1(NK), SIS1(NK), STT1(NK), E1ALL(NK,6)
!/ST3      LOGICAL                 :: LLWS(NSPEC)
!/ST4      LOGICAL                 :: LLWS(NSPEC)
!/ST4      REAL                    :: LAMBDA(NSPEC)
      CHARACTER*4               :: VAR1(6)
      DOUBLE PRECISION          :: timenc
      INTEGER                   :: iyc,imc,idc,ihc,iminc,isc
!
      DATA VAR1   / 'Sin ' , 'Snl ', 'Sds ' , 'Sbt ' , 'Sice', 'Stot' /



!/
!/ ------------------------------------------------------------------- /
!/
! 1. Initialisations
!
!/S      CALL STRACE (IENT, 'W3EXNC')
!
      IF ( FLAGLL ) THEN
          M2KM   = 1.
        ELSE
          M2KM   = 1.E-3
        END IF
!
      XL     = 1./XFR - 1.
      XH     =  XFR - 1.
      XL2    = XL**2
      XH2    = XH**2
!
      IF ( ITYPE .EQ. 3 ) THEN
          XLN = 0.
          XIN = 0.
          XNL = 0.
          XTR = 0.
          XDS = 0.
          XDB = 0.
          XBT = 0.
          XBS = 0.
          XWL = 0.
          XIS = 0.
          XXX = 0.
        END IF

!
!/T      WRITE (NDST,9000) (FLREQ(J),J=1,NOPTS)
!/T      WRITE (NDST,9001) ITYPE, OTYPE, NREQ, SCALE1, SCALE2, FLSRCE

!
!--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
!     Loop over output points.
!



!
! Selects first station index 
!
      IF (TOGETHER) THEN
        J=1
      ELSE
        J=I
        END IF
!
! Short version of the ww3_ounp code for ITYPE = 1
!                                    and OTYPE = 3
!
      IF (SHORT.AND.ITYPE.EQ.1.AND.OTYPE.EQ.3) THEN 

          DEPTH  = MAX ( DMIN, DPO(J) )
          SQRTH  = SQRT ( depth )
          DO IK=1, NK
            SIX    = SIG(IK) * SQRTH
            I1     = INT(SIX/DSIE)
            IF (I1.LE.N1MAX) THEN
              I2 = I1 + 1
              R1 = SIX/DSIE - REAL(I1)
              R2 = 1. - R1
              WN(IK) = ( R2*EWN1(I1) + R1*EWN1(I2) ) / depth
              CG(IK) = ( R2*ECG1(I1) + R1*ECG1(I2) ) * SQRTH
            ELSE
              WN(IK) = SIG(IK)*SIG(IK)/GRAV
              CG(IK) = 0.5 * GRAV / SIG(IK)
              END IF
!/T              WRITE (NDST,9011) IK, TPI/SIG(IK), WN(IK), CG(IK)
!
            END DO


!
! Computes 2nd order spectrum 
!
!/IG1      IF (IGPARS(2).EQ.1) THEN 
!/IG1        IF(IGPARS(1).EQ.1) THEN 
!/IG1          CALL W3ADDIG(SPCO(:,J),DPO(J),WN,CG,0)
!/IG1        ELSE
!/IG1          CALL W3ADD2NDORDER(SPCO(:,J),DPO(J),WN,CG,0)
!/IG1          END IF
!/IG1        END IF
!

        DO J1=1, NREQ
          DO IK=1, NK
            DO ITH=1, NTH
              ISP    = ITH + (IK-1)*NTH
              E3(ITH,IK,J1) = SPCO(ISP,INDREQ(J1))
            END DO
          END DO
        END DO
        iyc=TIME(1)/10000
        imc=(TIME(1)-iyc*10000)/100
        idc=TIME(1)-iyc*10000-100*imc
        ihc=TIME(2)/10000
        iminc=(TIME(2)-ihc*10000)/100
        isc=TIME(2)-ihc*10000-100*iminc

        timenc=DBLE(julday(idc,imc,iyc)) - DBLE(julday(1,1,1990))              &
               + DBLE((ihc + DBLE((iminc + DBLE(isc /60.)) /60.)) /24.)
!
        iret=nf90_put_var(ncid,varid(1),timenc,(/IOUT/))
        call check_err(iret)
! 
        IF (IOUT.EQ.1) THEN 
          DO J1=1, NREQ
            iret=nf90_put_var(ncid,varid(27),INDREQ(J1),(/J1/))
            call check_err(iret)
            iret=nf90_put_var(ncid,varid(2),PTNME(INDREQ(J1)),         &
                start=(/1,J1/),count=(/LEN_TRIM(PTNME(INDREQ(J1))) ,1/))
            call check_err(iret)
          END DO
        END IF
!
        DO J1=1, NREQ
          IF ((FLWW3.NE.0).AND.(ORDER)) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
          IF ((FLWW3.NE.0).AND.(.NOT.ORDER)) iret=nf90_put_var(ncid,varid(3),FLWW3,(/IOUT,J1/))
          call check_err(iret)
        END DO
! 
        IF ( FLFORM ) THEN
          IF(ORDER) iret=nf90_put_var(ncid,varid(4),PTLOC(1,INDREQ(1:NREQ)),(/1,IOUT/))
          IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(4),PTLOC(1,INDREQ(1:NREQ)),(/IOUT,1/))
          call check_err(iret)
          IF(ORDER) iret=nf90_put_var(ncid,varid(5),PTLOC(2,INDREQ(1:NREQ)),(/1,IOUT/))
          IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(5),PTLOC(2,INDREQ(1:NREQ)),(/IOUT,1/))
          call check_err(iret)
        ELSE
          IF(ORDER) iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,INDREQ(1:NREQ)),(/1,IOUT/))
          IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,INDREQ(1:NREQ)),(/IOUT,1/))
          call check_err(iret)
          IF(ORDER) iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,INDREQ(1:NREQ)),(/1,IOUT/))
          IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,INDREQ(1:NREQ)),(/IOUT,1/))
          call check_err(iret)
        END IF
!             
        DO J1=1,NREQ
          UDIR1(J1)  = MOD ( 270. - WDO(INDREQ(J1))*RADE , 360. )
          CDIR1(J1)   = MOD ( 270. - CDO(INDREQ(J1))*RADE , 360. )
        END DO
! 
        IF(ORDER) iret=nf90_put_var(ncid,varid(11),DPO(INDREQ(1:NREQ)),(/1,IOUT/))
        IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(11),DPO(INDREQ(1:NREQ)),(/IOUT,1/))
        call check_err(iret)
        IF(ORDER) iret=nf90_put_var(ncid,varid(12),WAO(INDREQ(1:NREQ)),(/1,IOUT/))
        IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(12),WAO(INDREQ(1:NREQ)),(/IOUT,1/))
        call check_err(iret)
        IF(ORDER) iret=nf90_put_var(ncid,varid(13),UDIR1,(/1,IOUT/))
        IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(13),UDIR1,(/IOUT,1/))
        call check_err(iret)
        IF(ORDER) iret=nf90_put_var(ncid,varid(14),CAO(INDREQ(1:NREQ)),(/1,IOUT/))
        IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(14),CAO(INDREQ(1:NREQ)),(/IOUT,1/))
        call check_err(iret)
        IF(ORDER) iret=nf90_put_var(ncid,varid(15),CDIR1,(/1,IOUT/))
        IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(15),CDIR1,(/IOUT,1/))
        call check_err(iret)
!
        IF(ORDER) iret=nf90_put_var(ncid,varid(10),E3(1:NTH,1:NK,1:NREQ), &
                           start=(/1,1,1,IOUT/),count=(/NTH,NK,NREQ,1/))
        IF(.NOT.ORDER) iret=nf90_put_var(ncid,varid(10),E3(1:NTH,1:NK,1:NREQ), &
                           start=(/1,1,IOUT,1/),count=(/NTH,NK,1,NREQ/))
        call check_err(iret)
!
! End of short version 
!
      ELSE 
!
! And here is the full thing with all options ITYPE and OTYPE ... 
!
      J1=1
      LASTSTATION=.FALSE.
!
      DO WHILE (.NOT.LASTSTATION)
!
        IF ( FLREQ(J) ) THEN
!
!  Open netCDF file
!
!/T            WRITE (NDST,9002) PTNME(J)
!
! 2. Calculate grid parameters using and inlined version of WAVNU1.
!
          DEPTH    = MAX ( DMIN, DPO(J) )
          SQRTH    = SQRT ( depth )
          UDIR     = MOD ( 270. - WDO(J)*RADE , 360. )
          UDIRCA   = WDO(J)*RADE
          UDIRR    = WDO(J)
          UABS     = MAX ( 0.001 , WAO(J) )
          CDIR     = MOD ( 270. - CDO(J)*RADE , 360. )
!/IS2           ICEDMAX  = MAX ( 0., ICEFO(J))
!/IS2           ICEF     = ICEDMAX
          ICETHICK = MAX (0., ICEHO(J))
          ICECON   = MAX (0., ICEO(J))
!
!/STAB2            STAB0  = ZWIND * GRAV / 273.
!/STAB2            STAB   = STAB0 * ASO(J) / MAX(5.,WAO(J))**2
!/STAB2            STAB   = MAX ( -1. , MIN ( 1. , STAB ) )
!/STAB2            THARG1 = MAX ( 0. , FFNG*(STAB-OFSTAB))
!/STAB2            THARG2 = MAX ( 0. , FFPS*(STAB-OFSTAB))
!/STAB2            COR1   = CCNG * TANH(THARG1)
!/STAB2            COR2   = CCPS * TANH(THARG2)
!/STAB2            ASFAC  = SQRT ( (1.+COR1+COR2)/SHSTAB )
!
!/T            WRITE (NDST,9010) depth
          DO IK=1, NK
            SIX    = SIG(IK) * SQRTH
            I1     = INT(SIX/DSIE)
            IF (I1.LE.N1MAX) THEN
              I2 = I1 + 1
              R1 = SIX/DSIE - REAL(I1)
              R2 = 1. - R1
              WN(IK) = ( R2*EWN1(I1) + R1*EWN1(I2) ) / depth
              CG(IK) = ( R2*ECG1(I1) + R1*ECG1(I2) ) * SQRTH
            ELSE
              WN(IK) = SIG(IK)*SIG(IK)/GRAV
              CG(IK) = 0.5 * GRAV / SIG(IK)
            END IF
!/T              WRITE (NDST,9011) IK, TPI/SIG(IK), WN(IK), CG(IK)
!
          END DO
!
! Computes 2nd order spectrum 
!
!/IG1      IF (IGPARS(2).EQ.1) THEN 
!/IG1        IF(IGPARS(1).EQ.1) THEN 
!/IG1          CALL W3ADDIG(SPCO(:,J),DPO(J),WN,CG,0)
!/IG1        ELSE
!/IG1          CALL W3ADD2NDORDER(SPCO(:,J),DPO(J),WN,CG,0)
!/IG1          END IF
!/IG1        END IF
!
!
! 3.  Prepare spectra etc.
! 3.a Mean wave parameters.
!
          ET     = 0.
          EWN    = 0.
          ETR    = 0.
          ETX    = 0.
          ETY    = 0.
          DO IK=1, NK
            EBND   = 0.
            EBX    = 0.
            EBY    = 0.
            DO ITH=1, NTH
              ISP    = ITH + (IK-1)*NTH
              E(IK,ITH) = SPCO(ISP,J)
              EBND   = EBND + SPCO(ISP,J)
              EBX    = EBX  + SPCO(ISP,J)*ECOS(ITH)
              EBY    = EBY  + SPCO(ISP,J)*ESIN(ITH)
            END DO
            E1(IK) = EBND * DTH
            APM(IK)= E1(IK) / ( TPI * GRAV**2 / SIG(IK)**5  )
            IF ( E1(IK) .GT. 1.E-5) THEN
              THBND(IK) = MOD(630.- RADE*ATAN2(EBY,EBX),360.)
              SPBND(IK) = RADE * SQRT ( MAX ( 0. , 2.*( 1. -      &
                SQRT( MAX(0.,(EBX**2+EBY**2)/EBND**2) ) ) ) )
            ELSE
              THBND(IK) = -999.9
              SPBND(IK) = -999.9
            END IF
            EBND   = E1(IK) * DSII(IK) * TPIINV
            ET     = ET  + EBND
            EWN    = EWN + EBND / WN(IK)
            ETR    = ETR + EBND / SIG(IK)
            ETX    = ETX + EBX * DSII(IK)
            ETY    = ETY + EBY * DSII(IK)
          END DO
!
! tail factors for radian action etc ...!
!
          EBND   = E1(NK) * TPIINV / ( SIG(NK) * DTH )
          ET     = ET  + FTE *EBND
          EWN    = EWN + FTWL*EBND
          ETR    = ETR + FTTR*EBND
          ETX    = DTH*ETX*TPIINV + FTE*EBX*TPIINV/SIG(NK)
          ETY    = DTH*ETY*TPIINV + FTE*EBY*TPIINV/SIG(NK)
!
          HSIG   = 4. * SQRT ( ET )
          IF ( HSIG .GT. HSMIN ) THEN
            WLEN   = EWN / ET * TPI
            TMEAN  = ETR / ET * TPI
            THMEAN = MOD ( 630. - RADE*ATAN2(ETY,ETX) , 360. )
            THSPRD = RADE * SQRT ( MAX ( 0. , 2.*( 1. - SQRT(     &
                       MAX(0.,(ETX**2+ETY**2)/ET**2) ) ) ) )
            IF ( THSPRD .LT. 0.01*RADE*DTH ) THSPRD = 0.
          ELSE
            WLEN   = 0.
            TMEAN  = 0.
            THMEAN = 0.
            THSPRD = 0.
            E1(1:NK) = 0.
            E(1:NK,1:NTH) = 0.
          END IF
!
! 3.b peak frequency
!
          EMAX   = E1(NK)
          IKM    = NK
!
          DO IK=NK-1, 1, -1
            IF ( E1(IK) .GT. EMAX ) THEN
              EMAX   = E1(IK)
              IKM    = IK
            END IF
          END DO
!
          IKL    = MAX (  1 , IKM-1 )
          IKH    = MIN ( NK , IKM+1 )
          EL     = E1(IKL) - E1(IKM)
          EH     = E1(IKH) - E1(IKM)
          DENOM  = XL*EH - XH*EL
!
          IF ( HSIG .GE. HSMIN ) THEN
            FP     = SIG(IKM) * ( 1. + 0.5 * ( XL2*EH - XH2*EL )  &
                        / SIGN ( MAX(ABS(DENOM),1.E-15) , DENOM ) )
            THP    = THBND(IKM)
            SPP    = SPBND(IKM)
            IF ( SPP .LT. 0.01*RADE*DTH ) SPP = 0.
          ELSE
            FP     = 0.
            THP    = 0.
            SPP    = 0.
          END IF
!
! 3.c spectral partitioning
!
          IF ( ITYPE.EQ.1 .AND. OTYPE.EQ.4 ) THEN
            CALL W3PART( E, UABS, UDIRCA, depth, WN, NPART, XPART, DIMXP )
          END IF
!
! 3.d nondimensional parameters
!
          IF ( ( ITYPE.EQ.2 .AND. (OTYPE.EQ.3.OR.OTYPE.EQ.4) ) .OR. &
             ( ITYPE.EQ.1 .AND. (OTYPE.EQ.2) ) ) THEN
!
            DO IK=1, NK
              FACTOR = TPIINV * CG(IK) / SIG(IK)
              DO ITH=1, NTH
                ISP    = ITH + (IK-1)*NTH
                A(ITH,IK)   = FACTOR * SPCO(ISP,J)
                WN2(ITH,IK) = WN(IK)
              END DO
            END DO
!
!/STAB2                UABS   = UABS / ASFAC
!
!/ST0                ZWND   = 10.
!/ST1                ZWND   = 10.
!/ST2                ZWND   = ZWIND
!/ST3                ZWND   = ZZWND
!/ST3                TAUWX  = 0.
!/ST3                TAUWY  = 0.
!/ST3                LLWS(:)  = .TRUE.
!/ST4                LLWS(:)  = .TRUE.
!/ST4                ZWND   = ZZWND
!/ST4                TAUWX  = 0.
!/ST4                TAUWY  = 0.
!/ST6                ZWND   = 10.

!
!/ST1                CALL W3SPR1 (A, CG, WN, EMEAN, FMEAN, WNMEAN, AMAX)
!/ST1                FP     = 0.85 * FMEAN
!/ST2                CALL W3SPR2 (A, CG, WN, DEPTH, FP , UABS, USTAR, &
!/ST2                             EMEAN, FMEAN, WNMEAN, AMAX, ALPHA, FP )
!/ST3                CALL W3SPR3 (A, CG, WN, EMEAN, FMEAN, FMEANS,       &
!/ST3                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,&
!/ST3                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/ST4                CALL W3SPR4 (A, CG, WN, EMEAN, FMEAN, FMEAN1,       &
!/ST4                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,&
!/ST4                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/ST6                CALL W3SPR6 (A, CG, WN, EMEAN, FMEAN, WNMEAN, AMAX, FP)
!
!/FLX1                CALL W3FLX1 ( ZWND, UABS, UDIRR,                   &
!/FLX1                              USTAR, USTD, Z0, CD )
!/FLX2                CALL W3FLX2 ( ZWND, DEPTH, FP, UABS, UDIRR,        &
!/FLX2                                          USTAR, USTD, Z0, CD )
!/FLX3                CALL W3FLX3 ( ZWND, DEPTH, FP, UABS, UDIRR,        &
!/FLX3                                          USTAR, USTD, Z0, CD )
!/FLX4                CALL W3FLX4 ( ZWND, UABS, UDIRR, USTAR, USTD, Z0, CD )
!/FLXX                CALL W3FLXX
!
            DO ITT=1, 3
!/ST2                  CALL W3SIN2 (A, CG, WN2, UABS, UDIRR, CD, Z0,    &
!/ST2                                                 FPI, XIN, DIA )
!/ST2                  CALL W3SPR2 (A, CG, WN, DEPTH, FPI, UABS, USTAR, &
!/ST2                               EMEAN, FMEAN, WNMEAN, AMAX, ALPHA, FP )
!/ST3                  IX=1
!/ST3                  IY=1
!/ST3                  CALL W3SIN3 ( A, CG, WN2, UABS, USTAR, DAIR/DWAT,&
!/ST3                               ASO(J), UDIRR, Z0, CD, TAUWX, TAUWY,&
!/ST3                               TAUWNX, TAUWNY, ICE, XIN, DIA, LLWS, IX, IY )
!/ST3                  CALL W3SPR3 (A, CG, WN, EMEAN, FMEAN, FMEANS,       &
!/ST3                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,&
!/ST3                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/ST4                  IX=1
!/ST4                  IY=1
!/ST4                   CALL W3SDS4 ( A, WN, CG, USTAR,  USTD, DEPTH, XDS,   &
!/ST4                      DIA, IX, IY, LAMBDA, WHITECAP )
!/ST4                   CALL W3SDS4 ( A, WN, CG, USTAR,  USTD, DEPTH, XDS,   &
!/ST4                              DIA, IX, IY, LAMBDA, WHITECAP )
!/ST4                   CALL W3SDS4 ( A, WN, CG, USTAR,  USTD, DEPTH, XDS,   &
!/ST4                              DIA, IX, IY, LAMBDA, WHITECAP )
!/ST4                   CALL W3SIN4 (A, CG, WN2, UABS, USTAR, DAIR/DWAT,     &
!/ST4                           ASO(J), UDIRR, Z0, CD, TAUWX, TAUWY, TAUWNX, &
!/ST4                           TAUWNY, XIN, DIA, LLWS, IX, IY, LAMBDA )
!/ST4                   CALL W3SPR4 (A, CG, WN, EMEAN, FMEAN, FMEAN1,      &
!/ST4                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,&
!/ST4                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/FLX2                  CALL W3FLX2 ( ZWND, DEPTH, FP, UABS, UDIRR,      &
!/FLX2                                            USTAR, USTD, Z0, CD )
!/FLX3                  CALL W3FLX3 ( ZWND, DEPTH, FP, UABS, UDIRR,      &
!/FLX3                                            USTAR, USTD, Z0, CD )
            END DO
!
! Add alternative flux calculations here as part of !/ST2 option ....
! Also add before actual source term calculation !!!
!
!/STAB2                UABS   = UABS * ASFAC
!
            IF ( WAO(J) .LT. 0.01 ) THEN
              UNORM  = 0.
              ESTAR  = 0.
              FPSTAR = 0.
            ELSE
              IF ( OTYPE.EQ.3 ) THEN
                UNORM  = USTAR
              ELSE
                UNORM  = WAO(J)
              END IF
              ESTAR  = ET * GRAV**2 / UNORM**4
              FPSTAR = FP * TPIINV * UNORM / GRAV
              XSTAR  = PTLOC(1,J) * GRAV / UNORM**2
              YSTAR  = PTLOC(2,J) * GRAV / UNORM**2
              IF ( FLAGLL ) THEN
                XSTAR  = XSTAR * DERA * RADIUS &
                       * COS(PTLOC(2,J)*DERA)
                YSTAR  = YSTAR * DERA * RADIUS
              END IF
            END IF
!
          END IF ! 3.d

!
! 3.e source terms
!
          IF ( ITYPE.EQ.3 ) THEN
!
            DO IK=1, NK
              FACTOR = TPIINV * CG(IK) / SIG(IK)
              DO ITH=1, NTH
                A(ITH,IK)   = FACTOR * SPCO(ITH+(IK-1)*NTH,J)
                WN2(ITH,IK) = WN(IK)
              END DO
            END DO
!
!/STAB2                UABS   = UABS / ASFAC
!
!/ST0                ZWND   = 10.
!/ST1                ZWND   = 10.
!/ST2                ZWND   = ZWIND
!/ST3                ZWND   = ZZWND
!/ST0                USTAR  = 1.
!/ST1                USTAR  = 1.
!/ST2                USTAR  = 1.
!/ST3                USTAR  = 0.
!/ST3                USTD   = 0.
!/ST3                TAUWX  = 0.
!/ST3                TAUWY  = 0.
!/ST4                ZWND   = ZZWND
!/ST4                USTAR  = 0.
!/ST4                USTD   = 0.
!/ST4                TAUWX  = 0.
!/ST4                TAUWY  = 0.
!/ST6                ZWND   = 10.
!
!/ST0                FHIGH  = SIG(NK)
!/ST1                CALL W3SPR1 (A, CG, WN, EMEAN, FMEAN, WNMEAN, AMAX)
!/ST1                FP     = 0.85 * FMEAN
!/ST1                FH1    = FXFM * FMEAN
!/ST1                FH2    = FXPM / USTAR
!/ST1                FHIGH  = MAX ( FH1 , FH2 )
!/ST2                CALL W3SPR2 (A, CG, WN, DEPTH, FP , UABS, USTAR, &
!/ST2                             EMEAN, FMEAN, WNMEAN, AMAX, ALPHA, FP )
!/ST3                CALL W3SPR3 (A, CG, WN, EMEAN, FMEAN, FMEANS,       &
!/ST3                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,&
!/ST3                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/ST4                CALL W3SPR4 (A, CG, WN, EMEAN, FMEAN,  FMEAN1,        &
!/ST4                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,&
!/ST4                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/ST4                CALL W3SDS4 ( A, WN, CG, USTAR,  USTD, DEPTH, XDS,   &
!/ST4                             DIA, IX, IY, LAMBDA, WHITECAP )
!/ST6                CALL W3SPR6 (A, CG, WN, EMEAN, FMEAN, WNMEAN, AMAX, FP)
!/ST6                FHIGH  = SIG(NK)
!/STX                CALL W3SPRX
!
!/FLX1                CALL W3FLX1 ( ZWND, UABS, UDIRR,                   &
!/FLX1                              USTAR, USTD, Z0, CD )
!/FLX2                CALL W3FLX2 ( ZWND, DEPTH, FP, UABS, UDIRR,        &
!/FLX2                                          USTAR, USTD, Z0, CD )
!/FLX3                CALL W3FLX3 ( ZWND, DEPTH, FP, UABS, UDIRR,        &
!/FLX3                                          USTAR, USTD, Z0, CD )
!/FLX4                CALL W3FLX4 ( ZWND, UABS, UDIRR, USTAR, USTD, Z0, CD )
!/FLXX                CALL W3FLXX
!
            DO ITT=1, 3
!/ST2                  CALL W3SIN2 (A, CG, WN2, UABS, UDIRR, CD, Z0,    &
!/ST2                                                 FPI, XIN, DIA )
!/ST2                  CALL W3SPR2 (A, CG, WN, DEPTH, FPI, UABS, USTAR, &
!/ST2                               EMEAN, FMEAN, WNMEAN, AMAX, ALPHA, FP )
!/ST3                  CALL W3SPR3 (A, CG, WN, EMEAN, FMEAN, FMEANS,       &
!/ST3                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,&
!/ST3                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/ST3                  CALL W3SIN3 ( A, CG, WN2, UABS, USTAR, DAIR/DWAT,&
!/ST3                                ASO(J), UDIRR, Z0, CD,TAUWX, TAUWY, &
!/ST3                                TAUWNX, TAUWNY, ICE, XIN, DIA, LLWS, IX, IY )
!/ST4                  CALL W3SPR4 (A, CG, WN, EMEAN, FMEAN, FMEAN1,       &
!/ST4                             WNMEAN, AMAX, UABS, UDIRR, USTAR, USTD,  &
!/ST4                             TAUWX, TAUWY, CD, Z0, CHARN, LLWS, FMEANWS )
!/ST4                  CALL W3SIN4 (A, CG, WN2, UABS, USTAR, DAIR/DWAT,    &
!/ST4                           ASO(J), UDIRR, Z0, CD, TAUWX, TAUWY,TAUWNX,&
!/ST4                           TAUWNY, XIN, DIA, LLWS, IX, IY, LAMBDA )
!/FLX2                  CALL W3FLX2 ( ZWND, DEPTH, FP, UABS, UDIRR,      &
!/FLX2                                            USTAR, USTD, Z0, CD )
!/FLX3                  CALL W3FLX3 ( ZWND, DEPTH, FP, UABS, UDIRR,      &
!/FLX3                                            USTAR, USTD, Z0, CD )
            END DO
!
!/ST2                FHIGH  = XFC * FPI
!
            IF ( FLSRCE(2) ) THEN
!/LN1                    CALL W3SLN1 (WN, FHIGH, USTAR, UDIRR, XLN )
!/LNX                    CALL W3SLNX
!
!/ST1                    CALL W3SIN1 (A, WN2, USTAR, UDIRR,  XIN, DIA )
!/ST2                    CALL W3SIN2 (A, CG, WN2, UABS, UDIRR, CD, Z0,&
!/ST2                                                   FPI, XIN, DIA )
!/ST3                    CALL W3SIN3 ( A, CG, WN2, UABS, USTAR,       &
!/ST3                                  DAIR/DWAT, ASO(J), UDIRR,      &
!/ST3                            Z0, CD, TAUWX, TAUWY,TAUWNX, TAUWNY, &
!/ST3                            ICE, XIN, DIA, LLWS, IX, IY )
!
!/ST4                    CALL W3SDS4 ( A, WN, CG, USTAR,  USTD, DEPTH, XDS,  &
!/ST4                              DIA, IX, IY, LAMBDA, WHITECAP )
!/ST4                    CALL W3SIN4 (A, CG, WN2, UABS, USTAR, DAIR/DWAT,    &
!/ST4                           ASO(J), UDIRR, Z0, CD, TAUWX, TAUWY, TAUWNX, &
!/ST4                           TAUWNY, XIN, DIA, LLWS, IX, IY, LAMBDA )
!/ST6                    CALL W3SIN6 (A, CG, WN2, USTAR, UDIRR, CD,   &
!/ST6                           TAUWX, TAUWY, TAUWNX, TAUWNY, XIN, DIA )
!/STX                    CALL W3SINX
            END IF
            IF ( FLSRCE(3) ) THEN
!/NL1                    CALL W3SNL1 ( A, CG, WNMEAN*DEPTH,  XNL, DIA )
!/NL2                    CALL W3SNL2 ( A, CG, DEPTH,         XNL, DIA )
!/NL3                    CALL W3SNL3 ( A, CG, WN, DEPTH,     XNL, DIA ) 
!/NL4                    CALL W3SNL4 ( A, CG, WN, DEPTH,     XNL, DIA ) 
!/NLX                    CALL W3SNLX
!
!/TRX                    CALL W3STRX
            END IF
            IF ( FLSRCE(4) ) THEN
!/ST1                    CALL W3SDS1 ( A, WN2, EMEAN, FMEAN, WNMEAN,  &
!/ST1                                                        XDS, DIA )
!/ST2                    CALL W3SDS2 ( A, CG, WN, FPI, USTAR,         &
!/ST2                                  ALPHA,                XDS, DIA )
!/ST3                    CALL W3SDS3 ( A, WN, CG, EMEAN, FMEANS, WNMEAN,  &
!/ST3                                  USTAR, USTD, DEPTH, XDS, DIA, IX, IY )
!/ST4                    CALL W3SDS4 ( A, WN, CG,  USTAR, USTD, DEPTH, XDS, &
!/ST4                                  DIA, IX, IY, LAMBDA, WHITECAP )
!/ST6                    CALL W3SDS6 ( A, CG, WN, XDS, DIA )
!/ST6                    IF (SWL6S6) CALL W3SWL6 ( A, CG, WN, XWL, DIA )
!/STX                    CALL W3SDSX
!
!/DB1                    CALL W3SDB1 ( A, WN2, DEPTH, EMEAN, FMEAN,   &
!/DB1                                  WNMEAN,               XDB, DIA )
!/DBX                    CALL W3SDBX
            END IF
            IF ( FLSRCE(5) ) THEN
!/BT1                    CALL W3SBT1 ( A, CG, WN, DEPTH,     XBT, DIA )
!/BT2                    SBTC2 = 2. * -0.067 / GRAV
!/BT2                    CALL W3SBT2 ( A, CG, WN, DEPTH, XBT, DIA, SBTC2 )
!/BT4                    IX=1    ! to be fixed later 
!/BT4                    IY=1    ! to be fixed later 
!/BT4                    ISEA=1  ! to be fixed later 
!/BT4                    D50 = SED_D50(ISEA)
!/BT4                    PSIC= SED_PSIC(ISEA)
!/BT4                    CALL W3SBT4 ( A, CG, WN, DEPTH, D50, PSIC, TAUBBL,   & 
!/BT4                                       BEDFORM, XBT, DIA, IX, IY )

! see remarks about BT8 and BT9 in ww3_outp.ftn
!....broken....!/BT8        CALL W3SBT8 ( SPEC, DEPTH, VSBT, VDBT, IX, IY )
!....broken....!/BT9        CALL W3SBT9 ( SPEC, DEPTH, VSBT, VDBT, IX, IY )

!/BTX                    CALL W3SBTX

!
!/BS1                    CALL W3SBS1 ( A, CG, WN, DEPTH,              &
!/BS1                           CAO(J)*COS(CDO(J)), CAO(J)*SIN(CDO(J)), &
!/BS1                                  TAUSCX, TAUSCY,   XBS, DIA )
!/BSX                    CALL W3SBSX
!
            END IF
            IF ( FLSRCE(6) ) THEN
              IF (IICEDISP) THEN  
                      CALL LIU_FORWARD_DISPERSION (ICETHICK,0.,DEPTH, &
                     SIG,WN_R,CG_ICE,ALPHA_LIU)
              ELSE
                WN_R=WN
                CG_ICE=CG
                END IF
!
!/IS2              CALL W3SIS2(A, DEPTH, ICECON, ICETHICK, ICEF, ICEDMAX,  &
!/IS2                          IX, IY, XIS, DIA, DIA2, WN, CG, WN_R, CG_ICE, R)          
            END IF
!
!/XXX                CALL W3SXXX
!
!/STAB2                UABS   = UABS * ASFAC
!
            IF ( ISCALE.EQ.0 .OR. ISCALE.EQ.3 ) THEN
              FACF   = TPIINV
              FACE   = 1.
              FACS   = 1.
            ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.4 ) THEN
              FACF   = TPIINV * UABS / GRAV
              FACE   = GRAV**3 / UABS**5
              FACS   = GRAV**2 / UABS**4
            ELSE IF ( ISCALE.EQ.2 .OR. ISCALE.EQ.5 ) THEN
              FACF   = TPIINV * USTAR / GRAV
              FACE   = GRAV**3 / USTAR**5
              FACS   = GRAV**2 / USTAR**4
            END IF
!
            DO IK=1, NK
              FACTOR = TPI / CG(IK) * SIG(IK)
              E1  (IK) = 0.
              SIN1(IK) = 0.
              SNL1(IK) = 0.
              SDS1(IK) = 0.
              SBT1(IK) = 0.
              SIS1(IK) = 0.
              STT1(IK) = 0.
              DO ITH=1, NTH
                ISP         = ITH + (IK-1)*NTH
                E  (IK,ITH) = SPCO(ISP,J)
                SWN(IK,ITH) = ( XLN(ITH,IK) + XIN(ITH,IK) ) * FACTOR
                SNL(IK,ITH) = ( XNL(ITH,IK) + XTR(ITH,IK) ) * FACTOR
                SDS(IK,ITH) = ( XDS(ITH,IK) + XDB(ITH,IK) ) * FACTOR
!/ST6         SDS(IK,ITH) =   SDS(IK,ITH) +(XWL(ITH,IK)   * FACTOR)
                SBT(IK,ITH) = ( XBT(ITH,IK) + XBS(ITH,IK) ) * FACTOR
                SIS(IK,ITH) = XIS(ITH,IK) * FACTOR
                STT(IK,ITH) = SWN(IK,ITH) + SNL(IK,ITH) + SDS(IK,ITH) + &
                              SBT(IK,ITH) + SIS(IK,ITH) + XXX(ITH,IK) * FACTOR
                E1  (IK) = E1  (IK) + E(IK,ITH)
                SIN1(IK) = SIN1(IK) + SWN(IK,ITH)
                SNL1(IK) = SNL1(IK) + SNL(IK,ITH)
                SDS1(IK) = SDS1(IK) + SDS(IK,ITH)
                SBT1(IK) = SBT1(IK) + SBT(IK,ITH)
                SIS1(IK) = SIS1(IK) + SIS(IK,ITH)
              END DO
              E1  (IK) = E1(IK)   * DTH * FACE
              SIN1(IK) = SIN1(IK) * DTH * FACS
              SNL1(IK) = SNL1(IK) * DTH * FACS
              SDS1(IK) = SDS1(IK) * DTH * FACS
              SBT1(IK) = SBT1(IK) * DTH * FACS
              SIS1(IK) = SIS1(IK) * DTH * FACS
            END DO
!
            STT1       = SIN1 + SNL1 + SDS1 + SBT1 + SIS1
            E1ALL(:,1) = SIN1
            E1ALL(:,2) = SNL1
            E1ALL(:,3) = SDS1
            E1ALL(:,4) = SBT1
            E1ALL(:,5) = SIS1
            E1ALL(:,6) = STT1
!
          END IF ! 3.e

!
! 4.a Perform output type 1 ( print plots / tables / file )
!
          IF ( ITYPE .EQ. 1 ) THEN
!
!  Format Time
!
            IF ( OTYPE .NE. 1 ) THEN

              iyc=TIME(1)/10000
              imc=(TIME(1)-iyc*10000)/100
              idc=TIME(1)-iyc*10000-100*imc
              ihc=TIME(2)/10000
              iminc=(TIME(2)-ihc*10000)/100
              isc=TIME(2)-ihc*10000-100*iminc
   
              timenc=DBLE(julday(idc,imc,iyc)) - DBLE(julday(1,1,1990))              &
                     + DBLE((ihc + DBLE((iminc + DBLE(isc /60.)) /60.)) /24.)

              iret=nf90_put_var(ncid,varid(1),timenc,(/IOUT/))
              call check_err(iret)
            END IF  


!
!  Performs subtype 1 
!
            IF ( OTYPE .EQ. 1 ) THEN
!
              IF ( SCALE1 .GE. 0. )                             &
                  CALL PRT1DS (NDSO, NK, E1, SIG(1:NK), 'RAD/S',&
                       17, SCALE1, 'E(f)', 'm^2s', PTNME(J) )
              IF ( SCALE2 .GE. 0. )                             &
                  CALL PRT2DS (NDSO, NK, NK, NTH, E, SIG(1:NK), &
                       'RAD/S', 1., SCALE2, 0.0001, 'E(f,th)',  &
                       'm^2s', PTNME(J) )
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,910) DPO(J), UABS
              IF ( (WAO(J) .GT. 0.) .AND. (IAPROC .EQ. NAPOUT) ) WRITE (NDSO,911) UDIR
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,912) ASO(J), CAO(J)
              IF ( (CAO(J) .GT. 0.) .AND. (IAPROC .EQ. NAPOUT) ) WRITE (NDSO,913) CDIR
              IF ( IAPROC .EQ. NAPOUT ) WRITE (NDSO,914) HSIG, WLEN, TMEAN, THMEAN, THSPRD


!
!  Performs subtype 2
!
            ELSE IF ( OTYPE .EQ. 2 ) THEN

              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              call check_err(iret)
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(7),DPO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(8),USTAR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(9),WAO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(10),UDIR,(/J1,IOUT/))

              IF ( FP .EQ. 0. ) FP = SIG(NK)
              iret=nf90_put_var(ncid,varid(11),SIG(1:NK)/FP,start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(12),E1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(13),THBND(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(14),SPBND(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(15),APM(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))


!
!  Performs subtype 3
!
            ELSE IF ( OTYPE .EQ. 3 ) THEN
              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              call check_err(iret)
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              IF ( FLFORM ) THEN
                iret=nf90_put_var(ncid,varid(4),PTLOC(1,J),(/J1,IOUT/))
                iret=nf90_put_var(ncid,varid(5),PTLOC(2,J),(/J1,IOUT/))
              ELSE
                iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
                iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              END IF
              iret=nf90_put_var(ncid,varid(11),DPO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(12),WAO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(13),UDIR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(14),CAO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(15),CDIR,(/J1,IOUT/))


              iret=nf90_put_var(ncid,varid(10),TRANSPOSE(E(1:NK,1:NTH)), &
                             start=(/1,1,J1,IOUT/),count=(/NTH,NK,1,1/))
!
!  Performs subtype 4
!
            ELSE IF ( OTYPE .EQ. 4 ) THEN
!
              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              call check_err(iret)
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(6),NPART,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(7),depth,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(8),WAO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(9),UDIR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(10),CAO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(11),CDIR,(/J1,IOUT/))
              ! XPART infos - see w3partmd.ftn - SUBROUTINE PTMEAN
              iret=nf90_put_var(ncid,varid(12),XPART(1,0:NPART),  &
                                start=(/1,J1,IOUT/),count=(/NPART,1,1/))
              iret=nf90_put_var(ncid,varid(13),XPART(2,0:NPART),  &
                                start=(/1,J1,IOUT/),count=(/NPART,1,1/))
              iret=nf90_put_var(ncid,varid(14),XPART(3,0:NPART),start=(/1,J1,IOUT/),count=(/NPART,1,1/))
              iret=nf90_put_var(ncid,varid(15),XPART(4,0:NPART),start=(/1,J1,IOUT/),count=(/NPART,1,1/))
              iret=nf90_put_var(ncid,varid(16),XPART(5,0:NPART),start=(/1,J1,IOUT/),count=(/NPART,1,1/))
              iret=nf90_put_var(ncid,varid(17),XPART(6,0:NPART),start=(/1,J1,IOUT/),count=(/NPART,1,1/))
!
            END IF

!
! 4.b Perform output type 2 ( tables )
!
          ELSE IF ( ITYPE .EQ. 2 ) THEN

!  Format Time
            iyc=TIME(1)/10000
            imc=(TIME(1)-iyc*10000)/100
            idc=TIME(1)-iyc*10000-100*imc
            ihc=TIME(2)/10000
            iminc=(TIME(2)-ihc*10000)/100
            isc=TIME(2)-ihc*10000-100*iminc

            timenc=DBLE(julday(idc,imc,iyc)) - DBLE(julday(1,1,1990))              &
                   + DBLE((ihc + DBLE((iminc + DBLE(isc /60.)) /60.)) /24.)

            iret=nf90_put_var(ncid,varid(1),timenc,(/IOUT/))

!
!  Performs subtype 1 
!
            IF ( OTYPE .EQ. 1 ) THEN

              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(6),DPO(J),(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(7),CAO(J),(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(8),CDIR,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(9),WAO(J),(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(10),UDIR,(/ J1,IOUT /))
!/SETUP             iret=nf90_put_var(ncid,varid(11),ZET_SETO,(/ J1,IOUT /))

!
!  Performs subtype 2
!
            ELSE IF ( OTYPE .EQ. 2 ) THEN

              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(6),HSIG,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(7),WLEN,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(8),TMEAN,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(9),THMEAN,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(10),THSPRD,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(11),FP*TPIINV,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(12),THP,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(13),SPP,(/ J1,IOUT /))

!
!  Performs subtype 3
!
            ELSE IF ( OTYPE .EQ. 3 ) THEN

              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),1.E-4*XSTAR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),1.E-4*YSTAR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(6),UNORM,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(7),ESTAR,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(8),FPSTAR,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(9),CD*1000.,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(10),APM(NK)*100.,(/ J1,IOUT /))

!
!  Performs subtype 4
!
            ELSE IF ( OTYPE .EQ. 4 ) THEN

              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),XSTAR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),YSTAR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(6),UNORM,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(7),ESTAR,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(8),FPSTAR,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(9),CD*1000.,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(10),APM(NK)*100.,(/ J1,IOUT /))

!
!  Performs subtype 5
!
            ELSE IF ( OTYPE .EQ. 5 ) THEN
              HMAT   = MIN ( 100. , 3.33*GRAV*HSIG/UABS**2 )
              IF ( HSIG .GE. HSMIN ) THEN
                CALL WAVNU1 ( FP, DPO(J), WNA, XYZ )
                AGE1   = MIN ( 100. , FP / WNA / UABS )
                AFR    = TPI / TMEAN
                CALL WAVNU1 ( AFR, DPO(J), WNA, XYZ )
                AGE2   = MIN ( 100. , AFR / WNA / UABS )
              ELSE
                AGE1   = NF90_FILL_FLOAT
                AGE2   = NF90_FILL_FLOAT
              END IF

              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(6),WAO(J),(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(7),UDIR,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(8),HSIG,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(9),HMAT,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(10),AGE1,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(11),AGE2,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(12),ASO(J),(/ J1,IOUT /))

!
!  Performs subtype 6
!
            ELSE IF ( OTYPE .EQ. 6 ) THEN

              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(6),WAO(J),(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(7),UDIR,(/ J1,IOUT /))
              iret=nf90_put_var(ncid,varid(8),HSIG,(/ J1,IOUT /))

              IF ( HSIG .GE. HSMIN ) THEN
                iret=nf90_put_var(ncid,varid(9),TPI/FP,(/ J1,IOUT /))
              ELSE
                iret=nf90_put_var(ncid,varid(9),0.0,(/ J1,IOUT /))
              END IF
            END IF ! OTYPE

!
! 4.c Perform output type 3 ( source terms )
!
          ELSE IF ( ITYPE .EQ. 3 ) THEN
!
!  Format Time
!
            IF ( OTYPE .NE. 1 ) THEN

              iyc=TIME(1)/10000
              imc=(TIME(1)-iyc*10000)/100
              idc=TIME(1)-iyc*10000-100*imc
              ihc=TIME(2)/10000
              iminc=(TIME(2)-ihc*10000)/100
              isc=TIME(2)-ihc*10000-100*iminc
   
              timenc=DBLE(julday(idc,imc,iyc)) - DBLE(julday(1,1,1990))              &
                     + DBLE((ihc + DBLE((iminc + DBLE(isc /60.)) /60.)) /24.)

              iret=nf90_put_var(ncid,varid(1),timenc,(/IOUT/))
            END IF  
!
!  Performs subtype 1 
!
            IF ( OTYPE .EQ. 1 ) THEN
!
              IF ( SCALE1 .GE. 0. ) THEN
                  IF ( FLSRCE(1) )                              &
                      CALL PRT1DS (NDSO, NK, E1, SIG(1:NK),     &
                           'RAD/S', 17,  0., 'E(f)', 'm^2s',    &
                           PTNME(J) )
                  IF (FLSRCE(2) .OR. FLSRCE(3) .OR.             &
                      FLSRCE(4) .OR. FLSRCE(5) .OR.             &
                      FLSRCE(6) .OR. FLSRCE(7) )                &
                      CALL PRT1DM (NDSO, NK, 6, E1ALL, SIG(1:NK),&
                           'RAD/S', 17, SCALE1, VAR1, 'M2',     &
                           PTNME(J) )
              END IF
              IF ( SCALE2 .GE. 0. ) THEN
                  IF ( FLSRCE(1) )                              &
                      CALL PRT2DS (NDSO, NK, NK, NTH, E,        &
                           SIG(1:NK), 'RAD/S', 1., 0., 0.0001,  &
                           'E(f,th)', 'm^2s', PTNME(J) )
                  IF ( FLSRCE(2) )                              &
                      CALL PRT2DS (NDSO, NK, NK, NTH, SWN,      &
                          SIG(1:NK), 'RAD/S', 1., SCALE2, 0.0001,&
                           'Sin(f,th)', 'm^2', PTNME(J) )
                  IF ( FLSRCE(3) )                              &
                      CALL PRT2DS (NDSO, NK, NK, NTH, SNL,      &
                          SIG(1:NK), 'RAD/S', 1., SCALE2, 0.0001,&
                           'Snl(f,th)', 'm^2', PTNME(J) )
                  IF ( FLSRCE(4) )                              &
                      CALL PRT2DS (NDSO, NK, NK, NTH, SDS,      &
                          SIG(1:NK), 'RAD/S', 1., SCALE2, 0.0001,&
                           'Sds(f,th)', 'm^2', PTNME(J) )
                  IF ( FLSRCE(5) )                              &
                      CALL PRT2DS (NDSO, NK, NK, NTH, SBT,      &
                          SIG(1:NK), 'RAD/S', 1., SCALE2, 0.0001,&
                           'Sbt(f,th)', 'm^2', PTNME(J) )
                  IF ( FLSRCE(6) )                              &
                      CALL PRT2DS (NDSO, NK, NK, NTH, SIS,      &
                          SIG(1:NK), 'RAD/S', 1., SCALE2, 0.0001,&
                           'Sice(f,th)', 'm^2', PTNME(J) )
                          IF ( FLSRCE(7) )                              &
                      CALL PRT2DS (NDSO, NK, NK, NTH, STT,      &
                          SIG(1:NK), 'RAD/S', 1., SCALE2, 0.0001,&
                           'Stot(f,th)', 'm^2', PTNME(J) )
              END IF
!
!  Performs subtype 2 
!
            ELSE IF ( OTYPE .EQ. 2 ) THEN
!
              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(7),DPO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(8),USTAR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(9),WAO(J),(/J1,IOUT/))
              IF ( ISCALE.GE.3 ) FACF = 1. / FP
              iret=nf90_put_var(ncid,varid(6),FACF*SIG(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(10),E1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(11),SIN1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(12),SNL1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(13),SDS1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(14),SBT1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))
              iret=nf90_put_var(ncid,varid(15),SIS1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))              
              iret=nf90_put_var(ncid,varid(16),STT1(1:NK),start=(/1,J1,IOUT /),count=(/NK,1,1/))

!
!  Performs subtype 3
!
            ELSE IF ( OTYPE .EQ. 3 ) THEN
!
              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(7),DPO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(8),USTAR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(9),WAO(J),(/J1,IOUT/))
              IF ( ISCALE.GE.3 ) FACF = 1. / FP
              DO IK=1, NK
                FACT   = 1. / MAX ( 1.E-10 , E1(IK) )
                iret=nf90_put_var(ncid,varid(6),FACF*SIG(IK),(/ IK,J1,IOUT /))
                iret=nf90_put_var(ncid,varid(10),E1(IK),(/ IK,J1,IOUT /))
                IF ( E1(IK) .GT. 1.E-10 ) THEN
                  iret=nf90_put_var(ncid,varid(11),FACT*SIN1(IK),(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(12),FACT*SNL1(IK),(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(13),FACT*SDS1(IK),(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(14),FACT*SBT1(IK),(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(15),FACT*SIS1(IK),(/ IK,J1,IOUT /))                
                  iret=nf90_put_var(ncid,varid(16),FACT*STT1(IK),(/ IK,J1,IOUT /))
                ELSE
                  iret=nf90_put_var(ncid,varid(11),NF90_FILL_FLOAT,(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(12),NF90_FILL_FLOAT,(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(13),NF90_FILL_FLOAT,(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(14),NF90_FILL_FLOAT,(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(15),NF90_FILL_FLOAT,(/ IK,J1,IOUT /))
                  iret=nf90_put_var(ncid,varid(16),NF90_FILL_FLOAT,(/ IK,J1,IOUT /))
                END IF
              END DO

!
!  Performs subtype 4
!
            ELSE IF ( OTYPE .EQ. 4 ) THEN
!
              iret=nf90_put_var(ncid,varid(27),J,(/J1/))
              iret=nf90_put_var(ncid,varid(2),PTNME(J),start=(/1,J1/),count=(/LEN_TRIM(PTNME(J)) ,1/))
              IF (FLWW3.NE.0) iret=nf90_put_var(ncid,varid(3),FLWW3,(/J1,IOUT/))
              IF ( FLFORM ) THEN
                iret=nf90_put_var(ncid,varid(4),PTLOC(1,J),(/J1,IOUT/))
                iret=nf90_put_var(ncid,varid(5),PTLOC(2,J),(/J1,IOUT/))
              ELSE
                iret=nf90_put_var(ncid,varid(4),M2KM*PTLOC(1,J),(/J1,IOUT/))
                iret=nf90_put_var(ncid,varid(5),M2KM*PTLOC(2,J),(/J1,IOUT/))
              END IF
              iret=nf90_put_var(ncid,varid(10),DPO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(11),WAO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(12),UDIR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(13),CAO(J),(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(14),CDIR,(/J1,IOUT/))
              iret=nf90_put_var(ncid,varid(15),USTAR,(/J1,IOUT/))

              IF ( FLSRCE(1) )  iret=nf90_put_var(ncid,varid(16),   &
                 TRANSPOSE(E(1:NK,1:NTH)), start=(/1,1,J1,IOUT/),   &
                 count=(/NTH,NK,1,1/)                               )
              IF ( FLSRCE(2) )  iret=nf90_put_var(ncid,varid(17),   &
                 TRANSPOSE(SWN(1:NK,1:NTH)), start=(/1,1,J1,IOUT/), &
                 count=(/NTH,NK,1,1/)                               )
              IF ( FLSRCE(3) )  iret=nf90_put_var(ncid,varid(18),   &
                 TRANSPOSE(SNL(1:NK,1:NTH)), start=(/1,1,J1,IOUT/), &
                 count=(/NTH,NK,1,1/)                               )
              IF ( FLSRCE(4) )  iret=nf90_put_var(ncid,varid(19),   &
                 TRANSPOSE(SDS(1:NK,1:NTH)), start=(/1,1,J1,IOUT/), &
                 count=(/NTH,NK,1,1/)                               )
              IF ( FLSRCE(5) )  iret=nf90_put_var(ncid,varid(20),   &
                 TRANSPOSE(SBT(1:NK,1:NTH)), start=(/1,1,J1,IOUT/), &
                 count=(/NTH,NK,1,1/)                               )
              IF ( FLSRCE(6) )  iret=nf90_put_var(ncid,varid(21),   &
                 TRANSPOSE(SIS(1:NK,1:NTH)), start=(/1,1,J1,IOUT/), &
                 count=(/NTH,NK,1,1/)                               )            
              IF ( FLSRCE(7) )  iret=nf90_put_var(ncid,varid(22),   &
                 TRANSPOSE(STT(1:NK,1:NTH)), start=(/1,1,J1,IOUT/), &
                 count=(/NTH,NK,1,1/)                               )
!
            END IF
!                 
          END IF ! ITYPE

!
! ... End of fields loop
!
          IF (TOGETHER) J1=J1+1

        END IF ! FLREQ(J)
!
! Selects next station index or end up if not together
!
        IF (TOGETHER) THEN
          J=J+1
          IF (J.GT.NOPTS) LASTSTATION=.TRUE.
        ELSE
          LASTSTATION=.TRUE.
        END IF

      END DO ! DO WHILE (.NOT. LASTSTATION)

      END IF  ! NOT SHORT ... 
!
      RETURN
!
! Formats
!
  910 FORMAT (/15X,' Water depth       :',F7.1,'  (m)'/               &
               15X,' Wind speed        :',F8.2,' (m/s)')
  911 FORMAT ( 15X,' Wind direction    :',F7.1,'  (degr)')
  912 FORMAT ( 15X,' Air-sea temp. dif.:',F7.1,'  (degr)'/            &
               15X,' sea water speed     :',F8.2,' (m/s)')
  913 FORMAT ( 15X,' direction of current (from) :',F7.1,'  (degr)')
  914 FORMAT ( 15X,' Wave height       :',F8.2,' (m)'/                &
               15X,' Mean wave length  :',F6.0,'   (m)'/              &
               15X,' Mean wave period  :',F7.1,'  (s)'/               &
               15X,' Mean wave direct. :',F7.1,'  (degr)'/            &
               15X,' Direct. spread    :',F7.1,'  (degr)'/)
!
!/T 9000 FORMAT (' TEST W3EXNC : FLAGS :',40L2)
!/T 9001 FORMAT (' TEST W3EXNC : ITPYE  :',I4/                        &
!/T              '               OTPYE  :',I4/                        &
!/T              '               NREQ   :',I4/                        &
!/T              '               SCALE1 :',E10.3/                     &
!/T              '               SCALE2 :',E10.3/                     &
!/T              '               FLSRCE :',6L2)
!/T 9002 FORMAT (' TEST W3EXNC : OUTPUT POINT : ',A)
!/T 9010 FORMAT (' TEST W3EXNC : depth =',F7.1,'  IK, T, K, CG :')
!/T 9011 FORMAT ('               ',I3,F8.2,F8.4,F8.2)
!/
!/ End of W3EXNC ----------------------------------------------------- /
!/
      END SUBROUTINE W3EXNC





!--------------------------------------------------------------------------
      SUBROUTINE W3CRNC (itype, otype, nctype, ncfile, ncid, dimid, dimln, varid, one, two, FLSRCE)


      USE W3GDATMD
      USE NETCDF

      implicit none


      INTEGER, INTENT(IN)               :: itype,otype,nctype, one, two
      CHARACTER*(128), INTENT(IN)       :: ncfile
      INTEGER, INTENT(IN)               :: dimln(5)
      INTEGER, INTENT(OUT)              :: dimid(7), varid(28),ncid
      INTEGER                           :: iret
!/NC4    INTEGER                             :: deflate=1
      REAL(kind=4)                      :: freq(NK), freq1(NK),freq2(NK), dir(NTH)
      LOGICAL, INTENT(IN), OPTIONAL     :: FLSRCE(7)

!
! Creation in netCDF3 or netCDF4
!
      IF(NCTYPE.EQ.3)  iret = nf90_create(trim(ncfile), NF90_CLOBBER, ncid)
!/NC4      IF(NCTYPE.EQ.4) iret = nf90_create(trim(ncfile), NF90_NETCDF4, ncid)
      call check_err(iret)

!
!     Define generals dimensions
!
      iret = nf90_def_dim(ncid, 'time', dimln(1), dimid(1))
      call check_err(iret)
      iret = nf90_def_dim(ncid, 'station', dimln(2), dimid(2))
      call check_err(iret)
      iret = nf90_def_dim(ncid, 'string16', dimln(3), dimid(3))
      call check_err(iret)

!
!     define generals variables
!

!  time
      iret=nf90_def_var(ncid, 'time', NF90_DOUBLE, dimid(1), varid(1))
      call check_err(iret)
!/NC4      IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(1), 1, 1, deflate)
      iret=nf90_put_att(ncid,varid(1),'long_name','julian day (UT)')
      iret=nf90_put_att(ncid,varid(1),'standard_name','time')
      iret=nf90_put_att(ncid,varid(1),'units','days since 1990-01-01 00:00:00')
      iret=nf90_put_att(ncid,varid(1),'conventions','Relative julian days with decimal part (as parts of the day)')
      iret=nf90_put_att(ncid,varid(1),'axis','T') 

!  station
      iret=nf90_def_var(ncid, 'station', NF90_INT, (/dimid(2)/), varid(27))
      call check_err(iret)
!/NC4      IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(27), 1, 1, deflate)
      iret=nf90_put_att(ncid,varid(27),'long_name','station id')
      iret=nf90_put_att(ncid,varid(27),'_FillValue',NF90_FILL_INT)
      iret=nf90_put_att(ncid,varid(27),'axis','X') 

!  string16
      iret=nf90_def_var(ncid, 'string16', NF90_INT, (/dimid(3)/), varid(28))
      call check_err(iret)
!/NC4      IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(28), 1, 1, deflate)
      iret=nf90_put_att(ncid,varid(28),'long_name','station_name number of characters')
      iret=nf90_put_att(ncid,varid(28),'_FillValue',NF90_FILL_INT)
      iret=nf90_put_att(ncid,varid(28),'axis','W') 

!  station_name
      iret=nf90_def_var(ncid, 'station_name', NF90_CHAR, (/dimid(3),dimid(2)/), varid(2))
      call check_err(iret)
!/NC4      IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(2), 1, 1, deflate)
      iret=nf90_put_att(ncid,varid(2),'long_name','station name')
      iret=nf90_put_att(ncid,varid(2),'content','XW')
      iret=nf90_put_att(ncid,varid(2),'associates','station string16')

      IF (FLWW3.NE.0) THEN
!  wwIII param version
        iret=nf90_def_var(ncid, 'WWIII_param_version', NF90_SHORT, (/dimid(two),dimid(one)/), varid(3))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(3), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(3),'long_name','WaveWatch III parameters version')
        iret=nf90_put_att(ncid,varid(3),'standard_name','WWIII_param_version')
        iret=nf90_put_att(ncid,varid(3),'globwave_name','WWIII_param_version')
        iret=nf90_put_att(ncid,varid(3),'units','-')
        iret=nf90_put_att(ncid,varid(3),'scale_factor',1)
        iret=nf90_put_att(ncid,varid(3),'add_offset',0)
        iret=nf90_put_att(ncid,varid(3),'valid_min',1)
        iret=nf90_put_att(ncid,varid(3),'valid_max',999)
        iret=nf90_put_att(ncid,varid(3),'_FillValue',NF90_FILL_SHORT)
        iret=nf90_put_att(ncid,varid(3),'content','TX')
        iret=nf90_put_att(ncid,varid(3),'associates','time station')
        END IF

      IF (FLAGLL) THEN 
!  longitude
        iret=nf90_def_var(ncid, 'longitude', NF90_FLOAT, (/dimid(two),dimid(one)/), varid(4))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(4), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(4),'long_name','longitude')
        iret=nf90_put_att(ncid,varid(4),'standard_name','longitude')
        iret=nf90_put_att(ncid,varid(4),'globwave_name','longitude')
        iret=nf90_put_att(ncid,varid(4),'units','degree_east')
        iret=nf90_put_att(ncid,varid(4),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(4),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(4),'valid_min',-180.0)
        iret=nf90_put_att(ncid,varid(4),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(4),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(4),'content','TX')
        iret=nf90_put_att(ncid,varid(4),'associates','time station')

!  latitude
        iret=nf90_def_var(ncid, 'latitude', NF90_FLOAT, (/dimid(two),dimid(one)/), varid(5))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(5), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(5),'long_name','latitude')
        iret=nf90_put_att(ncid,varid(5),'standard_name','latitude')
        iret=nf90_put_att(ncid,varid(5),'globwave_name','latitude')
        iret=nf90_put_att(ncid,varid(5),'units','degree_north')
        iret=nf90_put_att(ncid,varid(5),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(5),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(5),'valid_min',-90.0)
        iret=nf90_put_att(ncid,varid(5),'valid_max',180.)
        iret=nf90_put_att(ncid,varid(5),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(5),'content','TX')
        iret=nf90_put_att(ncid,varid(5),'associates','time station')


      ELSE
!  longitude
        iret=nf90_def_var(ncid, 'x', NF90_FLOAT, (/dimid(two),dimid(one)/), varid(4))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(4), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(4),'long_name','x')
        iret=nf90_put_att(ncid,varid(4),'standard_name','x')
        iret=nf90_put_att(ncid,varid(4),'globwave_name','x')
        iret=nf90_put_att(ncid,varid(4),'units','m')
        iret=nf90_put_att(ncid,varid(4),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(4),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(4),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(4),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(4),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(4),'content','TX')
        iret=nf90_put_att(ncid,varid(4),'associates','time station')
   

!  latitude
        iret=nf90_def_var(ncid, 'y', NF90_FLOAT, (/dimid(two),dimid(one)/), varid(5))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(5), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(5),'long_name','y')
        iret=nf90_put_att(ncid,varid(5),'standard_name','y')
        iret=nf90_put_att(ncid,varid(5),'globwave_name','y')
        iret=nf90_put_att(ncid,varid(5),'units','m')
        iret=nf90_put_att(ncid,varid(5),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(5),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(5),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(5),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(5),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(5),'content','TX')
        iret=nf90_put_att(ncid,varid(5),'associates','time station')

      END IF 

! Process freq and dir dimension values
      freq(1:NK)=SIG(1:NK)*TPIINV
      freq1(1:NK)=freq(1:NK)-0.5*(freq(1:NK)-(freq(1:NK)/XFR))
      freq2(1:NK)=freq(1:NK)+0.5*(-freq(1:NK)+(freq(1:NK)*XFR))
      freq1(1)=SIG(1)*TPIINV
      freq2(NK)=SIG(NK)*TPIINV
      dir(1:NTH)=MOD(450-THD(1:NTH),360.)

!
! ... ITYPE = 1 AND OTYPE = 2
!

      IF (ITYPE.EQ.1 .AND. OTYPE.EQ.2) THEN
!
!     Define specifics dimensions
!
        iret = nf90_def_dim(ncid, 'frequency', dimln(4), dimid(4))
        call check_err(iret)

!
!     define specifics variables
!

!  frequency
         iret=nf90_def_var(ncid, 'frequency', NF90_FLOAT, dimid(4), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','frequency of center band')
        iret=nf90_put_att(ncid,varid(6),'standard_name','sea_surface_wave_frequency')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','frequency')
        iret=nf90_put_att(ncid,varid(6),'units','s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'axis','Y') 
!d
         iret=nf90_def_var(ncid, 'dpt', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','depth')
        iret=nf90_put_att(ncid,varid(7),'standard_name','depth')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','depth')
        iret=nf90_put_att(ncid,varid(7),'units','m')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',-100.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!Ust
         iret=nf90_def_var(ncid, 'ust', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','friction velocity')
        iret=nf90_put_att(ncid,varid(8),'standard_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(8),'units','m s-1')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!U10
         iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(9),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'units','m s-1')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')
!Dir
         iret=nf90_def_var(ncid, 'wnddir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','wind direction')
        iret=nf90_put_att(ncid,varid(10),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(10),'units','degree')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')


!f/fp
         iret=nf90_def_var(ncid, 'ffp', NF90_FLOAT, (/ dimid(4),dimid(two),dimid(one) /), varid(11))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'long_name','ffp')
        iret=nf90_put_att(ncid,varid(11),'standard_name','ffp')
        iret=nf90_put_att(ncid,varid(11),'globwave_name','ffp')
        iret=nf90_put_att(ncid,varid(11),'units','1')
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TXY')
        iret=nf90_put_att(ncid,varid(11),'associates','time station frequency')
!F
         iret=nf90_def_var(ncid, 'f', NF90_FLOAT, (/ dimid(4),dimid(two),dimid(one) /), varid(12))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(22), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'long_name','f')
        iret=nf90_put_att(ncid,varid(12),'standard_name','f')
        iret=nf90_put_att(ncid,varid(12),'globwave_name','f')
        iret=nf90_put_att(ncid,varid(12),'units','-')
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TXY')
        iret=nf90_put_att(ncid,varid(12),'associates','time station frequency')
!th1m
         iret=nf90_def_var(ncid, 'th1m', NF90_FLOAT, (/ dimid(4),dimid(two),dimid(one) /), varid(13))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(13), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(13),'long_name','mean wave direction from spectral moments')
        iret=nf90_put_att(ncid,varid(13),'standard_name','mean_wave_direction')
        iret=nf90_put_att(ncid,varid(13),'globwave_name','mean_wave_direction')
        iret=nf90_put_att(ncid,varid(13),'units','degree')
        iret=nf90_put_att(ncid,varid(13),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(13),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(13),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(13),'content','TXY')
        iret=nf90_put_att(ncid,varid(13),'associates','time station frequency')
!sth1m
         iret=nf90_def_var(ncid, 'sth1m', NF90_FLOAT,(/ dimid(4),dimid(two),dimid(one) /), varid(14))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(14), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(14),'long_name','directional spread from spectral moments')
        iret=nf90_put_att(ncid,varid(14),'standard_name','mean_wave_spreading')
        iret=nf90_put_att(ncid,varid(14),'globwave_name','mean_wave_spreading')
        iret=nf90_put_att(ncid,varid(14),'units','degree')
        iret=nf90_put_att(ncid,varid(14),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(14),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(14),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(14),'content','TXY')
        iret=nf90_put_att(ncid,varid(14),'associates','time station frequency')
!alpha
         iret=nf90_def_var(ncid, 'alpha', NF90_FLOAT, (/ dimid(4),dimid(two),dimid(one) /), varid(15))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(15), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(15),'long_name','alpha')
        iret=nf90_put_att(ncid,varid(15),'standard_name','alpha')
        iret=nf90_put_att(ncid,varid(15),'globwave_name','alpha')
        iret=nf90_put_att(ncid,varid(15),'units','-')
        iret=nf90_put_att(ncid,varid(15),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(15),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(15),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(15),'content','TXY')
        iret=nf90_put_att(ncid,varid(15),'associates','time station frequency')


        ! Add values in netCDF file      
        iret=nf90_enddef(ncid)
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(6),freq(1:NK))
        call check_err(iret)


!
! ... ITYPE = 1 AND OTYPE = 3
!

      ELSE IF (ITYPE.EQ.1 .AND. OTYPE.EQ.3) THEN
!
!     Define specifics dimensions
!
        iret = nf90_def_dim(ncid, 'frequency', dimln(4), dimid(4))
        call check_err(iret)
        iret = nf90_def_dim(ncid, 'direction', dimln(5), dimid(5))
        call check_err(iret)

!
!     define specifics variables
!

!frequency
        iret=nf90_def_var(ncid, 'frequency', NF90_FLOAT, (/dimid(4)/), varid(6))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','frequency of center band')
        iret=nf90_put_att(ncid,varid(6),'standard_name','sea_surface_wave_frequency')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','frequency')
        iret=nf90_put_att(ncid,varid(6),'units','s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'axis','Y') 

!frequency1
        iret=nf90_def_var(ncid, 'frequency1', NF90_FLOAT, (/dimid(4)/), varid(7))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','frequency of lower band')
        iret=nf90_put_att(ncid,varid(7),'standard_name','frequency_of_lower_band')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','frequency_lower_band')
        iret=nf90_put_att(ncid,varid(7),'units','s-1')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','Y')
        iret=nf90_put_att(ncid,varid(7),'associates','frequency')

!frequency2
        iret=nf90_def_var(ncid, 'frequency2', NF90_FLOAT, (/dimid(4)/), varid(8))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','frequency of upper band')
        iret=nf90_put_att(ncid,varid(8),'standard_name','frequency_of_upper_band')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','frequency_upper_band')
        iret=nf90_put_att(ncid,varid(8),'units','s-1')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','Y')
        iret=nf90_put_att(ncid,varid(8),'associates','frequency')


!direction
        iret=nf90_def_var(ncid, 'direction', NF90_FLOAT, (/dimid(5)/), varid(9))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','sea surface wave to direction')
        iret=nf90_put_att(ncid,varid(9),'standard_name','sea_surface_wave_to_direction')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','direction')
        iret=nf90_put_att(ncid,varid(9),'units','degree')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'axis','Z') 

!Efth
        iret=nf90_def_var(ncid,'efth',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(10))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name',&
                          'sea surface wave directional variance spectral density')
        iret=nf90_put_att(ncid,varid(10),'standard_name',&
                          'sea_surface_wave_directional_variance_spectral_density')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','directional_variance_spectral_density')
        iret=nf90_put_att(ncid,varid(10),'units','m2 s rad-1')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',1.E20)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TXYZ')
        iret=nf90_put_att(ncid,varid(10),'associates','time station frequency direction')
!d
        iret=nf90_def_var(ncid, 'dpt', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(11))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'long_name','depth')
        iret=nf90_put_att(ncid,varid(11),'standard_name','depth')
        iret=nf90_put_att(ncid,varid(11),'globwave_name','depth')
        iret=nf90_put_att(ncid,varid(11),'units','m')
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',-100.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TX')
        iret=nf90_put_att(ncid,varid(11),'associates','time station')

!U10
        iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(12))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(12), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(12),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(12),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(12),'units','m s-1')
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TX')
        iret=nf90_put_att(ncid,varid(12),'associates','time station')
!Dir
        iret=nf90_def_var(ncid, 'wnddir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(13))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(13), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(13),'long_name','wind direction')
        iret=nf90_put_att(ncid,varid(13),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(13),'globwave_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(13),'units','degree')
        iret=nf90_put_att(ncid,varid(13),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(13),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(13),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(13),'content','TX')
        iret=nf90_put_att(ncid,varid(13),'associates','time station')

!Uc
        iret=nf90_def_var(ncid, 'cur', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(14))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(14), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(14),'long_name','sea water speed')        
        iret=nf90_put_att(ncid,varid(14),'standard_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(14),'globwave_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(14),'units','m s-1')
        iret=nf90_put_att(ncid,varid(14),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(14),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(14),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(14),'content','TX')
        iret=nf90_put_att(ncid,varid(14),'associates','time station')

!Dir
        iret=nf90_def_var(ncid, 'curdir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(15))
        call check_err(iret)
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(15), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(15),'long_name','direction from of sea water velocity')
        iret=nf90_put_att(ncid,varid(15),'standard_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(15),'globwave_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(15),'units','degree')
        iret=nf90_put_att(ncid,varid(15),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(15),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(15),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(15),'content','TX')
        iret=nf90_put_att(ncid,varid(15),'associates','time station')


        ! Add values in netCDF file      
        iret=nf90_enddef(ncid)
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(6),freq(1:NK))
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(7),freq1(1:NK))
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(8),freq2(1:NK))
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(9),dir(1:NTH))
        call check_err(iret)




!
! ... ITYPE = 1 AND OTYPE = 4
!

      ELSE IF (ITYPE.EQ.1 .AND. OTYPE.EQ.4) THEN
!
!     Define specifics dimensions
!
        iret = nf90_def_dim(ncid, 'npart', dimln(4), dimid(4))
        call check_err(iret)

!
!     define specifics variables
!

!npart
         iret=nf90_def_var(ncid, 'npart', NF90_INT, (/ dimid(two),dimid(one) /), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','npart')
        iret=nf90_put_att(ncid,varid(6),'standard_name','npart')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','npart')
        iret=nf90_put_att(ncid,varid(6),'units','1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_INT)
        iret=nf90_put_att(ncid,varid(6),'axis','Y') 
!d
         iret=nf90_def_var(ncid, 'dpt', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','depth')
        iret=nf90_put_att(ncid,varid(7),'standard_name','depth')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','depth')
        iret=nf90_put_att(ncid,varid(7),'units','m')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',-100.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!U10
         iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(8),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(8),'units','m s-1')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!Dir
         iret=nf90_def_var(ncid, 'wnddir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','wind direction')
        iret=nf90_put_att(ncid,varid(9),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(9),'units','degree')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')
!Uc
         iret=nf90_def_var(ncid, 'cur', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','sea water speed')
        iret=nf90_put_att(ncid,varid(10),'standard_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(10),'units','m s-1')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')
!Dir
         iret=nf90_def_var(ncid, 'curdir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(11))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'long_name',' direction from of sea water velocity')
        iret=nf90_put_att(ncid,varid(11),'standard_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(11),'globwave_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(11),'units','degree')
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TX')
        iret=nf90_put_att(ncid,varid(11),'associates','time station')


!Hs
         iret=nf90_def_var(ncid, 'hs', NF90_FLOAT, (/dimid(4),dimid(two),dimid(one)/),varid(12))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(12), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'long_name','spectral estimate of significant wave height')
        iret=nf90_put_att(ncid,varid(12),'standard_name','sea_surface_wave_significant_height')
        iret=nf90_put_att(ncid,varid(12),'globwave_name','significant_wave_height')
        iret=nf90_put_att(ncid,varid(12),'units','m')
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TXY')
        iret=nf90_put_att(ncid,varid(12),'associates','time station npart')
!Tp
         iret=nf90_def_var(ncid, 'tp', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/),varid(13))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(13), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(13),'long_name','dominant_wave_period')
        iret=nf90_put_att(ncid,varid(13),'standard_name','dominant_wave_period')
        iret=nf90_put_att(ncid,varid(13),'globwave_name','dominant_wave_period')
        iret=nf90_put_att(ncid,varid(13),'units','s')
        iret=nf90_put_att(ncid,varid(13),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(13),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(13),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(13),'content','TXY')
        iret=nf90_put_att(ncid,varid(13),'associates','time station npart')
!L
         iret=nf90_def_var(ncid, 'lm', NF90_FLOAT, (/dimid(4),dimid(two),dimid(one)/),varid(14))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(14), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(14),'long_name','mean wave length')
        iret=nf90_put_att(ncid,varid(14),'standard_name','mean_wave_length')
        iret=nf90_put_att(ncid,varid(14),'globwave_name','mean_wave_length')
        iret=nf90_put_att(ncid,varid(14),'units','m')
        iret=nf90_put_att(ncid,varid(14),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(14),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(14),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(14),'content','TXY')
        iret=nf90_put_att(ncid,varid(14),'associates','time station npart')
!Tr
         iret=nf90_def_var(ncid, 'tr', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/),varid(15))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(15), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(15),'long_name','tr')
        iret=nf90_put_att(ncid,varid(15),'standard_name','tr')
        iret=nf90_put_att(ncid,varid(15),'globwave_name','tr')
        iret=nf90_put_att(ncid,varid(15),'units','-')
        iret=nf90_put_att(ncid,varid(15),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(15),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(15),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(15),'content','TXY')
        iret=nf90_put_att(ncid,varid(15),'associates','time station npart')
!th1m
         iret=nf90_def_var(ncid, 'th1m', NF90_FLOAT, (/dimid(4),dimid(two),dimid(one)/),varid(16))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(16), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(16),'long_name','mean wave direction from spectral moments')
        iret=nf90_put_att(ncid,varid(16),'standard_name','mean_wave_direction')
        iret=nf90_put_att(ncid,varid(16),'globwave_name','mean_wave_direction')
        iret=nf90_put_att(ncid,varid(16),'units','degree')
        iret=nf90_put_att(ncid,varid(16),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(16),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(16),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(16),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(16),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(16),'content','TXY')
        iret=nf90_put_att(ncid,varid(16),'associates','time station npart')
!sth1m
         iret=nf90_def_var(ncid, 'sth1m', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/),varid(17))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(17), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(17),'long_name','directional spread from spectral moments')
        iret=nf90_put_att(ncid,varid(17),'standard_name','mean_wave_spreading')
        iret=nf90_put_att(ncid,varid(17),'globwave_name','mean_wave_spreading')
        iret=nf90_put_att(ncid,varid(17),'units','degree')
        iret=nf90_put_att(ncid,varid(17),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(17),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(17),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(17),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(17),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(17),'content','TXY')
        iret=nf90_put_att(ncid,varid(17),'associates','time station npart')

        ! nf90_enddef function
        iret=nf90_enddef(ncid)
        call check_err(iret)

!
! ... ITYPE = 2 AND OTYPE = 1
!


      ELSE IF (ITYPE.EQ.2 .AND. OTYPE.EQ.1) THEN
!d
         iret=nf90_def_var(ncid, 'dpt', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','depth')
        iret=nf90_put_att(ncid,varid(6),'standard_name','depth')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','depth')
        iret=nf90_put_att(ncid,varid(6),'units','m')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',-100.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'content','TX')
        iret=nf90_put_att(ncid,varid(6),'associates','time station')

!Uc
         iret=nf90_def_var(ncid, 'cur', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','sea water speed')
        iret=nf90_put_att(ncid,varid(7),'standard_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(7),'units','m s-1')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!Dir
         iret=nf90_def_var(ncid, 'curdir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','direction from of sea water velocity')
        iret=nf90_put_att(ncid,varid(8),'standard_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(8),'units','degree')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!U10
         iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(9),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'units','m s-1')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')
!Dir
         iret=nf90_def_var(ncid, 'wnddir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','wind direction')
        iret=nf90_put_att(ncid,varid(10),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(10),'units','degree')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')
!zeta_setup
!/SETUP         iret=nf90_def_var(ncid, 'wave_setup', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(11))
!/SETUP!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
!/SETUP        iret=nf90_put_att(ncid,varid(11),'long_name','wave setup')
!/SETUP        iret=nf90_put_att(ncid,varid(11),'standard_name','wave_induced_setup')
!/SETUP        iret=nf90_put_att(ncid,varid(11),'globwave_name','wave_induced_setup')
!/SETUP        iret=nf90_put_att(ncid,varid(11),'units','m')
!/SETUP        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
!/SETUP        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
!/SETUP        iret=nf90_put_att(ncid,varid(11),'valid_min',-100.)
!/SETUP        iret=nf90_put_att(ncid,varid(11),'valid_max',100.)
!/SETUP        iret=nf90_put_att(ncid,varid(11),'_FillValue',NF90_FILL_FLOAT)
!/SETUP        iret=nf90_put_att(ncid,varid(11),'content','TX')
!/SETUP        iret=nf90_put_att(ncid,varid(11),'associates','time station')

        ! nf90_enddef function
        iret=nf90_enddef(ncid)
        call check_err(iret)

!
! ... ITYPE = 2 AND OTYPE = 2
!

      ELSE IF (ITYPE.EQ.2 .AND. OTYPE.EQ.2) THEN
!Hs
         iret=nf90_def_var(ncid, 'hs', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(6))  
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','spectral estimate of significant wave height') 
        iret=nf90_put_att(ncid,varid(6),'standard_name','sea_surface_wave_significant_height')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','significant_wave_height') 
        iret=nf90_put_att(ncid,varid(6),'units','m')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'content','TX')
        iret=nf90_put_att(ncid,varid(6),'associates','time station')
!L
         iret=nf90_def_var(ncid, 'lm', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','mean wave length')
        iret=nf90_put_att(ncid,varid(7),'standard_name','mean_wave_length')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','mean_wave_length')
        iret=nf90_put_att(ncid,varid(7),'units','m')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!Tr
         iret=nf90_def_var(ncid, 'tr', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','tr')
        iret=nf90_put_att(ncid,varid(8),'standard_name','tr')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','tr')
        iret=nf90_put_att(ncid,varid(8),'units','-')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!th1p
         iret=nf90_def_var(ncid, 'th1p', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','mean wave direction from spectral moments at spectral peak')
        iret=nf90_put_att(ncid,varid(9),'standard_name','dominant_wave_direction')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','dominant_wave_direction')
        iret=nf90_put_att(ncid,varid(9),'units','degree')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')
!sth1p
         iret=nf90_def_var(ncid, 'sth1p', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','directional spread at spectral peak')
        iret=nf90_put_att(ncid,varid(10),'standard_name','dominant_wave_spreading')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','dominant_wave_spreading')
        iret=nf90_put_att(ncid,varid(10),'units','degree')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')
!fp
         iret=nf90_def_var(ncid, 'fp', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(11))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'long_name','  peak frequency (Fp=1/Tp)')
        iret=nf90_put_att(ncid,varid(11),'standard_name','dominant_wave_frequency')
        iret=nf90_put_att(ncid,varid(11),'globwave_name','dominant_wave_frequency')
        iret=nf90_put_att(ncid,varid(11),'units','s-1')
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TX')
        iret=nf90_put_att(ncid,varid(11),'associates','time station')
!th1m
         iret=nf90_def_var(ncid, 'th1m', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(12))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(12), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'standard_name','mean wave direction from spectral moments')
        iret=nf90_put_att(ncid,varid(12),'standard_name','mean_wave_direction')
        iret=nf90_put_att(ncid,varid(12),'globwave_name','mean_wave_direction')
        iret=nf90_put_att(ncid,varid(12),'units','degree')
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TX')
        iret=nf90_put_att(ncid,varid(12),'associates','time station')
!sth1m
         iret=nf90_def_var(ncid, 'sth1m', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(13))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(13), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(13),'long_name','directional spread from spectral moments')
        iret=nf90_put_att(ncid,varid(13),'standard_name','mean_wave_spreading')
        iret=nf90_put_att(ncid,varid(13),'globwave_name','mean_wave_spreading')
        iret=nf90_put_att(ncid,varid(13),'units','degree')
        iret=nf90_put_att(ncid,varid(13),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(13),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(13),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(13),'content','TX')
        iret=nf90_put_att(ncid,varid(13),'associates','time station')

        ! nf90_enddef function
        iret=nf90_enddef(ncid)
        call check_err(iret)


!
! ... ITYPE = 2 AND OTYPE = 3
!

      ELSE IF (ITYPE.EQ.2 .AND. OTYPE.EQ.3) THEN
!Ust
         iret=nf90_def_var(ncid, 'ust', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','friction velocity')
        iret=nf90_put_att(ncid,varid(6),'standard_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(6),'units','m s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'content','TX')
        iret=nf90_put_att(ncid,varid(6),'associates','time station')
!Efst
         iret=nf90_def_var(ncid, 'efst', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name',                     &
                          'nondimensionalized using surface elevation variance spectrum')
        iret=nf90_put_att(ncid,varid(7),'standard_name','sea_surface_wave_variance_spectral_density')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','variance_spectral_density')
        iret=nf90_put_att(ncid,varid(7),'units','-')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!fpst
         iret=nf90_def_var(ncid, 'fpst', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','nondimensionalized using peak frequency (Fp=1/Tp)')
        iret=nf90_put_att(ncid,varid(8),'standard_name','dominant_wave_frequency')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','dominant_wave_frequency')
        iret=nf90_put_att(ncid,varid(8),'units','-')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!Cd
         iret=nf90_def_var(ncid, 'cd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','cd')
        iret=nf90_put_att(ncid,varid(9),'standard_name','cd')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','cd')
        iret=nf90_put_att(ncid,varid(9),'units','*1000')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')
!alpha
         iret=nf90_def_var(ncid, 'alpha', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','alpha')
        iret=nf90_put_att(ncid,varid(10),'standard_name','alpha')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','alpha')
        iret=nf90_put_att(ncid,varid(10),'units','*100')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')


        ! nf90_enddef function
        iret=nf90_enddef(ncid)
        call check_err(iret)


!
! ... ITYPE = 2 AND OTYPE = 4
!

      ELSE IF (ITYPE.EQ.2 .AND. OTYPE.EQ.4) THEN
!U10
         iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(6),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(6),'units','m s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'content','TX')
        iret=nf90_put_att(ncid,varid(6),'associates','time station')
!Efst
         iret=nf90_def_var(ncid, 'efst', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name',                    &
                          'nondimensionalized using surface elevation variance spectrum')
        iret=nf90_put_att(ncid,varid(7),'standard_name','sea_surface_wave_variance_spectral_density')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','variance_spectral_density')
        iret=nf90_put_att(ncid,varid(7),'units','-')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!fpst
         iret=nf90_def_var(ncid, 'fpst', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name',                    &
                          'nondimensionalized using peak frequency (Fp=1/Tp)')
        iret=nf90_put_att(ncid,varid(8),'standard_name','dominant_wave_frequency')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','dominant_wave_frequency')
        iret=nf90_put_att(ncid,varid(8),'units','-')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!Cd
         iret=nf90_def_var(ncid, 'cd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','cd')
        iret=nf90_put_att(ncid,varid(9),'standard_name','cd')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','cd')
        iret=nf90_put_att(ncid,varid(9),'units','*1000')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')
!alpha
         iret=nf90_def_var(ncid, 'alpha', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','alpha')
        iret=nf90_put_att(ncid,varid(10),'standard_name','alpha')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','alpha')
        iret=nf90_put_att(ncid,varid(10),'units','*100')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')

        ! nf90_enddef function
        iret=nf90_enddef(ncid)
        call check_err(iret)


!
! ... ITYPE = 2 AND OTYPE = 5
!

      ELSE IF (ITYPE.EQ.2 .AND. OTYPE.EQ.5) THEN
!U10
         iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(6),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(6),'units','m s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'content','TX')
        iret=nf90_put_att(ncid,varid(6),'associates','time station')
!Dir
         iret=nf90_def_var(ncid, 'wnddir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','wind direction')
        iret=nf90_put_att(ncid,varid(7),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(7),'units','degree')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!Hs
         iret=nf90_def_var(ncid, 'hs', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','spectral estimate of significant wave height') 
        iret=nf90_put_att(ncid,varid(8),'standard_name','sea_surface_wave_significant_height') 
        iret=nf90_put_att(ncid,varid(8),'globwave_name','significant_wave_height') 
        iret=nf90_put_att(ncid,varid(8),'units','m')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!Hsst
         iret=nf90_def_var(ncid, 'hsst', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name',                    &
                          'nondimensionalized using spectral estimate of significant wave height') 
        iret=nf90_put_att(ncid,varid(9),'standard_name','sea_surface_wave_significant_height') 
        iret=nf90_put_att(ncid,varid(9),'globwave_name','significant_wave_height') 
        iret=nf90_put_att(ncid,varid(9),'units','-')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')
!cp/U
         iret=nf90_def_var(ncid, 'cpu', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','cpu')
        iret=nf90_put_att(ncid,varid(10),'standard_name','cpu')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','cpu')
        iret=nf90_put_att(ncid,varid(10),'units','-')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')
!cm/U
         iret=nf90_def_var(ncid, 'cmu', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(11))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'long_name','cmu')
        iret=nf90_put_att(ncid,varid(11),'standard_name','cmu')
        iret=nf90_put_att(ncid,varid(11),'globwave_name','cmu')
        iret=nf90_put_att(ncid,varid(11),'units','-')
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TX')
        iret=nf90_put_att(ncid,varid(11),'associates','time station')
!Dt
         iret=nf90_def_var(ncid, 'ast', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(12))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(12), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'long_name','air sea temperature difference')
        iret=nf90_put_att(ncid,varid(12),'standard_name','air_sea_temperature_difference')
        iret=nf90_put_att(ncid,varid(12),'globwave_name','air_sea_temperature_difference')
        iret=nf90_put_att(ncid,varid(12),'units','degree')
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TX')
        iret=nf90_put_att(ncid,varid(12),'associates','time station')


        ! nf90_enddef function
        iret=nf90_enddef(ncid)
        call check_err(iret)


!
! ... ITYPE = 2 AND OTYPE = 6
!

      ELSE IF (ITYPE.EQ.2 .AND. OTYPE.EQ.6) THEN
!U10
         iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(6),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(6),'units','m s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'content','TX')
        iret=nf90_put_att(ncid,varid(6),'associates','time station')
!Dir
         iret=nf90_def_var(ncid, 'wnddir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','wind direction')
        iret=nf90_put_att(ncid,varid(7),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(7),'units','degree')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')
!Hs
         iret=nf90_def_var(ncid, 'hs', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','spectral estimate of significant wave height')
        iret=nf90_put_att(ncid,varid(8),'standard_name','sea_surface_wave_significant_height')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','significant_wave_height')  
        iret=nf90_put_att(ncid,varid(8),'units','m')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')
!Tp
         iret=nf90_def_var(ncid, 'tp', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','dominant wave period')
        iret=nf90_put_att(ncid,varid(9),'standard_name','dominant_wave_period')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','dominant_wave_period')
        iret=nf90_put_att(ncid,varid(9),'units','s')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')

        ! nf90_enddef function
        iret=nf90_enddef(ncid)
        call check_err(iret)

!
! ... ITYPE = 3 AND OTYPE = 2
!
      ELSE IF (ITYPE.EQ.3 .AND. OTYPE.EQ.2) THEN
!
!     Define specifics dimensions
!
        iret = nf90_def_dim(ncid, 'frequency', dimln(4), dimid(4))
        call check_err(iret)

!
!     define specifics variables
!

!  frequency / frequencyst / ffp
        IF (ISCALE.EQ.0) THEN
          iret=nf90_def_var(ncid, 'frequency', NF90_FLOAT, dimid(4), varid(6))
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_def_var(ncid, 'frequencyst', NF90_FLOAT, dimid(4), varid(6))
        ELSE
          iret=nf90_def_var(ncid, 'ffp', NF90_FLOAT, dimid(4), varid(6))
        END IF
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','frequency of center band')
        iret=nf90_put_att(ncid,varid(6),'standard_name','sea_surface_wave_frequency')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','frequency')
        iret=nf90_put_att(ncid,varid(6),'units','s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'axis','Y') 

!d
        iret=nf90_def_var(ncid, 'dpt', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','depth')
        iret=nf90_put_att(ncid,varid(7),'standard_name','depth')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','depth')
        iret=nf90_put_att(ncid,varid(7),'units','m')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',-100.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')

!Ust
        iret=nf90_def_var(ncid, 'ust', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','friction velocity')
        iret=nf90_put_att(ncid,varid(8),'standard_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(8),'units','m s-1')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')

!U10
        iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(9),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'units','m s-1')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')

!Ef / Efst
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'ef', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(10))
          iret=nf90_put_att(ncid,varid(10),'long_name','surface elevation variance spectrum')
          iret=nf90_put_att(ncid,varid(10),'standard_name','sea_surface_wave_variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'globwave_name','variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'units','m2 s')
        ELSE
          iret=nf90_def_var(ncid, 'efst', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(10))
          iret=nf90_put_att(ncid,varid(10),'long_name',                 &
                            'nondimensionalized using surface elevation variance spectrum')
          iret=nf90_put_att(ncid,varid(10),'standard_name','sea_surface_wave_variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'globwave_name','variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(10),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(10),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(10),'associates','time station ffp')
        END IF

!Sin / Sinst
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'sin', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(11))
          iret=nf90_put_att(ncid,varid(11),'long_name','wind input source term')
          iret=nf90_put_att(ncid,varid(11),'standard_name','wind_input_source_term')
          iret=nf90_put_att(ncid,varid(11),'globwave_name','wind_input_source_term')
          iret=nf90_put_att(ncid,varid(11),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'sinst', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(11))
          iret=nf90_put_att(ncid,varid(11),'long_name',                 &
                            'nondimensionalized using wind input source term')
          iret=nf90_put_att(ncid,varid(11),'standard_name','wind_input_source_term')
          iret=nf90_put_att(ncid,varid(11),'globwave_name','wind_input_source_term')
          iret=nf90_put_att(ncid,varid(11),'units','-')
        END IF
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(11),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(11),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(11),'associates','time station ffp')
        END IF


!Snl / Snlst
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'snl', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(12))
          iret=nf90_put_att(ncid,varid(12),'long_name','nonlinear 4 wave source term')
          iret=nf90_put_att(ncid,varid(12),'standard_name','nonlinear_4_wave_source_term')
          iret=nf90_put_att(ncid,varid(12),'globwave_name','nonlinear_4_wave_source_term')
          iret=nf90_put_att(ncid,varid(12),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'snlst', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(12))
          iret=nf90_put_att(ncid,varid(12),'long_name',                 &
                            'nondimensionalized using nonlinear 4 wave source term')
          iret=nf90_put_att(ncid,varid(12),'standard_name','nonlinear_4_wave_source_term')
          iret=nf90_put_att(ncid,varid(12),'globwave_name','nonlinear_4_wave_source_term')
          iret=nf90_put_att(ncid,varid(12),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(12), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(12),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(12),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(12),'associates','time station ffp')
        END IF


!Sds / Sdsst
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'sds', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(13))
          iret=nf90_put_att(ncid,varid(13),'long_name','wave breaking source term')
          iret=nf90_put_att(ncid,varid(13),'standard_name','wave_breaking_source_term')
          iret=nf90_put_att(ncid,varid(13),'globwave_name','wave_breaking_source_term')
          iret=nf90_put_att(ncid,varid(13),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'sdsst', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(13))
          iret=nf90_put_att(ncid,varid(13),'long_name',                 &
                            'nondimensionalized using wave breaking source term')
          iret=nf90_put_att(ncid,varid(13),'standard_name','wave_breaking_source_term')
           iret=nf90_put_att(ncid,varid(13),'globwave_name','wave_breaking_source_term')
         iret=nf90_put_att(ncid,varid(13),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(13), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(13),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(13),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(13),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(13),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(13),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(13),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(13),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(13),'associates','time station ffp')
        END IF

!Sbt / Sbtst
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'sbt', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(14))
          iret=nf90_put_att(ncid,varid(14),'long_name','depth induced breaking source term')
          iret=nf90_put_att(ncid,varid(14),'standard_name','depth_induced_breaking_source_term')
          iret=nf90_put_att(ncid,varid(14),'globwave_name','depth_induced_breaking_source_term')
          iret=nf90_put_att(ncid,varid(14),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'sbtst', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(14))
          iret=nf90_put_att(ncid,varid(14),'long_name',                 &
                            'nondimensionalized using depth induced breaking source term')
          iret=nf90_put_att(ncid,varid(14),'standard_name','depth_induced_breaking_source_term')
          iret=nf90_put_att(ncid,varid(14),'globwave_name','depth_induced_breaking_source_term')
          iret=nf90_put_att(ncid,varid(14),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(14), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(14),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(14),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(14),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(14),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(14),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(14),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(14),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(14),'associates','time station ffp')
        END IF

!Sice / Sicest
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'sice', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(15))
          iret=nf90_put_att(ncid,varid(15),'long_name','wave-ice interactions source term')
          iret=nf90_put_att(ncid,varid(15),'standard_name','wave_ice_interactions_source_term')
          iret=nf90_put_att(ncid,varid(15),'globwave_name','wave_ice_interactions_source_term')
          iret=nf90_put_att(ncid,varid(15),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'sicest', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(15))
          iret=nf90_put_att(ncid,varid(15),'long_name','nondimensionalized using wave-ice interactions source term')
          iret=nf90_put_att(ncid,varid(15),'standard_name','wave_ice_interactions_source_term')
          iret=nf90_put_att(ncid,varid(15),'globwave_name','wave_ice_interactions_source_term')
          iret=nf90_put_att(ncid,varid(15),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(15), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(15),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(15),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(15),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(15),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(15),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(15),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(15),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(15),'associates','time station ffp')
        END IF

!Stot / Stotst
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'stot', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(16))
          iret=nf90_put_att(ncid,varid(16),'long_name','total source term')
          iret=nf90_put_att(ncid,varid(16),'standard_name','total_source_term')
          iret=nf90_put_att(ncid,varid(16),'globwave_name','total_source_term')
          iret=nf90_put_att(ncid,varid(16),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'stotst', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(16))
          iret=nf90_put_att(ncid,varid(16),'long_name','nondimensionalized using total source term')
          iret=nf90_put_att(ncid,varid(16),'standard_name','total_source_term')
          iret=nf90_put_att(ncid,varid(16),'globwave_name','total_source_term')
          iret=nf90_put_att(ncid,varid(16),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(16), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(16),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(16),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(16),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(16),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(16),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(16),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(16),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(16),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(16),'associates','time station ffp')
        END IF

!  Add values in netCDF file   
        iret=nf90_enddef(ncid)
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(6),freq(1:NK))
        call check_err(iret)


!
! ... ITYPE = 3 AND OTYPE = 3
!
      ELSE IF (ITYPE.EQ.3 .AND. OTYPE.EQ.3) THEN
!
!     Define specifics dimensions
!
        iret = nf90_def_dim(ncid, 'frequency', dimln(4), dimid(4))
        call check_err(iret)

!
!     define specifics variables
!

!  frequency / frequencyst / ffp
        IF (ISCALE.EQ.0) THEN
          iret=nf90_def_var(ncid, 'frequency', NF90_FLOAT, dimid(4), varid(6))
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_def_var(ncid, 'frequencyst', NF90_FLOAT, dimid(4), varid(6))
        ELSE
          iret=nf90_def_var(ncid, 'ffp', NF90_FLOAT, dimid(4), varid(6))
        END IF
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','frequency of center band')
        iret=nf90_put_att(ncid,varid(6),'standard_name','sea_surface_wave_frequency')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','frequency')
        iret=nf90_put_att(ncid,varid(6),'units','s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'axis','Y') 

!d
        iret=nf90_def_var(ncid, 'dpt', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','depth')
        iret=nf90_put_att(ncid,varid(7),'standard_name','depth')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','depth')
        iret=nf90_put_att(ncid,varid(7),'units','m')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',-100.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','TX')
        iret=nf90_put_att(ncid,varid(7),'associates','time station')

!Ust
        iret=nf90_def_var(ncid, 'ust', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','friction velocity')
        iret=nf90_put_att(ncid,varid(8),'standard_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(8),'units','m s-1')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','TX')
        iret=nf90_put_att(ncid,varid(8),'associates','time station')

!U10
        iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(9),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(9),'units','m s-1')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'content','TX')
        iret=nf90_put_att(ncid,varid(9),'associates','time station')

!Ef / Efst
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'ef', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(10))
          iret=nf90_put_att(ncid,varid(10),'long_name','surface elevation variance spectrum')
          iret=nf90_put_att(ncid,varid(10),'standard_name','sea_surface_wave_variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'globwave_name','variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'units','m2 s')
        ELSE
          iret=nf90_def_var(ncid, 'efst', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(10))
          iret=nf90_put_att(ncid,varid(10),'long_name',                 &
                            'nondimensionalized using surface elevation variance spectrum')
          iret=nf90_put_att(ncid,varid(10),'standard_name','sea_surface_wave_variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'globwave_name','variance_spectral_density')
          iret=nf90_put_att(ncid,varid(10),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(10),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(10),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(10),'associates','time station ffp')
        END IF

!Tini / Tinist
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'tini', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(11))
          iret=nf90_put_att(ncid,varid(11),'long_name','tini')
          iret=nf90_put_att(ncid,varid(11),'standard_name','tini')
          iret=nf90_put_att(ncid,varid(11),'globwave_name','tini')
          iret=nf90_put_att(ncid,varid(11),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'tinist', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(11))
          iret=nf90_put_att(ncid,varid(11),'long_name','nondimensionalized using tini')
          iret=nf90_put_att(ncid,varid(11),'standard_name','tinist')
          iret=nf90_put_att(ncid,varid(11),'globwave_name','tinist')
          iret=nf90_put_att(ncid,varid(11),'units','-')
        END IF
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(11),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(11),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(11),'associates','time station ffp')
        END IF


!Tnli / Tnlist
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'tnli', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(12))
          iret=nf90_put_att(ncid,varid(12),'long_name','tnli')
          iret=nf90_put_att(ncid,varid(12),'standard_name','tnli')
          iret=nf90_put_att(ncid,varid(12),'globwave_name','tnli')
          iret=nf90_put_att(ncid,varid(12),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'tnlist', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(12))
          iret=nf90_put_att(ncid,varid(12),'long_name','nondimensionalized using tnli')
          iret=nf90_put_att(ncid,varid(12),'standard_name','tnlist')
          iret=nf90_put_att(ncid,varid(12),'globwave_name','tnlist')
          iret=nf90_put_att(ncid,varid(12),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(12), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(12),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(12),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(12),'associates','time station ffp')
        END IF


!Tdsi / Tdsist
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'tdsi', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(13))
          iret=nf90_put_att(ncid,varid(13),'long_name','tdsi')
          iret=nf90_put_att(ncid,varid(13),'standard_name','tdsi')
          iret=nf90_put_att(ncid,varid(13),'globwave_name','tdsi')
          iret=nf90_put_att(ncid,varid(13),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'tdsist', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(13))
          iret=nf90_put_att(ncid,varid(13),'long_name','nondimensionalized using tdsi')
          iret=nf90_put_att(ncid,varid(13),'standard_name','tdsist')
          iret=nf90_put_att(ncid,varid(13),'globwave_name','tdsist')
          iret=nf90_put_att(ncid,varid(13),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(13), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(13),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(13),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(13),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(13),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(13),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(13),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(13),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(13),'associates','time station ffp')
        END IF

!Tbti / Tbtist
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'tbti', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(14))
          iret=nf90_put_att(ncid,varid(14),'long_name','tbti')
          iret=nf90_put_att(ncid,varid(14),'standard_name','tbti')
          iret=nf90_put_att(ncid,varid(14),'globwave_name','tbti')
          iret=nf90_put_att(ncid,varid(14),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'tbtist', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(14))
          iret=nf90_put_att(ncid,varid(14),'long_name','nondimensionalized using tbti')
          iret=nf90_put_att(ncid,varid(14),'standard_name','tbtist')
          iret=nf90_put_att(ncid,varid(14),'globwave_name','tbtist')
          iret=nf90_put_att(ncid,varid(14),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(14), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(14),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(14),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(14),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(14),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(14),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(14),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(14),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(14),'associates','time station ffp')
        END IF

!Ticei / Ticeist
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'ticei', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(15))
          iret=nf90_put_att(ncid,varid(15),'long_name','ticei')
          iret=nf90_put_att(ncid,varid(15),'standard_name','ticei')
          iret=nf90_put_att(ncid,varid(15),'globwave_name','ticei')
          iret=nf90_put_att(ncid,varid(15),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'ticeist', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(15))
          iret=nf90_put_att(ncid,varid(15),'long_name','nondimensionalized using ticei')
          iret=nf90_put_att(ncid,varid(15),'standard_name','ticeist')
          iret=nf90_put_att(ncid,varid(15),'globwave_name','ticeist')
          iret=nf90_put_att(ncid,varid(15),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(15), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(15),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(15),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(15),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(15),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(15),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(15),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(15),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(15),'associates','time station ffp')
        END IF

!Ttoti / Ttotist
        IF (ISCALE.EQ.0 .OR. ISCALE.EQ.3) THEN
          iret=nf90_def_var(ncid, 'ttoti', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(16))
          iret=nf90_put_att(ncid,varid(16),'long_name','ttoti')
          iret=nf90_put_att(ncid,varid(16),'standard_name','ttoti')
          iret=nf90_put_att(ncid,varid(16),'globwave_name','ttoti')
          iret=nf90_put_att(ncid,varid(16),'units','m2')
        ELSE
          iret=nf90_def_var(ncid, 'ttotist', NF90_FLOAT,(/dimid(4),dimid(two),dimid(one)/), varid(16))
          iret=nf90_put_att(ncid,varid(16),'long_name','nondimensionalized using ttoti')
          iret=nf90_put_att(ncid,varid(16),'standard_name','ttotist')
          iret=nf90_put_att(ncid,varid(16),'globwave_name','ttotist')
          iret=nf90_put_att(ncid,varid(16),'units','-')
        END IF
!/NC4       IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(16), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(16),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(16),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(16),'valid_min',-1.)
        iret=nf90_put_att(ncid,varid(16),'valid_max',1.)
        iret=nf90_put_att(ncid,varid(16),'_FillValue', NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(16),'content','TXY')
        IF (ISCALE.EQ.0) THEN
          iret=nf90_put_att(ncid,varid(16),'associates','time station frequency')
        ELSE IF ( ISCALE.EQ.1 .OR. ISCALE.EQ.2 ) THEN
          iret=nf90_put_att(ncid,varid(16),'associates','time station frequencyst')
        ELSE
          iret=nf90_put_att(ncid,varid(16),'associates','time station ffp')
        END IF

!  Add values in netCDF file   
        iret=nf90_enddef(ncid)
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(6),freq(1:NK))
        call check_err(iret)


!
! ... ITYPE = 3 AND OTYPE = 4
!
      ELSE IF (ITYPE.EQ.3 .AND. OTYPE.EQ.4) THEN
!
!     Define specifics dimensions
!
        iret = nf90_def_dim(ncid, 'frequency', dimln(4), dimid(4))
        iret = nf90_def_dim(ncid, 'direction', dimln(5), dimid(5))
        call check_err(iret)

!
!     define specifics variables
!

!frequency
        iret=nf90_def_var(ncid, 'frequency', NF90_FLOAT, (/dimid(4)/), varid(6))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(6), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(6),'long_name','frequency of center band')
        iret=nf90_put_att(ncid,varid(6),'standard_name','sea_surface_wave_frequency')
        iret=nf90_put_att(ncid,varid(6),'globwave_name','frequency')
        iret=nf90_put_att(ncid,varid(6),'units','s-1')
        iret=nf90_put_att(ncid,varid(6),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(6),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(6),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(6),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(6),'axis','Y') 

!frequency1
        iret=nf90_def_var(ncid, 'frequency1', NF90_FLOAT, (/dimid(4)/), varid(7))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(7), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(7),'long_name','frequency of lower band')
        iret=nf90_put_att(ncid,varid(7),'standard_name','frequency_of_lower_band')
        iret=nf90_put_att(ncid,varid(7),'globwave_name','frequency_lower_band')
        iret=nf90_put_att(ncid,varid(7),'units','s-1')
        iret=nf90_put_att(ncid,varid(7),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(7),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(7),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(7),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(7),'content','Y')
        iret=nf90_put_att(ncid,varid(7),'associates','frequency')

!frequency2
        iret=nf90_def_var(ncid, 'frequency2', NF90_FLOAT, (/dimid(4)/), varid(8))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(8), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(8),'long_name','frequency of upper band')
        iret=nf90_put_att(ncid,varid(8),'standard_name','frequency_of_upper_band')
        iret=nf90_put_att(ncid,varid(8),'globwave_name','frequency_upper_band')
        iret=nf90_put_att(ncid,varid(8),'units','s-1')
        iret=nf90_put_att(ncid,varid(8),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(8),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(8),'valid_max',10.)
        iret=nf90_put_att(ncid,varid(8),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(8),'content','Y')
        iret=nf90_put_att(ncid,varid(8),'associates','frequency')


!direction
        iret=nf90_def_var(ncid, 'direction', NF90_FLOAT, (/dimid(5)/), varid(9))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(9), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(9),'long_name','sea surface wave to direction')
        iret=nf90_put_att(ncid,varid(9),'standard_name','sea_surface_wave_to_direction')
        iret=nf90_put_att(ncid,varid(9),'globwave_name','direction')
        iret=nf90_put_att(ncid,varid(9),'units','degree')
        iret=nf90_put_att(ncid,varid(9),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(9),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(9),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(9),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(9),'axis','Z') 

!d
        iret=nf90_def_var(ncid, 'dpt', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(10))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(10), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(10),'long_name','depth')
        iret=nf90_put_att(ncid,varid(10),'standard_name','depth')
        iret=nf90_put_att(ncid,varid(10),'globwave_name','depth')
        iret=nf90_put_att(ncid,varid(10),'units','m')
        iret=nf90_put_att(ncid,varid(10),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(10),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(10),'valid_min',-100.)
        iret=nf90_put_att(ncid,varid(10),'valid_max',10000.)
        iret=nf90_put_att(ncid,varid(10),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(10),'content','TX')
        iret=nf90_put_att(ncid,varid(10),'associates','time station')

!U10
        iret=nf90_def_var(ncid, 'wnd', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(11))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(11), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(11),'long_name','wind speed at 10m')
        iret=nf90_put_att(ncid,varid(11),'standard_name','wind_speed')
        iret=nf90_put_att(ncid,varid(11),'globwave_name','wind_speed')
        iret=nf90_put_att(ncid,varid(11),'units','m s-1')
        iret=nf90_put_att(ncid,varid(11),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(11),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(11),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(11),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(11),'content','TX')
        iret=nf90_put_att(ncid,varid(11),'associates','time station')
!Dir
        iret=nf90_def_var(ncid, 'wnddir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(12))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(12), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(12),'long_name','wind direction')
        iret=nf90_put_att(ncid,varid(12),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(12),'standard_name','wind_from_direction')
        iret=nf90_put_att(ncid,varid(12),'units','degree')
        iret=nf90_put_att(ncid,varid(12),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(12),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(12),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(12),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(12),'content','TX')
        iret=nf90_put_att(ncid,varid(12),'associates','time station')

!Uc
        iret=nf90_def_var(ncid, 'cur', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(13))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(13), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(13),'long_name','sea water speed')        
        iret=nf90_put_att(ncid,varid(13),'standard_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(13),'globwave_name','sea_water_speed')
        iret=nf90_put_att(ncid,varid(13),'units','m s-1')
        iret=nf90_put_att(ncid,varid(13),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(13),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(13),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(13),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(13),'content','TX')
        iret=nf90_put_att(ncid,varid(13),'associates','time station')

!Dir
        iret=nf90_def_var(ncid, 'curdir', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(14))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(14), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(14),'long_name','direction from of sea water velocity')
        iret=nf90_put_att(ncid,varid(14),'standard_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(14),'globwave_name','direction_of_sea_water_velocity')
        iret=nf90_put_att(ncid,varid(14),'units','degree')
        iret=nf90_put_att(ncid,varid(14),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(14),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(14),'valid_max',360.)
        iret=nf90_put_att(ncid,varid(14),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(14),'content','TX')
        iret=nf90_put_att(ncid,varid(14),'associates','time station')

!Ust
        iret=nf90_def_var(ncid, 'ust', NF90_FLOAT, (/ dimid(two),dimid(one) /), varid(15))
!/NC4        IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(15), 1, 1, deflate)
        iret=nf90_put_att(ncid,varid(15),'long_name','friction velocity')
        iret=nf90_put_att(ncid,varid(15),'standard_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(15),'globwave_name','friction_velocity')
        iret=nf90_put_att(ncid,varid(15),'units','m s-1')
        iret=nf90_put_att(ncid,varid(15),'scale_factor',1.)
        iret=nf90_put_att(ncid,varid(15),'add_offset',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_min',0.)
        iret=nf90_put_att(ncid,varid(15),'valid_max',100.)
        iret=nf90_put_att(ncid,varid(15),'_FillValue',NF90_FILL_FLOAT)
        iret=nf90_put_att(ncid,varid(15),'content','TX')
        iret=nf90_put_att(ncid,varid(15),'associates','time station')

!Efth
        IF ( PRESENT(FLSRCE) ) THEN
          IF ( FLSRCE(1) )  THEN
            iret=nf90_def_var(ncid,'efth',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(16))
!/NC4            IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(16), 1, 1, deflate)
            iret=nf90_put_att(ncid,varid(16),'long_name',&
                              'sea surface wave directional variance spectral density')
            iret=nf90_put_att(ncid,varid(16),'standard_name',&
                              'sea_surface_wave_directional_variance_spectral_density')
            iret=nf90_put_att(ncid,varid(16),'globwave_name','directional_variance_spectral_density')
            iret=nf90_put_att(ncid,varid(16),'units','m2 s rad-1')
            iret=nf90_put_att(ncid,varid(16),'scale_factor',1.)
            iret=nf90_put_att(ncid,varid(16),'add_offset',0.)
            iret=nf90_put_att(ncid,varid(16),'valid_min',0.)
            iret=nf90_put_att(ncid,varid(16),'valid_max',1.E20)
            iret=nf90_put_att(ncid,varid(16),'_FillValue',NF90_FILL_FLOAT)
            iret=nf90_put_att(ncid,varid(16),'content','TXYZ')
            iret=nf90_put_att(ncid,varid(16),'associates','time station frequency direction')
          ENDIF

!Swn
          IF ( FLSRCE(2) )  THEN
            iret=nf90_def_var(ncid,'sin',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(17))
!/NC4            IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(17), 1, 1, deflate)
            iret=nf90_put_att(ncid,varid(17),'long_name','wind input source term')
            iret=nf90_put_att(ncid,varid(17),'standard_name','wind_input_source_term')
            iret=nf90_put_att(ncid,varid(17),'globwave_name','wind_input_source_term')
            iret=nf90_put_att(ncid,varid(17),'units','m2 rad-1')
            iret=nf90_put_att(ncid,varid(17),'scale_factor',1.)
            iret=nf90_put_att(ncid,varid(17),'add_offset',0.)
            iret=nf90_put_att(ncid,varid(17),'valid_min',-1.)
            iret=nf90_put_att(ncid,varid(17),'valid_max',1.)
            iret=nf90_put_att(ncid,varid(17),'_FillValue',NF90_FILL_FLOAT)
            iret=nf90_put_att(ncid,varid(17),'content','TXYZ')
            iret=nf90_put_att(ncid,varid(17),'associates','time station frequency direction')
          ENDIF

!Snl
          IF ( FLSRCE(3) )  THEN
            iret=nf90_def_var(ncid,'snl',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(18))
!/NC4            IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(18), 1, 1, deflate)
            iret=nf90_put_att(ncid,varid(18),'long_name','nonlinear 4 wave source term')
            iret=nf90_put_att(ncid,varid(18),'standard_name','nonlinear_4_wave_source_term')
            iret=nf90_put_att(ncid,varid(18),'globwave_name','nonlinear_4_wave_source_term')
            iret=nf90_put_att(ncid,varid(18),'units','m2 rad-1')
            iret=nf90_put_att(ncid,varid(18),'scale_factor',1.)
            iret=nf90_put_att(ncid,varid(18),'add_offset',0.)
            iret=nf90_put_att(ncid,varid(18),'valid_min',-1.)
            iret=nf90_put_att(ncid,varid(18),'valid_max',1.)
            iret=nf90_put_att(ncid,varid(18),'_FillValue',NF90_FILL_FLOAT)
            iret=nf90_put_att(ncid,varid(18),'content','TXYZ')
            iret=nf90_put_att(ncid,varid(18),'associates','time station frequency direction')
          ENDIF

!Sds
          IF ( FLSRCE(4) )  THEN
            iret=nf90_def_var(ncid,'sds',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(19))
!/NC4            IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(19), 1, 1, deflate)
            iret=nf90_put_att(ncid,varid(19),'long_name','wave breaking source term')
            iret=nf90_put_att(ncid,varid(19),'standard_name','wave_breaking_source_term')
            iret=nf90_put_att(ncid,varid(19),'globwave_name','wave_breaking_source_term')
            iret=nf90_put_att(ncid,varid(19),'units','m2 rad-1')
            iret=nf90_put_att(ncid,varid(19),'scale_factor',1.)
            iret=nf90_put_att(ncid,varid(19),'add_offset',0.)
            iret=nf90_put_att(ncid,varid(19),'valid_min',-1.)
            iret=nf90_put_att(ncid,varid(19),'valid_max',1.)
            iret=nf90_put_att(ncid,varid(19),'_FillValue',NF90_FILL_FLOAT)
            iret=nf90_put_att(ncid,varid(19),'content','TXYZ')
            iret=nf90_put_att(ncid,varid(19),'associates','time station frequency direction')
          ENDIF

!Sbt
          IF ( FLSRCE(5) )  THEN
            iret=nf90_def_var(ncid,'sbt',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(20))
!/NC4            IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(20), 1, 1, deflate)
            iret=nf90_put_att(ncid,varid(20),'long_name','depth induced breaking source term')
            iret=nf90_put_att(ncid,varid(20),'standard_name','depth_induced_breaking_source_term')
            iret=nf90_put_att(ncid,varid(20),'globwave_name','depth_induced_breaking_source_term')
            iret=nf90_put_att(ncid,varid(20),'units','m2 rad-1')
            iret=nf90_put_att(ncid,varid(20),'scale_factor',1.)
            iret=nf90_put_att(ncid,varid(20),'add_offset',0.)
            iret=nf90_put_att(ncid,varid(20),'valid_min',-1.)
            iret=nf90_put_att(ncid,varid(20),'valid_max',1.)
            iret=nf90_put_att(ncid,varid(20),'_FillValue',NF90_FILL_FLOAT)
            iret=nf90_put_att(ncid,varid(20),'content','TXYZ')
            iret=nf90_put_att(ncid,varid(20),'associates','time station frequency direction')
          ENDIF

!Sice
          IF ( FLSRCE(6) )  THEN
            iret=nf90_def_var(ncid,'sice',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(21))
!/NC4            IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(21), 1, 1, deflate)
            iret=nf90_put_att(ncid,varid(21),'long_name','wave-ice interactions source term')
            iret=nf90_put_att(ncid,varid(21),'standard_name','wave_ice_intercations_source_term')
            iret=nf90_put_att(ncid,varid(21),'globwave_name','wave_ice_intercations_source_term')
            iret=nf90_put_att(ncid,varid(21),'units','m2 rad-1')
            iret=nf90_put_att(ncid,varid(21),'scale_factor',1.)
            iret=nf90_put_att(ncid,varid(21),'add_offset',0.)
            iret=nf90_put_att(ncid,varid(21),'valid_min',-1.)
            iret=nf90_put_att(ncid,varid(21),'valid_max',1.)
            iret=nf90_put_att(ncid,varid(21),'_FillValue',NF90_FILL_FLOAT)
            iret=nf90_put_att(ncid,varid(21),'content','TXYZ')
            iret=nf90_put_att(ncid,varid(21),'associates','time station frequency direction')
          ENDIF

!Stt
          IF ( FLSRCE(7) )  THEN
            iret=nf90_def_var(ncid,'stt',NF90_FLOAT,(/dimid(5),dimid(4),dimid(two),dimid(one)/),varid(22))
!/NC4            IF (NCTYPE.EQ.4) iret=nf90_def_var_deflate(ncid, varid(22), 1, 1, deflate)
            iret=nf90_put_att(ncid,varid(22),'long_name','Stt')
            iret=nf90_put_att(ncid,varid(22),'standard_name','Stt')
            iret=nf90_put_att(ncid,varid(22),'globwave_name','Stt')
            iret=nf90_put_att(ncid,varid(22),'units','m2 rad-1')
            iret=nf90_put_att(ncid,varid(22),'scale_factor',1.)
            iret=nf90_put_att(ncid,varid(22),'add_offset',0.)
            iret=nf90_put_att(ncid,varid(22),'valid_min',-1.)
            iret=nf90_put_att(ncid,varid(22),'valid_max',1.)
            iret=nf90_put_att(ncid,varid(22),'_FillValue',NF90_FILL_FLOAT)
            iret=nf90_put_att(ncid,varid(22),'content','TXYZ')
            iret=nf90_put_att(ncid,varid(22),'associates','time station frequency direction')
          ENDIF
        ENDIF

!  Add values in netCDF file      
        iret=nf90_enddef(ncid)
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(6),freq(1:NK))
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(7),freq1(1:NK))
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(8),freq2(1:NK))
        call check_err(iret)
        iret=nf90_put_var(ncid,varid(9),dir(1:NTH))
        call check_err(iret)
!
      END IF
!
      return


      END SUBROUTINE W3CRNC 


!--------------------------------------------------------------------------
!--------------------------------------------------------------------------

      subroutine check_err(iret)

      USE NETCDF
      implicit none
      
      integer iret

      if (iret .ne. NF90_NOERR) then
         print *, nf90_strerror(iret)
         stop
      endif
      return
      END subroutine check_err

!--------------------------------------------------------------------------
!--------------------------------------------------------------------------


!/
!/ End of W3OUNP ----------------------------------------------------- /
!/
      END PROGRAM W3OUNP

