-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_empty_netcdfs.py
75 lines (61 loc) · 2.34 KB
/
find_empty_netcdfs.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
"""
"""
__author__ = "Jordan Wiker"
__copyright__ = "Copyright 2022, JHUAPL"
__version__ = "1.0.0"
__maintainer__ = "Jordan Wiker"
__email__ = "[email protected]"
__status__ = "Development"
# import datetime as dt
# import sys
# from dateutil.relativedelta import relativedelta
import os
import netCDF4 as nc
import helper
import subprocess
# import upload_nc_to_zenodo
# START_DATE = dt.datetime.now()
# END_DATE = dt.datetime(1993, 1, 1)
def main():
# date = START_DATE
# while date >= END_DATE:
netcdfDir = '/project/superdarn/data/netcdf'
logDir = helper.LOG_DIR + 'netCDF_check'
emptynetcdfLogFile = logDir + '/empty_netCDFs.log'
summaryLogFile = logDir + '/empty_netCDFs_summary.log'
numEmptyNetCDFs = 0
# with open(emptynetcdfLogFile, "a+") as fp:
# fp.write('Empty NetCDFs\n')
with open(summaryLogFile, "a+") as fp:
fp.write('Month Num Empty NetCDFs\n')
for path, currentDirectory, files in os.walk(netcdfDir):
print('Current Dir: {0}'.format(path))
numEmptyNetCDFs = 0
for file in files:
# Disregard any non-netCDF files
if os.path.splitext(file)[-1] == '.nc':
try:
ds = nc.Dataset(os.path.join(path, file))
except:
# If it doesn't open (e.g. "NetCDF: HDF error"), add it to the list of bad files
numEmptyNetCDFs += 1
with open(emptynetcdfLogFile, "a+") as fp:
fp.write(file + ' (HDF error)\n')
continue
numDataPoints = ds.dimensions['npts'].size
if numDataPoints == 0:
numEmptyNetCDFs += 1
with open(emptynetcdfLogFile, "a+") as fp:
fp.write(file + '\n')
# if numEmptyNetCDFs != 0:
# Get 'YYYY-MM' month format
month = path.split('/')[-2] + '-' + path.split('/')[-1]
logText = '{0}: {1}\n'.format(month, numEmptyNetCDFs)
with open(summaryLogFile, "a+") as fp:
fp.write(logText)
subprocess.call(
'sort {0} -o {1}'.format(summaryLogFile, summaryLogFile), shell=True)
subprocess.call(
'sort {0} -o {1}'.format(emptynetcdfLogFile, emptynetcdfLogFile), shell=True)
if __name__ == '__main__':
main()