From b680bee070dc5e20d74bc10a725dc65431d06afa Mon Sep 17 00:00:00 2001 From: burrches Date: Sat, 28 Sep 2019 19:11:12 -0400 Subject: [PATCH] update all print statements to functions --- .../run_statistical_downscaling.py | 2 +- docs/source/ocw/overview.rst | 6 +-- obs4MIPs/Toolbox/CMORresources.py | 4 +- obs4MIPs/Toolbox/ESGFexcel.py | 15 ++++---- obs4MIPs/Toolbox/ESGFresources.py | 19 +++++----- obs4MIPs/factory/formats.py | 6 ++- obs4MIPs/obs4MIPs_process.py | 38 ++++++++++--------- 7 files changed, 49 insertions(+), 41 deletions(-) diff --git a/RCMES/statistical_downscaling/run_statistical_downscaling.py b/RCMES/statistical_downscaling/run_statistical_downscaling.py index 18b39fe1..832c2836 100644 --- a/RCMES/statistical_downscaling/run_statistical_downscaling.py +++ b/RCMES/statistical_downscaling/run_statistical_downscaling.py @@ -91,7 +91,7 @@ def extract_data_at_nearest_grid_point(target_dataset, longitude, latitude): config_file = str(sys.argv[1]) -print 'Reading the configuration file ', config_file +print('Reading the configuration file ', config_file) config = yaml.load(open(config_file)) diff --git a/docs/source/ocw/overview.rst b/docs/source/ocw/overview.rst index 13f4aba7..702f5389 100644 --- a/docs/source/ocw/overview.rst +++ b/docs/source/ocw/overview.rst @@ -68,7 +68,7 @@ In general, it's uncommon to run a metric outside of an evaluation, however you >>> import ocw.metrics >>> # Load 2 datasets >>> bias = ocw.metrics.Bias() ->>> print bias.run(dataset1, dataset2) +>>> print(bias.run(dataset1, dataset2)) While this can be useful for one-off situations, it's far more likely that you'll need to run a number of metrics over a number of datasets. This is where running metrics within an evaluation comes in (covered in greater detail below). @@ -141,8 +141,8 @@ When you have a large collection of datasets and a large collection of metrics t >>> >>> new_eval = eval.Evaluation(ref_dataset, target_datasets, metrics) >>> new_eval.run() ->>> print new_eval.results ->>> print new_eval.unary_results +>>> print(new_eval.results) +>>> print(new_eval.unary_results) First, we load the datasets to process and perform any necessary manipulations (which are omitted for brevity). Then, we load the metrics that we want to run (namely, ``Bias`` and ``TemporalStdDev``). We then load our evaluation object:: diff --git a/obs4MIPs/Toolbox/CMORresources.py b/obs4MIPs/Toolbox/CMORresources.py index d20f9711..4aab0fca 100644 --- a/obs4MIPs/Toolbox/CMORresources.py +++ b/obs4MIPs/Toolbox/CMORresources.py @@ -15,6 +15,8 @@ # specific language governing permissions and limitations # under the License. +from __future__ import print_function + import netCDF4 import re import pdb @@ -34,7 +36,7 @@ def __init__( self, inpath, table, entry=None ): f=open( inpath + '/' + table, 'r') if( f == None ): - print "Table file %s does not exist " % (inpath + "/" + table ) + print("Table file {} does not exist ".format(inpath + "/" + table)) lines = f.readlines() diff --git a/obs4MIPs/Toolbox/ESGFexcel.py b/obs4MIPs/Toolbox/ESGFexcel.py index 92fd4df0..e559e2a6 100644 --- a/obs4MIPs/Toolbox/ESGFexcel.py +++ b/obs4MIPs/Toolbox/ESGFexcel.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +from __future__ import print_function import pdb import xlrd @@ -39,8 +40,8 @@ def __init__(self,excelFile): if( os.path.isfile(self.xcl) ): wb=xlrd.open_workbook(self.xcl) else: - print "****** Could not find "+self.xcl+" file ****" - print "****** Please check excel file name ****" + print("****** Could not find {} file ****".format(self.xcl)) + print("****** Please check excel file name ****") raise NameError(self.xcl) sheet=wb.sheet_by_name('Resources') @@ -55,7 +56,7 @@ def __init__(self,excelFile): for i in arange(sheet.nrows-1) + 1] ] ) pdb.set_trace() self.ReadXCL() - print self.resources.keys() + print(self.resources.keys()) def ReadXCL(self): ''' @@ -64,14 +65,14 @@ def ReadXCL(self): try: import xlrd except: - print "****** Could not find xlrd Python Package ****" - print "****** Please install xlrd package to read excel files ****" + print("****** Could not find xlrd Python Package ****") + print("****** Please install xlrd package to read excel files ****") if( os.path.isfile(self.xcl) ): wb=xlrd.open_workbook(self.xcl) else: - print "****** Could not find "+self.xcl+" file ****" - print "****** Please check excel file name ****" + print("****** Could not find {} file ****".format(self.xcl)) + print("****** Please check excel file name ****") raise NameError(self.xcl) sheet=wb.sheet_by_name('Variables') diff --git a/obs4MIPs/Toolbox/ESGFresources.py b/obs4MIPs/Toolbox/ESGFresources.py index ddbfe60f..57378ad3 100644 --- a/obs4MIPs/Toolbox/ESGFresources.py +++ b/obs4MIPs/Toolbox/ESGFresources.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +from __future__ import print_function import pdb import shutil @@ -83,14 +84,14 @@ def ReadXCL(self): try: import xlrd except: - print "****** Could not find xlrd Python Package ****" - print "****** Please install xlrd package to read excel files ****" + print("****** Could not find xlrd Python Package ****") + print("****** Please install xlrd package to read excel files ****") if( os.path.isfile(self.xcl) ): wb=xlrd.open_workbook(self.xcl) else: - print "****** Could not find "+self.xcl+" file ****" - print "****** Please check excel file name ****" + print("****** Could not find {} file ****".format(self.xcl)) + print("****** Please check excel file name ****") raise NameError(self.xcl) sheet=wb.sheet_by_name('Variables') @@ -184,7 +185,7 @@ def movefiles(rc): if files.endswith(".nc"): filetimestamp = files.split('_')[-1].strip(".nc") file = os.path.join(r,files) - print file + print(file) # ----------------- # Delete attributes # ------------------ @@ -192,7 +193,7 @@ def movefiles(rc): DelGlbAttributes=eval(rc['DelGlbAttributes'].\ replace('\\','\'')) for attribute in DelGlbAttributes: - print "Deleting attribute: %s" % attribute + print("Deleting attribute: {}".format(attribute)) Attr.GlbDel(attribute) # ----------------- # set attributes @@ -200,7 +201,7 @@ def movefiles(rc): SetGlbAttributes=eval(rc['SetGlbAttributes'].\ replace('\\','\'')) for (attribute,Value) in SetGlbAttributes: - print "Assigning attribute (%s,%s)" % (attribute,Value) + print("Assigning attribute ({},{})".format(attribute, Value)) Attr.GlbSet(attribute,Value) Attr.close() @@ -221,8 +222,8 @@ def movefiles(rc): # ----------- # Move files # ----------- - print file - print newfilename + print(file) + print(newfilename) os.rename(file,newfilename) diff --git a/obs4MIPs/factory/formats.py b/obs4MIPs/factory/formats.py index ced7d746..ed5091c0 100644 --- a/obs4MIPs/factory/formats.py +++ b/obs4MIPs/factory/formats.py @@ -19,6 +19,8 @@ # # Select the right handler depending on the file format +from __future__ import print_function + import os import pdb import sys @@ -220,7 +222,7 @@ def open(self, file, variable=None): for file in self.flist: filename=file.strip() if( not os.path.exists(filename) ): - print "File %s does not exist in filelist" % filename + print("File {} does not exist in filelist".format(filename)) # -------------------------------------------------------- # Extract General information from first file in the list @@ -252,7 +254,7 @@ def getData(self, variable=None): # Concatenate following files # --------------------------- for filename in self.flist[ 1: ]: - print "reading %s" % filename.strip() + print("reading {}".format(filename.strip())) f = cdms2.open( filename.strip(), 'r' ) data2 = f(self.vartoread)[:] data = numpy.concatenate((data,data2), axis=0) diff --git a/obs4MIPs/obs4MIPs_process.py b/obs4MIPs/obs4MIPs_process.py index 3370100c..3a03c927 100755 --- a/obs4MIPs/obs4MIPs_process.py +++ b/obs4MIPs/obs4MIPs_process.py @@ -17,6 +17,8 @@ # specific language governing permissions and limitations # under the License. +from __future__ import print_function + import cdms2 import cdtime import cmor @@ -62,12 +64,12 @@ def process( rc ): except: tmplFile = rc['file_template'].format(year) if( not os.path.isfile( tmplFile) ) : - print "**** Warning %s not found\n" % ( tmplFile ) + print("**** Warning {} not found\n".format(tmplFile)) continue files= os.popen( "ls " + tmplFile).readlines() if( files == [] ): - print "No file found: Check your resource file" + print("No file found: Check your resource file") return -1 # ------------------------------------------------ # Get the right handler to manage this file format @@ -137,13 +139,13 @@ def process( rc ): variable=aVariable[j] Handler.open(fnm, variable=variable) rc['cvrt_original_var'] = aVariable[j] - print "Working on variable %s " % variable + print("Working on variable {} ".format(variable)) except: if( aVariable[j] != 'equation' ) : - print "Variable %s can't open" % variable + print("Variable {} can't open".format(variable)) continue else: - print "Executing %s " % eval(rc['equation'])[j] + print("Executing {} ".format(eval(rc['equation'])[j])) # pdb.set_trace() rc['cvrt_original_units'] = eval(rc['original_units'])[j] @@ -405,19 +407,19 @@ def usage(message): ''' Describe program synopsis. ''' - print - print "*************************" - print message - print "*************************" - print - print - print "obs4MIPS_process.py [-h] -r resource" - print " resource: File containing Global attributes" - print "" - print "obs4MIPS will convert an input data file into CMIP5 format using " - print "CMOR. A directory path will be creating using CMOR by default or " - print "using a template provided in the resource file." - print + print() + print("*************************") + print(message) + print("*************************") + print() + print() + print("obs4MIPS_process.py [-h] -r resource") + print(" resource: File containing Global attributes") + print("") + print("obs4MIPS will convert an input data file into CMIP5 format using ") + print("CMOR. A directory path will be creating using CMOR by default or ") + print("using a template provided in the resource file.") + print() # ******************************************************************** #