FV3 Bundle
gsiprofiles_bin2nc4.f90
Go to the documentation of this file.
2 
3  USE netcdf
4 
5  USE ncd_kinds,ONLY: r_single, r_kind, i_kind
6 
7  IMPLICIT NONE
8 
9  REAL,PARAMETER:: missing = -9.99e9
10  INTEGER,PARAMETER:: imissing = -999999
11 
12  INTEGER nargs, iargc, n,i
13  CHARACTER*256, ALLOCATABLE :: arg(:)
14 
15  INTEGER(i_kind),PARAMETER :: max_name_length=56,max_vars=50
16 
17 ! commandline variables
18  LOGICAL :: debug
19  LOGICAL :: append_suffix
20 
21  CHARACTER*256 infn, outfn
22  LOGICAL linfile, loutfile
23 
24  INTEGER,PARAMETER :: inlun = 51
25  INTEGER,PARAMETER :: outlun= 52
26 
27  INTEGER strlen, iflag
28 
29  LOGICAL,DIMENSION(:),ALLOCATABLE :: luse
30 
31 ! single variables used later for printing purposes
32  CHARACTER(len=max_name_length), DIMENSION(max_vars) :: varnames
33 
34  INTEGER :: idate,nsig,nvars,naeros,nvarsphys,nsig_plus_one,nobs
35 
36  REAL(r_single), ALLOCATABLE, DIMENSION(:) :: tvp,qvp,prsltmp
37  REAL(r_single), ALLOCATABLE, DIMENSION(:) :: prsitmp
38  REAL(r_single), ALLOCATABLE, DIMENSION(:,:) :: aeros
39 
40  INTEGER, DIMENSION(2) :: start,count_nsig,count_nsig_plus_one
41 
42  INTEGER :: ncfileid,ncstatus,&
43  &dimid_nsig,dimid_nsig_plus_one,dimid_nobs
44 
45  INTEGER, DIMENSION(2) :: dimid_2d
46 
47  INTEGER :: ncid_tvp,ncid_qvp,ncid_prsltmp,ncid_prsitmp
48  INTEGER, ALLOCATABLE, DIMENSION(:) :: ncid_aeros
49 
50  nargs = iargc()
51  IF( nargs.EQ.0 ) THEN
52  CALL usage
53  ELSE
54  debug = .false.
55  append_suffix = .false.
56 
57  ALLOCATE(arg(nargs))
58  DO n=1,nargs
59  CALL getarg(n,arg(n))
60  ENDDO
61  DO n=1,nargs
62  IF (trim(arg(n)).EQ.'-debug' ) debug=.true.
63  IF (trim(arg(n)).EQ.'-append_nc4') append_suffix=.true.
64  ENDDO
65  ENDIF
66 
67  IF (debug) WRITE(*,*)'Debugging on - Verbose Printing'
68 
69 ! get infn from command line
70  CALL getarg(nargs, infn)
71 
72  strlen = len(trim(infn))
73 
74  WRITE(*,*)'Input bin diag: ',trim(infn)
75  INQUIRE(file=trim(infn), exist=linfile)
76  IF (.NOT. linfile) THEN
77  WRITE(*,*)trim(infn) // ' does not exist - exiting'
78  CALL abort
79  ENDIF
80 
81  IF (.NOT. append_suffix) THEN
82  outfn = infn(1:strlen-3) // 'nc4' ! assumes GMAO diag filename format ending with .bin, and replaces it
83  ELSE
84  outfn = infn(1:strlen) // '.nc4' ! if not GMAO format, use append_suffix = .true. in namelist
85 ! to simply append infile with .nc4 suffix
86  ENDIF
87 
88  WRITE(*,*)'Output NC4 diag: ',trim(outfn)
89  INQUIRE(file=trim(outfn), exist=loutfile)
90  IF (loutfile) WRITE(*,*)'WARNING: ' // trim(outfn) // ' exists - overwriting'
91 
92  iflag = 0
93 
94  OPEN(inlun,file=infn,form='unformatted',convert='big_endian')
95  ncstatus = nf90_create(trim(outfn),nf90_clobber,ncid=ncfileid)
96 
97  varnames=''
98  CALL read_profiles_header( inlun,idate,nsig,nvars,naeros,varnames,&
99  &iflag,debug)
100 
101  nvarsphys=nvars-naeros
102  nsig_plus_one=nsig+1
103 
104  ncstatus = nf90_def_dim(ncfileid,'nsig',nsig,dimid_nsig)
105  ncstatus = nf90_def_dim(ncfileid,'nsig_plus_one',nsig_plus_one,&
106  &dimid_nsig_plus_one)
107  ncstatus = nf90_def_dim(ncfileid,'nobs',nf90_unlimited, dimid_nobs)
108 
109  ALLOCATE(ncid_aeros(naeros))
110 
111  dimid_2d=(/dimid_nsig,dimid_nobs/)
112 
113  ncstatus = nf90_def_var(ncfileid,"temperature",nf90_double,dimid_2d,&
114  &ncid_tvp)
115  ncstatus = nf90_def_var(ncfileid,"humidity_mixing_ratio",nf90_double,dimid_2d,&
116  &ncid_qvp)
117  ncstatus = nf90_def_var(ncfileid,"air_pressure",nf90_double,dimid_2d,&
118  &ncid_prsltmp)
119 
120  DO i=1,naeros
121  ncstatus = nf90_def_var(ncfileid,trim(varnames(nvarsphys+i)),&
122  &nf90_double,dimid_2d,ncid_aeros(i))
123  ENDDO
124 
125  dimid_2d=(/dimid_nsig_plus_one,dimid_nobs/)
126 
127  ncstatus = nf90_def_var(ncfileid,"air_pressure_levels",nf90_double,dimid_2d,&
128  &ncid_prsitmp)
129 
130  ncstatus = nf90_put_att(ncfileid, nf90_global, 'date_time', idate)
131 
132  ncstatus = nf90_enddef(ncfileid)
133 
134 ! PRINT *,trim(nf90_strerror(ncstatus))
135 
136  nvarsphys=nvars-naeros
137 
138  ALLOCATE(tvp(nsig),qvp(nsig),prsltmp(nsig),prsitmp(nsig+1),&
139  &aeros(nsig,naeros))
140 
141  n=0
142 
143  iflag=0
144 
145  start=(/1,1/)
146  count_nsig=(/nsig,1/)
147  count_nsig_plus_one=(/nsig_plus_one,1/)
148 
149  DO WHILE (iflag == 0)
150 
151  CALL read_profiles(inlun,nsig,nvarsphys,naeros,&
152  &tvp,qvp,prsltmp,prsitmp,aeros,iflag,debug)
153 
154  tvp(1:nsig)=tvp(nsig:1:-1)
155  qvp(1:nsig)=qvp(nsig:1:-1)*1000_r_single
156  prsltmp(1:nsig)=prsltmp(nsig:1:-1) * 10_r_single
157  prsitmp(1:nsig+1)=prsitmp(nsig+1:1:-1) *10_r_single
158  aeros(1:nsig,:)=aeros(nsig:1:-1,:)
159 
160  IF (iflag /= 0) EXIT
161 
162  start(2)=n+1
163 
164  ncstatus = nf90_put_var(ncfileid,ncid_tvp,tvp,start=start,&
165  &count=count_nsig)
166  ncstatus = nf90_put_var(ncfileid,ncid_qvp,qvp,start=start,&
167  &count=count_nsig)
168  ncstatus = nf90_put_var(ncfileid,ncid_prsltmp,prsltmp,&
169  &start=start,count=count_nsig)
170 
171  ncstatus = nf90_put_var(ncfileid,ncid_prsitmp,prsitmp,&
172  &start=start,count=count_nsig_plus_one)
173 
174  DO i=1,naeros
175  ncstatus = nf90_put_var(ncfileid,ncid_aeros(i),aeros(:,i),&
176  &start=start,count=count_nsig)
177  ENDDO
178 
179  n=n+1
180 
181  ENDDO
182 
183  print *,'There are ',n,' observations'
184 
185  ncstatus = nf90_close(ncfileid)
186 
187  DEALLOCATE(ncid_aeros,tvp,qvp,prsltmp,prsitmp,aeros)
188 
189 CONTAINS
190 
191  SUBROUTINE usage
193  WRITE(6,100)
194 100 FORMAT( "Usage: ",/,/ &
195  " convert_aod_diag.x <options> <filename>",/,/ &
196  "where options:",/ &
197  " -debug : Set debug verbosity",/ &
198  " -append_txt : Append .txt suffix, instead of replace last three",/ &
199  " characters (default: replaced)",/ &
200  " Note: The GMAO diag files end with .bin or .nc4,",/ &
201  " which is where fixed 3-char truncation originates",/,/,/ &
202  " Example:",/ &
203  " convert_aod_diag.x nc_4emily.diag_hirs4_n19_ges.20161202_00z.bin",/ &
204  " Output file:",/ &
205  " nc_4emily.diag_hirs4_n19_ges.20161202_00z.nc4",/ &
206  )
207  stop
208 
209  END SUBROUTINE usage
210 
211 
212  SUBROUTINE read_profiles_header(ftin,idate,nsig,nvars,naeros,varnames,iflag,lverbose)
213 ! . . . .
214 ! Declare passed arguments
215  INTEGER,INTENT(in) :: ftin
216  INTEGER,INTENT(out) :: idate,nsig,nvars,naeros
217  CHARACTER(len=max_name_length), DIMENSION(max_vars) :: &
218  &varnames
219  INTEGER,INTENT(out) :: iflag
220  LOGICAL,OPTIONAL,INTENT(in) :: lverbose
221 
222  LOGICAL loutall
223 
224  loutall=.true.
225  IF(PRESENT(lverbose)) loutall=lverbose
226 
227 ! Read header (fixed_part).
228 
229  READ(ftin,iostat=iflag) nsig,nvars,naeros,idate
230  READ(ftin,iostat=iflag) varnames(1:nvars)
231 
232  END SUBROUTINE read_profiles_header
233 
234  SUBROUTINE read_profiles(ftin,nsig,nvarsphys,naeros,&
235  &tvp,qvp,prsltmp,prsitmp,aeros,iflag,lverbose)
236 ! . . . .
237 ! Declare passed arguments
238  INTEGER,INTENT(in) :: ftin
239  INTEGER,INTENT(in) :: nsig,nvarsphys,naeros
240  REAL(r_single), DIMENSION(nsig) :: tvp,qvp,prsltmp
241  REAL(r_single), DIMENSION(nsig+1) :: prsitmp
242  REAL(r_single), DIMENSION(nsig,naeros) :: aeros
243  INTEGER,INTENT(out) :: iflag
244  LOGICAL,OPTIONAL,INTENT(in) :: lverbose
245 
246  LOGICAL loutall
247 
248  loutall=.true.
249  IF(PRESENT(lverbose)) loutall=lverbose
250 
251  READ(ftin,iostat=iflag) tvp,qvp,prsltmp,prsitmp
252  READ(ftin,iostat=iflag) aeros
253 
254  END SUBROUTINE read_profiles
255 
256 END PROGRAM gsiprofiles_bin2nc4
subroutine read_profiles_header(ftin, idate, nsig, nvars, naeros, varnames, iflag, lverbose)
integer, parameter, public strlen
integer, parameter, public i_kind
Definition: ncd_kinds.F90:71
program gsiprofiles_bin2nc4
subroutine usage
subroutine read_profiles(ftin, nsig, nvarsphys, naeros, tvp, qvp, prsltmp, prsitmp, aeros, iflag, lverbose)
integer, parameter, public r_single
Definition: ncd_kinds.F90:79
integer, parameter, public r_kind
Definition: ncd_kinds.F90:108