Skip to content

Commit

Permalink
moved sheopn to new pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ledm committed Mar 22, 2024
1 parent 8ca9a5e commit a880296
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 53 deletions.
17 changes: 11 additions & 6 deletions bgcval2/bgcvaltools/makeMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,12 @@ def makeMask(name, newSlice, xt, xz, xy, xx, xd, debug=False):
shelveFn = bvt.folder(os.path.join(paths.shelvedir, "MatchingMasks/"))+ newSlice+"_diag_maskMask.shelve"

try:
s = shOpen(shelveFn)
lldict = s['lldict']
s.close()
with shOpen(shelveFn) as sh:
lldict = sh['lldict']
# s = shOpen(shelveFn)
# lldict = s['lldict']
# s.close()

except:
lldict = {}

Expand Down Expand Up @@ -517,9 +520,11 @@ def makeMask(name, newSlice, xt, xz, xy, xx, xd, debug=False):
nmask[i] = 1
if i > 0:
try:
s = shOpen(shelveFn)
s['lldict'] = lldict
s.close()
# s = shOpen(shelveFn)
# s['lldict'] = lldict
# s.close()
with shOpen(shelveFn) as sh:
sh['lldict'] = lldict
except:
print(
"makeMask:\tWARNING:\tUnable to save lldict at this time")
Expand Down
28 changes: 13 additions & 15 deletions bgcval2/bgcvaltools/removeMaskFromshelve.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@

def removeFromShelves(fn, removeRegions):
print('removing:', removeRegions, 'from', fn)
sh = shOpen(fn)

modeldata = sh['modeldata']

for key in list(modeldata.keys()):
try:
(r, l, m) = key
except:
continue
if r in removeRegions:
print('modeldata[', (r, l, m), '] will be deleted')
del modeldata[(r, l, m)]

sh['modeldata'] = modeldata
sh.close()
with shOpen(fn) as sh:
modeldata = sh['modeldata']
for key in list(modeldata.keys()):
try:
(r, l, m) = key
except:
continue
if r in removeRegions:
print('modeldata[', (r, l, m), '] will be deleted')
del modeldata[(r, l, m)]

sh['modeldata'] = modeldata
# sh.close()


removeRegions = [
Expand Down
75 changes: 43 additions & 32 deletions bgcval2/timeseries/profileAnalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,13 @@ def loadModel(self):
if self.clean:
print("profileAnalysis:\tloadModel:\tUser requested clean run. Wiping old data.")
assert 0
sh = shOpen(self.shelvefn)
readFiles = sh['readFiles']
modeldataD = sh['modeldata']
sh.close()

with shOpen(self.shelvefn) as sh:
readFiles = sh['readFiles']
modeldataD = sh['modeldata']
# sh = shOpen(self.shelvefn)
# readFiles = sh['readFiles']
# modeldataD = sh['modeldata']
# sh.close()
print("OprofileAnalysis:\tloadModel:\tpened shelve:", self.shelvefn, '\tread', len(
readFiles))
except:
Expand Down Expand Up @@ -300,19 +302,25 @@ def loadModel(self):
if openedFiles:
print("Saving shelve:", self.dataType, self.shelvefn, '\tread', len(
readFiles))
sh = shOpen(self.shelvefn)
sh['readFiles'] = readFiles
sh['modeldata'] = modeldataD
sh.close()
with shOpen(self.shelvefn) as sh:
sh['readFiles'] = readFiles
sh['modeldata'] = modeldataD
# sh = shOpen(self.shelvefn)
# sh['readFiles'] = readFiles
# sh['modeldata'] = modeldataD
# sh.close()
openedFiles = 0
if openedFiles:
print("Saving shelve:", self.dataType, self.shelvefn, '\tread', len(
readFiles))
sh = shOpen(self.shelvefn)
sh['readFiles'] = readFiles
sh['modeldata'] = modeldataD
sh.close()
with shOpen(self.shelvefn) as sh:
sh['readFiles'] = readFiles
sh['modeldata'] = modeldataD

# sh = shOpen(self.shelvefn)
# sh['readFiles'] = readFiles
# sh['modeldata'] = modeldataD
# sh.close()
self.modeldataD = modeldataD
if self.debug:
print("profileAnalysis:\tloadModel.\t Model loaded:", list(self.modeldataD.keys(
Expand Down Expand Up @@ -375,9 +383,11 @@ def loadData(self):
if self.clean:
print("profileAnalysis:\t loadData\tUser requested clean run. Wiping old data.")
assert 0
sh = shOpen(self.shelvefn_insitu)
dataD = sh['dataD']
sh.close()
with shOpen(self.shelvefn_insitu) as sh:
dataD = sh['dataD']
# sh = shOpen(self.shelvefn_insitu)
# dataD = sh['dataD']
# sh.close()
print("profileAnalysis:\t loadData\tOpened shelve:", self.shelvefn_insitu)
self.dataD = dataD
except:
Expand Down Expand Up @@ -488,18 +498,20 @@ def loadData(self):
r, l), 'mean:\t', meandatad)

print("profileAnalysis:\t loadData.\tSaving shelve: (layer", l, ")", self.shelvefn_insitu)
sh = shOpen(self.shelvefn_insitu)
sh['dataD'] = dataD
sh.close()

with shOpen(self.shelvefn_insitu) as sh:
sh['dataD'] = dataD
# sh = shOpen(self.shelvefn_insitu)
# sh['dataD'] = dataD
# sh.close()
###############
# Savng shelve
print("profileAnalysis:\t loadData.\tSaving shelve:", self.shelvefn_insitu)
try:
sh = shOpen(self.shelvefn_insitu)
sh['dataD'] = dataD
sh.close()

with shOpen(self.shelvefn_insitu) as sh:
sh['dataD'] = dataD
# sh = shOpen(self.shelvefn_insitu)
# sh['dataD'] = dataD
# sh.close()
print("profileAnalysis:\t loadData.\tSaved shelve:", self.shelvefn_insitu)

except:
Expand All @@ -509,18 +521,17 @@ def loadData(self):
for key in sorted(dataD.keys()):

print(key, ':\t', dataD[key])
sh = shOpen(bvt.folder('./tmpshelves') + 'tmshelve.shelve')
sh['dataD'] = dataD[key]
sh.close()
with shOpen(bvt.folder('./tmpshelves') + 'tmshelve.shelve') as sh:
sh['dataD'] = dataD[key]
# sh = shOpen(bvt.folder('./tmpshelves') + 'tmshelve.shelve')
# sh['dataD'] = dataD[key]
# sh.close()
print("saved fine:\t./tmpshelves/tmshelve.shelve")

shutil.move(self.shelvefn_insitu, self.shelvefn_insitu + '.broken')

# try:
sh = shOpen(self.shelvefn_insitu)
sh['dataD'] = dataD
sh.close()

with shOpen(self.shelvefn_insitu) as sh:
sh['dataD'] = dataD

# except:
# print "profileAnalysis:\t WARNING.\tUnable to Save in situ shelve.\tYou'll have to input it each time.",self.shelvefn_insitu
Expand Down

0 comments on commit a880296

Please sign in to comment.