rda-python-icoads 1.0.7__py3-none-any.whl → 1.0.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of rda-python-icoads might be problematic. Click here for more details.

@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env python3
2
+ #
3
+ ##################################################################################
4
+ #
5
+ # Title : fixiidx
6
+ # Author : Zaihua Ji, zji@ucar.edu
7
+ # Date : 06/23/2023
8
+ # 2025-03-04 transferred to package rda_python_icoads from
9
+ # https://github.com/NCAR/rda-icoads.git
10
+ # Purpose : read ICOADS data from IVADDB and fix the iidx values in ireanqc and iivad
11
+ # attms
12
+ #
13
+ # Github : https://github.com/NCAR/rda-python-icoads.git
14
+ #
15
+ ##################################################################################
16
+
17
+ import sys
18
+ from rda_python_common import PgLOG
19
+ from rda_python_common import PgDBI
20
+
21
+ IIDX = {}
22
+
23
+ def main():
24
+
25
+ argv = sys.argv[1:]
26
+ if argv and argv[0] == "-b": PgLOG.PGLOG['BCKGRND'] = 1
27
+ PgDBI.ivaddb_dbname()
28
+ fixiidx('iivad', 8, 24)
29
+ fixiidx('ireanqc', 1, 25)
30
+
31
+
32
+ def fixiidx(aname, t1, t2):
33
+
34
+ global IIDX
35
+ count = 100000
36
+ while t1 <= t2:
37
+ IIDX = {}
38
+ tcnt = fcnt = 0
39
+ offset = 0
40
+ tidx = t1
41
+ t1 += 1
42
+ tname = "{}_{}".format(aname, tidx)
43
+ while True:
44
+ pgrecs = PgDBI.pgmget(tname, 'lidx, iidx, uid', 'OFFSET {} LIMIT {}'.format(offset, count))
45
+ if not pgrecs: break
46
+ cnt = len(pgrecs['lidx'])
47
+ for i in range(cnt):
48
+ iidx = get_iidx(pgrecs['uid'][i], tidx)
49
+ if iidx != pgrecs['iidx'][i]:
50
+ fcnt += PgDBI.pgexec("UPDATE {} SET iidx = {} WHERE lidx = {}".format(tname, iidx, pgrecs['lidx'][i]), PgLOG.LGEREX)
51
+ offset += count
52
+ tcnt += cnt
53
+ PgLOG.pglog("{}/{} records fixed for {}.iidx".format(fcnt, tcnt, tname), PgLOG.LOGWRN)
54
+
55
+
56
+ def get_iidx(uid, tidx):
57
+
58
+ if uid not in IIDX:
59
+ tname = 'iuida_{}'.format(tidx)
60
+ cnd = "uid = '{}'".format(uid)
61
+ pgrec = PgDBI.pgget(tname, 'iidx', cnd)
62
+ if not pgrec: PgLOG.pglog("{}: Error get iidx for {}".format(tname, cnd), PgLOG.LGEREX)
63
+ IIDX[uid] = pgrec['iidx']
64
+
65
+ return IIDX[uid]
66
+
67
+ #
68
+ # call main() to start program
69
+ #
70
+ if __name__ == "__main__": main()
@@ -0,0 +1,262 @@
1
+ #!/usr/bin/env python3
2
+ #
3
+ ##################################################################################
4
+ #
5
+ # Title : maxsst
6
+ # Author : Zaihua Ji, zji@ucar.edu
7
+ # Date : 01/09/2021
8
+ # 2025-03-04 transferred to package rda_python_icoads from
9
+ # https://github.com/NCAR/rda-icoads.git
10
+ # Purpose : read ICOADS data from IVADDB and find maximum SST records dailly, monthly
11
+ # or yearly, and their associated time and locations
12
+ #
13
+ # Github : https://github.com/NCAR/rda-python-icoads.git
14
+ #
15
+ ##################################################################################
16
+
17
+ import sys
18
+ import os
19
+ import re
20
+ from os import path as op
21
+ from rda_python_common import PgLOG
22
+ from rda_python_common import PgDBI
23
+ from rda_python_common import PgUtil
24
+ from . import PgIMMA
25
+
26
+ PVALS = {
27
+ 'bdate' : None,
28
+ 'edate' : None,
29
+ 'bpdate' : [],
30
+ 'epdate' : [],
31
+ 'period' : [],
32
+ 'group' : None,
33
+ 'oname' : None
34
+ }
35
+
36
+ LFLDS = 'yr, mo, dy, hr, lat, lon, id'
37
+ RFLDS = 'r.iidx, r.uid, sst, it, si'
38
+ IFLDS = 'dck, sid, pt'
39
+ TFLDS = ['yr', 'mo', 'dy', 'hr', 'lat', 'lon', 'id','sst', 'it', 'si', 'dck', 'sid', 'pt', 'uid']
40
+ TITLE = ','.join(TFLDS)
41
+
42
+ MAXSST = 400
43
+
44
+ #
45
+ # main function
46
+ #
47
+ def main():
48
+
49
+ option = None
50
+ argv = sys.argv[1:]
51
+
52
+ for arg in argv:
53
+ if arg == "-b":
54
+ PgLOG.PGLOG['BCKGRND'] = 1
55
+ continue
56
+ ms = re.match(r'-([go])$', arg)
57
+ if ms:
58
+ option = ms.group(1)
59
+ continue
60
+ if re.match(r'^-', arg): PgLOG.pglog(arg + ": Invalid Option", PgLOG.LGWNEX)
61
+ elif option:
62
+ if option == 'g':
63
+ PVALS['group'] = arg
64
+ elif option == 'o':
65
+ PVALS['oname'] = arg
66
+ option = ''
67
+ elif not PVALS['bdate']:
68
+ PVALS['bdate'] = arg
69
+ elif not PVALS['edate']:
70
+ PVALS['edate'] = arg
71
+ else:
72
+ PgLOG.pglog(arg + ": Invalid parameter", PgLOG.LGWNEX)
73
+
74
+ PgDBI.ivaddb_dbname()
75
+
76
+ if not (PVALS['bdate'] and PVALS['edate'] and re.match(r'^(daily|monthly|yearly|all)$', PVALS['group'])):
77
+ pgrec = PgDBI.pgget("cntldb.inventory", "min(date) bdate, max(date) edate", '', PgLOG.LGEREX)
78
+ print("Usage: maxsst -g GroupBy (daily|monthly|yearly|all) BeginDate EndDate")
79
+ print(" Group by Daily, Monthly, Yearly or All is mandatory")
80
+ print(" Set BeginDate and EndDate between '{}' and '{}'".format(pgrec['bdate'], pgrec['edate']))
81
+ sys.exit(0)
82
+
83
+ if PgUtil.diffdate(PVALS['bdate'], PVALS['edate']) > 0:
84
+ tmpdate = PVALS['bdate']
85
+ PVALS['bdate'] = PVALS['edate']
86
+ PVALS['edate'] = tmpdate
87
+
88
+ PgLOG.PGLOG['LOGFILE'] = "icoads.log"
89
+ PgLOG.cmdlog("maxsst {}".format(' '.join(argv)))
90
+
91
+ if not PVALS['oname']:
92
+ PVALS['oname'] = "SST_MAXIMUMS_{}_{}-{}.csv".format(PVALS['group'].upper(), PVALS['bdate'], PVALS['edate'])
93
+
94
+ IMMA = open(PVALS['oname'], 'w')
95
+ IMMA.write("period, {}\n".format(', '.join(TFLDS)))
96
+ maximum_sst(IMMA)
97
+ IMMA.close()
98
+ PgLOG.cmdlog()
99
+ sys.exit(0)
100
+
101
+ #
102
+ # maximum the SST values daily/monthly/yearly
103
+ #
104
+ def maximum_sst(fd):
105
+
106
+ pmax = init_periods()
107
+ getdaily = 1 if PVALS['group'] == 'daily' else 0
108
+
109
+ for pidx in range(pmax):
110
+ if getdaily:
111
+ maxrec = maximum_daily_sst(PVALS['bpdate'][pidx])
112
+ else:
113
+ maxrec = maximum_period_sst(pidx)
114
+ if not maxrec: continue
115
+ mcnt = len(maxrec['iidx'])
116
+ for i in range(mcnt):
117
+ line = PVALS['period'][pidx]
118
+ for fld in TFLDS:
119
+ line += ", {}".format(maxrec[fld][i])
120
+ fd.write(line + "\n")
121
+
122
+ #
123
+ # read icoads record from given file name and save them into RDADB
124
+ #
125
+ def maximum_period_sst(pidx):
126
+
127
+ bdate = PVALS['bpdate'][pidx]
128
+ edate = PVALS['epdate'][pidx]
129
+ btidx = PgIMMA.date2tidx(bdate, False)
130
+ etidx = PgIMMA.date2tidx(edate, True)
131
+ tblmax = etidx-btidx+1
132
+
133
+ maxrec = {}
134
+ cnds = ['']*tblmax
135
+ itable = 'cntldb.inventory'
136
+ bcnd = "tidx = {} AND date >= '{}'"
137
+ ecnd = "tidx = {} AND date <= '{}'"
138
+ brec = PgDBI.pgget(itable, 'min(miniidx) iidx', bcnd.format(btidx, bdate))
139
+ erec = PgDBI.pgget(itable, 'max(maxiidx) iidx', ecnd.format(etidx, edate))
140
+
141
+ PgLOG.pglog("Find MAXIMUM SST for {} from IVADDB".format(PVALS['period'][pidx]), PgLOG.WARNLG)
142
+
143
+ if tblmax == 1:
144
+ if brec and erec['iidx'] >= brec['iidx']:
145
+ icnd = "iidx BETWEEN {} AND {}".format(brec['iidx'], erec['iidx'])
146
+ maximum_table_sst(btidx, maxrec, icnd)
147
+ else:
148
+ for i in range(tblmax):
149
+ if i == 0:
150
+ if brec:
151
+ icnd = "iidx >= {}".format(brec['iidx'])
152
+ maximum_table_sst(btidx+i, maxrec, icnd)
153
+ elif i == tblmax-1:
154
+ if erec:
155
+ icnd = "iidx >= {}".format(erec['iidx'])
156
+ maximum_table_sst(etidx, maxrec, icnd)
157
+ else:
158
+ maximum_table_sst(btidx + i, maxrec, '')
159
+
160
+ if maxrec:
161
+ mcnt = len(maxrec['iidx'])
162
+ s = 's' if mcnt > 1 else ''
163
+ PgLOG.pglog("MAX SST {}: {} record{} for {}".format(maxrec['sst'][0], mcnt, s, PVALS['period'][pidx]), PgLOG.LOGWRN)
164
+
165
+ return maxrec
166
+
167
+ #
168
+ # maximum IMMA records for given date
169
+ #
170
+ def maximum_daily_sst(cdate):
171
+
172
+ tidx = PgIMMA.date2tidx(cdate)
173
+ maxrec = {}
174
+ itable = 'cntldb.inventory'
175
+ PgLOG.pglog("Find MAXIMUM SST for {} from IVADDB".format(cdate), PgLOG.WARNLG)
176
+ bcnd = "tidx = {} AND date >= '{}'"
177
+ ecnd = "tidx = {} AND date <= '{}'"
178
+ brec = PgDBI.pgget(itable, 'min(miniidx) iidx', ecnd.format(tidx, cdate))
179
+ erec = PgDBI.pgget(itable, 'max(maxiidx) iidx', ecnd.format(tidx, cdate))
180
+ if brec and erec['iidx'] >= brec['iidx']:
181
+ icnd = "iidx BETWEEN {} AND {}".format(brec['iidx'], erec['iidx'])
182
+ maximum_table_sst(tidx, maxrec, icnd)
183
+ if maxrec:
184
+ mcnt = len(maxrec['iidx'])
185
+ s = 's' if mcnt > 1 else ''
186
+ PgLOG.pglog("MAX SST {}: {} record{} for {}".format(maxrec['sst'][0], mcnt, s, cdate), PgLOG.LOGWRN)
187
+
188
+ return maxrec
189
+
190
+ #
191
+ # maximum IMMA records from one table
192
+ #
193
+ def maximum_table_sst(tidx, maxrec, cnd):
194
+
195
+ rtable = "icorereg_{}".format(tidx)
196
+ ltable = "icoreloc_{}".format(tidx)
197
+ itable = "iicoads_{}".format(tidx)
198
+ jtables = "{} r, {} i".format(rtable, itable)
199
+ if cnd: cnd = 'r.{} AND '.format(cnd)
200
+ jcnd = cnd + "r.iidx = i.iidx AND pt = 13 AND "
201
+ # mcnd = jcnd + "sst < {} AND si >= 0 AND it >= 0".format(MAXSST)
202
+ mcnd = jcnd + "sst < {}".format(MAXSST)
203
+ if maxrec: mcnd += " AND sst > {}".format(maxrec['sst'][0])
204
+ srec = PgDBI.pgget(jtables, 'max(sst) sst', mcnd, PgLOG.LGEREX)
205
+ if srec['sst'] is None: return
206
+
207
+ # mcnd = jcnd + "sst = {} AND si >= 0 AND it >= 0".format(srec['sst'])
208
+ mcnd = jcnd + "sst = {}".format(srec['sst'])
209
+ srec = PgDBI.pgmget(jtables, RFLDS, mcnd, PgLOG.LGEREX)
210
+ for fld in srec: maxrec[fld] = srec[fld]
211
+
212
+ mcnt = len(srec['iidx'])
213
+ if mcnt == 1:
214
+ mcnd = 'iidx = {}'.format(srec['iidx'][0])
215
+ else:
216
+ mcnd = 'iidx IN ({})'.format(','.join(map(str, srec['iidx'])))
217
+
218
+ srec = PgDBI.pgmget(ltable, LFLDS, mcnd, PgLOG.LGEREX)
219
+ for fld in srec: maxrec[fld] = srec[fld]
220
+
221
+ srec = PgDBI.pgmget(itable, IFLDS, mcnd, PgLOG.LGEREX)
222
+ for fld in srec: maxrec[fld] = srec[fld]
223
+
224
+ #
225
+ # initialize period list
226
+ #
227
+ def init_periods():
228
+
229
+ bdate = PVALS['bdate']
230
+ if PVALS['group'] == "all":
231
+ PVALS['bpdate'].append(bdate)
232
+ PVALS['period'].append('ALL')
233
+ PVALS['epdate'].append(PVALS['edate'])
234
+ return 1
235
+ elif PVALS['group'] == "yearly":
236
+ dfmt = "YYYY"
237
+ eflg = "Y"
238
+ elif PVALS['group'] == 'monthly':
239
+ dfmt = "YYYY-MM"
240
+ eflg = "M"
241
+ else: # must be daily
242
+ dfmt = "YYYY-MM-DD"
243
+ eflg = ""
244
+ pmax = 0
245
+ while True:
246
+ pmax += 1
247
+ PVALS['bpdate'].append(bdate)
248
+ PVALS['period'].append(PgUtil.format_date(bdate, dfmt))
249
+ edate = PgUtil.enddate(bdate, 0, eflg) if eflg else bdate
250
+ if PgUtil.diffdate(PVALS['edate'], edate) > 0:
251
+ PVALS['epdate'].append(edate)
252
+ bdate = PgUtil.adddate(edate, 0, 0, 1)
253
+ else:
254
+ PVALS['epdate'].append(PVALS['edate'])
255
+ break
256
+
257
+ return pmax
258
+
259
+ #
260
+ # call main() to start program
261
+ #
262
+ if __name__ == "__main__": main()