FLEXPART v9.3.0
 All Classes Files Functions Variables
grib2nc4.F90
Go to the documentation of this file.
1 PROGRAM grib2nc4
2 
3  !*************************************************************************
4  ! This program uses the met file preprocessing capabilities of *
5  ! FLEXPART to extract key 3d variables, process them into the FP *
6  ! coordinate system, and write to NetCDF4. *
7  ! *
8  ! Don Morton (Boreal Scientific Computing LLC) *
9  ! Preprocessing methods, M. Harustak *
10  ! *
11  ! May 2016 *
12  !*************************************************************************
13 
14  USE par_mod
15  USE com_mod
16 
17  USE netcdf
18  USE fp2nc4io_mod ! Specialised module to interface preprocessed
19  ! FP met data with NetCDF4 files
20 
21  IMPLICIT NONE
22 
23  LOGICAL :: metfile_exists
24  INTEGER :: i, j, k
25  INTEGER :: num_optional_vars, num_vars
26  INTEGER, PARAMETER :: deflate_level = 2 ! Compression level (0-9)
27  CHARACTER(LEN=512) :: met_filepath, netcdf4_filepath
28  CHARACTER, DIMENSION(:), ALLOCATABLE :: vars_list ! list of variables
29 
30  INTEGER :: metdata_format = unknown_metdata ! From FP par_mod
31 
32  !--------------------------------------------------------
33 
34  ! Read in mandatory arguments
35  IF (iargc() < 2) THEN
36  print *, 'Usage: grib2netcdf4 <inpath> <outpath> [optional varnames]'
37  stop
38  ELSE
39  CALL getarg(1, met_filepath)
40  !PRINT *, 'met_filepath: ', met_filepath
41  CALL getarg(2, netcdf4_filepath)
42  ENDIF
43 
44  ! We want to insert 'u', 'v', and 't' into vars_list, by default
45  ! So, our list needs to have three elements, plus any optional args
46  ! from the command line
47 
48  ! First, get the number of optional args and allocate vars_list,
49  ! and fill the first three elements
50  IF (iargc() > 2) THEN
51  num_optional_vars = iargc() - 2
52  ELSE
53  num_optional_vars = 0
54  ENDIF
55 
56  num_vars = num_optional_vars + 3
57  ALLOCATE( vars_list(num_vars) )
58  vars_list(1) = 'u'
59  vars_list(2) = 'v'
60  vars_list(3) = 't'
61 
62  ! Read in optional variable arguments, starting at element 4 of vars_list
63  IF (iargc() > 2) THEN
64  DO i=1,num_optional_vars
65  CALL getarg( i+2, vars_list(i+3) )
66  ENDDO
67  ENDIF
68 
69  ! Before proceeding, let's make sure the vars_list is good - otherwise,
70  ! we don't want to waste time processing before finding out
71  IF ( .NOT. fp2nc4io_vars_are_valid(num_vars, vars_list) ) THEN
72  print *, 'The variables list has an invalid variable...'
73  print *, 'Valid variables: '
75 
76  print *,
77  print *, 'Your vars_list:'
78  DO i=1,num_vars
79  print *, vars_list(i)
80  ENDDO
81  print *,
82  print *, 'Exiting...'
83  stop
84  ENDIF
85 
86 
87  ! Insure that metfile_path is valid. If so, put the file info
88  ! in com_mod variables numbwf and wfname
89  INQUIRE( file=met_filepath, exist=metfile_exists )
90  IF ( metfile_exists ) THEN
91  numbwf = 1
92  wfname(1) = met_filepath
93  ELSE
94  print *, 'Unable to find metfile: ', trim(met_filepath)
95  stop
96  ENDIF
97 
98  ! Check the type of metdata using FP routine detectformat()
99  CALL detectformat(metdata_format)
100  IF (metdata_format == ecmwf_metdata) THEN
101  print *, ("ECMWF met data detected...")
102  ELSEIF (metdata_format == gfs_metdata) THEN
103  print *, ("NCEP met data detected...")
104  ELSE
105  print *, ("Unknown met data detected...")
106  stop
107  ENDIF
108 
109  ! Set a couple of com_mod variables - I honestly don't know the reason
110  ! for this now, but it was done in GRIB2FLEXPART, so I'm repeating it here.
111  ! The comment in there says "Reset the times of the wind fields that are
112  ! kept in memory to no time"
113  DO i=1,2
114  memind(i) = i
115  memtime(i) = 999999999
116  ENDDO
117 
118  ! Read the model grid specifications,
119  ! both for the mother domain and eventual nests
120  !**********************************************
121  if (metdata_format == ecmwf_metdata) CALL gridcheck_ecmwf
122  if (metdata_format == gfs_metdata) CALL gridcheck_gfs
123 
124  ! This is not yet implemented for nests. It would probably be trivial
125  ! to do so
126  !CALL gridcheck_nests
127 
128  print *, 'Calling processmetfields()...'
129  call processmetfields( 1, metdata_format)
130 
131  print *, 'Calling fp2nc4io_dump()...'
132  call fp2nc4io_dump( netcdf4_filepath, num_vars, vars_list, deflate_level)
133 
134  print *, 'End of grib2nc4'
135 
136 
137 END PROGRAM grib2nc4
logical function fp2nc4io_vars_are_valid(num_vars, dump_vars)
subroutine fp2nc4io_print_valid_vars
subroutine fp2nc4io_dump(nc4_filepath, num_vars, dump_vars, deflate_level)
subroutine gridcheck_ecmwf
Definition: gridcheck.F90:22
subroutine processmetfields(ind, metdata_format)
subroutine detectformat(metdata_format)
program grib2nc4
Definition: grib2nc4.F90:1
subroutine gridcheck_gfs