Example #1
0
def main(args):
    ap = argparse.ArgumentParser(prog=os.path.basename(__file__),
                                 formatter_class=argparse.RawDescriptionHelpFormatter,
                                 description='',
                                 epilog=__doc__)

    ap.add_argument('-c', '--config-file',
                    required=False,
                    help='the configuration file, default to "$HOME/.sdm.cfg"')
    ap.add_argument('-V', '--verbose',
                    action='store_true',
                    default=False,
                    help='be more chatty')
    ap.add_argument('-v', '--version',
                    action='version',
                    version='%s: v%s' % (ap.prog, __version__))

    subparsers = ap.add_subparsers(dest='sub_command',
                                   title='List of sub-commands',
                                   metavar='sub-command',
                                   help='"%s sub-command -h" for more help' % ap.prog)

    cod_getpath_parser = subparsers.add_parser('cod-getpath',
                                               help='get the full path to a CoD file')

    cod_getpath_parser.add_argument('-m', '--model',
                                    required=True,
                                    help='model name')
    cod_getpath_parser.add_argument('-c', '--scenario',
                                    required=False,
                                    help='scenario name, e.g. historical, rcp45, rcp85')
    cod_getpath_parser.add_argument('-r', '--region-type',
                                    required=True,
                                    help='pre-defined region type name, e.g. sea, sec, tas ...')
    cod_getpath_parser.add_argument('-s', '--season',
                                    required=True,
                                    help='season number, e.g. 1 (DJF), 2 (MAM), 3 (JJA), or 4 (SON)')
    cod_getpath_parser.add_argument('-p', '--predictand',
                                    required=True,
                                    help='predictand name, e.g. rain, tmax, tmin')

    dxt_gridded_parser = subparsers.add_parser('dxt-gridded',
                                               help='extract gridded data using the given cod file')
    dxt_gridded_parser.add_argument('cod_file_path',
                                    help='full path to the CoD file')
    dxt_gridded_parser.add_argument('output_file',
                                    help='output netCDF file name')
    dxt_gridded_parser.add_argument('-R', '--region',
                                    required=False,
                                    help='the region where the data are to be extracted')

    dxt_gridded2_parser = subparsers.add_parser('dxt-gridded2',
                                                help='extract gridded data with the given parameters')
    dxt_gridded2_parser.add_argument('output_file',
                                     help='output netCDF file name')
    dxt_gridded2_parser.add_argument('-m', '--model',
                                     required=True,
                                     help='model name')
    dxt_gridded2_parser.add_argument('-c', '--scenario',
                                     required=False,
                                     help='scenario name, e.g. historical, rcp45, rcp85')
    dxt_gridded2_parser.add_argument('-r', '--region-type',
                                     required=True,
                                     help='pre-defined region type name, e.g. sea, sec, tas ...')
    dxt_gridded2_parser.add_argument('-s', '--season',
                                     required=True,
                                     help='season number, e.g. 1 (DJF), 2 (MAM), 3 (JJA), or 4 (SON)')
    dxt_gridded2_parser.add_argument('-p', '--predictand',
                                     required=True,
                                     help='predictand name, e.g. rain, tmax, tmin')
    dxt_gridded2_parser.add_argument('-R', '--region',
                                     required=False,
                                     help='the region where the data are to be extracted (default to region-type)')

    ns = ap.parse_args(args)

    config = read_config(ns.config_file)

    if ns.sub_command == 'cod-getpath':
        print CoD(config.get('dxt', 'cod_base_dir'), verbose=ns.verbose).get_cod_file_path(
            ns.model, ns.scenario, ns.region_type, ns.season, ns.predictand)

    elif ns.sub_command in ('dxt-gridded', 'dxt-gridded2'):
        gridded_extractor = GriddedExtractor(cod_base_dir=config.get('dxt', 'cod_base_dir'),
                                             mask_base_dir=config.get('dxt', 'mask_base_dir'),
                                             gridded_base_dir=config.get('dxt', 'gridded_base_dir'),
                                             verbose=ns.verbose)

        if ns.sub_command == 'dxt-gridded':
            model, scenario, region_type, season, predictand = CoD.get_components_from_path(ns.cod_file_path)
        else:
            model, scenario, region_type, season, predictand = \
                ns.model, ns.scenario, ns.region_type, ns.season, ns.predictand

        data, dates, lat, lon = gridded_extractor.extract(model, scenario, region_type, season, predictand,
                                                          ns.region)
        GriddedExtractor.save_netcdf(ns.output_file, data, dates, lat, lon,
                                     model, scenario, region_type, season, predictand)
Example #2
0
import numpy as np

from sdm.cod import CoD
from sdm.extractor import GriddedExtractor

data_extractor = GriddedExtractor(cod_base_dir=r'C:\Users\ywang\tmp\CMIP5_v2',
                                  mask_base_dir=r'C:\Users\ywang\tmp\Masks',
                                  gridded_base_dir=r'C:\Users\ywang\tmp\AWAP',
                                  verbose=True)

data, dates, lat, lon = data_extractor.extract('ACCESS1.0', 'historical',
                                               'tas', '2', 'tmin')

GriddedExtractor.save_netcdf('tmp.nc', data, dates, lat, lon, 'ACCESS1.0',
                             'historical', 'tas', '2', 'tmin')

data2, dates, lat, lon = data_extractor.extract(*CoD.get_components_from_path(
    r'C:\Users\ywang\tmp\CMIP5_v2\ACCESS1.0_historical\tas\tmin\season_2\rawfield_analog_2'
))

np.testing.assert_equal(data, data2)
Example #3
0
def main(args):
    ap = argparse.ArgumentParser(prog=os.path.basename(__file__),
                                 formatter_class=argparse.RawDescriptionHelpFormatter,
                                 description='',
                                 epilog=__doc__)

    ap.add_argument('-c', '--config-file',
                    required=False,
                    help='the configuration file, default to "$HOME/.sdm.cfg"')
    ap.add_argument('-v', '--verbose',
                    action='store_true',
                    default=False,
                    help='be more chatty')
    ap.add_argument('-V', '--version',
                    action='version',
                    version='%s: v%s' % (ap.prog, __version__))
    ap.add_argument('--debug',
                    action='store_true',
                    default=False,
                    help='print debug messages')
    # Overriding config options
    ap.add_argument('--dxt-cod_base_dir',
                    help='override cod_base_dir option of the dxt section')
    ap.add_argument('--dxt-mask_base_dir',
                    help='override mask_base_dir option of the dxt section')
    ap.add_argument('--dxt_gridded_base_dir',
                    help='override gridded_base_dir option of the dxt section')

    subparsers = ap.add_subparsers(dest='sub_command',
                                   title='List of sub-commands',
                                   metavar='sub-command',
                                   help='"%s sub-command -h" for more help' % ap.prog)

    cod_getpath_parser = subparsers.add_parser('cod-getpath',
                                               help='get the full path to a CoD file')

    cod_getpath_parser.add_argument('-m', '--model',
                                    required=True,
                                    help='model name')
    cod_getpath_parser.add_argument('-c', '--scenario',
                                    required=False,
                                    help='scenario name, e.g. historical, rcp45, rcp85')
    cod_getpath_parser.add_argument('-r', '--region-type',
                                    required=True,
                                    help='pre-defined region type name, e.g. sea, sec, tas ...')
    cod_getpath_parser.add_argument('-s', '--season',
                                    required=True,
                                    help='season number, e.g. 1 (DJF), 2 (MAM), 3 (JJA), or 4 (SON)')
    cod_getpath_parser.add_argument('-p', '--predictand',
                                    required=True,
                                    help='predictand name, e.g. rain, tmax, tmin')

    dxt_gridded_parser = subparsers.add_parser('dxt-gridded',
                                               help='extract gridded data using the given cod file')
    dxt_gridded_parser.add_argument('cod_file_path',
                                    help='full path to the CoD file')
    dxt_gridded_parser.add_argument('output_file',
                                    help='output netCDF file name')
    dxt_gridded_parser.add_argument('-R', '--region',
                                    required=False,
                                    help='the region where the data are to be extracted')

    dxt_gridded2_parser = subparsers.add_parser('dxt-gridded2',
                                                help='extract gridded data with the given parameters')
    dxt_gridded2_parser.add_argument('output_file',
                                     help='output netCDF file name')
    dxt_gridded2_parser.add_argument('-m', '--model',
                                     required=True,
                                     help='model name')
    dxt_gridded2_parser.add_argument('-c', '--scenario',
                                     required=False,
                                     help='scenario name, e.g. historical, rcp45, rcp85')
    dxt_gridded2_parser.add_argument('-r', '--region-type',
                                     required=True,
                                     help='pre-defined region type name, e.g. sea, sec, tas ...')
    dxt_gridded2_parser.add_argument('-s', '--season',
                                     required=True,
                                     help='season number, e.g. 1 (DJF), 2 (MAM), 3 (JJA), or 4 (SON)')
    dxt_gridded2_parser.add_argument('-p', '--predictand',
                                     required=True,
                                     help='predictand name, e.g. rain, tmax, tmin')
    dxt_gridded2_parser.add_argument('-R', '--region',
                                     required=False,
                                     help='the region where the data are to be extracted (default to region-type)')

    to_3d_parser = subparsers.add_parser('to-3d',
                                         help='Convert and save the 2D (dates, gpnames) file to 3D (dates, lat, lon)')
    to_3d_parser.add_argument('data2d_file',
                              help='the input file containing the 2D data')
    to_3d_parser.add_argument('data3d_file',
                              help='the output file')
    to_3d_parser.add_argument('-R', '--region',
                              help='the region of the downscaled data')

    ns = ap.parse_args(args)

    if ns.debug:
        logging.basicConfig(format='%(asctime)s %(levelname)s [%(funcName)s] - %(message)s',
                            level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.WARNING)

    config = read_config(ns.config_file)
    # Process any overriding
    if ns.dxt_cod_base_dir:
        config.set('dxt', 'cod_base_dir', ns.dxt_cod_base_dir)
    if ns.dxt_mask_base_dir:
        config.set('dxt', 'mask_base_dir', ns.dxt_mask_base_dir)
    if ns.dxt_gridded_base_dir:
        config.set('dxt', 'gridded_base_dir', ns.dxt_gridded_base_dir)

    if ns.sub_command == 'cod-getpath':
        main_parameters = MainParameters(ns.model, ns.scenario, ns.region_type, ns.season, ns.predictand)
        print CoD(config.get('dxt', 'cod_base_dir')).get_cod_file_path(main_parameters)

    elif ns.sub_command in ('dxt-gridded', 'dxt-gridded2'):
        gridded_extractor = GriddedExtractor(cod_base_dir=config.get('dxt', 'cod_base_dir'),
                                             mask_base_dir=config.get('dxt', 'mask_base_dir'),
                                             gridded_base_dir=config.get('dxt', 'gridded_base_dir'))

        if ns.sub_command == 'dxt-gridded':
            main_parameters = MainParameters.from_filepath(ns.cod_file_path)
        else:
            main_parameters = MainParameters(ns.model, ns.scenario, ns.region_type, ns.season, ns.predictand)

        data = gridded_extractor.extract(main_parameters, ns.region)

        data.save_nc(ns.output_file, main_parameters)

    elif ns.sub_command == 'to-3d':
        main_parameters = MainParameters.from_filepath(ns.data2d_file)
        data2d = Data2DReader().read(ns.data2d_file)
        mask_reader = MaskReader(base_dir=config.get('dxt', 'mask_base_dir'))
        mask = mask_reader.read(ns.region if ns.region else main_parameters.region_type)
        data3d = data2d.to_3d(mask.crop())

        data3d.save_nc(ns.data3d_file, main_parameters=main_parameters)

    else:
        sys.stderr.write('Unknown sub-command: {}'.format(ns.sub_command))
import numpy as np

from sdm.cod import CoD
from sdm.extractor import GriddedExtractor

data_extractor = GriddedExtractor(cod_base_dir=r'C:\Users\ywang\tmp\CMIP5_v2',
                                  mask_base_dir=r'C:\Users\ywang\tmp\Masks',
                                  gridded_base_dir=r'C:\Users\ywang\tmp\AWAP',
                                  verbose=True)

data, dates, lat, lon = data_extractor.extract('ACCESS1.0', 'historical', 'tas', '2', 'tmin')

GriddedExtractor.save_netcdf('tmp.nc', data, dates, lat, lon,
                             'ACCESS1.0', 'historical', 'tas', '2', 'tmin')

data2, dates, lat, lon = data_extractor.extract(
    *CoD.get_components_from_path(
        r'C:\Users\ywang\tmp\CMIP5_v2\ACCESS1.0_historical\tas\tmin\season_2\rawfield_analog_2'))

np.testing.assert_equal(data, data2)
Example #5
0
def main(args):
    ap = argparse.ArgumentParser(
        prog=os.path.basename(__file__),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='',
        epilog=__doc__)

    ap.add_argument('-c',
                    '--config-file',
                    required=False,
                    help='the configuration file, default to "$HOME/.sdm.cfg"')
    ap.add_argument('-V',
                    '--verbose',
                    action='store_true',
                    default=False,
                    help='be more chatty')
    ap.add_argument('-v',
                    '--version',
                    action='version',
                    version='%s: v%s' % (ap.prog, __version__))

    subparsers = ap.add_subparsers(dest='sub_command',
                                   title='List of sub-commands',
                                   metavar='sub-command',
                                   help='"%s sub-command -h" for more help' %
                                   ap.prog)

    cod_getpath_parser = subparsers.add_parser(
        'cod-getpath', help='get the full path to a CoD file')

    cod_getpath_parser.add_argument('-m',
                                    '--model',
                                    required=True,
                                    help='model name')
    cod_getpath_parser.add_argument(
        '-c',
        '--scenario',
        required=False,
        help='scenario name, e.g. historical, rcp45, rcp85')
    cod_getpath_parser.add_argument(
        '-r',
        '--region-type',
        required=True,
        help='pre-defined region type name, e.g. sea, sec, tas ...')
    cod_getpath_parser.add_argument(
        '-s',
        '--season',
        required=True,
        help='season number, e.g. 1 (DJF), 2 (MAM), 3 (JJA), or 4 (SON)')
    cod_getpath_parser.add_argument(
        '-p',
        '--predictand',
        required=True,
        help='predictand name, e.g. rain, tmax, tmin')

    dxt_gridded_parser = subparsers.add_parser(
        'dxt-gridded', help='extract gridded data using the given cod file')
    dxt_gridded_parser.add_argument('cod_file_path',
                                    help='full path to the CoD file')
    dxt_gridded_parser.add_argument('output_file',
                                    help='output netCDF file name')
    dxt_gridded_parser.add_argument(
        '-R',
        '--region',
        required=False,
        help='the region where the data are to be extracted')

    dxt_gridded2_parser = subparsers.add_parser(
        'dxt-gridded2', help='extract gridded data with the given parameters')
    dxt_gridded2_parser.add_argument('output_file',
                                     help='output netCDF file name')
    dxt_gridded2_parser.add_argument('-m',
                                     '--model',
                                     required=True,
                                     help='model name')
    dxt_gridded2_parser.add_argument(
        '-c',
        '--scenario',
        required=False,
        help='scenario name, e.g. historical, rcp45, rcp85')
    dxt_gridded2_parser.add_argument(
        '-r',
        '--region-type',
        required=True,
        help='pre-defined region type name, e.g. sea, sec, tas ...')
    dxt_gridded2_parser.add_argument(
        '-s',
        '--season',
        required=True,
        help='season number, e.g. 1 (DJF), 2 (MAM), 3 (JJA), or 4 (SON)')
    dxt_gridded2_parser.add_argument(
        '-p',
        '--predictand',
        required=True,
        help='predictand name, e.g. rain, tmax, tmin')
    dxt_gridded2_parser.add_argument(
        '-R',
        '--region',
        required=False,
        help=
        'the region where the data are to be extracted (default to region-type)'
    )

    ns = ap.parse_args(args)

    config = read_config(ns.config_file)

    if ns.sub_command == 'cod-getpath':
        print CoD(config.get('dxt', 'cod_base_dir'),
                  verbose=ns.verbose).get_cod_file_path(
                      ns.model, ns.scenario, ns.region_type, ns.season,
                      ns.predictand)

    elif ns.sub_command in ('dxt-gridded', 'dxt-gridded2'):
        gridded_extractor = GriddedExtractor(
            cod_base_dir=config.get('dxt', 'cod_base_dir'),
            mask_base_dir=config.get('dxt', 'mask_base_dir'),
            gridded_base_dir=config.get('dxt', 'gridded_base_dir'),
            verbose=ns.verbose)

        if ns.sub_command == 'dxt-gridded':
            model, scenario, region_type, season, predictand = CoD.get_components_from_path(
                ns.cod_file_path)
        else:
            model, scenario, region_type, season, predictand = \
                ns.model, ns.scenario, ns.region_type, ns.season, ns.predictand

        data, dates, lat, lon = gridded_extractor.extract(
            model, scenario, region_type, season, predictand, ns.region)
        GriddedExtractor.save_netcdf(ns.output_file, data, dates, lat, lon,
                                     model, scenario, region_type, season,
                                     predictand)