C Modified by Wolfram Ruehaak Summer 2000
C
C ATTENTION
C THE VALUE G(I,J,L) IS NOT(!) in the way of 
C           G(x-direction,y-direction,z-direktion)
C     BUT
C           G(y-direction,x-direction,z-direktion)
C            ===         ===
C
C           L=1 (z-dir) is the lowest (buttom) layer, 
C           L=NL        is the highest one (top)
C
C foo_to_v5d.f
C
C This is a skeleton conversion program for converting your data format
C to VIS-5D's v5d format (which is directly read by vis5d).

C This version only allows a rectangular lat/lon projection and equally
C spaced vertical levels in km.  See foo2_to_v5d for more advanced
C options.

C You need to insert code to do two things:
C     1. Open your input file and read the header information, putting
C        that information into the required variables.  If your file
C        doesn't contain some of the needed values you can just assign
C        constants to the variables.
C     2. read your grid data into the array G for each timestep and
C        variable.

C Pay special attention to comments in UPPERCASE.  They give more
C information about what you have to do.

C The rest of the program will handle creating the v5d file, writing the
C grid data, and closing the file.

C Compile this program with the foo_to_v5d.f.m makefile.
C Run this program with two arguments:  the name of your input file and
C the name of the v5d output file.

      program write_v5d

      implicit none

C WR      include "../src/v5df.h"
      include "src/v5df.h"      
      integer iargc
c     Local vars
      integer n
C WR include i
      integer i, T
      
      real*4 G(MAXROWS, MAXCOLUMNS, MAXLEVELS)
      character*100 inname, outname

C  THE FOLLOWING VARIABLES DESCRIBE THE DATASET TO BE CONVERTED.  YOU
C  MUST INITIALIZE ALL THESE VARIABLES WITH VALUES FROM YOUR FILE OR
C  ASSIGN SUITABLE CONSTANTS.  SEE THE README FILE FOR DESCRIPTIONS
C  OF THESE VARIABLES.
      integer nr, nc, nl
      integer numtimes
      integer numvars
      character*10 varname(MAXVARS)
      integer dates(MAXTIMES)
      integer times(MAXTIMES), time1h
      real northlat
      real latinc
      real westlon
      real loninc
      real bottomhgt
      real hgtinc

c     initialize the variables to missing values
      data nr,nc,nl / IMISSING, IMISSING, IMISSING /
      data numtimes,numvars / IMISSING, IMISSING /
      data (varname(i),i=1,MAXVARS) / MAXVARS*"          " /
      data (dates(i),i=1,MAXTIMES) / MAXTIMES*IMISSING /
      data (times(i),i=1,MAXTIMES) / MAXTIMES*IMISSING /
      data northlat, latinc / MISSING, MISSING /
      data westlon, loninc / MISSING, MISSING /
      data bottomhgt, hgtinc / MISSING, MISSING /      

c     get command line arguments
      if (iargc() .ne. 2) then
         print *,"Error:  two filename arguments are needed."
         call exit(1)
      else
         call getarg(1, inname)
         call getarg(2, outname)
      endif
      print 10,"Input file: ", inname
      print 10,"Output file: ", outname
 10   format (A,A)

C  OPEN YOUR DATAFILE (inname) HERE AND READ ITS HEADER INFORMATION TO
C  INITIALIZE THE ABOVE VARIABLES.
C                           INSERT CODE HERE
C **********************************************************************
      OPEN(90,file=inname)
      REWIND(90)

      numtimes = 24
      numvars = 12
      nr = 22
      nc = 28
      nl = 16

      varname(1)= 'U'
      varname(2)= 'V'
      varname(3)= 'W'      
      varname(4)= 'RAIN'
      varname(5)= 'TEMP'
      varname(6)= 'SRFWAT'
      varname(7)= 'POND'
      varname(8)= 'SHUM'
      varname(9)= 'RHUM'
      varname(10)= 'CW'
      varname(11)= 'CI'
      varname(12)= 'GRP'            

      time1h = 0
      
C SIMULATION START TIME IS CURRENTLY 02.05.1998, 00:00 BERLIN TIME      
      DO T=1, numtimes
      dates(T) = T     
      time1h = time1h+6000 
      times(T) = time1h
      ENDDO
C AREA OF BRAUNK_PART
C THE GRID HAS THE SIZE OF 5km X 5km
      northlat    =  52.
      latinc      =    .00898431*5.      
      westlon     =  -12.
      loninc      =   0.01455707*5.
      bottomhgt   =   0.
      hgtinc      =   1.
C ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C           REAL HIGTH INCREASE
C ZW =   
C     L1  =       0.
C     L2  =       20.         +20
C     L3  =       60.         +40
C     L4  =       250.        +190
C     L5  =       500.        +250
C     L6  =       750.        +250
C     L7  =       1000.       +250
C     L8  =       1500.       +500
C     L9  =       2000.       +500
C     L10 =       3000.       +1000
C     L11 =       4000.       +1000
C     L12 =       5000.       +1000
C     L13 =       6000.       +1000
C     L14 =       7000.       +1000
C     L15 =       8250.       +1250
C     L16 =       9750.       +1250
C     L17 =       11000.      +1250
C ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      
C **********************************************************************


C  IF YOU FAIL TO INITIALIZE SOME VARIABLES, THEY WILL BE DETECTED AND
C  REPORTED BY THE V5D LIBRARY.

c     Create the v5d file.
      n = v5dcreatesimple( outname, numtimes, numvars, nr, nc, nl,
     *                 varname, times, dates,
     *                 northlat, latinc, westlon, loninc,
     *                 bottomhgt, hgtinc )
      if (n .eq. 0) then
         call exit(1)
      endif

c     enter the conversion subroutine
      call convert( nr, nc, nl, numtimes, numvars, G )

c     close the v5d file and exit
      n = v5dclose()
      if (n .eq. 0) then
c        failed
         call exit(1)
      else
c        success
         call exit(0)
      endif

      end


c     Read 3-D grids from input file, write them to the output file.
      subroutine convert( nr, nc, nl, numtimes, numvars, G )
      implicit none
C      include "../src/v5df.h"
      include 'src/v5df.h'
c     Arguments
      integer nr
      integer nc
      integer nl
      integer numtimes
      integer numvars
      real*4 G(nr, nc, nl)
c     Local vars
      integer it, iv, n
C WR
      INTEGER T, I, J, L



      do it=1,numtimes
         do iv=1,numvars

C  READ YOUR DATA FOR TIME STEP NUMBER IT AND VARIABLE NUMBER IV INTO
C  THE ARRAY G HERE.  NOTE THAT G(1,1,1) IS THE NORTH WEST BOTTOM CORNER
C  AND G(NR,NC,NL) IS THE SOUTH EAST TOP CORNER.  ALSO, THE LOOPS CAN
C  BE CHANGED TO WORK IN ANY ORDER, THE v5dwrite CALL WILL STILL WORK.
C  VALUES GREATER THEN 1.0E30 ARE CONSIDERED TO BE 'MISSING'.
C                           INSERT CODE HERE
            DO L = 1, nl
                 DO I=1, nr
                 READ(90,'(28(E10.4,2X))') (G(I,J,L), J=1,nc)      
                 ENDDO
            ENDDO

c           write the 3-D grid to the v5d file
            n = v5dwrite( it, iv, G )
            if (n .eq. 0) then
c              error
               call exit(1)
            endif
         enddo
      enddo

      end
