Пример #1
0
def _main(argv):
    "Main part of the program"
    gConfigFile = flConfigFile()
    logFile = flConfigFile(".log")
    verbose = False
    logger = logging.getLogger()

    try:
        opts, args = getopt.getopt(argv, "hc:l:v", ["help", "config=", "logfile=", "verbose"])
    except getopt.GetoptError:
        _ShowSyntax()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            _ShowSyntax()
            sys.exit(2)
        elif opt in ("-c", "--config"):
            gConfigFile = arg
        elif opt in ("-l", "--logfile"):
            logFile = arg
        elif opt in ("-v", "--verbose"):
            verbose = True

    flStartLog(
        cnfGetIniValue(gConfigFile, "Logging", "LogFile", flConfigFile(".log")),
        cnfGetIniValue(gConfigFile, "Logging", "LogLevel", "INFO"),
        cnfGetIniValue(gConfigFile, "Logging", "Verbose", False),
    )

    inputFile = cnfGetIniValue(gConfigFile, "Input", "File")
    logger.info("Processing %s" % inputFile)
    source = cnfGetIniValue(gConfigFile, "Input", "Source")
    delta = cnfGetIniValue(gConfigFile, "Output", "Delta", 0.1)

    nid, newtime, newdates, nLon, nLat, nthetaFm, nvFm, npCentre, npEnv, nrMax = interpolateTrack(
        gConfigFile, inputFile, source, delta
    )
    header = ""
    outputFile = cnfGetIniValue(gConfigFile, "Output", "File")
    logger.info("Saving interpolated data to %s" % (outputFile))
    fh = open(outputFile, "w")
    for i in xrange(len(newtime)):
        fh.write(
            "%d,%5.1f,%s,%6.2f,%6.2f,%6.2f,%6.2f,%7.2f,%7.2f,%5.1f\n"
            % (
                nid[i],
                newtime[i],
                newdates[i].strftime("%Y-%m-%d %H:%M"),
                nLon[i],
                nLat[i],
                nthetaFm[i],
                nvFm[i],
                npCentre[i],
                npEnv[i],
                nrMax[i],
            )
        )
    fh.close()
    logger.info("Completed %s" % (sys.argv[0]))
Пример #2
0
def cnfCacheIniFile(configFile=None):
    """
    A wrapper to the ConfigParser module which caches a dictionary of
    dictionaries. Each dictionary in the parent dictionary is named
    after sections in the configuration file. Keys in the
    sub-directories are named by the options in each section.

    Input: configuration file name (optional, defaults to output from
           flConfigFile())
    Output: configuration dictionary
    Example: config_dict = cnfCacheIniFile(configFile)
    """
    if configFile:
        try:
            FH = open(configFile)
        except IOError:
            logger.info("Cannot open {0}".format(configFile))
            return config_dict
    elif len(sys.argv) > 1:
        try:
            FH = open(sys.argv[1])
        except IOError:
            logger.info("No configuration file given at command line")
    else:
        try:
            FH = open(flConfigFile(level=len(inspect.stack())))
            logger.info("Opening default config file %s" % flConfigFile(level=len(inspect.stack())))
        except IOError:
            logger.info("Cannot open default config file %s" % flConfigFile(level=len(inspect.stack())))
            return config_dict

    cp = ConfigParser.ConfigParser()
    cp.optionxform = str
    cp.readfp(FH)
    for sec in cp.sections():
        name = sec
        if name not in config_dict:
            config_dict[name] = {}
        for opt in cp.options(sec):
            try:
                config_dict[name][opt] = cp.getint(sec, opt)
            except ValueError:
                try:
                    config_dict[name][opt] = cp.getfloat(sec, opt)
                except ValueError:
                    try:
                        config_dict[name][opt] = cp.getboolean(sec, opt)
                    except ValueError:
                        config_dict[name][opt] = cp.get(sec, opt)
    FH.close()
    return config_dict
Пример #3
0
def cnfGetIniList(configFile, section, first=1, last=None):
    """
    Get a list of values for integer options in a configuration file.
    Input: ini file name, section, first value (optional, default 1)
    Output: list, or number of values in scalar context
    Example: out = cnfGetIniList(fileName, section, first=1, last=None)

    First value defaults to 1. Last is the last value which will be
    tried.  Allows gaps in the sequence, but there should not be
    duplicate values (we can't define which would be retrieved!).
    """
    if configFile is None:
        FH = open(flConfigFile())
    else:
        FH = open(configFile)
    cp = ConfigParser.ConfigParser()
    cp.optionxform = str
    cp.readfp(FH)
    values = []
    try:
        options = cp.options(section)
    except ConfigParser.NoSectionError:
        logger.exception("No section named {0} in configuration file {1}".format(section, configFile))
        FH.close()
        raise

    options = [int(o) for o in options]
    options.sort()
    for opt in options:
        if last:
            if (int(opt) >= first) and (int(opt) <= last):
                try:
                    values.append(cp.getint(section, str(opt)))
                except ValueError:
                    try:
                        values.append(cp.getfloat(section, str(opt)))
                    except ValueError:
                        try:
                            values.append(cp.getboolean(section, str(opt)))
                        except ValueError:
                            values.append(cp.get(section, str(opt)))
        else:
            if int(opt) >= first:
                try:
                    values.append(cp.getint(section, str(opt)))
                except ValueError:
                    try:
                        values.append(cp.getfloat(section, str(opt)))
                    except ValueError:
                        try:
                            values.append(cp.getboolean(section, str(opt)))
                        except ValueError:
                            values.append(cp.get(section, str(opt)))
    FH.close()
    return values
Пример #4
0
def _ShowSyntax(exit_code=0):
    print sys.argv[0]
    print "Interpolate the observed points of a tropical cyclone temporally"
    print "for use in modelling a scenario event in TCRM"
    print "Example usage:"
    print "{0} -c <config file> -l <log file> -v".format(sys.argv[0])
    print ""
    print "Options:"
    print "-h, --help:   prints this help message"
    print "-c, --config: configuration path (default value is {0})".format(flConfigFile())
    print "-l --logfile: path to log file to record actions (default value is {0})".format(flConfigFile(".log"))
    print "-v --verbose: True|False - print all logging messages to the screen"
    print ""
    print "Created by Craig Arthur, 2007-10-25 9:51:AM"
    print __version__
    sys.exit(exit_code)
Пример #5
0
def cnfGetIniFileValue(configFile, section, option, default=None):
    """
    Get a value directly from the configuration file, rather than
    using a cached value.  This shouldn't be used in anger, as it may
    produce adverse effects if you make changes to the config file
    while a program is running. Optionally takes a default value to
    return if the option is not in the configuration file.

    Input: configuration file name, section name, option name, default
    value.
    Output: value directly from the configuration file.
    Example: value = cnfGetIniFileValue(configFile, section, option,
                                        default)
    """
    if configFile is None:
        FH = open(flConfigFile())
    else:
        FH = open(configFile)
    cp = ConfigParser.ConfigParser()
    cp.optionxform = str
    cp.readfp(FH)
    try:
        sections = cp.sections()
    except ConfigParser.NoSectionError:
        logger.exception("No section named {0} in configuration file {1}".format(section, configFile))
        FH.close()
        raise

    if section in sections:
        options = cp.options(section)
        if option in options:
            try:
                value = cp.getint(section, option)
            except ValueError:
                try:
                    value = cp.getfloat(section, option)
                except ValueError:
                    try:
                        value = cp.getboolean(section, option)
                    except ValueError:
                        value = cp.get(section, option)
        else:
            value = default
    else:
        value = default
    FH.close()
    return value
Пример #6
0
    # Name of the output shape file, with no extension:
    if not isdir(abspath(output_path)):
        try:
            os.makedirs(abspath(output_path))
        except:
            print "Cannot create output path: {0}".format(output_path)
            raise

    # Load the exposure file:
    shapes, fields, records = parseShapefile(shape_file)
    features = AggUtils.loadZonesFromRecords(records, fields, featureid,
                                             zonefield)

    aggregateLoss(records, fields, features, featureid, output_path)


if __name__ == '__main__':

    if len(sys.argv) == 1:
        showSyntax()

    __STARTTIME__ = datetime.now()

    LOG = flStartLog(log_file=flConfigFile('.log'),
                     log_level='INFO',
                     verbose=True,
                     datestamp=True)

    main()
Пример #7
0
import my_tool as myutils
import metutils
import map as maputils
import stats
import numpy
import pylab
from files import flConfigFile, flLoadFile
from config import cnfGetIniValue, gConfigFile

global gConfigFile

try:
    gConfigFile = sys.argv[1]
except IndexError:
    gConfigFile = flConfigFile()


inputFile = cnfGetIniValue(gConfigFile, 'Input', 'File')
data = flLoadFile(inputFile, ",")
cNum = data[:, 0]
cAge = data[:, 1]
cLon = data[:, 2]
cLat = data[:, 3]
cVfm = data[:, 4]
cTheta = data[:, 5]
cPrs = data[:, 6]
cPe = data[:, 7]
cRm = data[:, 8]

cInd = ones(len(cNum))
Пример #8
0
def main():
    """
    Main section of the script - process command line arguments and call
    other functions to process the data
    """

    flStartLog(log_file=flConfigFile('.log'), log_level='INFO',
               verbose=True, datestamp=True)
    LOG.info("Parsing command line arguments")
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--costs',
                        help=('csv format file containing the cost data '
                              'for building types and land-use groupings'))
    parser.add_argument('-o', '--outputpath',
                        help=('Path to folder for storing the output' ))
    parser.add_argument('-s', '--shapefile',
                        help=('Path (including extension) of the shape '
                              'file holding the zone features to process '
                              '(e.g. land-use parcels, meshblocks, etc.)'))
    parser.add_argument('-v', '--vulnerability',
                        help=('csv format file containing the mean, sigma '
                              'and scale values for building '
                              'vulnerability curves'))
    
    args = parser.parse_args()
    cost_file = args.costs
    output_path = args.outputpath
    shape_file = args.shapefile
    vulnerability_file = args.vulnerability

    # Name of the output shape file, with no extension:
    if not isdir(abspath(output_path)):
        try:
            os.makedirs(abspath(output_path))
        except:
            print "Cannot create output path: {0}".format(output_path)
            raise
            
    if not isdir(abspath(pjoin(output_path, 'plots'))):
        try:
            os.makedirs(pjoin(abspath(output_path), 'plots'))
        except:
            print "Cannot create output path: {0}".format(pjoin(output_path,
                                                                'plots'))
            raise    

    # Add a datestring to the output file name so we know when it was created
    curdate = datetime.now()
    curdatestr = curdate.strftime('%Y%m%d%H%M')
    
    output_file = pjoin(abspath(output_path), 
                        "event_loss" )#_{0}".format(curdatestr))
    

    # Load the exposure file
    shapes, fields, records = parseShapefile(shape_file)
    # Get the projection
    spatial_ref = getProjection(shape_file)

    # Load the vulnerability file:
    building_types = parseVulnerability(vulnerability_file)

    # Process damage:
    output_fields, output_records = processDamage(fields, records,
                                                  building_types,
                                                  cost_file)

    # Write out the data to another shapefile
    writeShapefile(output_file, output_fields, shapes, output_records)
    writeProjectionFile(spatial_ref, output_file)

    # Do the damage state calclations:
    for state in ['slight', 'moderate', 'extensive', 'complete']:
        output_fields, output_records = processFragility(fields, records,
                                                         building_types,
                                                         state)

    output_fields, output_records = calculatePopulation(output_fields,
                                                        output_records,
                                                        building_types)
    output_file = pjoin(abspath(output_path), 
                        "event_damage_states")
    writeShapefile(output_file, output_fields, shapes, output_records)
    writeProjectionFile(spatial_ref, output_file)

    pop_fields, pop_records = dropDamageStateFields(building_types, 
                                                    output_fields, 
                                                    output_records)

    output_file = pjoin(abspath(output_path), "event_pop_affect")
    writeShapefile(output_file, pop_fields, shapes, pop_records)
    writeProjectionFile(spatial_ref, output_file)
Пример #9
0
def cnfGetUnorderedList(configFile, section):
    """
    Get a list of unordered values in the given section.

    Input: ini file name, section
    Output: list, or number of values in scalar context
    Example: out = cnfGetUnorderdedList( filename, section )

    This function requires a different approach to reading
    the configuration file, as ConfigParser in Python versions
    prior to 2.7 cannot handle options with no value. The workaround
    reads the configuration file and constructs a dict similar
    to config_dict used elsewhere in this module.
    Lists of unordered values are included in the dict with the
    key and subkey equal to the section name.
    i.e. cfgDict[ section ][ section ] = [list]


    """
    sect = None
    sect_list = []
    cfgDict = {}
    if configFile is None:
        configFile = flConfigFile()
    try:
        FH = open(configFile)
    except IOError:
        logger.warn("Cannot open {0}".format(configFile))
    else:
        for line in FH:
            line = line.lstrip().rstrip("\n")
            cm = re.match("^;", line)
            if cm:
                # Ignore lines that begin with a comment character
                continue
            sm = re.match("^\[(\w*)\]", line)
            am = re.match("^([^=]+)=(.+)", line)
            if sm:
                new_sect = sm.group(1)
                if sect:
                    key = sect
                    subkey = key
                    cfgDict[key][subkey] = sect_list
                sect = new_sect
                sect_list = []

            elif am:
                # Attribute/value pair
                att = am.group(1)
                val = am.group(2)
                att = att.rstrip()
                val = val.rstrip().lstrip()
                if cfgDict.has_key(sect):
                    cfgDict[sect][att] = val
                else:
                    cfgDict[sect] = {}
                    cfgDict[sect][att] = val
            elif len(line):
                sect_list.append(line)

        FH.close()

    return cfgDict[section][section]
Пример #10
0
def _process(argv):
    """
    A wrapper function to provide an interface between the command line
    args and the actual plotField function. This function reads settings
    from a configuration file and then passes those arguments to the
    plotField function.
    """
    if len(argv)==0:
        _usage()
        sys.exit(2)

    logLevel = 'INFO'
    verbose = False
    configFile = flConfigFile()

    try:
        opts, args = getopt.getopt(argv, "c:hl:v", ["config=", "help",
                                                   "loglevel=", "verbose"])
    except getopt.GetoptError:
        _usage()
        sys.exit(2)
    for opt,arg in opts:
        if opt in ("-h", "--help"):
            _usage()
            sys.exit(2)
        elif opt in ("-c", "--config"):
            configFile = arg
        elif opt in ("-l", "--loglevel"):
            logLevel = arg
        elif opt in ("-v", "--verbose"):
            verbose = True

    flStartLog(cnfGetIniValue(configFile, 'Logging', 'LogFile', flConfigFile('.log')),
               cnfGetIniValue(configFile, 'Logging', 'LogLevel', logLevel),
               cnfGetIniValue(configFile, 'Logging', 'Verbose', verbose))

    # Input data:
    inputFile = cnfGetIniValue(configFile, 'Input', 'File')
    inputFormat = cnfGetIniValue(configFile, 'Input', 'Format', os.path.splitext(inputFile)[-1])
    varname = cnfGetIniValue(configFile,'Input','Variable','')
    record = cnfGetIniValue(configFile,'Input','Record',0)
    lvl = cnfGetIniValue(configFile,'Input','Level',0)

    # Output settings - the default is to use the input filename, with
    # the extension replaced by the image format:
    # The smoothing is optional. Set it to the number of grid points to
    # smooth over (recommend the reciprocal of the data resolution in degrees).
    imgfmt = cnfGetIniValue(configFile, 'Output', 'Format','png')
    outputFile = cnfGetIniValue(configFile, 'Output', 'File',
                                "%s.%s" % (os.path.splitext(inputFile)[0], imgfmt))
    smoothing = cnfGetIniValue(configFile, 'Output', 'Smoothing', False)
    cmapName = cnfGetIniValue(configFile, 'Output', 'ColourMap', 'gist_ncar')
    label = cnfGetIniValue(configFile, 'Output', 'Label', '')
    mask = cnfGetIniValue(configFile, 'Output', 'MaskLand', False)
    maskocean = cnfGetIniValue(configFile, 'Output', 'MaskOcean', False)
    fill = cnfGetIniValue(configFile, 'Output', 'FillContours', True)
    title = cnfGetIniValue(configFile,'Plot','Title',None)
    # Load data:
    if inputFormat == '.txt':
        # Attempt to load the dataset:
        try:
            lon,lat,data = grid.grdRead(inputFile)
        except:
            logger.critical("Cannot load input file: %s"%inputFile)
            raise
    elif inputFormat == '.nc':
        try:
            ncobj = nctools.ncLoadFile(inputFile)
            lon = nctools.ncGetDims(ncobj,'lon')
            lat = nctools.ncGetDims(ncobj,'lat')
            data = nctools.ncGetData(ncobj,varname)
            mv = getattr(ncobj.variables[varname],'_FillValue')
            ncobj.close()
        except:
            logger.critical("Cannot load input file: %s"%inputFile)
            raise
        if len(shape(data))==3:
            data = data[record,:,:]
        elif len(shape(data))==4:
            data = data[record,lvl,:,:]

        # Create a masked array:
        datamask = (data==mv)
        data = ma.array(data,mask=datamask)

    else:
        logger.critical("Unknown data format")
        raise IOError

    # Set defaults for the extent of the map to match the data in the
    # input file:
    llLon = min(lon)
    urLon = max(lon)
    llLat = min(lat)
    urLat = max(lat)
    res = 'l'
    dl = 10.

    # Domain settings - can override the default settings:
    domain = cnfGetIniValue(configFile, 'Domain', 'Name', None)
    if domain is not None:
        llLon = cnfGetIniValue(configFile, domain, 'LowerLeftLon', min(lon))
        llLat = cnfGetIniValue(configFile, domain, 'LowerLeftLat', min(lat))
        urLon = cnfGetIniValue(configFile, domain, 'UpperRightLon', max(lon))
        urLat = cnfGetIniValue(configFile, domain, 'UpperRightLat', max(lat))
        res = cnfGetIniValue(configFile, domain, 'Resolution', res)
        dl = cnfGetIniValue(configFile, domain, 'GridInterval', dl)

    [x,y] = meshgrid(lon, lat)

    # Set the scale:
    scaleMin = cnfGetIniValue(configFile, 'Output', 'ScaleMin', 0)
    scaleMax = cnfGetIniValue(configFile, 'Output', 'ScaleMax', 101)
    scaleInt = cnfGetIniValue(configFile, 'Output', 'ScaleInt', 10)
    levels = arange(scaleMin, scaleMax, scaleInt)
    plotField(x,y,data, llLon, llLat, urLon, urLat, res, dl, levels,
              cmapName, smoothing, title=title, xlab='Longitude',
              ylab='Latitude', clab=label, maskland=mask,
              maskocean=maskocean,outputFile=outputFile,fill=fill)

    logger.info("Completed %s"%sys.argv[0])