-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert2DGribToNetcdf.py
executable file
·73 lines (61 loc) · 2.42 KB
/
convert2DGribToNetcdf.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
import sys
import os
import netCDF4 as nc
import shutil
import datetime
from datetime import date
from datetime import timezone
import glob
gribDir = '/home/disk/data/nexrad/mrms/2DBaseReflQC'
filePrefix = 'MRMS_MergedBaseReflectivityQC'
extension = 'grib2'
ncDirBase = '/home/disk/bob/impacts/netcdf/mrms/base_refl'
mdvDirBase = '/home/disk/bob/impacts/mdv/mrms/2DBaseRefl'
ncVarName = 'DBZ_base'
domain = ['32.','-100.','48.','-66.'] # [minLat,minLon,maxLat,maxLon]
# get current date
dt = datetime.datetime.now(timezone.utc)
date = dt.strftime("%Y%m%d")
hour = dt.strftime("%H")
minute = dt.strftime("%M")
dateDir = gribDir+'/'+date
# get latest file
files_topLevel = sorted(glob.glob(dateDir+'/*'+extension))
gribFile = os.path.basename(files_topLevel[-1])
# convert grib2 to netcdf
(base,ext) = os.path.splitext(gribFile)
[mrms,product,level,dateAndTime] = base.split('_')
[date,time] = dateAndTime.split('-')
ncDir = ncDirBase+'/'+date
ncFile = mrms+'.'+product+'.'+date+'.'+time+'.nc'
if not os.path.isdir(ncDir):
os.makedirs(ncDir)
command = 'wgrib2 '+dateDir+'/'+gribFile+' -netcdf '+ncDir+'/'+ncFile
os.system(command)
# Got this error when running as meso@bob:
#/home/disk/sys/local/linux64/bin/wgrib2_stretch: error while loading shared libraries: libnetcdf.so.11: cannot open shared object file: No such file or directory
# change variable name from MergedBaseReflectivityQC_500mabovemeansealevel to DBZ_base
os.chdir(ncDir)
ncFileNew = ncFile+'.new'
command = 'ncrename -h -v MergedBaseReflectivityQC_500mabovemeansealevel,'+ncVarName+' '+ncFile+' '+ncFileNew
os.system(command)
# change base_refl attributes
#command = 'ncatted -O -h -a short_name,base_refl,o,c,base_refl '+ncFileNew
#os.system(command)
#command = 'ncatted -O -h -a _FillValue,base_refl,o,f,-99 '+ncFileNew
#os.system(command)
# change all base_refl values < 0 to -999
#with nc.Dataset(ncFileNew) as src:
# for varName, varIn in src.variables.items():
# if 'base_refl' in varName:
# base_refl = src[varName][:]
# base_refl[base_refl<0] = -999
# src[varName][:] = base_refl
# rename ncFileNew
shutil.move(ncFileNew,ncFile)
# change domain to IMPACTS region
#command = 'ncea -h -d latitude,32.,48. -d longitude,-100.,-66. '+ncFile+' '+ncFileNew
command = 'ncea -h -d latitude,'+domain[0]+','+domain[2]+' -d longitude,'+domain[1]+','+domain[3]+' '+ncFile+' '+ncFileNew
os.system(command)
shutil.move(ncFileNew,ncFile)