From a8802967530257989c8c7a168e3094eef61a19e8 Mon Sep 17 00:00:00 2001 From: Lee de Mora Date: Fri, 22 Mar 2024 11:53:40 +0000 Subject: [PATCH] moved sheopn to new pattern --- bgcval2/bgcvaltools/makeMask.py | 17 +++-- bgcval2/bgcvaltools/removeMaskFromshelve.py | 28 ++++---- bgcval2/timeseries/profileAnalysis.py | 75 ++++++++++++--------- 3 files changed, 67 insertions(+), 53 deletions(-) diff --git a/bgcval2/bgcvaltools/makeMask.py b/bgcval2/bgcvaltools/makeMask.py index e7463a2a..1a4f7a57 100644 --- a/bgcval2/bgcvaltools/makeMask.py +++ b/bgcval2/bgcvaltools/makeMask.py @@ -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 = {} @@ -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") diff --git a/bgcval2/bgcvaltools/removeMaskFromshelve.py b/bgcval2/bgcvaltools/removeMaskFromshelve.py index 875beff2..59ade665 100644 --- a/bgcval2/bgcvaltools/removeMaskFromshelve.py +++ b/bgcval2/bgcvaltools/removeMaskFromshelve.py @@ -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 = [ diff --git a/bgcval2/timeseries/profileAnalysis.py b/bgcval2/timeseries/profileAnalysis.py index 4f2f6ae7..1ee17f81 100644 --- a/bgcval2/timeseries/profileAnalysis.py +++ b/bgcval2/timeseries/profileAnalysis.py @@ -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: @@ -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( @@ -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: @@ -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: @@ -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