Пример #1
0
def main(args):
    '''
    The main function
    '''
    (
        baseline_data_source,
        historical_data_source,
        projection_data_source,
        variable_name,
        baseline_start_date,
        baseline_end_date,
        projection_start_date,
        projection_end_date,
    ) = get_args(args)

    measurement = transform.to_standard_variable_name(variable_name)
    units = transform.standard_units_from_measurement(measurement)

    climatedb.connect()

    for month in range(1, climatedb.MONTHS_PER_YEAR + 1):
        #print('.', end='', flush=True)
        print('For month %d' % month)
        calibration.calibrate(baseline_data_source, historical_data_source,
                              projection_data_source, measurement, units,
                              baseline_start_date, baseline_end_date,
                              projection_start_date, projection_end_date,
                              month)

    climatedb.close()
Пример #2
0
def main(args):
    '''
    The main function
    '''
    filename = get_args(args)

    climatedb.connect()

    geonames.load_geonames(filename)

    climatedb.close()
def main(args):
    '''
    The main function
    '''
    input_file, variable_name, data_source, ignore_scale_factor = get_args(args)

    print(variable_name, data_source)

    lat_arr, lon_arr, units, data_arr = get_data_from_file(input_file, variable_name, ignore_scale_factor)
    lon_arr, data_arr = transform.normalize_longitudes(lon_arr, data_arr)

    measurement = transform.to_standard_variable_name(variable_name)

    climatedb.connect()
    climatedb.save_nontemporal_data(lat_arr, lon_arr, units, data_arr, measurement, data_source)
    climatedb.close()

    return 0
Пример #4
0
def main(args):
    '''
    The main function
    '''
    data_source, variable_name, start_date, end_date, calibrated = get_args(args)

    measurement = transform.to_standard_variable_name(variable_name)
    units = transform.standard_units_from_measurement(measurement)

    climatedb.connect()

    unit_id = climatedb.fetch_unit(units)['id']
    measurement_id = climatedb.fetch_measurement(measurement)['id']
    data_source_record = climatedb.fetch_data_source(data_source)

    dataset = climatedb.fetch_dataset(
        data_source_record['id'],
        measurement_id,
        unit_id,
        start_date,
        end_date,
        calibrated=True if calibrated else False
    )

    print(data_source, measurement, start_date.year, end_date.year, 'year')
    lat_arr, lon_arr, normals = climatedb.fetch_normals_from_dataset_mean(dataset)

    projected_y_arr = geo.lat2y(lat_arr)
    projected_x_arr = geo.lon2x(lon_arr)

    output_folder = tiling.tile_folder(data_source, variable_name, start_date, end_date)
    tiling.save_contour_tiles(
        projected_y_arr,
        projected_x_arr,
        measurement,
        units,
        normals,
        output_folder,
        data_source_record['id']
    )

    for start_month in (12, 3, 6, 9):
        months = start_month, (start_month + 1) % 12, (start_month + 2) % 12
        print(data_source, measurement, start_date.year, end_date.year, months)

        aggregated_normals = None

        for month in months:
            lat_arr, lon_arr, normals = climatedb.fetch_normals_from_dataset(dataset, month)
            if aggregated_normals is None:
                aggregated_normals = normals.copy()
            else:
                aggregated_normals += normals

        aggregated_normals = aggregated_normals / len(months)

        output_folder = tiling.tile_folder(data_source, variable_name, start_date, end_date, months)

        tiling.save_contour_tiles(
            projected_y_arr,
            projected_x_arr,
            measurement,
            units,
            aggregated_normals,
            output_folder,
            data_source_record['id']
        )

    climatedb.close()
Пример #5
0
def main(args):
    '''
    The main function
    '''
    input_files, variable_name, start_year, end_year, data_source = get_args(
        args)

    # Extract normals from datasets
    def get_normals_function(month, start_time, end_time):
        def get_normals(input_file):
            input_fmt = get_input_fmt(input_file)

            if input_fmt == 'nc':
                return transform.normals_from_netcdf4(input_file,
                                                      variable_name,
                                                      start_time, end_time,
                                                      month)

            elif input_fmt in ('tif', 'bil'):
                return transform.normals_from_geotiff(input_file, input_fmt)

            elif input_fmt == 'folder':
                return transform.normals_from_folder(input_file, variable_name,
                                                     month)

            else:
                raise Exception('Unexpected input format "%s"' % input_fmt)

        return get_normals

    climatedb.connect()

    for month in range(1, climatedb.MONTHS_PER_YEAR + 1):
        print(data_source, variable_name, start_year, end_year, month)

        if month > 0:
            start_time = datetime(start_year, month, 1)
            # This will set the end time to the last second of the last day of the
            # specified month in the specified end year.
            # Credit to https://stackoverflow.com/a/4131114 for the div/mod by 12
            end_time = datetime(end_year + month // 12, month % 12 + 1,
                                1) - timedelta(seconds=1)
        else:
            start_time = datetime(start_year, 1, 1)
            end_time = datetime(end_year + 1, 1, 1) - timedelta(seconds=1)

        lat_arr, lon_arr, units, normals = \
            transform.aggregate_normals(input_files, get_normals_function(month, start_time, end_time))

        units, normals = transform.data_to_standard_units(
            units, normals, month)
        lon_arr, normals = transform.normalize_longitudes(lon_arr, normals)
        normals = pack.pack_array(normals)

        measurement = transform.to_standard_variable_name(variable_name)

        climatedb.save_normals(lat_arr, lon_arr, units, normals, measurement,
                               start_time, end_time, month, data_source)

    climatedb.close()

    return 0
Пример #6
0
# Copyright (c) 2020 Carlos Torchia
#

from flask import Flask
from flask import jsonify

import os

import tiling
import climatedb

app = Flask(__name__)

climatedb.connect()
ALLOWED_MEASUREMENTS = climatedb.fetch_measurements()
climatedb.close()

ALLOWED_PERIODS = [
    '12_01_02',
    '03_04_05',
    '06_07_08',
    '09_10_11',
    'year',
]


@app.route(
    '/climate/<string:data_source>/<int:start_year>-<int:end_year>/<string:measurement>-<string:period>/<int:zoom_level>/<int:x>/<int:y>.<string:ext>'
)
def climate_tile(data_source, start_year, end_year, measurement, period,
                 zoom_level, x, y, ext):
Пример #7
0
def teardown(error):
    '''
    Close the database when the request finishes.
    '''
    climatedb.close()