-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_coverage.py
136 lines (117 loc) · 4.63 KB
/
check_coverage.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/python
import re
import sys
import getopt
import os
import subprocess
import h5py
class CheckCoverage:
coords = []
data_file = ""
covered = "false"
lat_name = "latitude"
lon_name = "longitude"
north = 90
south = -90
east = 180
west = -180
def __init__(self, north=90, south=-90, east=180, west=-180, lat_name=None, lon_name=None):
if (lat_name != None):
self.lat_name = lat_name
if (lon_name != None):
self.lon_name = lon_name
self.north = north
self.south = south
self.east = east
self.west = west
def check(self, ifile):
lats = [float(x) for x in self.get_data_array(ifile, self.lat_name)]
lons = [float(x) for x in self.get_data_array(ifile, self.lon_name)]
coords = zip(lons, lats)
covered = False
#asurin 011017: adding those 2 lines to remove negtive result fast
if max(lons) < self.west or min(lons) > self.east or max(lats) < self.south or min(lats) > self.north:
return covered
for x in coords:
if self.west <= x[0] <= self.east and self.south <= x[1] <= self.north:
covered = True
break
return covered
# Dump the array specified
def get_data_array(self, ifile, array_name):
# does executable exist?
mimecmd = ['file', '--brief', ifile]
mime = subprocess.Popen(mimecmd, stdout=subprocess.PIPE).communicate()[0]
if re.search('Hierarchical.*version.4', mime):
hdp = os.path.join(os.getenv('LIB3_BIN'), 'hdp')
if not (os.path.isfile(hdp) and os.access(hdp, os.X_OK)):
print hdp, "is not executable."
return None
# dump file header
cmd = [hdp, 'dumpsds', '-d', '-n', array_name, ifile]
f = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
contents = f.read()
return contents.split()
elif re.search('Hierarchical.*version.5', mime):
data = h5py.File(ifile, 'r')
for name in array_name.split("/"):
if name != '':
data = data[name]
return data.value.reshape(data.maxshape[0] * data.maxshape[1])
# Asurin: 042517 this part is for viirs GMTCO file
elif re.search('data', mime):
data = h5py.File(ifile, 'r')
for name in array_name.split("/"):
if name != '':
data = data[name]
return data.value.reshape(data.shape[0] * data.shape[1])
elif re.search('NetCDF Data Format', mime):
ncdump_hdf = os.path.join(os.getenv('LIB3_BIN'), 'ncdump_hdf')
if not (os.path.isfile(ncdump_hdf) and os.access(ncdump_hdf, os.X_OK)):
print ncdump_hdf, "is not executable."
return None
cmd = ' '.join([ncdump_hdf, '-h', ifile])
f = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()
return f[0].split('\n')
else:
print "File format not recognized"
def usage():
print('check_coverage.py -n <north_lat> -s <south_lat> -e <east_lon> -w <west_lon> -i <infiles>')
print('infiles can be multiple files separated by commas. Ex file1,file2,file3')
if __name__ == "__main__":
argv = sys.argv[1:]
north = "-999"
south = "-999"
west = "-999"
east = "-999"
lat_name = None
lon_name = None
data_files_str = ""
try:
opts, args = getopt.getopt(argv, "hn:s:w:e:v:l:i:",
["north=", "south=", "west=", "east=", "ifiles=", "lat=", "lon="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(
"check_coverage.py -n <north_lat> -s <south_lat> -e <east_lon> -w <west_lon> [-v latitude_name -l longitude_name] -i <infiles>")
sys.exit()
elif opt in ("-v", "--lat"):
lat_name = arg
elif opt in ("-l", "-lon"):
lon_name = arg
elif opt in ("-n", "--north"):
north = float(arg)
elif opt in ("-s", "--south"):
south = float(arg)
elif opt in ("-w", "--west"):
west = float(arg)
elif opt in ("-e", "--east"):
east = float(arg)
elif opt in ("-i", "--infiles"):
data_files_str = arg
data_files = data_files_str.split(",")
objCheckCoverage = CheckCoverage(north, south, east, west, lat_name, lon_name)
print "needed_files=" + " ".join([f for f in data_files if os.path.isfile(f) and objCheckCoverage.check(f)])