climate-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From good...@apache.org
Subject svn commit: r1482342 - in /incubator/climate/trunk/rcmet/src/main/python: bin/rcmet.py rcmes/classes.py rcmes/cli/do_rcmes_processing_sub.py rcmes/cli/rcmet_ui.py rcmes/utils/misc.py
Date Tue, 14 May 2013 14:18:22 GMT
Author: goodale
Date: Tue May 14 14:18:22 2013
New Revision: 1482342

URL: http://svn.apache.org/r1482342
Log:
CLIMATE-6: Working toward pulling all of the Question / Answer code out and into smaller re-usable
functions
For complete details check: https://reviews.apache.org/r/11098/

Modified:
    incubator/climate/trunk/rcmet/src/main/python/bin/rcmet.py
    incubator/climate/trunk/rcmet/src/main/python/rcmes/classes.py
    incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/do_rcmes_processing_sub.py
    incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/rcmet_ui.py
    incubator/climate/trunk/rcmet/src/main/python/rcmes/utils/misc.py

Modified: incubator/climate/trunk/rcmet/src/main/python/bin/rcmet.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/rcmet/src/main/python/bin/rcmet.py?rev=1482342&r1=1482341&r2=1482342&view=diff
==============================================================================
--- incubator/climate/trunk/rcmet/src/main/python/bin/rcmet.py (original)
+++ incubator/climate/trunk/rcmet/src/main/python/bin/rcmet.py Tue May 14 14:18:22 2013
@@ -15,6 +15,7 @@ import storage.rcmed as db
 from toolkit import do_data_prep, process, metrics
 from utils import misc
 from classes import JobProperties, Model, GridBox
+from cli import rcmet_ui as ui
 
 parser = argparse.ArgumentParser(description='Regional Climate Model Evaluation Toolkit.
 Use -h for help and options')
 parser.add_argument('-c', '--config', dest='CONFIG', help='Path to an evaluation configuration
file')
@@ -38,29 +39,7 @@ def checkConfigSettings(config):
             _ =  misc.isDirGood(os.path.abspath(key_val[1]))
 
         else:
-            pass
-
-def getSettings(settings):
-    """
-    This function will collect 2 parameters from the user about the RCMET run they have started.
-    
-    Input::
-        settings - Empty Python Dictionary they will be used to store the user supplied inputs
-        
-    Output::
-        None - The user inputs will be added to the supplied dictionary.
-    """
-    settings['workDir'] = os.path.abspath(raw_input('Please enter workDir:\n> '))
-    if os.path.isdir(settings['workDir']):
-        pass
-    else:
-        makeDirectory(settings['workDir'])
-    
-    settings['cacheDir'] = os.path.abspath(raw_input('Please enter cacheDir:\n> '))
-    if os.path.isdir(settings['cacheDir']):
-        pass
-    else:
-        makeDirectory(settings['cacheDir'])    
+            pass    
 
 def setSettings(settings, config):
     """
@@ -76,14 +55,6 @@ def setSettings(settings, config):
     """
     pass
 
-def makeDirectory(directory):
-    print "%s doesn't exist.  Trying to create it now." % directory
-    try:
-        os.mkdir(directory)
-    except OSError:
-        print "This program cannot create dir: %s due to permission issues." % directory
-        sys.exit()
-
 def generateModels(modelConfig):
     """
     This function will return a list of Model objects that can easily be used for 
@@ -134,7 +105,7 @@ def generateSettings(config):
             workdir - string i.e. '/nas/run/rcmet/work/'
             cachedir - string i.e. '/tmp/rcmet/cache/'
     Output::
-        settings - Settings Object
+        JobProperties - JobProperties Object
     """
     # Setup the config Data Dictionary to make parsing easier later
     configData = {}
@@ -201,6 +172,8 @@ def runUsingConfig(argsConfig):
         raise
 
     jobProperties = generateSettings(userConfig.items('SETTINGS'))
+    workdir = jobProperties.workDir
+    
     try:
         gridBox = GridBox(jobProperties.latMin, jobProperties.lonMin, jobProperties.latMax,
                           jobProperties.lonMax, jobProperties.gridLonStep, jobProperties.gridLatStep)
@@ -210,20 +183,6 @@ def runUsingConfig(argsConfig):
     models = generateModels(userConfig.items('MODEL'))
     
     datasetDict = makeDatasetsDictionary(userConfig.items('RCMED'))
-    
-    
-    try:
-        subRegionConfig = misc.configToDict(userConfig.items('SUB_REGION'))
-        subRegions = misc.parseSubRegions(subRegionConfig)
-        # REORDER SUBREGION OBJECTS until we standardize on Python 2.7
-        # TODO Remove once Python 2.7 support is finalized
-        if subRegions:
-            subRegions.sort(key=lambda x:x.name)
-        
-    except ConfigParser.NoSectionError:
-        print 'SUB_REGION header not defined.  Processing without SubRegion support'
-        subRegions = False
-        
 
 
     # Go get the parameter listing from the database
@@ -246,9 +205,27 @@ def runUsingConfig(argsConfig):
     numOBS, numMDL, nT, ngrdY, ngrdX, Times, lons, lats, obsData, mdlData, obsList, mdlName
= do_data_prep.prep_data(jobProperties, obsDatasetList, gridBox, models)
 
     print 'Input and regridding of both obs and model data are completed. now move to metrics
calculations'
+    
+    try:
+        subRegionConfig = misc.configToDict(userConfig.items('SUB_REGION'))
+        subRegions = misc.parseSubRegions(subRegionConfig)
+        # REORDER SUBREGION OBJECTS until we standardize on Python 2.7
+        # TODO Remove once Python 2.7 support is finalized
+        if subRegions:
+            subRegions.sort(key=lambda x:x.name)
+        
+    except ConfigParser.NoSectionError:
+        
+        counts = {'observations': numOBS,
+                  'models'      : numMDL,
+                  'times'       : nT}
+        subRegions = misc.getSubRegionsInteractively(counts, workdir)
+        
+        if len(subRegions) == 0:
+            print 'Processing without SubRegion support'
+        
 
     # TODO: New function Call
-    workdir = jobProperties.workDir
     fileOutputOption = jobProperties.writeOutFile
     modelVarName = models[0].varName
     metrics.metrics_plots(modelVarName, numOBS, numMDL, nT, ngrdY, ngrdX, Times, lons, lats,
obsData, mdlData, obsList, mdlName, workdir, subRegions, fileOutputOption)
@@ -262,7 +239,6 @@ if __name__ == "__main__":
 
     else:
         print 'Interactive mode has been enabled'
-        #getSettings(SETTINGS)
-        print "But isn't implemented.  Try using the -c option instead"
+        ui.rcmetUI()
 
     #rcmet_cordexAF()

Modified: incubator/climate/trunk/rcmet/src/main/python/rcmes/classes.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/rcmet/src/main/python/rcmes/classes.py?rev=1482342&r1=1482341&r2=1482342&view=diff
==============================================================================
--- incubator/climate/trunk/rcmet/src/main/python/rcmes/classes.py (original)
+++ incubator/climate/trunk/rcmet/src/main/python/rcmes/classes.py Tue May 14 14:18:22 2013
@@ -5,6 +5,7 @@ import urllib
 
 import storage.files as files
 import toolkit.process as process
+import utils.misc
 
 class BoundingBox(object):
     
@@ -69,24 +70,49 @@ class JobProperties(object):
     
 class Model(object):
     
-    def __init__(self, filename, latVariable, lonVariable, timeVariable, timeStep, varName,
precipFlag):
-        self.filename = filename
-        self.name = os.path.basename(self.filename)
-        self.latVariable = latVariable
-        self.lonVariable = lonVariable
-        self.timeVariable = timeVariable
-        self.timeStep = timeStep
-        self.varName = varName
-        self.times, _ = process.getModelTimes(self.filename, self.timeVariable)
-        self.minTime = min(self.times)
-        self.maxTime = max(self.times)
-        self.setLatitudeRange()
-        self.setLongitudeRange()
-    
-        if precipFlag == 'True':
-            self.precipFlag = True
-        else:
-            self.precipFlag = False
+    def __init__(self, *args, **kwargs):
+        """
+        This Class can be instantiated with either a single filename or a dictionary of parameters.
+        If a single filename is used, then the class will use internal methods to parse the
file 
+        and set the needed attributes.
+        
+        Input (single file)::
+            filename - Full path to a local model file
+        
+        Input (keyword dictionary)::
+            filename - Full path to a local model file
+            latVariable - the name of the latitude variable within the file
+            lonVariable - the name of the longitude variable within the file
+            timeVariable - the name of the time variable within the file
+            timeStep - description of the time between readings ['hourly','daily','monthly','annual']
+            varName - name of the variable to analyze within the model
+            precipFlag - boolean telling if this is precipitation data
+        
+        Output::
+            Model object
+        """
+        if len(args) == 1:
+            self.filename = args[0]
+            self.name = os.path.basename(self.filename)
+            self.processModelFile()
+        if len(kwargs) == 7:
+            self.filename = kwargs['filename']
+            self.name = os.path.basename(self.filename)
+            self.latVariable = kwargs['latVariable']
+            self.lonVariable = kwargs['lonVariable']
+            self.timeVariable = kwargs['timeVariable']
+            self.timeStep = kwargs['timeStep']
+            self.varName = kwargs['varName']
+            self.times, _ = process.getModelTimes(self.filename, self.timeVariable)
+            self.minTime = min(self.times)
+            self.maxTime = max(self.times)
+            self.setLatitudeRange()
+            self.setLongitudeRange()
+    
+            if kwargs['precipFlag'] == 'True':
+                self.precipFlag = True
+            else:
+                self.precipFlag = False
         
     
     def setLatitudeRange(self):
@@ -94,6 +120,17 @@ class Model(object):
     
     def setLongitudeRange(self):
         self.lonMin, self.lonMax = files.getVariableRange(self.filename, self.lonVariable)
+    
+    def processModelFile(self):
+        """ This series of steps should be consolidated to merely pass around a PyNIO object
+            Until then we will be opening the same file repeatedly.  And clearly wasting
I/O
+        """
+        self.latVariable, self.lonVariable, self.latMin, self.latMax, self.lonMin, self.lonMax
= files.findLatLonVarFromFile(self.filename)
+        self.timeVariable, modelVariables = files.findTimeVariable(self.filename)
+        self.times, self.timeStep = process.getModelTimes(self.filename, self.timeVariable)
+        self.varName = utils.misc.askUserForVariableName(modelVariables, "analysis")
+        self.minTime = min(self.times)
+        self.maxTime = max(self.times)
         
 class Parameter(object):
     def __init__(self, param_id, shortName=None, description=None, endDate=None ):

Modified: incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/do_rcmes_processing_sub.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/do_rcmes_processing_sub.py?rev=1482342&r1=1482341&r2=1482342&view=diff
==============================================================================
--- incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/do_rcmes_processing_sub.py (original)
+++ incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/do_rcmes_processing_sub.py Tue
May 14 14:18:22 2013
@@ -726,5 +726,3 @@ def createTimeTitle( options, timeSlice,
     return timeTitle
 
 
-print 'RCMES processing completed.'
-

Modified: incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/rcmet_ui.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/rcmet_ui.py?rev=1482342&r1=1482341&r2=1482342&view=diff
==============================================================================
--- incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/rcmet_ui.py (original)
+++ incubator/climate/trunk/rcmet/src/main/python/rcmes/cli/rcmet_ui.py Tue May 14 14:18:22
2013
@@ -5,20 +5,17 @@
 """
 # Imports
 # Native Python Module Imports
-import glob
 import datetime
 import time
 import sys
-import os
-
-# Appending rcmes via relative path
-sys.path.append(os.path.abspath('../.'))
 
 # RCMES Imports
-import storage.files as files
-import storage.rcmed as db
-import toolkit.process as process
 import cli.do_rcmes_processing_sub as doProcess
+#import storage.files as files
+import storage.rcmed as rcmed
+import toolkit.do_data_prep
+from utils import misc
+from classes import Model, JobProperties
 
 
 # Empty dictionaries to collect all of the user's inputs 
@@ -34,171 +31,50 @@ def rcmetUI():
     Collects user OPTIONS then runs RCMET to perform processing.
     
     Duplicates job of GUI.
-    
-    Peter Lean   March 2011
-    
     """
     print 'Regional Climate Model Evaluation System BETA'
     print "Querying RCMED for available parameters..."
 
     try:
-        parameters = db.getParams()
+        parameters = rcmed.getParams()
     except Exception:
+        raise
         sys.exit()
 
     # Section 0: Collect directories to store RCMET working files.
-    SETTINGS['workDir'] = raw_input('Please enter workdir:\n> ')   # This is where the
images are created/stored
-    SETTINGS['cacheDir'] = raw_input('Please enter cachedir:\n> ') # This is where the
database cache files are stored
-    
+    misc.getDirSettings(SETTINGS)
+    # collect temporal step and spatial settings
+    misc.getTemporalGrid(SETTINGS)
+    SETTINGS['spatialGrid'] = misc.getSpatialGrid()
     # Section 1a: Enter model file/s
-    inputFileList = raw_input('Please enter model file (or specify multiple files using wildcard):\n>
')
-    SETTINGS['fileList'] = glob.glob(inputFileList)
-    
-    # Section 1b (i): 
-    #     Attempt to auto-detect latitude and longitude variable names
-    #     and lat, lon limits from first file in SETTINGS['fileList'] 
-    autoDetectLatLon, MODEL['latVariable'], MODEL['lonVariable'], PARAMS['latMin'], PARAMS['latMax'],
PARAMS['lonMin'], PARAMS['lonMax'], variableNameList = files.find_latlon_var_from_file(SETTINGS['fileList'])
-    
-    # Section 1b (ii)
-    #     If unable to auto-detect latitude and longitude variables from file,
-    #     then ask user to select from which variable corresponds to latitude and
-    #     which corresponds to longitude (from a list of available variables in the file).
 
-    if autoDetectLatLon == 0:
-        print 'Could not find latitude and longitude data in the file. \n'
-        
-        printVariableList(variableNameList)
-        userVarChoice = raw_input('Please help, by selecting which of the above variables
in the file is the latitude variable:\n (or if the latitudes and longitudes are stored in
a separate file enter "z")\n> ').lower()
-
-        if userVarChoice == 'z':
-            latLonFilename = raw_input('Please enter the full path of the file containing
the latitudes and longitudes:\n> ')
-            latLonFileList = glob.glob(latLonFilename)
-            autoDetectLatLon, MODEL['latVariable'], MODEL['lonVariable'], PARAMS['latMin'],
PARAMS['latMax'], PARAMS['lonMin'], PARAMS['lonMax'], variableNameList = files.find_latlon_var_from_file(latLonFileList)
-
-        # Section 1b (iii)
-        if userVarChoice != 'z':
-            try:
-                MODEL['latVariable'] = variableNameList[int(userVarChoice)]
-            except (IndexError, TypeError):
-                MODEL['latVariable'] = variableNameList[int(raw_input('Are you sure? The
previous selection did not work, please try again:\n> '))]
+    modelFiles = misc.getModelFiles()
 
-            try:
-                MODEL['lonVariable'] = variableNameList[int(raw_input('..and which of the
above variables is the longitude variable?\n> '))]
-            except (IndexError, TypeError):
-                MODEL['lonVariable'] = variableNameList[int(raw_input('Are you sure? The
previous selection did not work, please try again:\n> '))]
-
-        # Find lat/lon ranges by loading the data using the user supplied variable names
-        PARAMS['latMin'], PARAMS['latMax'], PARAMS['lonMin'], PARAMS['lonMax'] = files.find_latlon_ranges(SETTINGS['fileList'],

-                                                                                        
                 MODEL['latVariable'], 
-                                                                                        
                 MODEL['lonVariable']) 
-
-
-    print 'Found latitude and longitude variables in model data files: '
-    print 'Lat/Lon variable names : ', MODEL['latVariable'], MODEL['lonVariable']
-    print 'Minimum Latitude: ', PARAMS['latMin']
-    print 'Maximum Latitude: ', PARAMS['latMax']
-    print 'Minimum Longitude: ', PARAMS['lonMin']
-    print 'Maximum Longitude: ', PARAMS['lonMax']
-    
-    # Section 1c (i) Attempt to auto-detect the time variable in the file.
-    #     NB. name of time variable needs to passed into RCMET.
-    autoDetectTime, MODEL['timeVariable'], variableNameList = files.find_time_var_name_from_file(SETTINGS['fileList'])
-
-    print 'Found time variable name :', MODEL['timeVariable']
-    if autoDetectTime == 0:
-        print 'Could not find time data in the file.'
-        printVariableList(variableNameList)
-
-        try:
-            MODEL['timeVariable'] = variableNameList[int(raw_input('Please help, by selecting
which of the above variables in the file is the time variable:\n> '))]
-        except (IndexError, TypeError):
-            MODEL['timeVariable'] = variableNameList[int(raw_input('There was a problem with
your selection, please try again:\n> '))]
-
-    # Section 1c (ii): Attempt to decode model times into a python datetime object.
-    try:
-        modelTimes = process.decode_model_times(SETTINGS['fileList'], MODEL['timeVariable'])
-        modelStartTime = min(modelTimes)
-        modelEndTime = max(modelTimes)
-
-        print 'Model times decoded:'
-        print 'First model time : ', modelStartTime.strftime("%Y/%m/%d %H:%M")
-        print 'Last model time : ',  modelEndTime.strftime("%Y/%m/%d %H:%M")
-    except:
-        print 'Error: there was a problem decoding the model times.'
+    # Create a list of model objects for use later
+    models = [Model(modelFile) for modelFile in modelFiles]
 
-  
-    # Section 2a: Select which model variable to use for evaluation (from list of variables
in file)
-    printVariableList(variableNameList)
-
-    userVarChoice = raw_input('Which variable would you like to evaluate?\n> ')
-    try:
-        MODEL['varName'] = variableNameList[int(userVarChoice)]
-    except:
-        userVarChoice = raw_input('There was a problem with that selection, please try again:\n>
')    
-        MODEL['varName'] = variableNameList[int(userVarChoice)]
-
-  
-    # Ask user if the above variable is precipitation data 
-    # (as this needs some special treatment by RCMET, e.g. color tables, unit conversion
etc)
-
-    OPTIONS['precip'] = False
-    precipChoice = raw_input('Is this precipitation data? [y/n]\n> ').lower()
-    if precipChoice == 'y':
-        OPTIONS['precip'] = True
-
-  
-    # Section 3a: Select observation dataset from database
-  
-    # dbDatasets = ['TRMM','ERA-Interim','AIRS','MODIS','URD','CRU']
-    # replace with list comprehension
-    # dbDatasets = [parameter['longname'] for parameter in parameters]
-  
-  
-    # datasetIds = [3,1,2,5,4,6]
-    # dbDatasetStartTimes = [datetime.datetime(1998,1,1,0,0,0,0),datetime.datetime(1989,01,01,0,0,0,0),datetime.datetime(2002,8,31,0,0,0,0),datetime.datetime(2000,2,24,0,0,0,0),datetime.datetime(1948,1,1,0,0,0,0),datetime.datetime(1901,1,1,0,0,0,0)]
-    # dbDatasetEndTimes = [datetime.datetime(2010,1,1,0,0,0,0),datetime.datetime(2009,12,31,0,0,0,0),datetime.datetime(2010,1,1,0,0,0,0),datetime.datetime(2010,5,30,0,0,0,0),datetime.datetime(2010,1,1,0,0,0,0),datetime.datetime(2006,12,1,0,0,0,0)]
-
-    dbParameters = [['daily precip', 'monthly precip'], ['2m temp', '2m dew point'], ['2m
temp'], ['cloud fraction'], ['precip'], ['tavg', 'tmax', 'tmin', 'precip']]
+    # Section 3b: Select 1 Parameter from list
+    for parameter in parameters:
+        """( 38 ) - CRU3.1 Daily-Mean Temperature : monthly"""
+        print "({:^2}) - {:<54} :: {:<10}".format(parameter['parameter_id'], parameter['longname'],
parameter['timestep'])
 
-    # dbParameterIds = [[14,36],[12,13],[15],[31],[30],[33,34,35,32]]
-    # dbParameterIds = [parameter['parameterId'] for parameter in parameters]
+    obsDatasetList = []
+    validParamIds = [int(p['parameter_id']) for p in parameters]
+    while obsDatasetList == []:
+        print("Please select the available observation you would like to use from the list
above:")
+        userChoice = int(raw_input(">>>"))
+        if userChoice in validParamIds:
+            for param in parameters:
+                if param['parameter_id'] == userChoice:
+                    obsDatasetList.append(param)
+                else:
+                    pass
+        else:
+            print("Your selection '%s' is invalid.  Please make another selection." % userChoice)
 
-    # Not sure this is needed anymore since we have a parameters list object to work with
-    # datasetIds = [parameter['datasetId'] for parameter in parameters]
-  
-    try:
-        dbDatasetStartTimes = [datetime.datetime.strptime(parameter['start_date'], '%Y-%m-%d')
for parameter in parameters]
-    except:
-        print "Error parsing the start dates from RCMED. Expected Format 'YYYY-MM-DD'"
-    
-    try:
-        dbDatasetEndTimes = [datetime.datetime.strptime(parameter['end_date'], '%Y-%m-%d')
for parameter in parameters]
-    except:
-        print "Error parsing the end dates from RCMED. Expected Format 'YYYY-MM-DD'" 
-    
-    dbParameters = [parameter['dataset_id'] for parameter in parameters]
 
-  
-    # Section 3b: Select Dataset from list
+"""
 
-    for parameter in parameters:
-        """( 38 ) - CRU3.1 Daily-Mean Temperature : monthly"""
-        print "({:^2}) - {:<35} :: {:<10}".format(parameter['parameter_id'], parameter['longname'],
parameter['timestep'])
 
-    try:
-        datasetChoice = int(raw_input('Please select which observational dataset you wish
to compare against:\n> ')) 
-        selection = next((p for p in parameters if p['parameter_id'] == datasetChoice), None)
-        PARAMS['obsDatasetId'] = selection['dataset_id']
-        obsStartTime = datetime.datetime.strptime(selection['start_date'], "%Y-%m-%d")
-        obsEndTime = datetime.datetime.strptime(selection['end_date'], "%Y-%m-%d")
-        PARAMS['obsParamId'] = selection['parameter_id']
-    
-    except:
-        datasetChoice = raw_input('There was a problem with your selection, please try again:\n>
') 
-        selection = next((p for p in parameters if p['parameter_id'] == datasetChoice), None)
-        PARAMS['obsDatasetId'] = selection['dataset_id']
-        obsStartTime = datetime.datetime.strptime(selection['start_date'], "%Y-%m-%d")
-        obsEndTime = datetime.datetime.strptime(selection['end_date'], "%Y-%m-%d")
-        PARAMS['obsParamId'] = selection['parameter_id']
 
     # Section 4: Select time range to evaluate (defaults to overlapping times between model
and obs)
     
@@ -363,50 +239,66 @@ def rcmetUI():
     
     doProcess.do_rcmes( SETTINGS, PARAMS, MODEL, MASK, OPTIONS )
 
-def printVariableList(variableNames):
-    """Private function that will print a list of selections using a zero based
-    counter.  Typically used to gather user selections"""
-    i = 0
-    for variable in variableNames:
-        print '[', i, ']', variable
-        i += 1
-
-def getMetricFromUserInput():
-    """Collection of different Metrics that a user can run against datasets
-    they have previously selected.  
-    
-    TODO: Refactor this into the metrics module so the list is not maintained
-    here.
-    """
-    print 'Metric OPTIONS'
-    print '[0] Bias: mean bias across full time range'
-    print '[1] Mean Absolute Error: across full time range'
-    print '[2] Difference: calculated at each time unit'
-    print '[3] Pattern Correlation Timeseries> '
-    print '[4] Probability Distribution Function similarity score'
-    print '[5] RMS error'
-    print '[6] Coefficient of Efficiency'
-    print '[7] Standard deviation'  
-    print '[8] new Anomaly Correlation'  
-    choice = int(raw_input('Please make a selection from the OPTIONS above\n> '))
-    if choice == 0:
-         return 'bias'
-    if choice == 1:
-        return 'mae'
-    if choice == 2:
-        return 'difference'
-    if choice == 3:
-        return 'patcor'
-    if choice == 4:
-        return 'pdf'
-    if choice == 5:
-        return 'rms'
-    if choice == 6:
-        return 'coe'
-    if choice == 7:
-        return 'stddev'
-    if choice == 8:
-        return 'nacc'
+"""
+
+#def printVariableList(variableNames):
+#    """Private function that will print a list of selections using a zero based
+#    counter.  Typically used to gather user selections"""
+#    i = 0
+#    for variable in variableNames:
+#        print '[', i, ']', variable
+#        i += 1
+
+#    """ BLOCK TAKEN FROM THE NON-GUI rcmet.py """
+        # Go get the parameter listing from the database
+#    try:
+#        params = rcmed.getParams()
+#    except:
+#        raise
+
+#    obsDatasetList = []
+#    for param_id in datasetDict['obsParamId']:
+#        for param in params:
+#            if param['parameter_id'] == int(param_id):
+#                obsDatasetList.append(param)
+#            else:
+#                pass
+
+    #TODO: Unhardcode this when we decided where this belongs in the Config File
+#    jobProperties.maskOption = True
+#
+#    numOBS, numMDL, nT, ngrdY, ngrdX, Times, lons, lats, obsData, mdlData, obsList, mdlName
= toolkit.do_data_prep.prep_data(jobProperties, obsDatasetList, gridBox, models)
+#
+#    print 'Input and regridding of both obs and model data are completed. now move to metrics
calculations'
+#    
+#    try:
+#        subRegionConfig = misc.configToDict(userConfig.items('SUB_REGION'))
+#        subRegions = misc.parseSubRegions(subRegionConfig)
+#        # REORDER SUBREGION OBJECTS until we standardize on Python 2.7
+#        # TODO Remove once Python 2.7 support is finalized
+#        if subRegions:
+#            subRegions.sort(key=lambda x:x.name)
+#        
+#    except ConfigParser.NoSectionError:
+#        
+#        counts = {'observations': numOBS,
+#                  'models'      : numMDL,
+#                  'times'       : nT}
+#        subRegions = misc.getSubRegionsInteractively(counts, workdir)
+#        
+#        if len(subRegions) == 0:
+#            print 'Processing without SubRegion support'
+#        
+#
+#    # TODO: New function Call
+#    fileOutputOption = jobProperties.writeOutFile
+#    modelVarName = models[0].varName
+#    metrics.metrics_plots(modelVarName, numOBS, numMDL, nT, ngrdY, ngrdX, Times, lons, lats,
obsData, mdlData, obsList, mdlName, workdir, subRegions, fileOutputOption)
+#
+""" END OF BLOCK """
+
+
+
 
 
 # Actually call the UI function.

Modified: incubator/climate/trunk/rcmet/src/main/python/rcmes/utils/misc.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/rcmet/src/main/python/rcmes/utils/misc.py?rev=1482342&r1=1482341&r2=1482342&view=diff
==============================================================================
--- incubator/climate/trunk/rcmet/src/main/python/rcmes/utils/misc.py (original)
+++ incubator/climate/trunk/rcmet/src/main/python/rcmes/utils/misc.py Tue May 14 14:18:22
2013
@@ -53,6 +53,80 @@ def readSubRegionsFile(regionsFile):
     else:
         raise IOError
 
+def getSubRegionsInteractively(counts, workdir):
+    """
+    Purpose::
+        This function provides a commandline Q&A session to help users define 
+        SubRegion Objects.
+    Input::
+        counts - dictionary with int counts of various metric inputs
+            i.e. {'observations': 3,
+                  'models'      : 1,
+                  'times'       : 120}
+        workdir - string of the current working directory where auxillary files
+        can be found.  In this case the directory will be used to locate an 
+        existing sub_regions.txt file.
+
+    Output:: 
+        subRegions = List of Parsed SubRegion Objects based on User inputs 
+    """
+    do_timeseries = None
+    yes_no_list = ['y', 'n', '']
+    
+    while do_timeseries not in yes_no_list:
+        do_timeseries = raw_input('Calculate area-mean timeseries for subregions? y/n: [n]
\n>>>')
+        if do_timeseries not in yes_no_list:
+            print("'%s' is not a valid answer please try again" % do_timeseries)
+
+    if do_timeseries == 'y':
+        interactive_subregions = None
+        while interactive_subregions not in yes_no_list:
+            interactive_subregions = raw_input('Input Sub Region info interactively? y/n:
[n] \n>>>')
+            if interactive_subregions not in yes_no_list:
+                print("'%s' is not a valid answer please try again" % interactive_subregions)
+            
+        if interactive_subregions == 'y':
+            while interactive_subregions == 'y':
+                regions = []
+                region = createSubRegionObjectInteractively()
+                regions.append(region)
+                anotherRegion = None
+                while anotherRegion == None:
+                    another = raw_input("Would you like to add another sub region? y/n [n]
\n>>>")
+                    if another not in yes_no_list:
+                        print("'%s' is not a valid answer please try again" % another)
+                    elif another in ['', 'n']:
+                        anotherRegion = 'n'
+                        interactive_subregions = 'n'
+                    else:
+                        anotherRegion = 'y'
+
+        else:
+            subRegionFilename = None
+            while subRegionFilename == None:
+                readDefault = raw_input('Read from a default file (workdir + "/sub_regions.txt")?
y/n: [y] \n>>>')
+
+                if readDefault == 'y' or readDefault == '':
+                    subRegionFilename = workdir + "/sub_regions.txt"
+                    print("Attempting to parse %s..." % subRegionFilename)
+                    regions = readSubRegionsFile(subRegionFilename)
+
+                elif readDefault == 'n':
+                    while subRegionFilename == None:
+                        # ask about using a non default filename
+                        subRegionFilename = raw_input('Enter the full path to the Sub Region
file to read from:\n>>>')
+                        print("Attempting to parse %s..." % subRegionFilename)
+                        regions = readSubRegionsFile(subRegionFilename)
+
+                elif readDefault == 'NONE':
+                    subRegionFilename = 'NONE'
+                    regions = []
+            
+                else:
+                    print("'%'s is not a valid selection.  Please try again.  To proceed
without Sub Regions defined enter NONE at the prompt" % readDefault)
+
+        return regions
+
 def generateSubRegions(regions):
     """ Takes in a list of ConfigParser tuples and returns a list of SubRegion objects
     
@@ -921,28 +995,37 @@ def assign_subRgns_from_a_text_file(infi
     print 'subRgnLat1: ', subRgnLat1
     return numSubRgn, subRgnName, subRgnLon0, subRgnLon1, subRgnLat0, subRgnLat1
 
-def assign_subRgns_interactively():
+def createSubRegionObjectInteractively():
     """
-     Interacrively select subregions for which area-mean time series are to be evaluated
-     input : GUI, a logical variable used to decide input option
-                  (interactive screen IO vs. pre-determined processing steps)
+    Purpose::
+        Mini command line program to enable users to enter SubRegion Information
+    Input::
+        None
+    Output::
+        SubRegion Object
     """
-    numSubRgn = int(raw_input('Enter the number of sub-Regions for examining the area-mean
timeseries: \n> '))
-    subRgnName = []
-    subRgnLon0 = ma.zeros(numSubRgn)
-    subRgnLon1 = ma.zeros(numSubRgn)
-    subRgnLat0 = ma.zeros(numSubRgn)
-    subRgnLat1 = ma.zeros(numSubRgn)
-    if numSubRgn > 0:
-        for n in np.arange(numSubRgn):
-            print 'For the sub-region: ', n + 1
-            name = raw_input('Enter the sub-Region name: \n> ')
-            subRgnName.append(name)
-            subRgnLon0[n] = raw_input('Enter the beginning longitude of sub-Region: \n>
')
-            subRgnLon1[n] = raw_input('Enter the ending longitude of sub-Region: \n> ')
-            subRgnLat0[n] = raw_input('Enter the beginning latitude of sub-Region: \n>
')
-            subRgnLat1[n] = raw_input('Enter the ending latitude of sub-Region: \n> ')
-    return numSubRgn, subRgnName, subRgnLon0, subRgnLon1, subRgnLat0, subRgnLat1
+    rawInput = None
+    while rawInput == None:
+        userMessage = ("Enter information for 1 Sub Region using the following "
+                       "pipe '|' separated format: \n"
+                       "RegionName | Degrees North | Degrees South | Degrees East | Degrees
West \n>>>")
+        userInput = raw_input(userMessage)
+        inputList = userInput.split('|')
+        if len(inputList) != 5:
+            badLengthMessage = ("Unable to parse %s.  You should have 5 inputs "
+                                "separated by 4 pipe characters. \n"
+                                "Example:  Region Name | 85 | 80 | 10 | -30" % userInput)
+            print(badLengthMessage)
+        else:
+            name = str(inputList[0]).strip()
+            latMax = str(inputList[1]).strip()
+            latMin = str(inputList[2]).strip()
+            lonMax = str(inputList[3]).strip()
+            lonMin = str(inputList[4]).strip()
+            subRegion = classes.SubRegion(name, latMin, lonMin, latMax, lonMax)
+            rawInput = True
+
+    return subRegion
 
 def selectSubRegion(subRegions):
     # interactively select the sub-region ID for area-mean time-series evaluation
@@ -1004,7 +1087,106 @@ def getStartEndTimes(status, startOverLa
             return time
         else:
             return time
+
+def getDirSettings(settings):
+    """
+    This function will collect 2 parameters from the user about the RCMET run they have started.
+    
+    Input::
+        settings - Empty Python Dictionary they will be used to store the user supplied inputs
+        
+    Output::
+        None - The user inputs will be added to the supplied dictionary.
+    """
+    settings['workDir'] = os.path.abspath(raw_input('Please enter workDir:\n> '))
+    if os.path.isdir(settings['workDir']):
+        pass
+    else:
+        makeDirectory(settings['workDir'])
     
+    settings['cacheDir'] = os.path.abspath(raw_input('Please enter cacheDir:\n> '))
+    if os.path.isdir(settings['cacheDir']):
+        pass
+    else:
+        makeDirectory(settings['cacheDir'])
+
+def getModelFiles():
+    modelList = []
+    while len(modelList) < 1:
+        userInput = raw_input('Please enter model file (or specify multiple files using wildcard):\n>
')
+        modelList = glob.glob(userInput)
+        if len(modelList) == 0:
+            print("Sorry we were unable to find any files at - %s" % userInput)
+            print("Please try again, and use the asterisk * for the wildcard")
+        
+    return modelList
+
+def getTemporalGrid(settings):
+    options = ['annual', 'monthly', 'daily']
+    print("Please select one of the following Temporal Grid Options:\n")
+    for key, option in enumerate(options):
+        print("[%s] - %s" % (key, option))
+    choice = int(raw_input(">>>"))
+    try:
+        temporalGrid = options[choice]
+    except IndexError:
+        getTemporalGrid(settings)
+    else:
+        settings['temporalGrid'] = temporalGrid
+
+def getSpatialGrid():
+    options = ['obs', 'model', 'user']
+    print("Please select one of the following Spatial Grid Options:\n")
+    for key, option in enumerate(options):
+        print("[%s] - %s" % (key, option))
+
+    choice = int(raw_input(">>>"))
+    try:
+        spatialGrid = options[choice]
+    except IndexError:
+        getSpatialGrid()
+
+    return spatialGrid
+
+def askUserForVariableName(variableNames, targetName):
+    if targetName == "analysis":
+        print("Select the variable you want to use for analysis/metrics:")
+    else:
+        print("Select the variable that corresponds to %s:" % targetName)
+
+    for idx, variable in enumerate(variableNames):
+        print("[%s] - %s" % (idx, variable))
+    userChoice = int(raw_input(">>>"))
+    try:
+        variableName = variableNames[userChoice]
+    except IndexError:
+        askUserForVariableName(variableNames, targetName)
+    
+    return variableName
+        
+        
+
+def getLatLonStep(settings):
+    pass
+
+def getSpatialBounds(settings):
+    pass
+
+
+def getUserSpatialSettings(settings):
+    getLatLonStep(settings)
+    getSpatialBounds(settings)
+
+def makeDirectory(directory):
+    print "%s doesn't exist.  Trying to create it now." % directory
+    try:
+        os.mkdir(directory)
+        print("Created %s successfully" % directory)
+    except OSError:
+        print "This program cannot create dir: %s due to permission issues." % directory
+        sys.exit()
+
+
 def userDefinedStartEndTimes(obsList, modelList):
     """
     The function will interactively ask the user to select a start and end time based on
the start/end times
@@ -1112,12 +1294,11 @@ def select_metrics():
     print 'Metric options'
     print '[0] Bias: mean bias across full time range'
     print '[1] Mean Absolute Error: across full time range'
-    print '[2] Anomaly Correlation> in time: results in 2-d array of temporal anom correln'
-    print '[3] Anomaly Correlation> in space: results in a single correlation coeff'
-    print '[4] Pattern Correlation> in time: results in 2-d array of temporal anom correln'
-    print '[5] Pattern Correlation> in space: results in a single correlation coeff'
-    print '[6] RMSE in time: results in a 2-d array of RMSE over two time series'
-    print '[7] TODO: Probability Distribution Function similarity score'
+    print '[2] Spatial Pattern Correlation: results in a time series of spatial correlation
coeff'
+    print '[3] Temporal Correlation: results in 2-d array of temporal correlation coefficient'
+    print '[4] Spatial Pattern Correlation between averaged fields: results in a single correlation
coeff'
+    print '[5] RMSE in time: results in a 2-d array of RMSE over two time series'
+    print '[6] TODO: Probability Distribution Function similarity score'
     choice = int(raw_input('Please make a selection from the options above\n> '))
     # assign the evaluation metric to be calculated
     if choice == 0:
@@ -1125,16 +1306,14 @@ def select_metrics():
     elif choice == 1:
         metricOption = 'MAE'
     elif choice == 2:
-        metricOption = 'ACCt'
+        metricOption = 'PCt'
     elif choice == 3:
-        metricOption = 'ACCs'
+        metricOption = 'TC'
     elif choice == 4:
-        metricOption = 'PCCt'
+        metricOption = 'PCC'
     elif choice == 5:
-        metricOption = 'PCCs'
-    elif choice == 6:
         metricOption = 'RMSt'
-    elif choice == 7:
+    elif choice == 6:
         metricOption = 'pdfSkillScore'
     print 'in subroutine metricOption = ', metricOption
     return metricOption



Mime
View raw message