Skip to content
Snippets Groups Projects
infer_cloud_products.py 2.43 KiB
Newer Older
tomrink's avatar
tomrink committed
from util.geos_nav import get_navigation
tomrink's avatar
tomrink committed
from util.setup_cloud_products import model_path_cld_frac, model_path_cld_opd
tomrink's avatar
tomrink committed
from aeolus.datasource import CLAVRx
tomrink's avatar
tomrink committed
import os
tomrink's avatar
tomrink committed
from deeplearning.cloud_fraction_fcn_abi import SRCNN as SRCNN_CLD_FRAC
from deeplearning.cloud_opd_fcn_abi import SRCNN as SRCNN_CLD_OPD
tomrink's avatar
tomrink committed
from util.util import get_cartopy_crs, write_cld_prods_file_nc4
tomrink's avatar
tomrink committed
import numpy as np
tomrink's avatar
tomrink committed
import time
tomrink's avatar
tomrink committed


tomrink's avatar
tomrink committed
def infer_cloud_products(clvrx_path, output_dir, full_disk=True, satellite='GOES16', domain='FD', pattern=None):
tomrink's avatar
tomrink committed
    # -- location of the trained model
tomrink's avatar
tomrink committed
    ckpt_dir_s = os.listdir(model_path_cld_frac)
    ckpt_dir_cld_frac = model_path_cld_frac + ckpt_dir_s[0]

    ckpt_dir_s = os.listdir(model_path_cld_opd)
    ckpt_dir_cld_opd = model_path_cld_opd + ckpt_dir_s[0]
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    # -- Navigation parameters
tomrink's avatar
tomrink committed
    geos, xlen, xmin, xmax, ylen, ymin, ymax = get_cartopy_crs(satellite, domain)
    nav = get_navigation(satellite, domain)
    cc = np.arange(xlen)
    ll = np.arange(ylen)
    x_rad = cc * nav.CFAC + nav.COFF
    y_rad = ll * nav.LFAC + nav.LOFF

tomrink's avatar
tomrink committed
    # -- Create a model instance and initialize with trained model above
tomrink's avatar
tomrink committed
    nn_cld_frac = SRCNN_CLD_FRAC()
    nn_cld_frac.setup_inference(ckpt_dir_cld_frac)

    nn_cld_opd = SRCNN_CLD_OPD()
    nn_cld_opd.setup_inference(ckpt_dir_cld_opd)
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
    if pattern is not None:
        clvrx_ds = CLAVRx(clvrx_path, pattern=pattern)
    else:
        clvrx_ds = CLAVRx(clvrx_path)

tomrink's avatar
tomrink committed
    for pname, t_start, t_stop, dto in clvrx_ds:
tomrink's avatar
tomrink committed
        clvrx_str_time = dto.strftime('%Y-%m-%d_%H:%M')
tomrink's avatar
tomrink committed
        out_file = output_dir + 'cloud_fraction_opd_' + clvrx_str_time + '.nc'
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
        t0 = time.time()
tomrink's avatar
tomrink committed
        if full_disk:
tomrink's avatar
tomrink committed
            cld_frac = nn_cld_frac.run_inference_full_disk(pname, None)
tomrink's avatar
tomrink committed
            cld_opd, opd = nn_cld_opd.run_inference_full_disk(pname, None)
tomrink's avatar
tomrink committed
        else:
tomrink's avatar
tomrink committed
            cld_frac = nn_cld_frac.run_inference(pname, None)
tomrink's avatar
tomrink committed
            cld_opd, opd = nn_cld_opd.run_inference(pname, None)
tomrink's avatar
tomrink committed

        clr_cat = cld_frac == 0
        cld_cat = cld_frac == 4
tomrink's avatar
tomrink committed
        cld_opd[clr_cat] = opd[clr_cat]
        cld_opd[cld_cat] = opd[cld_cat]
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
        cld_opd[np.logical_and(cld_opd < 0.0, cld_opd > 160.0)] = -1.0
        cld_opd[np.isnan(opd)] = -1.0
tomrink's avatar
tomrink committed

tomrink's avatar
tomrink committed
        write_cld_prods_file_nc4(clvrx_str_time, out_file, cld_frac, cld_opd, x_rad, y_rad, None, None,
tomrink's avatar
tomrink committed
                                satellite=satellite, domain=domain, has_time=True)
tomrink's avatar
tomrink committed
        t1 = time.time()
        print('total time: ', (t1 - t0))
tomrink's avatar
tomrink committed
        print('Done: ', pname)