-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfx.py
executable file
·1420 lines (1199 loc) · 50.2 KB
/
fx.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Stephen Po-Chedley 9 May 2019
This is a library of helper functions for xagg.
Functions:
lookupCMIPMetadata
produceCMIP5Activity
ensure_dir
createGridLabel
parsePath
scantree
toSQLtime
sqltimeToDatetime
findDiskPaths
getDBPaths
getInvalidDBPaths
getRetiredDBPaths
initializeDB
parallelFindData
createLookupDictionary
getCMIPMeta
createFilename
xmlWrite
errorLogic
getWarnings
parseWarnings
sqlUpdate
sqlInsert
process_path
updateDatabaseHoldings
getScanList
writeStats
scanChunk
writeScanResults
resetXmlsByQuery
removeDatabasePathsByQuery
runLock
@author: pochedls
"""
import scandir
import sqlite3
import numpy as np
import re
import json
import datetime
from joblib import Parallel, delayed
import multiprocessing
import pickle
import re
import os
from subprocess import Popen,PIPE
import datetime
import time
import glob
def lookupCMIPMetadata(mip_era, cmipTable, variable, dictObj={}):
"""
frequency, realm, dimensions = lookupCMIPMetadata(mip_era, cmipTable, variable, dictObj={})
This function helps gather CMIP5/6 metadata needed to create the grid label.
It will use a pickle dictionary if provided, otherwise it gets the information
from the CMIP tables. Using the pickle file is much faster.
To create the pickle file, use: createLookupDictionary
To download the CMIP tables, use: ../tools/updateTables.sh
Inputs:
mip_era: 'CMIP5' | 'CMIP6'
cmipTable: e.g. 'Amon'
variable: e.g. 'ta'
dictObj: pickle file (dictionary) that returns appropriate outputs for given inputs.
Returns (strings):
frequency, realm, dimensions
"""
key = mip_era + '.' + cmipTable + '.' + variable
if key in dictObj:
frequency, realm, dimensions = dictObj[key]
return frequency, realm, dimensions
# https://github.com/PCMDI/cmip6-cmor-tables
frequency = []
realm = []
dimensions = []
if mip_era == 'CMIP6':
fn = 'data/cmip6/CMIP6_' + cmipTable + '.json'
with open(fn) as f:
data = json.load(f)
frequency = data['variable_entry'][variable]['frequency']
realm = data['variable_entry'][variable]['modeling_realm'].split(' ')[0]
dimensions = data['variable_entry'][variable]['dimensions'].split(' ')
elif mip_era == 'CMIP5':
fn = 'data/cmip5/CMIP5_' + cmipTable
f = open(fn, "r")
lines = f.readlines()
f.close()
inVar = False
for line in lines:
if line.find('frequency:') >= 0:
frequency = line.split(' ')[1].split('\n')[0]
if line.find('variable_entry:') >= 0:
if line.find(variable + '\n') >= 0:
inVar = True
if ((line.find('modeling_realm:') >= 0) & (inVar)):
realm = line.split(' ')[-1].split('\n')[0]
if ((line.find('dimensions:') >= 0) & (inVar)):
dimensions = line.split(' ')[-1].split('\n')[0].split(' ')
break
return frequency, realm, dimensions
def produceCMIP5Activity(experiment):
"""
activity = produceCMIP5Activity(experiment)
This function returns the appropriate activity for a given experiment. This
is essentially a hardcoded dictionary.
Inputs:
experiment (string): e.g. 'historical'
Returns:
activity (string)
"""
activityTable = {'sst2030' : 'CFMIP', 'sstClim' : 'RFMIP', 'sstClim4xCO2' : 'RFMIP',
'sstClimAerosol' : 'RFMIP', 'sstClimSulfate' : 'RFMIP',
'amip4xCO2' : 'CFMIP', 'amipFuture' : 'CFMIP', 'aquaControl' : 'CFMIP',
'aqua4xCO2' : 'CFMIP', 'aqua4K' : 'CFMIP', 'amip4K' : 'CFMIP',
'piControl' : 'CMIP', 'historical' : 'CMIP', 'esmControl' : 'CMIP',
'esmHistorical' : 'CMIP', '1pctCO2' : 'CMIP', 'abrupt4xCO2' : 'CMIP',
'amip' : 'CMIP', 'historicalExt' : 'CMIP', 'esmrcp85' : 'C4MIP',
'esmFixClim1' : 'C4MIP', 'esmFixClim2' : 'C4MIP', 'esmFdbk1' : 'C4MIP',
'esmFdbk2' : 'C4MIP', 'historicalNat' : 'DAMIP', 'historicalGHG' : 'DAMIP',
'historicalMisc' : 'DAMIP', 'midHolocene' : 'PMIP', 'lgm' : 'PMIP',
'past1000' : 'PMIP', 'rcp45' : 'ScenarioMIP', 'rcp85' : 'ScenarioMIP',
'rcp26' : 'ScenarioMIP', 'rcp60' : 'ScenarioMIP'}
reDec = re.compile(r'decadal[0-9]{4}')
if not not re.search(reDec, experiment):
activity = 'DCPP'
elif experiment in activityTable.keys():
activity = activityTable[experiment]
else:
activity = 'CMIP5'
return activity
def ensure_dir(file_path):
"""
Function ensures there is a directory for a given file.
"""
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError:
pass
def createGridLabel(mip_era, realm, cmipTable, grid, dimensions):
"""
gridLabel = createGridLabel(mip_era, realm, cmipTable, grid, dimensions)
This function creates a grid label.
Inputs:
mip_era: 'CMIP5' | 'CMIP6'
realm: e.g. 'atmos'
cmipTable: e.g. 'Amon'
grid: e.g. 'gm'
dimensions (list): e.g. ['longitude', 'latitude', 'plevs', 'time']
Returns:
gridLabel
Based on: https://docs.google.com/document/d/1bUwK6G_fVZO53UjLZbQUOuBP47PsT8lqKKhL1pjRnKg/edit
"""
# get realm id
realmIdLookup = {'aerosol' : 'ae', 'atmos' : 'ap', 'atmosChem' : 'ac',
'land' : 'ld', 'landIce' : 'gi', 'seaIce' : 'si',
'ocean' : 'op', 'ocnBgchem' : 'oc', 'river' : 'rr'}
# realmId = realmIdLookup[realm]
# vert-id lookup information
z1List = set(['height2m', 'height10m', 'depth0m', 'depth100m', 'olayer100m', 'sdepth1', 'sdepth10', 'height100m', 'depth300m', 'depth700m', 'depth2000m'])
lList = set(['olevel', 'olevhalf', 'alevel', 'alevhalf'])
reP = re.compile(r'p[0-9]')
pCheck = [not not re.search(reP,i) for i in dimensions]
rePl = re.compile(r'pl[0-9]')
plCheck = [not not re.search(rePl,i) for i in dimensions]
rePlev = re.compile(r'plev[0-9]')
plevCheck = [not not re.search(rePlev,i) for i in dimensions]
# get vert id
if len(dimensions) == 0:
vertId = 'x'
elif len(z1List.intersection(set(dimensions))) > 0:
vertId = 'z1'
elif (any(pCheck) | any(plCheck)):
vertId = 'p1'
elif len(lList.intersection(set(dimensions))) > 0:
vertId = 'l'
elif 'alev1'in dimensions:
vertId = 'l1'
elif 'sdepth'in dimensions:
vertId = 'z'
elif 'alt16'in dimensions:
vertId = 'z16'
elif 'alt40'in dimensions:
vertId = 'z40'
elif 'rho'in dimensions:
vertId = 'd'
elif 'plevs' in dimensions:
vertId = 'p17'
elif any(plevCheck):
dimensions = np.array(dimensions)
vertId = 'p' + dimensions[plevCheck][0].split('plev')[1]
else:
vertId = '2d'
# get region id
if mip_era == 'CMIP6':
if cmipTable in ['IfxAnt', 'IyrAnt', 'ImonAnt']:
regionId = 'ant'
elif cmipTable in ['IfxGre', 'IyrGre', 'ImonGre']:
regionId = 'gre'
else:
regionId = 'glb'
else:
regionId = 'glb'
# get h1 variable
locList = set(['site', 'oline', 'basin', 'siline', 'location'])
dimList = set(['latitude', 'yant', 'ygre', 'longitude', 'xant', 'yant'])
if len(locList.intersection(set(dimensions))) > 0:
h1 = 's'
elif cmipTable in ('AERmonZ', 'E6hrZ', 'EdayZ', 'EmonZ'):
h1 = 'z'
elif len(dimList.intersection(set(dimensions))) == 0:
h1 = 'm'
else:
h1 = 'g'
gridLabel = regionId + '-' + vertId + '-' + h1
if grid == 'gm':
gridStrip = 'n'
else:
gridStrip = grid.replace('g','').replace('a','').replace('z','')
gridLabel = gridLabel + gridStrip
return gridLabel
def parsePath(path, dictObj={}):
"""
validPath, keyId, mip_era, activity, institute, \
model, experiment, member, cmipTable, realm, \
frequency, variable, grid, gridLabel, version = parsePath(path, dictObj={})
This function parses a directory path for CMIP metadata. A pickle file
can optionally be provided to lookup missing CMIP metadata. If this is not
provided it will search the CMIP table information for the necesary information,
which is much slower (see lookupCMIPMetadata).
Inputs:
path (string)
dictObj (string of filename)
Returns:
validPath, keyId, mip_era, activity, institute, model, experiment, ...
member, cmipTable, realm, frequency, variable, grid, gridLabel, version
Note that validPath is a boolean denoting whether a path contains valid data (True)
or not (False) based on logic from the path only (it doesn't check for netCDF files).
"""
meta = path.split('/')[1:-1]
validPath = True
# remove double versions
if meta[-2] == meta[-3]:
meta.pop(-2)
# check for 'bad' directories
e = path.split('/')[-2]
bad = re.compile('bad[0-9]{1}')
check = re.match(bad, e)
checkBad = True
if check != None:
checkBad = False
if ((len(meta) > 10) & (checkBad)):
if meta[-10].upper() == 'CMIP6':
version = meta[-1]
grid = meta[-2]
variable = meta[-3]
cmipTable = meta[-4]
member = meta[-5]
experiment = meta[-6]
model = meta[-7]
institute = meta[-8]
activity = meta[-9]
mip_era = meta[-10].upper()
try:
frequency, realm, dimensions = lookupCMIPMetadata(mip_era, cmipTable, variable, dictObj)
gridLabel = createGridLabel(mip_era, realm, cmipTable, grid, dimensions)
except:
realm = 'unk'
frequency = 'unk'
gridLabel = 'unk'
elif ((meta[-1] != '1') & (meta[-1] != '2')):
variable = meta[-1]
version = meta[-2]
member = meta[-3]
cmipTable = meta[-4]
realm = meta[-5]
frequency = meta[-6]
experiment = meta[-7]
model = meta[-8]
institute = meta[-9]
activity = produceCMIP5Activity(experiment)
mip_era = 'CMIP5'
grid = 'gu'
frequencyx, realmx, dimensions = lookupCMIPMetadata(mip_era, cmipTable, variable, dictObj)
if frequency == 'monClim':
frequency = 'monC'
if 'time1' in dimensions:
frequency = frequency + 'Pt'
gridLabel = createGridLabel(mip_era, realm, cmipTable, grid, dimensions)
else:
variable = meta[-2]
version = meta[-1]
member = meta[-3]
cmipTable = meta[-4]
realm = meta[-5]
frequency = meta[-6]
experiment = meta[-7]
model = meta[-8]
institute = meta[-9]
activity = produceCMIP5Activity(experiment)
mip_era = 'CMIP5'
grid = 'gu'
frequencyx, realmx, dimensions = lookupCMIPMetadata(mip_era, cmipTable, variable, dictObj)
if frequency == 'monClim':
frequency = 'monC'
if 'time1' in dimensions:
frequency = frequency + 'Pt'
gridLabel = createGridLabel(mip_era, realm, cmipTable, grid, dimensions)
keyId = [mip_era, activity, institute, model, experiment, member, cmipTable, realm, frequency, variable, grid, gridLabel, version]
keyId = '.'.join(keyId)
else:
validPath = False
version = []
grid = []
variable = []
cmipTable = []
realm = []
frequency = []
gridLabel = []
member = []
experiment = []
model = []
institute = []
activity = []
mip_era = []
keyId = []
return validPath, keyId, mip_era, activity, institute, model, experiment, member, cmipTable, realm, frequency, variable, grid, gridLabel, version
def scantree(path):
"""
scantree(path)
This is an iterator method that recursively scans for directories that meet the
following criteria:
1. The directory has no sub-directories
2. The directory contains files
It is based on:
https://stackoverflow.com/questions/33135038/how-do-i-use-os-scandir-to-return-direntry-objects-recursively-on-a-directory
"""
for entry in scandir.walk(path):
# yield directory if there are any files in it
if (entry[2] != []):
yield entry[0] + '/'
def toSQLtime(time):
'''
sqlTime = toSQLtime(time)
Function takes a datetime object and returns a SQL-like datestring
'''
time = "{:02d}".format(time.year) + '-' + "{:02d}".format(time.month) + '-' + "{:02d}".format(time.day) + ' ' + "{:02d}".format(time.hour) + ':' + "{:02d}".format(time.minute) + ':' + "{:02d}".format(time.second)
return time
def sqltimeToDatetime(sqltime):
'''
time = sqltimeToDatetime(sqltime)
Function takes a SQL-like datestring and returns a datetime object
'''
d = sqltime.split(' ')[0]
t = sqltime.split(' ')[1]
y = int(d.split('-')[0])
mth = int(d.split('-')[1])
d = int(d.split('-')[2])
h = int(t.split(':')[0])
m = int(t.split(':')[1])
s = int(t.split(':')[2])
return datetime.datetime(y,mth,d,h,m,s)
def findDiskPaths(path):
'''
dpaths = findDiskPaths(path)
Function uses the scantree iterator to check all eligible paths
that fall under a parent path (input: path) for their created (ctime),
modified (mtime), and accessed (atime) times. It returns this in a dictionary
object:
Returns:
dbPaths[childPath] = {'ctime' : ctime, 'mtime' : mtime, 'atime' : atime}
'''
x = scantree(path)
dpaths = {}
s = time.time()
for file_path in x:
ts = scandir.stat(file_path)
ctime = toSQLtime(datetime.datetime.fromtimestamp(ts.st_ctime))
mtime = toSQLtime(datetime.datetime.fromtimestamp(ts.st_mtime))
atime = toSQLtime(datetime.datetime.fromtimestamp(ts.st_atime))
dpaths[file_path] = {'ctime' : ctime, 'mtime' : mtime, 'atime' : atime}
e = time.time()
print(path, e-s)
return dpaths
def getDBPaths(sqlDB):
'''
db = getDBPaths(sqlDB)
Function uses the specified sql database and returns all information
about each path in the database in a dictionary. The dictionary key
is the path.
Input:
sqlDb (string): filename of sqlite file
Returns:
db[path]
db[path] is then a sub-dictionary that contains all columns in the database:
keyid, mip_era, activity, institute, model, experiment, member, cmipTable, realm, ...
frequency, variable, grid, gridLabel, version, created, modified, accessed, xmlFile, ...
xmlwritedatetime, error, retired, retire_datetime, ignored, ignored_datetime
'''
conn = sqlite3.connect(sqlDB)
c = conn.cursor()
# Get all paths
c.execute('select * from paths;')
a = c.fetchall()
conn.close()
db = {}
for row in a:
db[row[0]] = {'keyid' : row[1], 'mip_era' : row[2], 'activity' : row[3], 'institute' : row[4], 'model' : row[5], 'experiment' : row[6], 'member' : row[7], 'cmipTable' : row[8], 'realm' : row[9], 'frequency' : row[10], 'variable' : row[11], 'grid' : row[12], 'gridLabel' : row[13], 'version' : row[14], 'created' : row[15], 'modified' : row[16], 'accessed' : row[17], 'xmlFile' : row[18], 'xmlwritedatetime' : row[19], 'error' : row[20], 'retired' : row[21], 'retire_datetime' : row[22], 'ignored' : row[23], 'ignored_datetime' : row[23]}
return db
def getInvalidDBPaths(sqlDB):
'''
db = getInvalidDBPaths(sqlDB)
Function uses the specified sql database and returns a list
of paths that are known to be invalid.
Input:
sqlDb (string): filename of sqlite file
Returns:
invalidPaths (list)
'''
conn = sqlite3.connect(sqlDB)
c = conn.cursor()
# Create table
c.execute('select path from invalid_paths;')
a = c.fetchall()
conn.close()
db = []
for row in a:
db.append(row[0])
return db
def getRetiredDBPaths(sqlDB):
'''
db = getRetiredDBPaths(sqlDB)
Function uses the specified sql database and returns a list
of paths that are known to be retired.
Input:
sqlDb (string): filename of sqlite file
Returns:
retiredPaths (list)
'''
conn = sqlite3.connect(sqlDB)
c = conn.cursor()
# Create table
c.execute('select path from paths where retired = 1;')
a = c.fetchall()
conn.close()
db = []
for row in a:
db.append(row[0])
return db
def initializeDB(sqlDB):
'''
initializeDB(sqlDB)
Function initialized a sqlite database with the correct
tables to be used with xagg software.
Input:
sqlDb (string): filename of sqlite file
'''
conn = sqlite3.connect(sqlDB)
c = conn.cursor()
# Create table
c.execute('''drop table if exists paths;''')
c.execute('''CREATE TABLE paths
(path varchar(255), keyid varchar(255), mip_era varchar(255), activity varchar(255), institute varchar(255), model varchar(255), experiment varchar(255), member varchar(255), cmipTable varchar(255), realm varchar(255), frequency varchar(255), variable varchar(255), grid varchar(255), gridLabel varchar(255), version varchar(255), created datetime, modified datetime, accessed datetime, xmlFile varchar(255), xmlwritedatetime datetime, error varchar(255), retired BOOLEAN, retire_datetime datetime, ignored BOOLEAN, ignored_datetime DATETIME)''')
c.execute('''CREATE INDEX pathIndex ON paths (path);''')
c.execute('''drop table if exists invalid_paths;''')
c.execute('''CREATE TABLE invalid_paths (path varchar(255), datetime DATETIME)''')
c.execute('''drop table if exists stats;''')
c.execute('''CREATE TABLE stats (indicator varchar(255), value int, datetime DATETIME)''')
c.execute('''drop table if exists runs;''')
c.execute('''CREATE TABLE runs (datetime DATETIME, total INT, new INT, invalid INT, modified INT, missing INT, returned INT, deleted INT)''')
# Save (commit) the changes
conn.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()
def parallelFindData(data_directories, numProcessors=20, split=[], rmDir=[]):
'''
diskStat = parallelFindData(data_directories, numProcessors=20, split=[], rmDir=[])
Function simply parallelizes findDiskPaths to speed up the search for eligible paths.
Input:
data_directories (list): list of parent directories to search
num_processors (int): number of processors to use (default 20)
split (list): list of parent directories that should be split apart into separate threads
e.g., '/parent/' -> ['parent/child1', 'parent/child2']
rmDir (list): list of directories to ignore while scanning
Returns:
dbPaths[childPath] = {'ctime' : ctime, 'mtime' : mtime, 'atime' : atime}
'''
# grab the right number of processors
for d in split:
dlist = glob.glob(d + '/*/')
data_directories = data_directories + dlist
for d in rmDir:
data_directories.remove(d)
if len(data_directories) > numProcessors:
nfscan = numProcessors
else:
nfscan = len(data_directories)
print('Using ' + str(nfscan) + ' processors to check ' + str(len(data_directories)) + ' directories...', end='\n \n')
results = Parallel(n_jobs=nfscan)(delayed(findDiskPaths)(parent)\
for (parent) in data_directories)
diskStat = {}
for d in results:
keys = d.keys()
for key in keys:
diskStat[key] = d[key]
return diskStat
def createLookupDictionary(paths, outfile='data/cmipMeta.pkl'):
"""
createLookupDictionary(paths, outfile='data/cmipMeta.pkl')
Function processes a bunch of paths and stores a dictionary file with
metadata lookup information. This allows you to quickly determine a path's
frequency, realm, and dimensions (which are not contained in the directory
structure).
Inputs:
paths (list)
outfile (string filename), default 'data/cmipMeta.pkl'
variable: e.g. 'ta'
dictObj: pickle file (dictionary) that returns appropriate outputs for given inputs.
"""
dictObj = {}
for i, p in enumerate(paths):
if np.mod(i,10000) == 0:
print(str(i) + '/' + str(len(paths)))
validPath, keyId, mip_era, activity, institute, model, experiment, member, cmipTable, realm, frequency, variable, grid, gridLabel, version = parsePath(p)
if validPath:
key = '.'.join([mip_era, cmipTable, variable])
else:
continue
if key in dictObj:
continue
else:
try:
frequency, realm, dimensions = lookupCMIPMetadata(mip_era, cmipTable, variable)
dictObj[key] = [frequency, realm, dimensions]
except:
continue
with open(outfile, 'wb') as f:
pickle.dump(dictObj, f, pickle.HIGHEST_PROTOCOL)
def getCMIPMeta(outfile='data/cmipMeta.pkl'):
"""
dictObj = getCMIPMeta(outfile='data/cmipMeta.pkl')
Function reads the CMIP Metadata pickle file and returns the information
as an in-memory dictionary.
"""
with open(outfile, 'rb') as f:
dictObj = pickle.load(f)
return dictObj
def createFilename(xmlOutputDir, pathMeta):
"""
fn = createFilename(xmlOutputDir, pathMeta)
Function contains the logic to create output filenames for a given output
directory and path metadata.
Inputs:
xmlOutputDir (string): directory which contains the xml file tree (e.g., '/data/goes/here/')
pathMeta (dictionary): dictionary object containing necesary metadata to create filename (specified below)
Returns:
fn (string)
pathMeta contains the following keys:
mip_era, activity, institute, model, experiment, member, cmipTable, realm, frequency, variable, grid, gridLabel, version
"""
reDec = re.compile(r'decadal[0-9]{4}')
if not not re.search(reDec, pathMeta['experiment']):
experimentPath = 'decadal'
else:
experimentPath = pathMeta['experiment']
# output filename
if pathMeta['frequency'] == 'fx':
fn = xmlOutputDir + '/' + pathMeta['mip_era'] + '/' + pathMeta['frequency'] + '/' + pathMeta['variable'] + '/' + pathMeta['mip_era'] + '.' + pathMeta['activity'] + '.' + pathMeta['experiment'] + '.' + pathMeta['institute'] + '.' + pathMeta['model'] + '.' + pathMeta['member'] + '.' + pathMeta['frequency'] + '.' + pathMeta['variable'] + '.' + pathMeta['realm'] + '.' + pathMeta['gridLabel'] + '.' + pathMeta['version'] + '.0000000.0.xml'
else:
fn = xmlOutputDir + '/' + pathMeta['mip_era'] + '/' + pathMeta['activity'] + '/' + experimentPath + '/' + pathMeta['realm'] + '/' + pathMeta['frequency'] + '/' + pathMeta['variable'] + '/' + pathMeta['mip_era'] + '.' + pathMeta['activity'] + '.' + pathMeta['experiment'] + '.' + pathMeta['institute'] + '.' + pathMeta['model'] + '.' + pathMeta['member'] + '.' + pathMeta['frequency'] + '.' + pathMeta['variable'] + '.' + pathMeta['realm'] + '.' + pathMeta['gridLabel'] + '.' + pathMeta['version'] + '.0000000.0.xml'
fn = fn.replace('//','/')
return fn
def xmlWrite(inpath, outfile):
"""
xmlWrite(inpath, outfile)
Function calls cdscan to create an xml file (outfile) for a given
directory of CMIP data (inpath).
Inputs:
inpath (string): directory containing input files
outfile (string): xml file to write
Returns:
out, err: command output and error message strings
"""
cmd = 'cdscan -x ' + outfile + ' ' + inpath + '/*.nc'
cmd = cmd.replace('//', '/')
p = Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE)
out,err = p.communicate()
return out, err
def errorLogic(fn, inpath, err):
"""
fn, err = errorLogic(fn, inpath, err)
Function processes error messages and groups the errors into a subset of categories. It then
renames the xmlFile (if present) to reflect these errors.
Inputs:
fn (string): xml filename
inpath: path of input netCDF data
err: error message returned during xmlWrite call
Returns:
fn: final filename
error: processed error message
"""
# check for zero sized files and other no write errors (if there xml did not write)
zeroSize = False
error = None
if not os.path.exists(fn):
fiter = glob.glob(inpath + '/*.nc')
fn = None
for fnc in fiter:
fsize = os.path.getsize(fnc)
if fsize == 0:
zeroSize = True
break
if zeroSize:
error = 'No write: filesize of zero'
elif str(err).find('CDMS I/O error: End of file') >= 0:
error = 'No write: CDMS I/O Error'
elif str(err).find('RuntimeError: Dimension time in files') >= 0:
error = 'RuntimeError: Dimension time in files'
elif str(err).find('CDMS I/O error: Determining type of file') >= 0:
error = 'CDMS I/O error: Determining type of file'
elif str(err).find('Cannot allocate memory') >= 0:
error = 'Cannot allocate memory'
elif str(err).find('Invalid relative time units') >= 0:
error = 'Invalid relative time units'
else:
error = 'No write'
else:
# if xml wrote to disk, update error codes in filename
errors = getWarnings(str(err))
if len(errors) > 0:
errorCode = parseWarnings(errors)
fnNew = fn.replace('0000000',errorCode)
os.rename(fn, fnNew)
fn = fnNew
if len(errors) > 255:
errors = errors[0:255]
error = errors
return fn, error
def getWarnings(err):
"""
errorCode = getWarnings(err)
Function parses warning messages for more specific error.
Inputs:
err: error message returned during xmlWrite call
Returns:
errorCode: subsetted warning message
"""
errstart = err.find('Warning') ; # Indexing returns value for "W" of warning
err1 = err.find(' to: [')
if err1 == -1: err1 = len(err)-1 ; # Warning resetting axis time values
err2 = err.find(': [')
if err2 == -1: err2 = len(err)-1 ; # Problem 2 - cdscan error - Warning: axis values for axis time are not monotonic: [
err3 = err.find(': [')
if err3 == -1: err3 = len(err)-1 ; # Problem 2 - cdscan error - Warning: resetting latitude values: [
err4 = err.find(') ')
if err4 == -1: err4 = len(err)-1 ; # Problem 1 - zero infile size ; Problem 4 - no outfile
err5 = err.find(', value=')
if err5 == -1: err5 = len(err)-1 ; # Problem 2 - cdscan error - 'Warning, file XXXX, dimension time overlaps file
errorCode = err[errstart:min(err1,err2,err3,err4,err5)]
errPython = err.find('Traceback (most recent call last)')
if errPython > 0:
errorCode = errorCode + 'Python Error'
return errorCode
def parseWarnings(err):
"""
errorCode = parseWarnings(err)
Function parses subsetted warning messages for exact error code [0-6].
Inputs:
err: Subsetted warning from getWarning.
Returns:
errorCode: string specifying error category (details below)
'0000000' - no warnings
1 - dimension time contains values...
1 - Warning, Axis values for axis are not monotonic...
1 - Warning: resetting latitude values...
1 - Zero infile size
1 - Dimension time overlaps...
1 - Your first bounds...
1 - Python error ...
"""
errorCode = list('0000000')
if err.find('dimension time contains values in file') >= 0:
errorCode[0] = '1'
if err.find('Warning: Axis values for axis time are not monotonic') >= 0:
errorCode[1] = '1'
if err.find('Warning: resetting latitude values') >= 0:
errorCode[2] = '1'
if err.find('zero infile size') >= 0:
errorCode[3] = '1'
if err.find('dimension time overlaps file') + err.find('dimension time contains values in file') >= 0:
errorCode[4] = '1'
if err.find('Your first bounds') >= 0:
errorCode[5] = '1'
if err.find('Python Error') >= 0:
errorCode[6] = '1'
errorCode = "".join(errorCode)
return errorCode
def sqlUpdate(sqlDB, table, columns, constraint, datalist):
"""
sqlUpdate(sqlDB, table, columns, constraint, datalist)
Function will update rows in sqlite database with new information.
Inputs:
sqlDB: string filename
table (string): table to update
columns (list): columns to update
constraint (string): column which is used as a constraint
dataList: list containing new data
dataList is a list such that each row contains columns to update
followed by the constraint
dataList: ['blue', None, 'audi', 5]
where 'newData', NoneN, and 'otherNewData' are the updated information and
5 is the constraint. This could correspond to a constraint like 'number'
and columns like ['color', 'animal', 'model_car'].
"""
if type(columns) == str:
columns = [columns]
q = 'UPDATE ' + table + ' SET ' + '=?, '.join(columns) + '=? WHERE ' + constraint + '=?;'
# connect to db
conn = sqlite3.connect(sqlDB)
c = conn.cursor()
# update data
if len(datalist) > 1000:
for i in range(int(np.ceil(len(datalist)/1000))):
ro = datalist[i*1000:i*1000+1000]
c.executemany(q, ro)
conn.commit()
else:
c.executemany(q, datalist)
conn.commit()
conn.close() # close connection
def sqlInsert(sqlDB, table, columns, datalist):
"""
sqlInsert(sqlDB, table, columns, datalist)
Function will update rows in sqlite database with new information.
Inputs:
sqlDB: string filename
table (string): table to update
columns (list): columns to update
dataList: list containing new data
dataList is a list such that each row contains columns to insert
dataList: ['blue', None, 'audi', 5]
where 'newData', None, and 'otherNewData' are the new information.
This could correspond to columns like ['color', 'animal', 'model_car'].
"""
q = 'INSERT INTO ' + table + '(' + ', '.join(columns) + ') VALUES(' + '?,' * (len(columns) - 1) + '?);'
# connect to db
conn = sqlite3.connect(sqlDB)
c = conn.cursor()
# update data
c.executemany(q, datalist)
conn.commit() # commit changes
conn.close() # close connection
def process_path(xmlOutputDir, pathMeta, inpath):
"""
inpath, fn, xmlwritetime, error = process_path(xmlOutputDir, pathMeta, inpath)
Function processes a path by creating an output filename for the xml file,
creates an xml file for a given directory, and processes any error messages.
Inputs:
xmlOutputDir (string): base directory for xml tree
pathMeta (dictionary): dictionary object containing necesary metadata to create filename (specified below)
inpath (string): directory which contains the netCDF data to process
Returns:
inpath (string): directory which contains the netCDF data to process
fn (string): filename of xmlfile written out (or None if applicable)
xmlwritetime: sql datetime string of xml write time
error: parsed error message
pathMeta contains the following keys:
mip_era, activity, institute, model, experiment, member, cmipTable, realm, frequency, variable, grid, gridLabel, version
"""
fn = createFilename(xmlOutputDir, pathMeta)
ensure_dir(fn)
out,err = xmlWrite(inpath, fn)
# get write time
xmlwritetime = toSQLtime(datetime.datetime.now())
# get warnings
fn, error = errorLogic(fn, inpath, err)
# update database with xml write
return inpath, fn, xmlwritetime, error
def updateDatabaseHoldings(sqlDB, diskPaths, diskStat, dbPaths, db, invalidPaths, retiredPaths, quiet=False):
"""
updateDatabaseHoldings(sqlDB, diskPaths, diskStat, dbPaths, db, invalidPaths, retiredPaths, quiet=False)
Function updates all database information based on scan of all disk paths. Functionality includes:
* Will kill job if more than 10% of data isn't in diskPaths
* Inserts new paths into database (with appropriate metadata)
* Updates modified times on modified directories
* Updates invalidPaths with new invalid paths
* Will retire paths that are removed and delete underlying xmls
* Will "un-retire" a path that re-appears on the disk
* Will delete xmls where path is modified (for later re-creation)
* Prints out relevant information and records this information in the database (runs table)
* Total Scanned, New, Invalid, Modified, Missing, Returned, Deleted
Inputs:
sqlDB: string filename
diskPaths: keys of all disk paths
diskStat: Dictionary of stat information for each path (see findDiskPaths)
dbPaths: keys of all paths in the database
db: database dictionary object (see getDBPaths)
invalidPaths: list of invalid paths (see getInvalidDBPaths)
retiredPaths: list of retired paths (see getRetiredDBPaths)
quiet (optional, boolean): suppress display information if True (default False)
"""
# create sets for lists to speed up logic
retiredPaths = set(retiredPaths)
invalidPaths = set(invalidPaths)
# Sanity check to make sure disks aren't unmounted
if len(diskPaths) < (len(dbPaths) - len(retiredPaths)) * 0.9:
raise ValueError('A large number of paths are missing - check disks')
# Bin paths into appropriate categories
newPaths = []
modifiedPaths = []
unretirePaths = []
for p in diskPaths:
if p in retiredPaths:
unretirePaths.append(p)
if p in invalidPaths:
continue
if p in dbPaths:
if sqltimeToDatetime(diskStat[p]['mtime']) > sqltimeToDatetime(db[p]['modified']):
modifiedPaths.append(p)
else:
newPaths.append(p)
missingPaths = []
for p in dbPaths:
if p in retiredPaths:
continue
if p not in diskPaths:
missingPaths.append(p)
newCount = 0
invalidCount = 0
modifiedCount = len(modifiedPaths)
missingCount = len(missingPaths)
unretiredCount = len(unretirePaths)
deleteCount = 0
## process new records (not in database)
if len(newPaths) > 0:
if not quiet:
print('Parsing data paths not in database')
print(time.ctime())
print()
# get metadata object
dictObj = getCMIPMeta()