GRIB2FLEXPART.F90 9.79 KB
Newer Older
Don Morton's avatar
Don Morton committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
!**********************************************************************
! Copyright 1998,1999,2000,2001,2002,2005,2007,2008,2009,2010         *
! Andreas Stohl, Petra Seibert, A. Frank, Gerhard Wotawa,             *
! Caroline Forster, Sabine Eckhardt, John Burkhart, Harald Sodemann   *
!                                                                     *
! This file is part of FLEXPART.                                      *
!                                                                     *
! FLEXPART is free software: you can redistribute it and/or modify    *
! it under the terms of the GNU General Public License as published by*
! the Free Software Foundation, either version 3 of the License, or   *
! (at your option) any later version.                                 *
!                                                                     *
! FLEXPART is distributed in the hope that it will be useful,         *
! but WITHOUT ANY WARRANTY; without even the implied warranty of      *
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
! GNU General Public License for more details.                        *
!                                                                     *
! You should have received a copy of the GNU General Public License   *
! along with FLEXPART.  If not, see <http://www.gnu.org/licenses/>.   *
!**********************************************************************

program grib2flexpart

  !*****************************************************************************
  !                                                                            *
  !     This is the GRIB 2 FP conversion routine                               *
  !                                                                            *
  !     Author: M. Harustak                                                    *
  !                                                                            *
  !     05 October 2015                                                        *
  !                                                                            *
  !*****************************************************************************
  !                                                                            *
  ! Variables:                                                                 *
  !                                                                            *
  ! Constants:                                                                 *
  !                                                                            *
  !*****************************************************************************

  use point_mod
  use par_mod
  use com_mod
  use conv_mod

  implicit none

  integer :: metdata_format = unknown_metdata
  integer :: itime, nstop1
  integer :: i
  character(len=256) :: arg
  character(len=512) :: dumpPath
  character(len=512) :: inputFileName
  character(len=512) :: nestedFileName
  character(len=32) :: lsubgridTXT
  integer :: useAvailable = 0
56
  integer :: overwritecheck
Don Morton's avatar
Don Morton committed
57
58
59

  ! Print the GPL License statement
  !*******************************************************
60
#if defined CTBTO
61
  print*,'Welcome to GRIB2FLEXPART Version 9.3.2 CTBTO'
62
#else
63
  print*,'Welcome to GRIB2FLEXPART Version 9.3.2'
64
65
#endif

Don Morton's avatar
Don Morton committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  print*,'FLEXPART is free software released under the GNU Genera'// &
       'l Public License.'

  ! no argument => error
  if (( iargc().lt.2 ).or.( iargc().eq.3)) then
    print *,' '
    print *,'Usage in use-available mode: grib2flexpart useAvailable <output directory>'
    print *,'Usage in command line mode: grib2flexpart forward|backward subgrid <output directory> <source file> [<source file>...]'
    print *,' '
    stop 'Error: Missing arguments'
  elseif ( iargc().eq.2 ) then
    call getarg(1,arg)
    if ( arg.eq."useAvailable" ) then
      print *,'Running in use-available mode'
      useAvailable = 1
    else
      print *,' '
      print *,'Usage in use-available mode: grib2flexpart useAvailable <output directory>'
      print *,'Usage in command line mode: grib2flexpart forward|backward subgrid <output directory> &
 <source file> [<source file>...]'
      print *,' '
      stop 'Error: Incorrect arguments'
    endif
  else
    ! 2 and more arguments => ok, parse arguments
    call getarg(1,arg)
    if ( arg.eq."forward") then
      ldirect = 1
    else if ( arg.eq."backward") then
      ldirect = -1
    else
      print *,' '
      print *,'Usage in use-available mode: grib2flexpart useAvailable <output directory>'
      print *,'Usage in command line mode: grib2flexpart forward|backward subgrid <output directory> &
<source file> [<source file>...]'
      print *,' '
      stop 'Error: Incorrect arguments'
    endif
    print *,'Running in command line mode'
    useAvailable = 0
  endif

  if ( useAvailable.eq.0 ) then
    call getarg(2,lsubgridTxt)
    read (lsubgridTxt, '(i10)') lsubgrid
    lsubgrid=1
    call getarg(3,dumpPath)
    do i=4,iargc()
      call getarg(i,inputFileName)
      numbwf = i - 4 + 1
      if ( ldirect.eq.1 ) then
        wfname(i-4+1) = inputFileName
118
119
120
121
122
123
124
        if ( overwritecheck( dumpPath, wfname(i-4+1), 0) == -1 ) then
          ! if the output and input directory is the same, exit with error
          print *, "Input and output paths must be different"
          print *, "Output: "//trim(dumpPath)
          print *, "input: "//trim(wfname(i-4+1))
          stop 'Error: Incorrect arguments'
        endif
Don Morton's avatar
Don Morton committed
125
126
      else
        wfname(iargc()+1-i) = inputFileName
127
128
129
130
131
132
133
        if ( overwritecheck( dumpPath, wfname(iargc()+1-i), 0) == -1 ) then
          ! if the output and input directory is the same, exit with error
          print *, "Input and output paths must be different"
          print *, "Output: "//trim(dumpPath)
          print *, "Input: "//trim(wfname(iargc()+1-i))
          stop 'Error: Incorrect arguments'
        endif
Don Morton's avatar
Don Morton committed
134
135
136
137
138
139
140
141
142
143
144
      endif
    end do

  else
    call getarg(2,dumpPath)

    call readpaths

    call readcommand

    call readavailable
145
146
147
148
149
150
151
152
153
    do i=1,numbwf 
      if ( overwritecheck( dumpPath, path(3)(1:length(3)) // trim(wfname(i)),0) == -1) then
          ! if the output and input directory is the same, exit with error
          print *, "Input and output paths must be different"
          print *, "Output: "//trim(dumpPath)
          print *, "Input: "//path(3)(1:length(3)) // trim(wfname(i))
          stop 'Error: Incorrect arguments'
        endif
    enddo
Don Morton's avatar
Don Morton committed
154
155
  endif

156

Don Morton's avatar
Don Morton committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
! Reset the times of the wind fields that are kept in memory to no time
 !**********************************************************************

  do i=1,2
    memind(i)=i
    memtime(i)=999999999
  end do

  ! Detect metdata format
  call detectformat(metdata_format)
  if (metdata_format.eq.ecmwf_metdata) then
    print*,'ECMWF metdata detected'
  elseif (metdata_format.eq.gfs_metdata) then
    print*,'NCEP metdata detected'
  else
    stop 'Unknown metdata format'
  endif

  ! Read the model grid specifications,
  ! both for the mother domain and eventual nests
  !**********************************************

  if (metdata_format.eq.ecmwf_metdata) call gridcheck_ecmwf
  if (metdata_format.eq.gfs_metdata) call gridcheck_gfs
  call gridcheck_nests

  do i=1,numbwf
    if ( ( useAvailable.eq.0 ).or.(numbwf.eq.1) ) then
      print *,' '
      print *,wfname(i)
      call convertfields( i, metdata_format, dumpPath)
    else
       if (((ldirect*wftime(i).le.0).and. &
             (ldirect*wftime(i+1).gt.0)).or. & 
          (ldirect*wftime(i).gt.0)) then
          print *,' '
          print *,wfname(i)
          call convertfields( i, metdata_format, dumpPath)
       endif
    endif
  end do

  write(*,*) 'CONGRATULATIONS: YOU HAVE SUCCESSFULLY COMPLETED A GRIB2FLE&
       &XPART PREPROCESSING RUN!'

end program grib2flexpart
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

! This function checks whether input and output directories differ
! It does so by creating tmp file in output directory and by checking for its presence in input one
! It's does this way to avoid the need for relative 2 absolute path expansion and to avoid handling of links
integer function overwritecheck( dump_path, input_path, input_is_path )
  character(len=*) :: dump_path, input_path
  integer :: input_is_path, open_status
  character(len=512) :: tmp_file_name, tmp_file_path, check_file_path
  character(len=64) :: pid, current_time
  logical :: exists

  overwritecheck = 1

  write (pid,*) getpid()
  write (current_time, *) time()
  ! generate tmp file name using PID and timestamp
219
220
  !! tmp_file_name = "overwritecheck_"//trim(adjustl(pid))//"_"//trim(adjustl(current_time))//".tmp"
  !! tmp_file_path = trim(dump_path)//"/"//trim(tmp_file_name)
221
222

  ! create tmp file in output directory
223
  !! open(10001, file=trim(tmp_file_path), status="new", action="write", iostat=open_status)
224
  ! check for tmp file
225
226
227
228
229
230
231
232
  !! if ( open_status /= 0 ) then
  !!   print *, "Output directory does not exist or is not writeable"
  !!   print *, "File "//trim(tmp_file_path)//", iostat=",open_status
  !!   print *, "PID: ", pid, getpid()
  !!   print *, "current_time: ", current_time

  !!   stop 'Error: Incorrect arguments'
  !! endif
233
234
235
    
  ! generate tmp file name in input directory
  if ( input_is_path == 1) then
236
237
    !! check_file_path = trim(input_path)//"/"//trim(tmp_file_name)
    stop 'Error: Incorrect arguments; input file is path'
238
  else
239
240
241
242
243
    if ( scan(input_path, '/') == 0 ) then
       check_file_path=trim(dump_path)//"/"//trim(input_path)
    else
       check_file_path=trim(dump_path)//trim(input_path(scan(input_path, '/', .TRUE.):))
    endif
244
245
  endif
  !check for file presence
246
  print *, "Check if output file "//trim(check_file_path)//" exists ..."
247
248
  inquire(file=TRIM(check_file_path), exist=exists)
  ! delete tmp file
249
  !! close(10001, status='DELETE')
250
251
  if ( exists ) then
    overwritecheck = -1
252
253
    print *, "Warning: Output file "//trim(check_file_path)//" exists"
    stop 'Please remove this file if the output directory is correct'
254
255
256
  endif
  
end function