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: ' |
---|
74 | CALL fp2nc4io_print_valid_vars |
---|
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 |
---|