Exemplo n.º 1
0
def _validate_bioclim_inputs(outputfname, verbose, **kwargs):
    '''Checks that the arguments submitted to submitCustomBioclim are 
    reasonable
    '''
    # first check that the URI is a valid url
    # May want to have this try a few times to be safe, 
    # some datasets take a long time and can time out once or twice
    try:
        urllib2.urlopen(kwargs["OPeNDAP_URI".replace('dods','http')] + ".html")
    except:
        raise Exception("Invalid or unresponsive OPeNDAP_URI provided:  " + kwargs["OPeNDAP_URI"])
    
    #Check that the passed variable names exist
    datatypes = webdata_handle.getDataType(kwargs["OPeNDAP_URI"], verbose=verbose)
    for var in ['prcp_var', 'tmax_var', 'tmin_var']: #Note tave_var is not required, not checked here.
        if not kwargs[var] is None:
            if kwargs[var] not in datatypes:
                msg = "Specified '" + var + "' (" + kwargs[var] 
                msg += ") not a datatype in specified OPeNDAP_URI ("
                msg += kwargs["OPeNDAP_URI"] + ")"
                msg += "\n existing variables:"
                msg += "\t" + ", ".join(datatypes[:(min(10, len(datatypes)))])
                raise Exception(msg)        
    
    #Check that the start and end times will work
    timerange = webdata_handle.getTimeRange(kwargs["OPeNDAP_URI"], 
                                  varID=kwargs["prcp_var"], verbose=verbose)
    uri_start = dateutil.parser.parse(timerange[0]).replace(tzinfo=None)
    uri_end = dateutil.parser.parse(timerange[1]).replace(tzinfo=None)
    arg_start = datetime.datetime(int(kwargs['start']), 1, 1)
    arg_end = datetime.datetime(int(kwargs['end']), 1, 1)
    if not uri_start <= arg_start <= uri_end:
        msg = "Specified start " + str(kwargs['start']) + " not between the "
        msg += "start and end dates of the OPeNDAP_URI dataset\n\t"
        msg += str(uri_start) + " !<= " + str(arg_start) + " !<= " + str(uri_end)
        raise Exception(msg) 
    if not uri_start <= arg_end <= uri_end:
        msg = "Specified end " + str(kwargs['end']) + " not between the "
        msg += "start and end dates of the OPeNDAP_URI dataset\n\t"
        msg += str(uri_start) + " !<= " + str(arg_end) + " !<= " + str(uri_end)
        raise Exception(msg) 
    
    #check that the folder of the output dataset exists and the output zip does not
    if not os.path.exists(os.path.split(outputfname)[0]):
        raise Exception("Folder of output file does not exist:\n " 
                    + os.path.split(outputfname)[0])
    if os.path.exists(outputfname):
        try:
            os.remove(outputfname)
        except:
            raise Exception("Could not delete existing output file:\n " 
                    + os.path.split(outputfname))
    
    #check that the bioclims are legit
    for bioclim in kwargs['bioclims']:
        if (not str(bioclim).isdigit() or bioclim < 1 or bioclim > 19):
            raise Exception ("The bioclim specified (" + str(bioclim) + ") is not between 1 and 19 inclusive" )
Exemplo n.º 2
0
def _validate_bioclim_inputs(outputfname, verbose, **kwargs):
    '''Checks that the arguments submitted to submitCustomBioclim are 
    reasonable
    '''
    # first check that the URI is a valid url
    # May want to have this try a few times to be safe, 
    # some datasets take a long time and can time out once or twice
    try:
        urllib2.urlopen(kwargs["OPeNDAP_URI".replace('dods','http')] + ".html")
    except:
        raise Exception("Invalid or unresponsive OPeNDAP_URI provided:  " + kwargs["OPeNDAP_URI"])
    
    #Check that the passed variable names exist
    datatypes = webdata_handle.getDataType(kwargs["OPeNDAP_URI"], verbose=verbose)
    for var in ['prcp_var', 'tmax_var', 'tmin_var']: #Note tave_var is not required, not checked here.
        if not kwargs[var] is None:
            if kwargs[var] not in datatypes:
                msg = "Specified '" + var + "' (" + kwargs[var] 
                msg += ") not a datatype in specified OPeNDAP_URI ("
                msg += kwargs["OPeNDAP_URI"] + ")"
                msg += "\n existing variables:"
                msg += "\t" + ", ".join(datatypes[:(min(10, len(datatypes)))])
                raise Exception(msg)        
    
    #Check that the start and end times will work
    timerange = webdata_handle.getTimeRange(kwargs["OPeNDAP_URI"], 
                                  varID=kwargs["prcp_var"], verbose=verbose)
    uri_start = dateutil.parser.parse(timerange[0]).replace(tzinfo=None)
    uri_end = dateutil.parser.parse(timerange[1]).replace(tzinfo=None)
    arg_start = datetime.datetime(int(kwargs['start']), 1, 1)
    arg_end = datetime.datetime(int(kwargs['end']), 1, 1)
    if not uri_start <= arg_start <= uri_end:
        msg = "Specified start " + str(kwargs['start']) + " not between the "
        msg += "start and end dates of the OPeNDAP_URI dataset\n\t"
        msg += str(uri_start) + " !<= " + str(arg_start) + " !<= " + str(uri_end)
        raise Exception(msg) 
    if not uri_start <= arg_end <= uri_end:
        msg = "Specified end " + str(kwargs['end']) + " not between the "
        msg += "start and end dates of the OPeNDAP_URI dataset\n\t"
        msg += str(uri_start) + " !<= " + str(arg_end) + " !<= " + str(uri_end)
        raise Exception(msg) 
    
    #check that the folder of the output dataset exists and the output zip does not
    if not os.path.exists(os.path.split(outputfname)[0]):
        raise Exception("Folder of output file does not exist:\n " 
                    + os.path.split(outputfname)[0])
    if os.path.exists(outputfname):
        try:
            os.remove(outputfname)
        except:
            raise Exception("Could not delete existing output file:\n " 
                    + os.path.split(outputfname))
    
    #check that the bioclims are legit
    for bioclim in kwargs['bioclims']:
        if (not str(bioclim).isdigit() or bioclim < 1 or bioclim > 19):
            raise Exception ("The bioclim specified (" + str(bioclim) + ") is not between 1 and 19 inclusive" )
Exemplo n.º 3
0
 def getDataType(self, dataSetURI, verbose=False):
     if verbose:
         ch.setLevel(logging.INFO)
     return webdata_handle.getDataType(dataSetURI, verbose)
Exemplo n.º 4
0
 def getDataType(self, dataSetURI, verbose=False):
     if verbose:
         ch.setLevel(logging.INFO)
     return webdata_handle.getDataType(dataSetURI, verbose)