|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2019 Scott Wales |
| 4 | +# |
| 5 | +# Author: Scott Wales <[email protected]> |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +from .main import cli |
| 20 | +from ..grid import UMGrid |
| 21 | +from ..regrid import regrid, esmf_generate_weights |
| 22 | +from ..um.create_ancillary import create_surface_ancillary |
| 23 | +import click |
| 24 | +import pandas |
| 25 | +import mule |
| 26 | +import iris |
| 27 | +import xarray |
| 28 | +from dask.diagnostics import ProgressBar |
| 29 | +import dask.distributed |
| 30 | +import matplotlib.pyplot as plt |
| 31 | + |
| 32 | +@cli.group() |
| 33 | +def um(): |
| 34 | + """ |
| 35 | + Tools for working with the Unified Model |
| 36 | + """ |
| 37 | + pass |
| 38 | + |
| 39 | +@um.group() |
| 40 | +def ancil(): |
| 41 | + """ |
| 42 | + Tools for working with ancil files |
| 43 | + """ |
| 44 | + pass |
| 45 | + |
| 46 | +def validate_date(ctx, param, value): |
| 47 | + """ |
| 48 | + Ensures an argument is a valid date |
| 49 | + """ |
| 50 | + try: |
| 51 | + return pandas.to_datetime(value, utc=True, dayfirst=True) |
| 52 | + except ValueError: |
| 53 | + raise click.BadParameter(f'unable to parse "{value}" as a date') |
| 54 | + |
| 55 | +def validate_um_ancil(ctx, param, value): |
| 56 | + """ |
| 57 | + Ensures an argument is a UM file |
| 58 | + """ |
| 59 | + try: |
| 60 | + return mule.AncilFile.from_file(value) |
| 61 | + except: |
| 62 | + raise click.BadParameter(f'"{value}" does not seem to be a UM ancil file') |
| 63 | + |
| 64 | +@ancil.command() |
| 65 | +@click.option('--start-date', callback=validate_date, required=True) |
| 66 | +@click.option('--end-date', callback=validate_date, required=True) |
| 67 | +@click.option('--target-mask', |
| 68 | + type=click.Path(exists=True, dir_okay=False)) |
| 69 | +@click.option('--output', required=True, |
| 70 | + type=click.Path(writable=True, dir_okay=False)) |
| 71 | +def era_sst(start_date, end_date, target_mask, output): |
| 72 | + """ |
| 73 | + Create ancil files from ERA reanalysis data |
| 74 | + """ |
| 75 | + |
| 76 | + um_grid = UMGrid.from_mask(target_mask) |
| 77 | + |
| 78 | + file_start = start_date - pandas.offsets.MonthBegin() |
| 79 | + file_end = end_date + pandas.offsets.MonthEnd() |
| 80 | + file_a = pandas.date_range(file_start,file_end,freq='MS') |
| 81 | + file_b = file_a + pandas.offsets.MonthEnd() |
| 82 | + |
| 83 | + dates = [f'{a.strftime("%Y%m%d")}_{b.strftime("%Y%m%d")}' |
| 84 | + for a,b in zip(file_a, file_b)] |
| 85 | + |
| 86 | + # Read and slice the source data |
| 87 | + tos = xarray.open_mfdataset(['/g/data1a/ub4/erai/netcdf/6hr/ocean/' |
| 88 | + 'oper_an_sfc/v01/tos/' |
| 89 | + 'tos_6hrs_ERAI_historical_an-sfc_'+d+'.nc' |
| 90 | + for d in dates], |
| 91 | + chunks={'time': 1,}) |
| 92 | + sic = xarray.open_mfdataset(['/g/data1a/ub4/erai/netcdf/6hr/seaIce/' |
| 93 | + 'oper_an_sfc/v01/sic/' |
| 94 | + 'sic_6hrs_ERAI_historical_an-sfc_'+d+'.nc' |
| 95 | + for d in dates], |
| 96 | + chunks={'time': 1,}) |
| 97 | + ds = xarray.Dataset({'tos': tos.tos, 'sic': sic.sic}) |
| 98 | + ds = ds.sel(time=slice(start_date, end_date)) |
| 99 | + |
| 100 | + weights = esmf_generate_weights(tos.tos.isel(time=0), um_grid, method='patch') |
| 101 | + newds = regrid(ds, weights=weights) |
| 102 | + |
| 103 | + print(newds) |
| 104 | + |
| 105 | + ancil = create_surface_ancillary(newds, {'tos': 24, 'sic': 31}) |
| 106 | + ancil.to_file(output) |
0 commit comments