Ejemplo n.º 1
0
def cut_kernel(xpos, ypos, arr, dist, probs=False):


    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)

    vdic = {}

    for d in probs.data_vars:

        var = u_arrays.cut_kernel(probs[d].values,xpos, ypos,dist)
        vdic[d] = var

    cnt2 = np.zeros_like(kernel)
    cnt2[np.isfinite(vdic[list(vdic.keys())[0]])] = 1


    if (np.sum(np.isfinite(kernel)) < 2):
        return

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1


    if kernel.shape != (dist*2+1, dist*2+1):
        pdb.set_trace()


    return kernel,  cnt, cnt2, vdic
Ejemplo n.º 2
0
def cut_kernel(xpos, ypos, arr, date, lon, lat, t, dist, probs=False):


    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)


    t = u_arrays.cut_kernel(probs['t'].values,xpos, ypos,dist)
    t = t - np.nanmean(t)

    u = u_arrays.cut_kernel(probs['u950'].values,xpos, ypos,dist)
    v = u_arrays.cut_kernel(probs['v950'].values, xpos, ypos, dist)
    shear = u_arrays.cut_kernel(probs['shear'].values, xpos, ypos, dist)

    cnt2 = np.zeros_like(u)
    cnt2[np.isfinite(u)] = 1


    if (np.sum(np.isfinite(kernel)) < 2):
        return

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1



    if kernel.shape != (dist*2+1, dist*2+1):
        pdb.set_trace()


    return kernel,  cnt, cnt2, t, u, v, shear
def cut_kernel(xpos,
               ypos,
               arr,
               date,
               lon,
               lat,
               t,
               parallax=False,
               rotate=False,
               probs=False):

    if parallax:
        km, coords = u_gis.call_parallax_era(date.month, t, lon, lat, 0, 0)
        lx, ly = km

        lx = int(np.round(lx / 3.))
        ly = int(np.round(ly / 3.))  # km into pixels
        xpos = xpos - lx
        ypos = ypos - ly

    dist = 200

    kernel = u_arrays.cut_kernel(arr, xpos, ypos, dist)

    if np.sum(probs) > 0:
        prob = u_arrays.cut_kernel(probs, xpos, ypos, dist)
        cnt2 = np.zeros_like(kernel)
        cnt2[np.isfinite(prob)] = 1
    else:
        prob = np.zeros_like(kernel)
        cnt2 = np.zeros_like(kernel)

    if rotate:
        kernel = u_met.era_wind_rotate(kernel,
                                       date,
                                       lat,
                                       lon,
                                       level=700,
                                       ref_angle=90)

    if (np.sum(np.isfinite(kernel)) < 2):
        return

    kernel3 = kernel - np.nanmean(kernel)

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1

    if kernel.shape != (dist * 2 + 1, dist * 2 + 1):
        pdb.set_trace()

    return kernel, kernel3, cnt, prob, cnt2
Ejemplo n.º 4
0
def cut_kernel(xpos, ypos, expos, eypos, arr, dist, probs=False):


    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)

    edist = int(dist/10)

    vdic = {}
    cnt2 = 0

    for d in probs.data_vars:
        #print(d)
        var = u_arrays.cut_kernel_3d(probs[d].values,expos, eypos,edist)
        var = np.mean(var[:,:, edist-1:edist+2], axis=2)
        #var = np.mean(var[:, edist - 1:edist + 2, :], axis=1)
        vdic[d] = var


    cnt2 = np.zeros_like(vdic[list(vdic.keys())[0]])
    cnt2[np.isfinite(vdic[list(vdic.keys())[0]])] = 1

    if (np.sum(np.isfinite(kernel)) < 2):
        return

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1


    return kernel,  cnt, cnt2, vdic
Ejemplo n.º 5
0
def cut_kernel(xpos, ypos, arr, date, lon, lat, t, parallax=False, rotate=False):

    if parallax:
        km, coords = u_gis.call_parallax_era(date.month, t, lon, lat, 0, 0)
        lx, ly = km

        lx = int(np.round(lx / 27.5))
        ly = int(np.round(ly / 27.5))  # km into pixels
        xpos = xpos - lx
        ypos = ypos - ly

    dist = 11

    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)

    if rotate:
        kernel = u_met.era_wind_rotate(kernel,date.month,lat,lon,level=700, ref_angle=90)

    # if (np.sum(np.isfinite(kernel)) < 0.10 * kernel.size):
    #     return

    kernel3 = kernel - np.nanmean(kernel)

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1

    if kernel.shape != (dist*2+1, dist*2+1):
        return None

    return kernel, kernel3, cnt
Ejemplo n.º 6
0
def cut_kernel(xpos, ypos, expos, eypos, arr, dist, probs=False):

    kernel = u_arrays.cut_kernel(arr, xpos, ypos, dist)

    edist = int(dist / 10)

    vdic = {}
    cnt2 = 0

    for d in probs.data_vars:
        #print(d)
        var = u_arrays.cut_kernel_3d(probs[d].values, expos, eypos, edist)
        var = np.mean(var[:, :, edist - 1:edist + 2], axis=2)
        #var = np.mean(var[:, edist - 1:edist + 2, :], axis=1)
        vdic[d] = var

    cnt2 = np.zeros_like(vdic[list(vdic.keys())[0]])
    cnt2[np.isfinite(vdic[list(vdic.keys())[0]])] = 1

    if (np.sum(np.isfinite(kernel)) < 2):
        return

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1

    return kernel, cnt, cnt2, vdic
Ejemplo n.º 7
0
def cut_kernel(xpos, ypos, arr, date, lon, lat, t, parallax=False, rotate=False, probs=False):

    if parallax:
        km, coords = u_gis.call_parallax_era(date.month, t, lon, lat, 0, 0)
        lx, ly = km

        lx = int(np.round(lx / 3.))
        ly = int(np.round(ly / 3.))  # km into pixels
        xpos = xpos - lx
        ypos = ypos - ly

    dist = 200

    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)

    vdic = {}

    for d in probs.data_vars:

        var = u_arrays.cut_kernel(probs[d].values,xpos, ypos,dist)
        vdic[d] = var

    cnt2 = np.zeros_like(vdic[list(vdic.keys())[0]])
    cnt2[np.isfinite(vdic[list(vdic.keys())[0]])] = 1

    if rotate:
        kernel = u_met.era_wind_rotate(kernel,date,lat,lon,level=700, ref_angle=90)

    if (np.sum(np.isfinite(kernel)) < 2):
        return

    kernel3 = kernel - np.nanmean(kernel)

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1



    if kernel.shape != (dist*2+1, dist*2+1):
        pdb.set_trace()



    return kernel, kernel3, cnt, cnt2, vdic
def cut_kernel(xpos, ypos, arr, dist, probs=False, probs2=False):

    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)

    vdic = {}

    for d in probs.data_vars:

        var = u_arrays.cut_kernel(probs[d].values,xpos, ypos,dist)
        vdic[d] = var

    cnt2 = np.zeros_like(kernel)
    cnt2[np.isfinite(vdic[list(vdic.keys())[0]])] = 1


    if (np.sum(np.isfinite(kernel)) < 2):
        return

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1


    if kernel.shape != (dist*2+1, dist*2+1):
        print('Kernels shape wrong!')
        ipdb.set_trace()

    kernel = kernel - np.nanmean(kernel)


    if np.nansum(probs2) > 0:
        prob = u_arrays.cut_kernel(probs2,xpos, ypos,dist)
        cnt3 = np.zeros_like(kernel)
        cnt3[np.isfinite(prob)] = 1

    else:
        prob = np.zeros_like(kernel)
        cnt3 = np.zeros_like(kernel)


    return kernel,  cnt, cnt2, vdic, prob, cnt3
Ejemplo n.º 9
0
def cut_kernel_lsta(xpos, ypos, arr, date, lat, lon):

    dist = 100

    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)

    if (np.sum(np.isfinite(kernel)) < 0.01 * kernel.size):
        return

    if kernel.shape != (2*dist+1, 2*dist+1):
        return

    return kernel
Ejemplo n.º 10
0
def cut_kernel(xpos, ypos, arr, date, lon, lat, t, parallax=False,  probs=False):

    if parallax:
        km, coords = u_gis.call_parallax_era(date.month, t, lon, lat, 0, 0)
        lx, ly = km

        lx = int(np.round(lx / 3.))
        ly = int(np.round(ly / 3.))  # km into pixels
        xpos = xpos - lx
        ypos = ypos - ly

    dist = 200

    kernel = u_arrays.cut_kernel(arr,xpos, ypos,dist)

    if np.nansum(probs) > 0:
        prob = u_arrays.cut_kernel(probs,xpos, ypos,dist)
        cnt2 = np.zeros_like(kernel)
        cnt2[np.isfinite(prob)] = 1

    else:
        prob = np.zeros_like(kernel)
        cnt2 = np.zeros_like(kernel)

    if (np.sum(np.isfinite(kernel)) < 2):
        return

    kernel3 = kernel - np.nanmean(kernel)

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1

    if kernel.shape != (dist*2+1, dist*2+1):
        pdb.set_trace()

    return kernel, kernel3, cnt, prob, cnt2
Ejemplo n.º 11
0
def cut_kernel(xpos,
               ypos,
               arr,
               date,
               lon,
               lat,
               t,
               parallax=False,
               rotate=False):

    if parallax:
        km, coords = u_gis.call_parallax_era(date.month, t, lon, lat, 0, 0)
        lx, ly = km

        lx = int(np.round(lx / 3.))
        ly = int(np.round(ly / 3.))  # km into pixels
        xpos = xpos - lx
        ypos = ypos - ly

    dist = 100

    kernel = u_arrays.cut_kernel(arr, xpos, ypos, dist)

    if rotate:
        kernel = u_met.era_wind_rotate(kernel,
                                       date,
                                       lat,
                                       lon,
                                       level=700,
                                       ref_angle=90)

    if (np.sum(np.isfinite(kernel)) < 0.10 * kernel.size):
        return

    kernel3 = kernel - np.nanmean(kernel)

    cnt = np.zeros_like(kernel)
    cnt[np.isfinite(kernel)] = 1

    if kernel.shape != (201, 201):
        return

    return kernel, kernel3, cnt
Ejemplo n.º 12
0
def cut_box(arr, xpos=None, ypos=None, dist=None):
    """

    :param xpos: x coordinate in domain for kernel centre point
    :param ypos: y coordinate in domain for kernel centre point
    :param arr: numpy array (2d)
    :param dist: distance from kernel centre point to kernel edge (total width = 2*dist+1)
    :return: the kernel of dimensions (2*dist+1, 2*dist+1)
    """

    if dist == None:
        'Distance missing. Please provide distance from kernel centre to edge (number of pixels).'
        return

    if arr.ndim == 2:
        kernel = ua.cut_kernel(arr.values,xpos, ypos,dist)
        if kernel.shape != (dist * 2 + 1, dist * 2 + 1):
            print("Please check kernel dimensions, there is something wrong")
            ipdb.set_trace()
    elif arr.ndim == 3:
        kernel = ua.cut_kernel_3d(arr.values,xpos, ypos,dist)

        if kernel.shape != (arr.shape[0], dist * 2 + 1, dist * 2 + 1):
            print("Please check kernel dimensions, there is something wrong")
            ipdb.set_trace()
    else:
        print('Dimension of array not supported, please check')
        ipdb.set_trace()

    if arr.ndim == 3:
        try:
            levels = arr.level.values
        except AttributeError:
            levels = arr.pressure.values

        return xr.DataArray(kernel, dims=['level','y','x'],
                            coords={'level' : arr.level.values})
    else:
        return xr.DataArray(kernel, dims=['y','x'])
Ejemplo n.º 13
0
def cut_kernel_lsta(xpos, ypos, arr, h):
    dist = 200

    kernel = ua.cut_kernel(arr, xpos, ypos, dist)

    if (np.sum(np.isfinite(kernel)) < 0.01 * kernel.size):
        return

    kmean = kernel

    if kernel.shape != (2 * dist + 1, 2 * dist + 1):
        print('WRONG SHAPE')
        return
    ycirc30, xcirc30 = ua.draw_circle(dist + 1, dist + 1, 6)  # 15km radius
    k30 = np.nanmean(kmean[ycirc30, xcirc30])

    ycirc100e, xcirc100e = ua.draw_circle(
        dist + 51, dist + 1, 25)  # at - 150km, draw 50km radius circle
    data = kmean[ycirc100e, xcirc100e]

    #     if h > 17:
    #         x2 = 95 # 285km upstream
    #     else:
    #         x2 = 80 # 240km
    #     data = kernel[dist - 10:dist + 10, dist+8:dist + x2]

    e100 = np.nanmean(data)

    #     if (np.sum(np.isfinite(data)) / data.size) < 0.01:
    #         print('Too small valid area')
    #         return

    if np.isnan(e100):
        print('Is nan')
        return

    return k30, e100, kernel
Ejemplo n.º 14
0
def cut_kernel(ypos, xpos, da, shift):

    pixshift = shift
    xpos = xpos+pixshift

    mdist = 2

    try:
        kernel = ua.cut_kernel(da,xpos, ypos,mdist)
    except ValueError:
        #ipdb.set_trace()
        return

    if kernel.shape != (mdist*2+1, mdist*2+1):
        print(kernel.shape)
        return
    try:
        lpoint = np.nanmean(kernel)
    except ValueError:
        ipdb.set_trace()
        lpoint = np.nan
        return lpoint

    return lpoint
Ejemplo n.º 15
0
def file_loop(f):
    print('Doing file: ' + f)
    dic = xr.open_dataset(f)
    out = dictionary()

    outt = dic['lw_out_PBLtop'].values
    try:
        outp = dic['lsRain'].values
    except KeyError:
        outp = dic['totRain'].values
    outu_srfc = dic['u_srfc'].values
    outu_mid = dic['u_mid'].values
    outshear = dic['shear'].values
    outq = dic['q_srfc'].values
    tmid = dic['t_mid'].values
    tsrfc = dic['t_srfc'].values

    pes = units.Quantity(650, 'hPa')
    tes = units.Quantity(tmid + 273.15, 'K')

    theta_low = u_met.theta_e(925, tsrfc, outq / 1000.)

    theta_es = calc.saturation_equivalent_potential_temperature(pes, tes)

    theta = theta_low - (np.array(theta_es) - 273.15)
    tcwv = dic['tcwv'].values
    tgrad = dic.attrs['Tgrad']
    tdiff = dic.attrs['Tgradbox']

    #print(theta)

    #ipdb.set_trace()

    out['lon'] = dic['longitude'].values
    out['lat'] = dic['latitude'].values
    out['hour'] = dic['time.hour'].item()
    out['month'] = dic['time.month'].item()
    out['year'] = dic['time.year'].item()
    out['date'] = dic['time'].values

    t_thresh = -50  # -40C ~ 167 W m-2
    mask = np.isfinite(outp) & (
        outt <= t_thresh) & np.isfinite(outq) & np.isfinite(outshear)
    antimask = ~mask

    for var in [
            outt, outp, outu_srfc, outu_mid, outshear, outq, theta, tmid, tsrfc
    ]:
        var[antimask] = np.nan

    if np.sum(mask) < 3:
        print('NOT ENOUGH IN MASK')
        return

    maxpos = np.unravel_index(np.nanargmax(outp), outp.shape)
    minpos = np.unravel_index(np.nanargmin(outt), outt.shape)

    out['area'] = np.sum((outt <= t_thresh))

    out['clat'] = np.min(out['lat']) + (
        (np.max(out['lat']) - np.min(out['lat'])) * 0.5)
    out['clon'] = np.min(out['lon']) + (
        (np.max(out['lon']) - np.min(out['lon'])) * 0.5)

    out['tmin'] = np.nanmin(outt)
    out['tmean'] = np.mean(outt[mask])
    out['tgrad'] = tgrad
    out['tdiff'] = tdiff

    out['pmax'] = np.nanmean(ua.cut_kernel(outp, maxpos[1], maxpos[0],
                                           1))  # degrade rainfall to 30km

    #out['pmax'] = np.max(outp[mask]) # rain at 4.4km
    out['pmean'] = np.mean(outp[mask])
    out['qmax'] = np.nanmean(ua.cut_kernel(
        outq, minpos[1], minpos[0], 3))  #np.max(outq[mask]) #30km q and shear

    out['qmean'] = np.nanmean(outq[mask])
    out['umax_srfc'] = np.max(outu_srfc[mask])
    out['umean_srfc'] = np.nanmean(outu_srfc[mask])
    out['umin_mid'] = np.min(outu_mid[mask])
    out['umean_mid'] = np.mean(outu_mid[mask])

    out['shearmin'] = np.nanmean(
        ua.cut_kernel(outshear, minpos[1], minpos[0], 3))

    out['shearmean'] = np.mean(outu_mid[mask]) - np.nanmean(
        outu_srfc[mask])  #np.mean(outshear[mask])
    #out['thetamax'] = np.max(theta[mask])

    out['thetamax'] = np.nanmean(ua.cut_kernel(theta, minpos[1], minpos[0], 3))
    out['tcwv'] = np.nanmean(ua.cut_kernel(tcwv, minpos[1], minpos[0], 3))
    out['tcwvmean'] = np.mean(tcwv[mask])

    out['thetamean'] = np.mean(theta[mask])
    out['tmidmax'] = np.max(tmid[mask])
    out['tmidmean'] = np.mean(tmid[mask])
    out['tsrfcmax'] = np.max(tsrfc[mask])
    out['tsrfcmean'] = np.mean(tsrfc[mask])

    out['pgt30'] = np.sum(outp[mask] > 30)
    out['isvalid'] = np.sum(mask)
    out['pgt01'] = np.sum(outp[mask] > 0.1)

    out['p'] = outp[mask]
    out['t'] = outt[mask]
    out['q'] = outq[mask]
    out['u_mid'] = outu_mid[mask]
    out['u_srfc'] = outu_srfc[mask]
    out['shear'] = outshear[mask]

    dic.close()

    print(out['pmax'])

    return out
Ejemplo n.º 16
0
def file_loop(f):
    print('Doing file: ' + f)
    dic = xr.open_dataset(f)

    tag = 'fut'

    mfiles = '/home/ck/DIR/cornkle/data/CP4/CLOVER/CP4_monthly_' + tag + '/'

    y = dic['time.year'].values
    m = dic['time.month'].values

    out = dictionary()

    outt = dic['lw_out_PBLtop'].values
    try:
        outp = dic['lsRain'].values
    except KeyError:
        outp = dic['totRain'].values

    out['lon'] = dic['longitude'].values
    out['lat'] = dic['latitude'].values
    out['hour'] = dic['time.hour'].item()
    out['month'] = dic['time.month'].item()
    out['year'] = dic['time.year'].item()
    out['date'] = dic['time'].values

    t_thresh = -50  # -40C ~ 167 W m-2

    maxpos = np.unravel_index(np.nanargmax(outp), outp.shape)
    minpos = np.unravel_index(np.nanargmin(outt), outt.shape)
    #ipdb.set_trace()
    latpick = out['lat'][minpos[0]]
    lonpick = out['lon'][minpos[1]]

    out['area'] = np.sum((outt <= t_thresh))

    out['clat'] = np.min(out['lat']) + (
        (np.max(out['lat']) - np.min(out['lat'])) * 0.5)
    out['clon'] = np.min(out['lon']) + (
        (np.max(out['lon']) - np.min(out['lon'])) * 0.5)

    if (out['clat'] < 9) | (out['clon'] < -15) | (out['clon'] > 15):
        print('MCS out of box')
        return

    tcwv = xr.open_dataarray(mfiles + 'tcwv/tcwv_monthly_12UTC_0.7deg_' + tag +
                             '_4km_' + str(y) + str(m).zfill(2) + '.nc')
    tpl = xr.open_dataarray(mfiles + 't_pl/t_pl_monthly_12UTC_0.7deg_' + tag +
                            '_4km_' + str(y) + str(m).zfill(2) + '.nc')
    uu = xr.open_dataarray(mfiles + 'u_pl/u_pl_monthly_12UTC_0.7deg_' + tag +
                           '_4km_' + str(y) + str(m).zfill(2) + '.nc')
    qq = xr.open_dataarray(mfiles + 'q_pl/q_pl_monthly_12UTC_0.7deg_' + tag +
                           '_4km_' + str(y) + str(m).zfill(2) + '.nc')

    tcwvout = tcwv.sel(latitude=latpick,
                       longitude=lonpick,
                       method='nearest',
                       tolerance=0.7)
    tt = tpl.sel(latitude=latpick,
                 longitude=lonpick,
                 method='nearest',
                 tolerance=0.7)
    uu = uu.sel(latitude=latpick,
                longitude=lonpick,
                method='nearest',
                tolerance=0.7)
    qq = qq.sel(latitude=latpick,
                longitude=lonpick,
                method='nearest',
                tolerance=0.7)

    outu_srfc = uu.sel(pressure=925).values
    outu_mid = uu.sel(pressure=650).values
    outshear = uu.sel(pressure=650).values - uu.sel(pressure=925).values
    outq = qq.sel(pressure=925).values / 100
    tmid = tt.sel(pressure=650).values / 100
    tsrfc = tt.sel(pressure=925).values / 100

    if np.isnan(tsrfc):
        print('TSRFC IS NAN, RETURN!!!')
        return

    pes = units.Quantity(650, 'hPa')
    tes = units.Quantity(tmid + 273.15, 'K')
    #ipdb.set_trace()
    theta_low = u_met.theta_e(925, tsrfc, outq / 1000.)

    theta_es = calc.saturation_equivalent_potential_temperature(pes, tes)

    theta = theta_low - (np.array(theta_es) - 273.15)

    tgrad = dic.attrs['Tgrad']
    tdiff = dic.attrs['Tgradbox']

    print(theta)

    #ipdb.set_trace()

    mask = np.isfinite(outp) & (outt <= t_thresh)
    antimask = ~mask

    if np.sum(mask) < 3:
        return

    for var in [outt, outp]:
        var[antimask] = np.nan

    out['tmin'] = np.nanmin(outt)
    out['tmean'] = np.mean(outt[mask])
    out['tgrad'] = tgrad
    out['tdiff'] = tdiff

    out['pmax'] = np.nanmean(ua.cut_kernel(outp, maxpos[1], maxpos[0],
                                           1))  # degrade rainfall to 30km

    out['pmean'] = np.mean(outp[mask])
    out['qmax'] = outq  #np.max(outq[mask]) #30km q and shear

    out['umax_srfc'] = outu_srfc
    out['umin_mid'] = outu_mid
    out['shearmin'] = outshear

    out['thetamax'] = theta
    out['tcwv'] = tcwvout.values

    out['tmidmax'] = tmid
    out['tsrfcmax'] = tsrfc

    out['pgt30'] = np.sum(outp[mask] > 30)
    out['isvalid'] = np.sum(mask)
    out['pgt01'] = np.sum(outp[mask] > 0.1)

    out['p'] = outp[mask]
    out['t'] = outt[mask]

    dic.close()

    #ipdb.set_trace()

    return out
Ejemplo n.º 17
0
def file_loop(f):
    print('Doing file: ' + f)

    dic = xr.open_dataset(f)
    edate = pd.Timestamp(dic.time.values)

    if np.nanmin(dic['tc_lag0'].values) > -53:
        return

    outt = dic['tc_lag0'].values
    outp = dic['p'].values

    tminpos = np.where(dic['tc_lag0'].values == np.nanmin(
        dic['tc_lag0'].values))  # era position close to min temp
    if len(tminpos[0]) > 1:
        ptmax = np.nanmax((dic['p'].values)[tminpos])
        if ptmax > 0:
            prpos = np.where((dic['p'].values)[tminpos] == ptmax)
            tminpos = ((tminpos[0])[prpos], (tminpos[1])[prpos])
        else:
            tminpos = ((tminpos[0])[0], (tminpos[1])[0])

    elon = dic['lon'].values[tminpos]
    elat = dic['lat'].values[tminpos]

    out = dictionary()
    out['lon'] = dic['lon'].values
    out['lat'] = dic['lat'].values
    out['hour'] = dic['time.hour'].item()
    out['month'] = dic['time.month'].item()
    out['year'] = dic['time.year'].item()
    out['date'] = dic['time'].values

    out['clat'] = np.min(out['lat']) + (
        (np.max(out['lat']) - np.min(out['lat'])) * 0.5)
    out['clon'] = np.min(out['lon']) + (
        (np.max(out['lon']) - np.min(out['lon'])) * 0.5)

    if (out['clat'] < 9) | (out['clon'] < -15) | (out['clon'] > 15):
        print('MCS out of box')
        return

    # if (edate.hour < 16):
    #     return

    try:
        #era_pl = xr.open_dataset('/home/ck/DIR/mymachine/ERA5/monthly/synoptic/pl_2004-2019_monthlyCLIM_synop_07x07.nc')
        era_pl = xr.open_dataset(
            '/home/ck/DIR/mymachine/ERA5/monthly/synoptic/pl_1979-2019_monthly_synop_07x07.nc'
        )
    except:
        print('ERA5 pl missing')
        return
    try:
        #era_srfc = xr.open_dataset('/home/ck/DIR/mymachine/ERA5/monthly/synoptic/srfc_2004-2019_monthlyCLIM_synop_07x07.nc')
        era_srfc = xr.open_dataset(
            '/home/ck/DIR/mymachine/ERA5/monthly/synoptic/srfc_1979-2019_monthly_synop_07x07.nc'
        )
    except:
        print('ERA5 srfc missing')
        return
    era_pl = uda.flip_lat(era_pl)
    era_srfc = uda.flip_lat(era_srfc)

    edate = edate.replace(hour=12, minute=0, day=1)

    era_pl_day = era_pl.sel(time=edate,
                            longitude=slice(-16, 17),
                            latitude=slice(
                                4, 26))  #.groupby('time.month').mean('time')
    era_srfc_day = era_srfc.sel(time=edate,
                                longitude=slice(-16, 17),
                                latitude=slice(
                                    4,
                                    26))  #.groupby('time.month').mean('time')

    # try:
    #     era_day = era.isel(time=int(getera[0]))
    # except TypeError:
    #     print('Era missing')
    #     return

    res = []

    try:
        era_day = era_pl_day.sel(latitude=elat,
                                 longitude=elon,
                                 method='nearest',
                                 tolerance=0.7)  # take point of minimum T
    except:
        return
    era_day_srfc = era_srfc_day.sel(latitude=elat,
                                    longitude=elon,
                                    method='nearest',
                                    tolerance=0.7)  # take point of minimum T

    del era_srfc_day

    e925 = era_day.sel(level=925).mean()

    e850 = era_pl_day['t'].sel(level=850)
    elow = era_day.sel(level=slice(925, 850)).mean('level').mean()
    e650 = era_day.sel(level=650).mean()
    emid = era_day.sel(level=slice(600, 700)).mean('level').mean()
    srfc = era_day_srfc.mean()

    t_thresh = -50  # -40C ~ 167 W m-2
    mask = np.isfinite(outp) & (outt <= t_thresh) & np.isfinite(outt)
    mask_area = (outt <= t_thresh) & np.isfinite(outt)
    mask70 = (outt <= -70) & np.isfinite(outt)

    if np.sum(mask) < 3:
        return

    print(
        np.nanmax(outt[mask])
    )  # can be bigger than cutout threshold because of interpolation to 5km grid after cutout

    out['area'] = np.sum(mask_area)
    out['area70'] = np.sum(mask70)

    out['clat'] = np.min(out['lat']) + (
        (np.max(out['lat']) - np.min(out['lat'])) * 0.5)
    out['clon'] = np.min(out['lon']) + (
        (np.max(out['lon']) - np.min(out['lon'])) * 0.5)

    out['tmin'] = np.min(outt[mask])
    out['tmean'] = np.mean(outt[mask])

    maxpos = np.unravel_index(np.nanargmax(outp), outp.shape)
    out['pmax'] = np.nanmean(ua.cut_kernel(outp, maxpos[1], maxpos[0],
                                           1))  #np.max(outp[mask])
    out['pmean'] = np.mean(outp[mask])

    dbox = e850.copy(deep=True)
    minlon = era_pl_day.sel(latitude=8,
                            longitude=np.min(out['lon']),
                            method='nearest')
    maxlon = era_pl_day.sel(latitude=8,
                            longitude=np.max(out['lon']),
                            method='nearest')

    del era_pl_day

    tgrad = dbox.sel(longitude=slice(
        minlon.longitude.values, maxlon.longitude.values)).mean('longitude')

    tmin = np.nanargmin(tgrad.values)
    tmax = np.nanargmax(tgrad.values)
    tgrad = tgrad.isel(latitude=slice(tmin, tmax))

    lingress = uda.linear_trend_lingress(tgrad)

    out['Tgrad'] = lingress['slope'].values

    tgrad2 = dbox.sel(longitude=slice(np.min(out['lon']), np.max(out['lon'])), latitude=slice(10, 20)).mean(
        ['longitude', 'latitude']) - \
             dbox.sel(longitude=slice(np.min(out['lon']), np.max(out['lon'])), latitude=slice(5, 7)).mean(['longitude', 'latitude'])
    out['Tgradbox'] = tgrad2.values

    try:
        out['q925'] = float(e925['q'])
    except TypeError:
        return

    out['q650'] = float(e650['q'])
    out['v925'] = float(e925['v'])
    out['v650'] = float(e925['v'])
    out['u925'] = float(e925['u'])
    out['u650'] = float(e650['u'])
    out['w925'] = float(e925['w'])
    out['w650'] = float(e650['w'])
    out['rh925'] = float(e925['r'])
    out['rh650'] = float(e650['r'])
    out['t925'] = float(e925['t'])
    out['t650'] = float(e650['t'])
    # out['pv925'] = float(e925['pv'])
    # out['pv650'] = float(e650['pv'])
    out['div925'] = float(e925['d'])
    out['div650'] = float(e650['d'])
    out['q_low'] = float(elow['q'])
    out['q_mid'] = float(emid['q'])
    out['tcwv'] = float(srfc['tcwv'])

    out['shear'] = float(e650['u'] - e925['u'])

    theta_down = u_met.theta_e(925, e925['t'] - 273.15, e925['q'])
    theta_up = u_met.theta_e(650, e650['t'] - 273.15, e650['q'])

    out['dtheta'] = (theta_down - theta_up).values
    out['thetaup'] = theta_up.values
    out['thetadown'] = theta_down.values

    out['pgt30'] = np.sum(outp[mask] >= 30)
    out['isvalid'] = np.sum(mask)
    out['pgt01'] = np.sum(outp[mask] >= 0.1)
    #
    out['p'] = outp[mask]
    out['t'] = outt[mask]
    #ipdb.set_trace()
    dic.close()

    del era_day
    del era_day_srfc
    del era_pl
    del era_srfc

    return out