def file_loop(passit):

    ds = xr.open_dataset(passit)

    dataset = passit[6:9]

    ds['tir'].values = ds['tir']

    bloblist = []
    tirlist = []
    lat = ds.lat
    lon = ds.lon

    for ids, day in enumerate(ds['tir']):

        print('id', ids)

        date = day.time
        day.values = day / 100
        if np.sum(day.values) == 0:
            continue
        img, nogood, t_thresh_size, t_thresh_cut, pix_nb = powerBlob_utils.filter_img(day.values, 5)

        power = util.waveletT(img, dataset='METEOSAT5K_vera')
        power_out = powerBlob_utils.find_scales_dominant(power, nogood, dataset=dataset)

        if power_out is None:
            continue

        new_savet = (day.values*100).astype(np.int16)
        bloblist.append(xr.DataArray(power_out.astype(np.int16), coords={'time': date, 'lat': lat, 'lon': lon},
                                     dims=['lat', 'lon']))  # [np.newaxis, :])
        tirlist.append(xr.DataArray(new_savet, coords={'time': date, 'lat': lat, 'lon': lon}, dims=['lat', 'lon']))

    ds_mfg = xr.Dataset()
    ds_mfg['blobs'] = xr.concat(bloblist, 'time')
    ds_mfg['tir'] = xr.concat(tirlist, 'time')
    ds_mfg.sel(lat=slice(5, 12), lon=slice(-13, 13))


    savefile = passit.replace('cores_', 'coresPower_')
    try:
        os.remove(savefile)
    except OSError:
        pass

    comp = dict(zlib=True, complevel=5)
    enc = {var: comp for var in ds_mfg.data_vars}

    ds_mfg.to_netcdf(path=savefile, mode='w', encoding=enc, format='NETCDF4')

    print('Saved ' + savefile)
Esempio n. 2
0
def _timeLoop(timeslice, inds_inter, weights_inter, shape_inter, tag):

    try:

        outt = u_int.interpolate_data(timeslice.values, inds_inter, weights_inter, shape_inter)
        #print('Interpolated', id)
    except ValueError:
        print('Interpolation value error!!')
        return

    outt_out = outt.copy()

    if np.sum(outt) == 0:   # if temperature is all empty
        print('Temperature empty, continue')
        return

    outt, nogood, t_thresh_size, t_thresh_cut, pix_nb = powerBlob_utils.filter_img(outt, 5)

    wav = util.waveletT(outt, dataset='METEOSAT5K_vera')
    outt[nogood] = np.nan

    power_msg = powerBlob_utils.find_scales_dominant(wav, nogood, dataset=tag)
    if power_msg is None:  # if power calculation failed
        print('Power calc fail, continue')
        return

    hour = timeslice['time.hour']
    minute = timeslice['time.minute']
    day = timeslice['time.day']
    month = timeslice['time.month']
    year = timeslice['time.year']

    date = dt.datetime(year, month, day, hour, minute)

    isnan = np.isnan(outt)
    outt[isnan] = 0
    new_savet = (outt_out * 100).astype(np.int16)

    return date, power_msg.astype(np.int16), new_savet, outt, nogood, t_thresh_size, t_thresh_cut, pix_nb, wav
def file_loop(fi):
    ret = []

    print('Doing file: ' + fi)

    dic = xr.open_dataset(fi)

    id = fi.split('/')[-1]

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

    lon = dic['lon'].values
    lat = dic['lat'].values

    outt[np.isnan(outt)] = 150
    outt[outt >= -40] = 150
    grad = np.gradient(outt)
    outt[outt == 150] = np.nan
    outp[np.isnan(outt)] = np.nan
    outpc[np.isnan(outt)] = np.nan

    area = np.nansum(outt <= -40)
    try:
        bulk_pmax = np.max(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    except ValueError:
        return ret
    try:
        bulk_pmin = np.min(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    except ValueError:
        return ret

    if (area * 25 < 15000) or (area * 25 > 800000) or (bulk_pmax > 200) or (bulk_pmin < 0):
        print(area * 25)
        print('throw out')
        return

    perc = np.percentile(outt[np.isfinite(outt)], 60)  # 60
    #perc = -60

    clat = np.min(dic.lat.values) + ((np.max(dic.lat.values) - np.min(dic.lat.values)) * 0.5)
    clon = np.min(dic.lon.values) + ((np.max(dic.lon.values) - np.min(dic.lon.values)) * 0.5)

    if (clon > 28) or (clon < -17.2) or (clat < 4.1):
        return

    lat_min = np.min(dic.lat.values)
    lat_max = np.max(dic.lat.values)
    lon_min = np.min(dic.lon.values)
    lon_max = np.max(dic.lon.values)

    bulk_tmean = np.nanmean(outt)
    bulk_tmin_p = np.min(outt[(np.isfinite(outp)) & (np.isfinite(outt))])
    bulk_tmean_p = np.mean(outt[(np.isfinite(outp)) & (np.isfinite(outt))])
    bulk_pmean = np.max(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    bulk_g30 = np.sum(outp[(np.isfinite(outp)) & (np.isfinite(outt))] >= 30)


    o2 = outt.copy()
    o2[np.isnan(o2)] = perc
    nok = np.where(abs(grad[0]) > 80)
    d = 2
    i = nok[0]
    j = nok[1]

    for ii, jj in zip(i, j):
        kern = o2[ii - d:ii + d + 1, jj - d:jj + d + 1]
        o2[ii - d:ii + d + 1, jj - d:jj + d + 1] = ndimage.gaussian_filter(kern, 3, mode='nearest')

    wav = util.waveletT(o2, 5)

    arr = np.array(wav['scales'], dtype=str)
    arrf = np.array(wav['scales'], dtype=float)

    scale_ind = range(arr.size)

    figure = np.zeros_like(outt)

    wll = wav['t']
    #maxs = np.zeros_like(wll)

    yyy = []
    xxx = []
    scal = []
    for nb in scale_ind[::-1]:

        orig = float(arr[nb])
        print(np.round(orig))
        wl = wll[nb, :, :]

        maxout = (
            wl == ndimage.maximum_filter(wl, (5, 5), mode='constant', cval=np.amax(wl) + 1))  # (np.round(orig / 5))

        try:
            yy, xx = np.where((maxout == 1) & (outt <= -40) & (wl >= np.percentile(wl[wl >= 0.5], 90)) & (wl > orig ** .5))
        except IndexError:
            continue

        for y, x in zip(yy, xx):

            ss = orig
            iscale = (np.ceil(ss / 2. / 5.)).astype(int)
            # if ss <= 20:
            #     iscale = iscale + 1

            ycirc, xcirc = ua.draw_cut_circle(x, y, iscale, outt)

            figure[ycirc, xcirc] = np.round(orig)
            xxx.append(x)
            yyy.append(y)
            scal.append(orig)

    figure[np.isnan(outt)] = 0
    figure[np.isnan(outp)] = 0

    circle_val_all = np.sum(figure > 0)

    xx = []
    yy = []

    for y, x, sc in zip(yyy[::-1], xxx[::-1], scal[::-1]):

        if figure[y, x] == 0:
            continue

        # if sc < 150:
        #     continue

        xx.append(x)
        yy.append(y)

        int_sc = np.round(sc)
        radius = sc
        iscale = (np.ceil(radius / 2. / 5.)).astype(int)
        # if int_sc <= 20:
        #     iscale = iscale + 1
        ycircf, xcircf = ua.draw_cut_circle(x, y, iscale, outt)

        pos = np.where((figure[ycircf, xcircf] == int_sc))
        if len(pos[0]) <= 3:
            continue

        circle_Tcenter = outt[y, x]
        t_para = np.nanmean(outt[ycircf[pos], xcircf[pos]])


        # if sc < 90:
        #     km, coords = u_gis.call_parallax_era(int(dic['time.month']), t_para, lon[y,x], lat[y,x], 0, 0)
        #     lx, ly = km
        #     lx = int(np.round(lx/5.))
        #     ly = int(np.round(ly/5.))  # km into pixels
        # else:
        lx = 0
        ly = 0

        if (int_sc >= 90):
            # outp[ycircf[pos] - ly, xcircf[pos] - lx] = 1000
            # outt[ycircf[pos], xcircf[pos]] = 1000
            ppos = np.where(outp >= 30)

            outt[np.isnan(outt)] = -40
            f = plt.figure()
            # plt.imshow(outp, cmap='jet', origin='lower')
            # f = plt.figure()
            # plt.imshow(outt, cmap='jet', origin='lower')
            # f = plt.figure()
            plt.imshow(outt, cmap='jet', origin='lower')

            plt.contour(outp, cmap='viridis', vmin=20)
            figure[figure<15] = np.nan
            plt.contourf(figure, cmap='Reds', vmin=9)
            plt.plot(ppos[1], ppos[0], 'ro')


        r = 20
        kernel = tm_utils.cut_kernel(outp, x-lx, y-ly, r)
        kernelt = tm_utils.cut_kernel(outt, x, y, r)

        if kernel.shape != (r * 2 + 1, r * 2 + 1):
            kernel = np.zeros((41, 41)) + np.nan
        if kernelt.shape != (r * 2 + 1, r * 2 + 1):
            kernelt = np.zeros((41, 41)) + np.nan

        circle_p = outp[ycircf[pos]-ly, xcircf[pos]-lx]
        #outp[ycircf[pos] - ly, xcircf[pos] - lx] = np.nan
        circle_pc = outpc[ycircf[pos]-ly, xcircf[pos]-lx]
        #outpc[ycircf[pos] - ly, xcircf[pos] - lx] = np.nan
        circle_t = outt[ycircf[pos], xcircf[pos]]
        circle_valid = np.sum(np.isfinite(circle_p))

        #
        #
        # if (int_sc >= 150) | (int_sc == 15):
        #     # outp[ycircf[pos] - ly, xcircf[pos] - lx] = 1000
        #     # outt[ycircf[pos], xcircf[pos]] = 1000
        #     f = plt.figure()
        #     plt.imshow(outp, cmap='jet')
        #     f = plt.figure()
        #     plt.imshow(outt, cmap='jet')
        #     f = plt.figure()
        #     plt.imshow(figure, cmap='jet')

        if ((circle_valid) < 3):
            continue

        circle_sum = np.nansum(circle_p)
        circle_nz = np.nansum(circle_p > 0.1)
        circle_g30 = np.nansum(circle_p >= 30)

        try:
            circle_max = np.nanmax(circle_p)
        except ValueError:
            circle_max = np.nan
        try:
            circle_p99 = np.percentile(circle_p[circle_p >= 0.1], 99)
        except IndexError:
            circle_p99 = np.nan
        try:
            circle_p95 = np.percentile(circle_p[circle_p >= 0.1], 95)
        except IndexError:
            circle_p95 = np.nan
        try:
            circle_p90 = np.percentile(circle_p[circle_p >= 0.1], 90)
        except IndexError:
            circle_p90 = np.nan

        #maxs[posi, y, x] = 1

        ret.append((kernel, kernelt, int_sc, id, dic['time.hour'].values.tolist(),
                    clat, clon, lat_min, lat_max, lon_min, lon_max, area,
                    bulk_pmax, bulk_pmean, bulk_tmean, bulk_tmean_p, bulk_tmin_p, bulk_g30,
                    len(ycircf), circle_Tcenter, circle_p, circle_t, circle_valid, circle_sum,
                    circle_nz, circle_g30, circle_max, circle_p99, circle_p95, circle_p90, circle_val_all, circle_pc))

    return ret
def run(fi):
    ret = []

    print('Doing file: ' + fi)

    dic = xr.open_dataset(fi)

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

    outplot = outp.copy()

    outt[np.isnan(outt)] = 150
    outt[outt >= -40] = 150
    grad = np.gradient(outt)
    outt[outt == 150] = np.nan
    outp[np.isnan(outt)] = np.nan
    outpc[np.isnan(outt)] = np.nan


    area = np.sum(outt <= -40)
    try:
        bulk_pmax = np.max(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    except ValueError:
        return ret
    try:
        bulk_pmin = np.min(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    except ValueError:
        return ret

    if (area * 25 < 15000) or (area * 25 > 800000)  or (bulk_pmax > 200) or (bulk_pmin < 0):
        print(area*25)
        print('throw out')
        return

    perc = np.percentile(outt[np.isfinite(outt)], 60)  # 60
    print('perc:',perc)
    perc=-60

    clat = np.min(dic.lat.values) + ((np.max(dic.lat.values) - np.min(dic.lat.values)) * 0.5)
    clon = np.min(dic.lon.values) + ((np.max(dic.lon.values) - np.min(dic.lon.values)) * 0.5)

    if (clon > 28) or (clon < -17.2) or (clat < 4.1):
        return

    figure = np.zeros_like(outt)

    o2 = outt.copy()
    o2[np.isnan(o2)] = perc
    nok = np.where(abs(grad[0]) > 80)
    d = 2
    i = nok[0]
    j = nok[1]

    for ii, jj in zip(i, j):
        kern = o2[ii - d:ii + d + 1, jj - d:jj + d + 1]
        o2[ii - d:ii + d + 1, jj - d:jj + d + 1] = ndimage.gaussian_filter(kern, 3, mode='nearest')

    wav = util.waveletT(o2, 5)

    arr = np.array(wav['scales'], dtype=str)
    arrf = np.array(wav['scales'], dtype=float)

    scale_ind = range(arr.size)

    yp, xp = np.where(outp > 30)

    figure = np.zeros_like(outt)

    wll = wav['t']
    maxs = np.zeros_like(wll)


    yyy=[]
    xxx=[]
    scal=[]
    for nb in scale_ind[::-1]:

        orig = float(arr[nb])
        print(np.round(orig))

        wl = wll[nb, :, :]
        maxout = (
            wl == ndimage.maximum_filter(wl, (5,5), mode='constant', cval=np.amax(wl) + 1))  # (np.round(orig / 5))

        try:
            yy, xx = np.where((maxout == 1) & (outt <= -40)  & (wl >= np.percentile(wl[wl >= 0.5], 90)) & (wl > orig**.5))# & (wl > orig**.5) )) #(wl >= np.percentile(wl[wl >= 0.5], 90)))# & (wl > orig**.5))#& (wl >= np.percentile(wl[wl >= 0.5], 90))) #)& (wl > orig**.5) (wl >= np.percentile(wl[wl >= 0.1], 90)) )#(wl > orig**.5))#  & (wlperc > orig**.5))# & (wlperc > np.percentile(wlperc[wlperc>=0.1], 80)))# & (wlperc > np.percentile(wlperc[wlperc>=0.1], 80) ))  # & (wl100 > 5)
        except IndexError:
            continue

        for y, x in zip(yy, xx):

            ss = orig
            iscale = (np.ceil(ss / 2. / 5.)).astype(int)
            if ss <= 20:
                iscale = iscale+1

            ycirc, xcirc = ua.draw_cut_circle(x, y, iscale, outp)

            figure[ycirc, xcirc] = np.round(orig)
            xxx.append(x)
            yyy.append(y)
            scal.append(orig)

    figure[np.isnan(outt)]=0

    ##file 130!!! nR
    spos = np.where(np.array(scal, dtype=int) == 15)
    figure[figure == 0] = np.nan


    f = plt.figure(figsize = (7,6), dpi=300)
    ax2 = f.add_subplot(111)
    # ax2.autoscale = False
    ttest = outplot.copy()
    ttest = ttest + 1
    ttest[np.isnan(ttest)] = 0
    ax2.contourf(np.arange(wll.shape[2]) ,  np.arange(wll.shape[1]) , outplot, cmap='Blues', zorder=1)

    ax2.imshow(outt, cmap='Greys', vmax=-40, zorder=2)
    mt = ax2.imshow(figure, cmap='OrRd_r', vmax=180, zorder=3)
    ax2.contour(np.arange(wll.shape[2]), np.arange(wll.shape[1]), ttest, cmap='Blues', levels=[-0.5, 0.5], zorder=4)
    plt.plot(np.array(xxx)[spos], np.array(yyy)[spos], 'wo', markersize=3, label='Wavelet power maximum', zorder=5)

    ax2.invert_yaxis()
    ax2.set_xlim(20, 140)
    ax2.set_ylim(20, 140)
    ax2.set_xticklabels(np.arange(100, 800, 100)-100)
    ax2.set_yticklabels(np.arange(100, 800, 100)-100)
    ax2.plot(xp , yp , 'o', markersize=3, label='Rain > 30mm h$^{-1}$', zorder=10)
    ax2.set_xlabel('Location (km)')
    ax2.set_ylabel('Location (km)')
    plt.colorbar(mt, label = 'Scale (km)')

    plt.tight_layout()
    plt.show()
    spath = '/users/global/cornkle/C_paper/wavelet/figs/paper/'
    plt.savefig(spath + '/method_circles2.png', dpi=300)

    f = plt.figure(figsize = (7,6), dpi=300)
    ax2 = f.add_subplot(111)
    # ax2.autoscale = False
    ttest = outplot.copy()
    ttest = ttest + 1
    ttest[np.isnan(ttest)] = 0
    ax2.imshow(outt, cmap='viridis', vmax=-40, zorder=2)

    ax2.invert_yaxis()
    ax2.set_xlim(20, 140)
    ax2.set_ylim(20, 140)
    ax2.set_xticklabels(np.arange(100, 800, 100)-100)
    ax2.set_yticklabels(np.arange(100, 800, 100)-100)
    ax2.plot(xp , yp , 'o', markersize=3, label='Rain > 30mm h$^{-1}$', zorder=10)
    ax2.set_xlabel('Location (km)')
    ax2.set_ylabel('Location (km)')

    plt.tight_layout()
    plt.show()
    spath = '/users/global/cornkle/C_paper/wavelet/figs/paper/'
    plt.savefig(spath + '/method_temp.png', dpi=300)


    #bla = wcno.file_loop(files[130])
    f = plt.figure(figsize = (6.5,11), dpi=300)

    gridspec.GridSpec(4,1)
    posi = 116
    ax1 = plt.subplot2grid((4,1),(0,0),rowspan=2)
    ax2 = plt.subplot2grid((4,1),(2,0))
    ax3 = plt.subplot2grid((4,1),(3,0))

    lev = np.arange(-90,-39,4)

    ax1.contourf(np.arange(wll.shape[2]) * 5, np.arange(wll.shape[1]) * 5, outplot, cmap='Blues')
    mt = ax1.contourf(np.arange(wll.shape[2])*5,np.arange(wll.shape[1])*5,outt, cmap='Greys', vmax=-40, levels = lev)
    ax1.plot(np.arange(wll.shape[2])*5, [posi*5]*len(np.arange(wll.shape[2])*5), linestyle = '--', linewidth=2, color = 'black')

    ttest = outplot.copy()
    ttest = ttest+1
    ttest[np.isnan(ttest)] = 0
    ax1.contour(np.arange(wll.shape[2]) * 5, np.arange(wll.shape[1]) * 5, ttest, cmap='Blues' , levels=[-0.5,0.5])

    ax1.invert_yaxis()
    ax1.set_xlim(100,700)
    ax1.set_ylim(100, 700)

    ax1.set_xticklabels(np.arange(100, 800, 100) - 100)
    ax1.set_yticklabels(np.arange(100, 800, 100) - 100)

    ax1.plot(xp*5, yp*5, 'o', markersize=3, label='Rain > 30mm h$^{-1}$')
    ax1.legend(loc=4)
    ax1.set_ylabel('Location (km)')
    ax1.set_title(str(dic['time.year'].values)+'-'+str(dic['time.month'].values)+'-'+str(dic['time.day'].values)+' '+str(dic['time.hour'].values)+':'+str(dic['time.minute'].values)+'UTC')

    colors = cm.viridis(np.linspace(0, 1, len([0,1, 2,5,10,20,40,60,80])))

    ax2.plot(np.arange(wll.shape[2])*5, outt[posi,:], color='r')  #118
    ax2.set_xlim(100, 700)
    ax2.set_xticklabels(np.arange(100, 800, 100) - 100)


    ax2.set_ylabel('Cloud-top temperature ($^{\circ}$C)')
    ax22 = ax2.twinx()
    ax22.set_xlim(100, 700)
    ax22.plot(np.arange(wll.shape[2])*5, outp[posi,:])
    ax22.set_ylabel('Rain (mm h$^{-1}$)')
    print(np.nanmax(wll[:,posi,:]))

    mp = ax3.contourf(np.arange(wll.shape[2])*5, arr,wll[:,posi,:], levels=[0,1, 2,5,10,20,40,80,100], colors=colors)
    maxs = np.mean(maxs[:,posi-1:posi+2, :], 1) # -1, +2

    ppos = np.where(maxs)

    for p1, p2 in zip(ppos[1], ppos[0]):
        ax3.errorbar((np.arange(wll.shape[2])*5)[p1], arrf[p2], xerr=arrf[p2]/2, fmt='o', ecolor='white', color='white', capthick=3, ms=3, elinewidth=0.7)
    ax3.set_xlim(100,700)
    ax3.set_xticklabels(np.arange(100, 800, 100) - 100)

    ax3.set_ylim(15, 180)
    ax3.set_xlabel('Location (km)')
    ax3.set_ylabel('Length scale (km)')

    plt.tight_layout()

    f.subplots_adjust(right=0.86)

    cax = f.add_axes([0.87, 0.545, 0.025, 0.415])
    cb = plt.colorbar(mt, cax=cax, label='Cloud-top temperature ($^{\circ}$C)')
    cb.ax.tick_params(labelsize=12)

    cax = f.add_axes([0.87, 0.065, 0.025, 0.175])
    cb = plt.colorbar(mp, cax=cax, label='Wavelet power')
    cb.ax.tick_params(labelsize=12)

    fsiz = 14
    x = 0.02
    plt.annotate('a)', xy=(x, 0.96), xytext=(0, 4), size=fsiz, xycoords=('figure fraction', 'figure fraction'),
                 textcoords='offset points')
    plt.annotate('b)', xy=(x, 0.51), xytext=(0, 4), size=fsiz, xycoords=('figure fraction', 'figure fraction'),
                 textcoords='offset points')
    plt.annotate('c)', xy=(x, 0.245), xytext=(0, 4), size=fsiz, xycoords=('figure fraction', 'figure fraction'),
                 textcoords='offset points')

    plt.show()
    spath = '/users/global/cornkle/C_paper/wavelet/figs/paper/'
    plt.savefig(spath+'/method2.png', dpi=300)

    dic.close()

    plt.close('all')
def file_loop(passit):

    gridd = passit[0]
    inds_inter = gridd[0]
    weights_inter = gridd[1]
    shape_inter = gridd[2]
    grid_inter = gridd[3]

    m = passit[1]
    file = passit[2]
    tag = passit[3]


    # if tag == 'mfg':
    #     strr = file.split(os.sep)[-2]
    #     if (np.int(strr[4:6]) != 9):
    #         print('Skip month')
    #         return
    # else:
    #     strr = file.split(os.sep)[-1]
    #     if (np.int(strr[4:6]) != 9):
    #         print('Skip month')
    #         return


    strr = file.split(os.sep)[-1]
    if ((strr[-2::]) != '00') & ((strr[-2::]) != '30'):
        print('Skip minute')
        return



    # if not ((np.int(strr[8:10]) >= 20)): #& (np.int(strr[8:10]) <= 19) ): #((np.int(strr[8:10]) > 3)): #not ((np.int(strr[8:10]) >= 16) & (np.int(strr[8:10]) <= 19) ): #& (np.int(strr[8:10]) < 18): #(np.int(strr[4:6]) != 6) & #(np.int(strr[8:10]) != 3) , (np.int(strr[8:10]) > 3)
    #     print('Skip hour')
    #     return

    lon, lat = grid_inter.ll_coordinates

    file = file + '.gra'

    print('Doing file: ' + file)
    try:
        mdic = m.read_data(file, llbox=[-25, 20, 2, 25])
    except FileNotFoundError:
        print('File not found')
        return

    if not mdic:
        print('File missing')
        return

    timeslice = mdic['t']

    #test = timeslice.copy(deep=True)
    try:
        outt = u_int.interpolate_data(timeslice.values, inds_inter, weights_inter, shape_inter)
        print('Interpolated')
    except ValueError:
        print('Interpolation value error!!')
        ipdb.set_trace()

    savet = outt.copy()

    t_thresh_size = -40
    t_thresh_cut = -50

    core_min = -50

    outt[outt>=t_thresh_size] = 0
    outt[np.isnan(outt)] = 0

    labels, numL = label(outt)

    u, inv = np.unique(labels, return_inverse=True)
    n = np.bincount(inv)

    pix_nb = 28

    badinds = u[(n < pix_nb)]  # all blobs with more than 1000 pixels = 25,000km2 (meteosat regridded 5km), 200pix = 5000km2, 8pix = 200km2
    # scale 30km, radius 15km ca. 700km2 circular area equals 28 pix

    for bi in badinds:
        inds = np.where(labels == bi)
        outt[inds] = 0

    outt[outt >=t_thresh_cut] = 150

    grad = np.gradient(outt)
    outt[outt == 150] = np.nan

    nogood = np.isnan(outt) # filters edge maxima later, no maxima in -40 edge area by definition!

    tdiff = np.nanmax(outt)-np.nanmin(outt) # define background temperature for image
    if tdiff > 28:  # temp difference of 28 degrees
        xmin = 15
    else:
        xmin = 10

    outt[nogood] = t_thresh_cut-xmin
    nok = np.where(abs(grad[0]) > 80)
    d = 2
    i = nok[0]
    j = nok[1]
    # edge smoothing for wavelet application
    for ii, jj in zip(i, j):
        kern = outt[ii - d:ii + d + 1, jj - d:jj + d + 1]
        outt[ii - d:ii + d + 1, jj - d:jj + d + 1] = ndimage.gaussian_filter(kern, 3, mode='nearest')

    wav = util.waveletT(outt, dataset='METEOSAT10K')

    outt[nogood] = np.nan

    arr = np.array(wav['scales'], dtype=str)

    scale_ind = range(arr.size)

    figure = np.zeros_like(outt)

    wll = wav['t']

    maxoutt = (
        wll == ndimage.maximum_filter(wll, (5,4,4), mode='reflect',   # 5,4,4
                                      cval=np.amax(wll) + 1))  # (np.round(orig / 5))

    yyy = []
    xxx = []
    scal = []
    for nb in scale_ind[::-1]:

        orig = float(arr[nb])

        scale = int(np.round(orig))

        print(np.round(orig))

        wl = wll[nb, :, :]
        maxout = maxoutt[nb, :, :]

        try:
            yy, xx = np.where((maxout == 1) & (outt <= core_min) &  (wl > orig**.5)) #&  (wl > orig**.5)) #  #  &
        except IndexError:
            continue

        print(outt[yy,xx])

        for y, x in zip(yy, xx):

            ss = orig
            iscale = (np.ceil(ss / 2. / 5.)).astype(int)

            ycirc, xcirc = ua.draw_cut_circle(x, y, iscale, outt)

            figure[ycirc, xcirc] = scale  #outt
            figure[y,x] = scale * -1
            xxx.append(x)
            yyy.append(y)
            scal.append(orig)

    figure[np.isnan(outt)] = 0

    hour = timeslice['time.hour']
    minute = timeslice['time.minute']
    day = timeslice['time.day']
    month = timeslice['time.month']
    year = timeslice['time.year']

    date = dt.datetime(year, month, day, hour, minute)

    isnan = np.isnan(savet)
    savet[isnan]=0
    new_savet = (savet*100).astype(np.int16)

    bloblist = xr.DataArray(figure.astype(np.int16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon']) #[np.newaxis, :])
    tirlist = xr.DataArray(new_savet, coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
    power1 = xr.DataArray((np.mean(wav['t'][0:5,:,:], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
    power2 = xr.DataArray((np.mean(wav['t'][13:17, :, :], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
    power3 = xr.DataArray((np.mean(wav['t'][29:32, :, :], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
    power4 = xr.DataArray((np.mean(wav['t'][-5:-3, :, :], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])

    ds = xr.Dataset()
    ds['blobs'] = bloblist
    ds['tir'] = tirlist
 #   ds['tir_w'] = xr.DataArray(outt, coords={'time': date, 'lat': lat[:, 0], 'lon': lon[0, :]}, dims=['lat', 'lon'])
    ds['power15-19km'] = power1
    ds['power32-38km'] = power2
    ds['power80-90km'] = power3
    ds['power160-170km'] = power4


    ds.attrs['radii']=(np.ceil(wav['scales'] / 2. / 5.)).astype(np.uint8)
    ds.attrs['scales_rounded'] = np.round(wav['scales']).astype(np.uint8)
    ds.attrs['scales_original'] = wav['scales']
    ds.attrs['cutout_T'] = t_thresh_size
    ds.attrs['core_minT'] = core_min
    ds.attrs['cutout_minPixelNb'] = pix_nb

    ds = ds.sel(lat=slice(2,17), lon=slice(-18,13))     #[-14, 2.5, 4, 11.5] cutout to remove dodgy boundaries

    print('Did ', file)

    return (ds)
def file_loop(passit):

    gridd = passit[0]
    inds = gridd[0]
    weights = gridd[1]
    shape = gridd[2]
    grid = gridd[3]

    m = passit[1]
    file = passit[2]

    #min = ['00', '30']

    strr = file.split(os.sep)[-1]

    if ((np.int(strr[4:6]) != 9) & (np.int(strr[4:6]) != 6)):
        print('Skip month')
        return

    if ((strr[-6:-5]) != '00') & ((strr[-6:-5]) != '30'):
        print('Skip minute')
        return

    # if not ((np.int(strr[8:10]) >= 20)): #& (np.int(strr[8:10]) <= 19) ): #((np.int(strr[8:10]) > 3)): #not ((np.int(strr[8:10]) >= 16) & (np.int(strr[8:10]) <= 19) ): #& (np.int(strr[8:10]) < 18): #(np.int(strr[4:6]) != 6) & #(np.int(strr[8:10]) != 3) , (np.int(strr[8:10]) > 3)
    #     print('Skip hour')
    #     return

    lon, lat = grid.ll_coordinates

    #file = files+min+'.gra'

    print('Doing file: ' + file)
    try:
        mdic = m.read_data(file, llbox=[-15, 1.5, 5, 11.5])
    except FileNotFoundError:
        print('File not found')
        return

    if not mdic:
        print('File missing')
        return
    outt = u_int.interpolate_data(mdic['t'].values, inds, weights, shape)
    savet = outt.copy()

    t_thresh_size = -40
    t_thresh_cut = -50

    core_min = -50

    outt[outt>=t_thresh_size] = 0
    outt[np.isnan(outt)] = 0

    labels, numL = label(outt)

    u, inv = np.unique(labels, return_inverse=True)
    n = np.bincount(inv)

    pix_nb = 28

    badinds = u[(n < pix_nb)]  # all blobs with more than 1000 pixels = 25,000km2 (meteosat regridded 5km), 200pix = 5000km2, 8pix = 200km2
    # scale 30km, radius 15km ca. 700km2 circular area equals 28 pix

    for bi in badinds:
        inds = np.where(labels == bi)
        outt[inds] = 0

    outt[outt >=t_thresh_cut] = 150

    grad = np.gradient(outt)
    outt[outt == 150] = np.nan

    nogood = np.isnan(outt) # filters edge maxima later, no maxima in -40 edge area by definition!

    tdiff = np.nanmax(outt)-np.nanmin(outt) # define background temperature for image
    if tdiff > 28:  # temp difference of 28 degrees
        xmin = 15
    else:
        xmin = 10

    outt[nogood] = t_thresh_cut-xmin
    nok = np.where(abs(grad[0]) > 80)
    d = 2
    i = nok[0]
    j = nok[1]
    # edge smoothing for wavelet application
    for ii, jj in zip(i, j):
        kern = outt[ii - d:ii + d + 1, jj - d:jj + d + 1]
        outt[ii - d:ii + d + 1, jj - d:jj + d + 1] = ndimage.gaussian_filter(kern, 3, mode='nearest')

    wav = util.waveletT(outt, dataset='METEOSAT5K')

    outt[nogood] = np.nan

    arr = np.array(wav['scales'], dtype=str)

    scale_ind = range(arr.size)

    figure = np.zeros_like(outt)

    wll = wav['t']

    maxoutt = (
        wll == ndimage.maximum_filter(wll, (5,4,4), mode='reflect',   # 5,4,4
                                      cval=np.amax(wll) + 1))  # (np.round(orig / 5))

    yyy = []
    xxx = []
    scal = []
    for nb in scale_ind[::-1]:

        orig = float(arr[nb])

        scale = int(np.round(orig))

        print(np.round(orig))

        wl = wll[nb, :, :]
        maxout = maxoutt[nb, :, :]

        try:
            yy, xx = np.where((maxout == 1) & (outt <= core_min) &  (wl > orig**.5)) #&  (wl > orig**.5)) #  #  &
        except IndexError:
            continue

        print(outt[yy,xx])

        for y, x in zip(yy, xx):

            ss = orig
            iscale = (np.ceil(ss / 2. / 5.)).astype(int)

            ycirc, xcirc = ua.draw_cut_circle(x, y, iscale, outt)

            figure[ycirc, xcirc] = scale  #outt
            figure[y,x] = scale * -1
            xxx.append(x)
            yyy.append(y)
            scal.append(orig)

    figure[np.isnan(outt)] = 0

    # f = plt.figure()
    # plt.contourf(outt)
    # plt.contour(figure)

    # figure[figure == 0] = np.nan
    # f = plt.figure()
    # f.add_subplot(111)
    # plt.imshow(outt, cmap='inferno')
    # plt.imshow(figure, cmap='viridis')
    # plt.colorbar()
    # plt.plot(xxx, yyy, 'yo', markersize=3)
    # ax = f.add_subplot(132, projection=ccrs.PlateCarree())
    # plt.contourf(lon, lat, figure, cmap='viridis', transform=ccrs.PlateCarree())
    # ax.coastlines()
    # ax.add_feature(cartopy.feature.BORDERS, linestyle='--');
    #
    # plt.colorbar()
    # f.add_subplot(131)
    # plt.imshow(outt, cmap='inferno')
    #
    #
    #plt.show()

    hour = mdic['time.hour']
    minute = mdic['time.minute' ]
    day = mdic['time.day']
    month = mdic['time.month']
    year = mdic['time.year']

    date = dt.datetime(year, month, day, hour, minute)

    ds = xr.Dataset()

    isnan = np.isnan(savet)
    savet[isnan]=0
    new_savet = (savet*100).astype(np.int16)

    ds['blobs'] = xr.DataArray(figure.astype(np.int16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon']) #[np.newaxis, :]
    ds['tir'] = xr.DataArray(new_savet, coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
 #   ds['tir_w'] = xr.DataArray(outt, coords={'time': date, 'lat': lat[:, 0], 'lon': lon[0, :]}, dims=['lat', 'lon'])
    ds['power15-19km'] = xr.DataArray((np.mean(wav['t'][0:5,:,:], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
    ds['power32-38km'] = xr.DataArray((np.mean(wav['t'][13:17, :, :], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
    ds['power80-90km'] = xr.DataArray((np.mean(wav['t'][29:32, :, :], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])
    ds['power160-170km'] = xr.DataArray((np.mean(wav['t'][-5:-3, :, :], axis=0)*100).astype(np.uint16), coords={'time': date, 'lat': lat[:,0], 'lon':lon[0,:]}, dims=['lat', 'lon'])


    ds.attrs['radii']=(np.ceil(wav['scales'] / 2. / 5.)).astype(np.uint8)
    ds.attrs['scales_rounded'] = np.round(wav['scales']).astype(np.uint8)
    ds.attrs['scales_original'] = wav['scales']
    ds.attrs['cutout_T'] = t_thresh_size
    ds.attrs['core_minT'] = core_min
    ds.attrs['cutout_minPixelNb'] = pix_nb


    print('Did ', file)

    return (ds)
def file_loop(fi):
    ret = []

    print('Doing file: ' + fi)

    dic = xr.open_dataset(fi)

    id = fi.split('/')[-1]

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

    outt[np.isnan(outt)] = 150
    outt[outt >= -40] = 150
    grad = np.gradient(outt)
    outt[outt == 150] = np.nan
    outp[np.isnan(outt)] = np.nan


    area = np.sum(outt <= -40)
    try:
        bulk_pmax = np.max(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    except ValueError:
        return ret

    try:
        bulk_pmin = np.min(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    except ValueError:
        return ret

    # 2004-07-17_04:09:00_80.nc (127) is a huge circular storm. good example for organisation
    #area gt 3000km2 cause that's 30km radius if circular
    if (area * 25 < 15000) or (area * 25 > 800000) or (bulk_pmax > 200) or (bulk_pmin < 0): #or (np.sum(np.isfinite(outp)) < (np.sum(np.isfinite(outt))*0.1)):
        print(area*25)
        print('throw out')
        return

    print('Area', area*25)

    perc = np.percentile(outt[np.isfinite(outt)], 60)  # 60

    clat = np.min(dic.lat.values) + ((np.max(dic.lat.values) - np.min(dic.lat.values)) * 0.5)
    clon = np.min(dic.lon.values) + ((np.max(dic.lon.values) - np.min(dic.lon.values)) * 0.5)

    lat_min = np.min(dic.lat.values)
    lat_max = np.max(dic.lat.values)
    lon_min = np.min(dic.lon.values)
    lon_max = np.max(dic.lon.values)

    bulk_tmean = np.nanmean(outt)
    lat = dic['lat'].values
    bulk_tmin_p = np.min(outt[(np.isfinite(outp)) & (np.isfinite(outt))])
    bulk_tmean_p = np.mean(outt[(np.isfinite(outp)) & (np.isfinite(outt))])
    bulk_pmean = np.max(outp[(np.isfinite(outp)) & (np.isfinite(outt))])
    bulk_g30 = np.sum(outp[(np.isfinite(outp)) & (np.isfinite(outt))] >= 30)


    o2 = outt.copy()
    o2[np.isnan(o2)] = perc
    nok = np.where(abs(grad[0]) > 80)
    d = 2
    i = nok[0]
    j = nok[1]

    for ii, jj in zip(i, j):
        kern = o2[ii - d:ii + d + 1, jj - d:jj + d + 1]
        o2[ii - d:ii + d + 1, jj - d:jj + d + 1] = ndimage.gaussian_filter(kern, 3, mode='nearest')

    wav = util.waveletT(o2, 5)
    wl = wav['t']  # [nb, :, :]

    # maxout = (
    #     wl == ndimage.maximum_filter(wl, (6, 6, 5), mode='constant',
    #                                  cval=np.amax(wl) + 1))  # (np.round(orig / 5))

    #(3, 3 ,len(wav['scales'])-1)
    arr = np.array(wav['scales'], dtype=str)

    scale_ind = range(arr.size)

    wlperc = wav['t'].copy()
    figure = np.zeros_like(outt)
    #
    # for s in range(wlperc.shape[0]):
    #     wlperc[s,:,:][wlperc[s,:,:] < np.percentile(wlperc[s,:,:][wlperc[s,:,:]>=0.05], 70)] = 0
    #
    # labels, numL = label(wlperc)
    # for s in [0,1]:
    #
    #      f = plt.figure()
    #      plt.imshow(labels[s,:,:])
    #
    # return

    yp, xp = np.where(outp > 30)

    size = [15,20,30,40,50,60,70,80,90,100,120,140,160,180,200,220]

    for sc in wav['scales'][::-1]:
        print(sc)

        mimin = np.argmin(np.abs(size-sc))


        idd = wav['scales'].tolist().index(sc)
        wll = wlperc[idd,:,:].copy()
      #  wll[wll < np.percentile(wll[wll >= 0.01], 90)] = 0
      #  wll[wll < np.percentile(np.arange(wll.min(), wll.max()+0.1,0.1), 5)] = 0
      #  wll[wll < np.percentile(wll[wll >= 0.05], 75)] = 0
        wll[(wll < sc**.5)] = 0


        labels, numL = label(wll)

        for ll in np.unique(labels):
            if ll==0:
                continue
            pos =  np.where(labels==ll)
            if np.max(wll[pos]) < np.percentile(wll[wll >= 1], 50):
                continue

            figure[pos] = size[mimin]


        #figure[np.nonzero(labels)] = size[mimin]
    figure[np.isnan(outt)]=0

    # pdb.set_trace()
    figure[figure==0]=np.nan
    f = plt.figure()
    ax1=f.add_subplot(131)
    plt.imshow(outt)
    plt.imshow(figure, cmap='viridis')

    ax2 = f.add_subplot(132)
    plt.imshow(figure, cmap='viridis')
    plt.plot(xp, yp, 'yo', markersize=3)
    ax3 = f.add_subplot(133)
    plt.imshow(outt)
    ax1.invert_yaxis()
    ax2.invert_yaxis()
    ax3.invert_yaxis()

    plt.show()
    return

    for sc in np.unique(figure):

        if sc == 0:
            continue

        perblob = figure.copy()
        perblob[perblob!=sc] = 0
        labels_blob, numL = label(perblob)

        for blob in np.unique(labels_blob):

            if blob == 0:
                continue

            pos = np.where(labels_blob == blob) #(figure == sc)

            # if len(pos[0]) < 5:
            #     continue

            circle_p = outp[pos]
            circle_t = outt[pos]
            circle_valid = np.sum(np.isfinite(circle_p))

            # if no valid temperature pixels (noise from wavelet outside of storm) or very little valid TRMM pixels
            # (very small overlap of TRMM and MSG), continue!
            if (np.sum(np.isfinite(circle_t)) <= 0 ):
                continue

            if  ((circle_valid) < 3 ):
                continue

            circle_sum = np.nansum(circle_p)
            ## some rain at least
            # if circle_sum < 0.1:
            #     continue

            circle_Tcenter = np.nanmin(outt[pos])

            y, x = np.where((outt == circle_Tcenter) & (labels_blob == blob) & (figure == sc))

            # figure[figure==0]=np.nan
            # f = plt.figure()
            # f.add_subplot(131)
            # plt.imshow(outt)
            # plt.imshow(figure, cmap='viridis')
            # f.add_subplot(132)
            # plt.imshow(figure, cmap='viridis')
            # plt.plot(xp, yp, 'yo', markersize=3)
            # plt.plot(pos[1], pos[0], 'ro', markersize=3)
            # f.add_subplot(133)
            # plt.imshow(outt)
            # plt.show()

            r = 20
            kernel = tm_utils.cut_kernel(outp, x[0], y[0], r)
            kernelt = tm_utils.cut_kernel(outt, x[0], y[0], r)

            if kernel.shape != (r * 2 + 1, r * 2 + 1):
                kernel = np.zeros((41, 41)) + np.nan
            if kernelt.shape != (r * 2 + 1, r * 2 + 1):
                kernelt = np.zeros((41, 41)) + np.nan

            #
            # if np.nansum(kernel) < 1:
            #     continue

            circle_nz = np.nansum(circle_p>0.1)
            circle_g30 = np.nansum(circle_p >= 30)

            try:
                circle_max = np.nanmax(circle_p)
            except ValueError:
                circle_max = np.nan
            try:
                circle_p99 = np.percentile(circle_p[circle_p>=0.1], 99)
            except IndexError:
                circle_p99 = np.nan
            try:
                circle_p95 = np.percentile(circle_p[circle_p>=0.1], 95)
            except IndexError:
                circle_p95 = np.nan
            try:
                circle_p90 = np.percentile(circle_p[circle_p>=0.1], 90)
            except IndexError:
                circle_p90 = np.nan


            #### HOW TO GIVE BACK THE MAX SCALE PER SYSTEM??

            ret.append((kernel, kernelt, sc, id, dic['time.hour'].values.tolist(),
                        clat, clon, lat_min, lat_max, lon_min, lon_max, area,
                        bulk_pmax, bulk_pmean, bulk_tmean, bulk_tmean_p, bulk_tmin_p, bulk_g30,
                        circle_Tcenter, circle_p, circle_t, circle_valid, circle_sum,
                        circle_nz, circle_g30, circle_max, circle_p99, circle_p95, circle_p90))


    figure[figure==0]=np.nan
    f = plt.figure()
    f.add_subplot(133)
    plt.imshow(outt, cmap='inferno')
    plt.imshow(figure, cmap='viridis')
    f.add_subplot(132)
    plt.imshow(figure, cmap='viridis')
    plt.colorbar()
    plt.plot(xp, yp, 'yo', markersize=3)
    f.add_subplot(131)
    plt.imshow(outt, cmap='inferno')
    plt.plot(xp, yp, 'yo', markersize=3)
    plt.show()
    # #
    #
    #f = plt.figure()

    # fcnt = 0
    # vv = 4
    # for s in scale_ind:
    #
    #     pos = np.where((maxout[s, :, :] == 1) & (outt <= -40))
    #
    #     if len(pos[0]) == 0:
    #         continue
    #     fcnt+=1
    #     ax = f.add_subplot(vv,vv,fcnt)
    #     ax.imshow(wl[s,:,:])
    #
    #    # plt.plot(xp, yp, 'yo', markersize=3)
    #
    #     ax.set_title(str(wav['scales'][s]))
    #
    # ax = f.add_subplot(vv, vv, fcnt+1)
    # plt.imshow(figure, cmap='viridis')
    # plt.plot(xp, yp, 'yo', markersize=3)
    # ax = f.add_subplot(vv, vv, fcnt + 2)
    # plt.imshow(outt)
    # ax = f.add_subplot(vv, vv, fcnt + 3)
    # plt.imshow(outp)
    # plt.plot(xp, yp, 'yo', markersize=3)
    # ax = f.add_subplot(vv, vv, fcnt + 4)
    # plt.imshow(figure, cmap='viridis')
    #
    #
    # plt.show()
    #
    # dic.close()

    return ret
Esempio n. 8
0
def circle():
    matplotlib.rc('xtick', labelsize=10)
    matplotlib.rc('ytick', labelsize=10)


    ellipse = np.zeros((100,100))+5
    short =25

    xcirc, ycirc = ua.draw_circle(50,50,short)


    ellipse[ycirc,xcirc] = -80
    nb = 21
    wav = util.waveletT(ellipse, dx=1, dist=0.08, start=15, nb=nb)#dx=5, dist=0.08,start=15,nb=15 )

    wll = wav['t']
    arr = np.round(wav['scales'])
    print('AVAIL WAVELET SCALES: ', arr)
    maxs = np.zeros_like(wll)
    yl = []
    xl = []
    for nb in range(wav['t'].shape[0]):

        orig = float(wav['scales'][nb])

        wl = wav['t'][nb, :, :]

        maxout = (
            wl == ndimage.maximum_filter(wl, (5, 5), mode='constant', cval=np.amax(wl) + 1))  # (np.round(orig / 5))

        try:
            yy, xx = np.where((maxout == 1) & (wl > orig ** .5))
        except IndexError:
            continue

        for y, x in zip(yy, xx):
            #print(arr[nb],y,x)

            maxs[nb,y,x] = 1
            #print('Power value',wll[nb,y,x])
            yl.append(y)
            xl.append(x)

    print('finish loop')

    maxout2 = (
            wll == ndimage.maximum_filter(wll, (5, 5,5), mode='reflect', cval=np.amax(wl) + 1))  # (np.round(orig / 5))

    zl, yl, xl = np.where((maxout2==1) & (wll > arr.repeat(100*100,axis=0).reshape((nb+1,100,100)) ** .5))
    wlmax = np.max(wll[zl,yl,xl])
    pl = np.where(wll == wlmax)

    zll, yll, xll = np.where((maxs == 1))
    wllmax = np.max(wll[zll,yll,xll])
    pll = np.where(wll == wllmax)

    print('Max point scales: ', arr[zl])

    amax = np.unravel_index(np.argmax(wll), wll.shape)
    print('Totalmax whole domain', arr[amax[0]])
    print('Totalmax 3d', arr[pl[0]])
    print('Totalmax 2d', arr[pll[0]])
    print('Scale of perfect circle', (2*short+1)*wav['res'])
    print('Pixel-adjusted perfect circle', (2 *short + 1 -2) * wav['res'])
    print('Max scale in pixelacross', arr[amax[0]]/wav['res'])
    print('Power max', np.max(wll))

    f = plt.figure(figsize = (6.5,11), dpi=300)
    #
    gridspec.GridSpec(3,1)
    posi = 56#116 ## 118
    ax1 = plt.subplot2grid((3,1),(0,0),rowspan=2)
    ax3 = plt.subplot2grid((3,1),(2,0))
    #
    lev = np.arange(-90,-39,4)
    mt = ax1.contourf(np.arange(wll.shape[2])*5,np.arange(wll.shape[1])*5,ellipse, cmap='Greys', vmax=-40, levels = lev)
    ax1.plot(np.arange(wll.shape[2])*5, [posi*5]*len(np.arange(wll.shape[2])*5), linestyle = '--', linewidth=2, color = 'black')
    ax1.invert_yaxis()
    ax1.legend(loc=4)
    ax1.set_ylabel('Spatial extent (km)')
    #colors = cm.viridis(np.linspace(0, 1, len([0,1, 2,5,10,20,40,60,80,100])))
    #
    mp = ax3.contourf(np.arange(wll.shape[2])*5, arr,wll[:,posi,:], levels=[0,1, 2,5,10,20,40,80,100, 130, 150, 180, 200, 300,400], cmap='viridis')
    maxs = np.mean(maxs[:,posi-1:posi+2, :], 1) # -1, +2
    #ax3.contour(np.arange(wll.shape[2])*5, arr,maxs, cmap='Greys_r')

    ppos = np.where(maxs)

    #for p1, p2 in zip(ppos[1], ppos[0]):
    #    ax3.errorbar((np.arange(wll.shape[2])*5)[p1], arr[p2], xerr=arr[p2]/2, fmt='o', ecolor='white', color='white', capthick=3, ms=3, elinewidth=0.7)
    #ax3.set_xlim(100,700)
    ax3.set_ylim(15, 180)
    ax3.set_xlabel('Spatial extent (km)')
    ax3.set_ylabel('Length scale (km)')

    #plt.tight_layout()

    f.subplots_adjust(right=0.86)

    cax = f.add_axes([0.87, 0.545, 0.025, 0.415])
    cb = plt.colorbar(mt, cax=cax, label='Cloud-top temperature ($^{\circ}$C)')
    cb.ax.tick_params(labelsize=12)

    cax = f.add_axes([0.87, 0.065, 0.025, 0.175])
    cb = plt.colorbar(mp, cax=cax, label='Wavelet power')
    cb.ax.tick_params(labelsize=12)

    plt.show()

    f = plt.figure()
    plt.imshow(ellipse)
    print(xl,yl)
    plt.plot(xl,yl, 'ro')
    plt.show()

    f = plt.figure()
    pos = np.argmin(np.abs((2*short+1)*wav['res']-arr))

    plt.imshow(wll[pos, :,:])
    print(xl,yl)
    plt.plot(xl,yl, 'ro')
    plt.show()
    f = plt.figure()
    plt.imshow(wll[amax[0], :,:])
    print(xl,yl)
    plt.plot(xl,yl, 'ro')
    plt.show()

    plt.figure()
    plt.plot(wll[amax[0],50,:])
    plt.plot(ellipse[50, :])
    plt.plot(wav['coeffs'][amax[0],50,:])

    print(np.sum(wll[amax[0],50,:]>0))
    print(amax[0])
Esempio n. 9
0
def circle():
    matplotlib.rc('xtick', labelsize=10)
    matplotlib.rc('ytick', labelsize=10)

    ellipse = np.zeros((100, 100)) + 5
    short = 25

    xcirc, ycirc = ua.draw_circle(50, 50, short)

    ellipse[ycirc, xcirc] = -80
    nb = 21
    wav = util.waveletT(ellipse, dx=1, dist=0.08, start=15,
                        nb=nb)  #dx=5, dist=0.08,start=15,nb=15 )

    wll = wav['t']
    arr = np.round(wav['scales'])
    print('AVAIL WAVELET SCALES: ', arr)
    maxs = np.zeros_like(wll)
    yl = []
    xl = []
    for nb in range(wav['t'].shape[0]):

        orig = float(wav['scales'][nb])

        wl = wav['t'][nb, :, :]

        maxout = (wl == ndimage.maximum_filter(wl, (5, 5),
                                               mode='constant',
                                               cval=np.amax(wl) + 1)
                  )  # (np.round(orig / 5))

        try:
            yy, xx = np.where((maxout == 1) & (wl > orig**.5))
        except IndexError:
            continue

        for y, x in zip(yy, xx):
            #print(arr[nb],y,x)

            maxs[nb, y, x] = 1
            #print('Power value',wll[nb,y,x])
            yl.append(y)
            xl.append(x)

    print('finish loop')

    maxout2 = (wll == ndimage.maximum_filter(wll, (5, 5, 5),
                                             mode='reflect',
                                             cval=np.amax(wl) + 1)
               )  # (np.round(orig / 5))

    zl, yl, xl = np.where((maxout2 == 1) & (
        wll > arr.repeat(100 * 100, axis=0).reshape((nb + 1, 100, 100))**.5))
    wlmax = np.max(wll[zl, yl, xl])
    pl = np.where(wll == wlmax)

    zll, yll, xll = np.where((maxs == 1))
    wllmax = np.max(wll[zll, yll, xll])
    pll = np.where(wll == wllmax)

    print('Max point scales: ', arr[zl])

    amax = np.unravel_index(np.argmax(wll), wll.shape)
    print('Totalmax whole domain', arr[amax[0]])
    print('Totalmax 3d', arr[pl[0]])
    print('Totalmax 2d', arr[pll[0]])
    print('Scale of perfect circle', (2 * short + 1) * wav['res'])
    print('Pixel-adjusted perfect circle', (2 * short + 1 - 2) * wav['res'])
    print('Max scale in pixelacross', arr[amax[0]] / wav['res'])
    print('Power max', np.max(wll))

    f = plt.figure(figsize=(6.5, 11), dpi=300)
    #
    gridspec.GridSpec(3, 1)
    posi = 56  #116 ## 118
    ax1 = plt.subplot2grid((3, 1), (0, 0), rowspan=2)
    ax3 = plt.subplot2grid((3, 1), (2, 0))
    #
    lev = np.arange(-90, -39, 4)
    mt = ax1.contourf(np.arange(wll.shape[2]) * 5,
                      np.arange(wll.shape[1]) * 5,
                      ellipse,
                      cmap='Greys',
                      vmax=-40,
                      levels=lev)
    ax1.plot(np.arange(wll.shape[2]) * 5,
             [posi * 5] * len(np.arange(wll.shape[2]) * 5),
             linestyle='--',
             linewidth=2,
             color='black')
    ax1.invert_yaxis()
    ax1.legend(loc=4)
    ax1.set_ylabel('Spatial extent (km)')
    #colors = cm.viridis(np.linspace(0, 1, len([0,1, 2,5,10,20,40,60,80,100])))
    #
    mp = ax3.contourf(
        np.arange(wll.shape[2]) * 5,
        arr,
        wll[:, posi, :],
        levels=[0, 1, 2, 5, 10, 20, 40, 80, 100, 130, 150, 180, 200, 300, 400],
        cmap='viridis')
    maxs = np.mean(maxs[:, posi - 1:posi + 2, :], 1)  # -1, +2
    #ax3.contour(np.arange(wll.shape[2])*5, arr,maxs, cmap='Greys_r')

    ppos = np.where(maxs)

    #for p1, p2 in zip(ppos[1], ppos[0]):
    #    ax3.errorbar((np.arange(wll.shape[2])*5)[p1], arr[p2], xerr=arr[p2]/2, fmt='o', ecolor='white', color='white', capthick=3, ms=3, elinewidth=0.7)
    #ax3.set_xlim(100,700)
    ax3.set_ylim(15, 180)
    ax3.set_xlabel('Spatial extent (km)')
    ax3.set_ylabel('Length scale (km)')

    #plt.tight_layout()

    f.subplots_adjust(right=0.86)

    cax = f.add_axes([0.87, 0.545, 0.025, 0.415])
    cb = plt.colorbar(mt, cax=cax, label='Cloud-top temperature ($^{\circ}$C)')
    cb.ax.tick_params(labelsize=12)

    cax = f.add_axes([0.87, 0.065, 0.025, 0.175])
    cb = plt.colorbar(mp, cax=cax, label='Wavelet power')
    cb.ax.tick_params(labelsize=12)

    plt.show()

    f = plt.figure()
    plt.imshow(ellipse)
    print(xl, yl)
    plt.plot(xl, yl, 'ro')
    plt.show()

    f = plt.figure()
    pos = np.argmin(np.abs((2 * short + 1) * wav['res'] - arr))

    plt.imshow(wll[pos, :, :])
    print(xl, yl)
    plt.plot(xl, yl, 'ro')
    plt.show()
    f = plt.figure()
    plt.imshow(wll[amax[0], :, :])
    print(xl, yl)
    plt.plot(xl, yl, 'ro')
    plt.show()

    plt.figure()
    plt.plot(wll[amax[0], 50, :])
    plt.plot(ellipse[50, :])
    plt.plot(wav['coeffs'][amax[0], 50, :])

    print(np.sum(wll[amax[0], 50, :] > 0))
    print(amax[0])