-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_inventory.py
125 lines (91 loc) · 4.52 KB
/
make_inventory.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
#*********************************************
# Make inventory listing of latest C3S run
# of counts in each month of the station
# record.
#
#*********************************************
import os
import numpy as np
import datetime as dt
import calendar
import setup
import qc_utils as utils
import io_utils as ioutils
MDI = -1.e30
month_names = [c.upper() for c in calendar.month_abbr[:]]
month_names[0] = "ANN"
TODAY = dt.datetime.now()
TODAY_MONTH = dt.datetime.strftime(TODAY, "%B").upper()
# ------------------------------------------------------------------------
# process the station list
def main(restart_id: str = "", end_id: str = "", clobber: bool = False) -> None:
"""
Main script. Makes inventory and other metadata files
:param str restart_id: which station to start on
:param str end_id: which station to end on
:param bool clobber: overwrite output file if exists
"""
# write the headers
with open(os.path.join(setup.SUBDAILY_METADATA_DIR, setup.INVENTORY), "w") as outfile:
outfile.write(" *** GLOBAL HISTORICAL CLIMATE NETWORK HOURLY DATA INVENTORY ***\n")
outfile.write("\n")
outfile.write("THIS INVENTORY SHOWS THE NUMBER OF WEATHER OBSERVATIONS BY STATION-YEAR-MONTH FOR BEGINNING OF RECORD\n")
outfile.write(f"THROUGH {TODAY_MONTH} {TODAY.year}. THE DATABASE CONTINUES TO BE UPDATED AND ENHANCED, AND THIS INVENTORY WILL BE \n")
outfile.write("UPDATED ON A REGULAR BASIS. QUALITY CONTROL FLAGS HAVE NOT BEEN INCLUDED IN THESE COUNTS, ALL OBSERVATIONS.\n")
outfile.write("\n")
month_string = " ".join([f"{c:<6s}" for c in month_names])
outfile.write(f"{'STATION':11s} {'YEAR':4s} {month_string:84s}\n")
outfile.write("\n")
# read in the station list
station_list = utils.get_station_list(restart_id=restart_id, end_id=end_id)
station_IDs = station_list.id
# now spin through each ID in the curtailed list
for st, station_id in enumerate(station_IDs):
print(f"{dt.datetime.now()} {station_id:11s} ({st+1}/{station_IDs.shape[0]})")
station = utils.Station(station_id, station_list.latitude[st], station_list.longitude[st], station_list.elevation[st])
try:
station, station_df = ioutils.read_station(os.path.join(setup.SUBDAILY_OUT_DIR, f"{station_id:11s}.qff{setup.OUT_COMPRESSION}"), station)
except OSError as e:
# file missing, move on to next in sequence
print(f"{station}, File Missing, {str(e)}")
continue
except ValueError as e:
# some issue in the raw file
print(f"{station}, Error in input file, {str(e)}")
continue
except EOFError as e:
# some issue in the gzip archive
print(f"{station}, Error in gzip file, {str(e)}")
continue
if len(station.years) == 0:
print(f"{station} has no data")
continue
for year in range(station.years[0], TODAY.year):
this_year_count = np.zeros(13)
for month in range(1, 13):
locs, = np.where(np.logical_and(station.years==year, station.months==month))
this_year_count[month] = len(locs)
# also store annual count
this_year_count[0] = this_year_count.sum()
year_string = " ".join([f"{c:<6.0f}" for c in this_year_count])
outfile.write(f"{station_id:11s} {year:4.0f} {year_string:84s}\n")
del(station_df)
del(station)
return
#************************************************************************
if __name__ == "__main__":
import argparse
# set up keyword arguments
parser = argparse.ArgumentParser()
parser.add_argument('--restart_id', dest='restart_id', action='store', default="",
help='Restart ID for truncated run, default=""')
parser.add_argument('--end_id', dest='end_id', action='store', default="",
help='End ID for truncated run, default=""')
parser.add_argument('--clobber', dest='clobber', action='store_true', default=False,
help='Overwrite output files if they exists.')
args = parser.parse_args()
main(restart_id=args.restart_id,
end_id=args.end_id,
clobber=args.clobber,
)
#************************************************************************