Esempio n. 1
0
def main():

    for f in file_sound:
        print f
        df = mf.parse_sounding2(f)

        fname = os.path.basename(f)
        ''' removes file extension and split date '''
        raw_date = fname[:-4].split('_')
        ''' some files have preffix, so I take only datetime'''
        raw_date = raw_date[-2:]
        if len(raw_date[1]) == 6:
            raw_date = raw_date[0] + raw_date[1]
            date = dt.datetime.strptime(raw_date, "%Y%m%d%H%M%S")
        else:
            raw_date = raw_date[0] + raw_date[1]
            date = dt.datetime.strptime(raw_date, "%Y%m%d%H%M")

        # plot_skew(df,date)
        plot_thermo(df, date, top=5.)
        # compare_potential_temp(df,date)
        # break
        # print df
    plt.show()
Esempio n. 2
0
def main():

    for f in file_sound:
        print f
        df = mf.parse_sounding2(f)

        fname = os.path.basename(f)
        ''' removes file extension and split date '''
        raw_date = fname[:-4].split('_')
        ''' some files have preffix, so I take only datetime'''
        raw_date = raw_date[-2:]
        if len(raw_date[1]) == 6:
            raw_date = raw_date[0]+raw_date[1]
            date = dt.datetime.strptime(raw_date, "%Y%m%d%H%M%S")
        else:
            raw_date = raw_date[0]+raw_date[1]
            date = dt.datetime.strptime(raw_date, "%Y%m%d%H%M")

        # plot_skew(df,date)
        plot_thermo(df, date, top=5.)
        # compare_potential_temp(df,date)
        # break
        # print df
    plt.show()
for n,par in enumerate(params):
    
    " synth data "
    out = vitas.main(par[0])
    p3u = out[0]
    p3v = out[1]
    p3wU = out[2] # from 230deg
    p3z = out[3]
    p3ulist.extend(p3u[:16])    
    p3vlist.extend(p3v[:16])    
    
    
    " balloon data " 
    infiles3,_ = so.get_sounding_files(par[1], homedir='/localdata')
    infiles3.sort()
    df = mf.parse_sounding2(infiles3[par[2]])
    bSu = df.u.values
    bSv = df.v.values
    bSz = df.index.values

    """ reduce resolution of bS by averaging 100-m layer
        centered at p3 altitude """
    bot = 25    
    top = 26
    for z in p3z[:16]:    
        center_idx = np.where(bSz==z*1000.)[0]
        if not center_idx:
            bSulist.append(np.nan)
            bSvlist.append(np.nan)
        else:
            c = center_idx[0]
Esempio n. 4
0
def get_raw_array(soundvar, file_sound):

    file_sound.sort()
    ''' height grid with 10 m resolution'''

    var = []  # 2D list
    timestamps = []
    top_limit = 10000  # [m]
    hgtgrid = np.asarray(range(10, 4010, 10))
    for f in file_sound:
        ''' sounding timestamps approached to
        the nearest 20 minute grid point'''
        fname = os.path.basename(f)
        raw_date = fname[:-4].split('_')[-2:]
        y = raw_date[0][:4]
        m = raw_date[0][4:6]
        d = raw_date[0][6:8]
        hh = int(raw_date[1][0:2])
        mm = int(raw_date[1][2:4])
        nearest = round_to_nearest(mm, 20)
        if nearest == 60:
            hh += 1
            mm = 0
        else:
            mm = nearest
        if hh == 24:
            d = str(int(d) + 1)
            hh = 0
        raw_date = y+'-'+m+'-'+d+'T' + \
            str(hh).zfill(2)+':'+str(mm).zfill(2)+':'+str(0).zfill(2)
        timestamps.append(np.datetime64(raw_date))
        ''' variable '''
        df = mf.parse_sounding2(f)
        # print df
        sv = df[soundvar][df.index < top_limit].values

        Z = df.index[df.index < top_limit].values
        ''' interpolate to a common vertical grid '''
        f = interp1d(Z, sv)
        svinterp = np.asarray([])
        for h in hgtgrid:
            try:
                svinterp = np.append(svinterp, f(h))
            except ValueError:
                svinterp = np.append(svinterp, np.nan)
        ''' take care of vertical nan values in raw soundings;
        more gaps can show up if the top of the vertical grid
        is increased (i.e. higher balloon altitudes show more
        missing data)
        '''
        if np.any(np.isnan(svinterp)):
            nanidx = np.where(np.isnan(svinterp))[0]
            diff = np.diff(nanidx)
            idxjump = np.where(diff > 1)[0]
            ngaps = len(idxjump) + 1
            if ngaps > 1 and nanidx[-1] != svinterp.size - 1:
                gapidx = np.split(nanidx, idxjump + 1)
                for g in gapidx:
                    first = g[0]
                    last = g[-1]
                    ''' pick good values between nans and
                    make a linear interpolation (hopefully there
                    few nans) '''
                    if last + 1 == svinterp.size:
                        x = [hgtgrid[first - 1], hgtgrid[last]]
                        y = [svinterp[first - 1], svinterp[last]]
                    elif first == 0:
                        x = [hgtgrid[first], hgtgrid[last + 1]]
                        y = [svinterp[first], svinterp[last + 1]]
                    else:
                        x = [hgtgrid[first - 1], hgtgrid[last + 1]]
                        y = [svinterp[first - 1], svinterp[last + 1]]
                    f = interp1d(x, y)
                    svinterp[g] = [f(h) for h in hgtgrid[g]]

            elif ngaps == 1 and nanidx[-1] != svinterp.size - 1:
                first = nanidx[0]
                last = nanidx[-1]
                if last + 1 == svinterp.size:
                    x = [hgtgrid[first - 1], hgtgrid[last]]
                    y = [svinterp[first - 1], svinterp[last]]
                elif first == 0:
                    x = [hgtgrid[first], hgtgrid[last + 1]]
                    y = [svinterp[first], svinterp[last + 1]]
                else:
                    x = [hgtgrid[first - 1], hgtgrid[last + 1]]
                    y = [svinterp[first - 1], svinterp[last + 1]]
                f = interp1d(x, y)
                svinterp[nanidx] = [f(h) for h in hgtgrid[nanidx]]
            var.append(svinterp)
        else:
            ''' add column to list '''
            var.append(svinterp)
    ''' time grid with 20 minute spacing'''
    t1 = timestamps[0] - np.timedelta64(20, 'm')
    t2 = timestamps[-1] + np.timedelta64(20, 'm')
    dates = np.arange(t1, t2, dtype='datetime64[20m]')
    ''' get index of each sounding date '''
    tidx = []
    for t in timestamps:
        tidx.append(np.where(dates == t)[0][0])
    ''' assign soundings to 2D array '''
    soundarray = np.full((hgtgrid.size, dates.size), np.nan)
    for n, i in enumerate(tidx):
        soundarray[:, i] = var[n]

    return soundarray, var, tidx, hgtgrid, dates, timestamps
Esempio n. 5
0
def get_raw_array(soundvar, file_sound):

    file_sound.sort()

    ''' height grid with 10 m resolution'''

    var = []  # 2D list
    timestamps = []
    top_limit = 10000  # [m]
    hgtgrid = np.asarray(range(10, 4010, 10))
    for f in file_sound:

        ''' sounding timestamps approached to
        the nearest 20 minute grid point'''
        fname = os.path.basename(f)
        raw_date = fname[:-4].split('_')[-2:]
        y = raw_date[0][:4]
        m = raw_date[0][4:6]
        d = raw_date[0][6:8]
        hh = int(raw_date[1][0:2])
        mm = int(raw_date[1][2:4])
        nearest = round_to_nearest(mm, 20)
        if nearest == 60:
            hh += 1
            mm = 0
        else:
            mm = nearest
        if hh == 24:
            d = str(int(d)+1)
            hh = 0
        raw_date = y+'-'+m+'-'+d+'T' + \
            str(hh).zfill(2)+':'+str(mm).zfill(2)+':'+str(0).zfill(2)
        timestamps.append(np.datetime64(raw_date))

        ''' variable '''
        df = mf.parse_sounding2(f)
        # print df
        sv = df[soundvar][df.index < top_limit].values

        Z = df.index[df.index < top_limit].values
        ''' interpolate to a common vertical grid '''
        f = interp1d(Z, sv)
        svinterp = np.asarray([])
        for h in hgtgrid:
            try:
                svinterp = np.append(svinterp, f(h))
            except ValueError:
                svinterp = np.append(svinterp, np.nan)

        ''' take care of vertical nan values in raw soundings;
        more gaps can show up if the top of the vertical grid
        is increased (i.e. higher balloon altitudes show more
        missing data)
        '''
        if np.any(np.isnan(svinterp)):
            nanidx = np.where(np.isnan(svinterp))[0]
            diff = np.diff(nanidx)
            idxjump = np.where(diff > 1)[0]
            ngaps = len(idxjump) + 1
            if ngaps > 1 and nanidx[-1] != svinterp.size-1:
                gapidx = np.split(nanidx, idxjump+1)
                for g in gapidx:
                    first = g[0]
                    last = g[-1]
                    ''' pick good values between nans and
                    make a linear interpolation (hopefully there
                    few nans) '''
                    if last+1 == svinterp.size:
                        x = [hgtgrid[first-1], hgtgrid[last]]
                        y = [svinterp[first-1], svinterp[last]]
                    elif first == 0:
                        x = [hgtgrid[first], hgtgrid[last+1]]
                        y = [svinterp[first], svinterp[last+1]]
                    else:
                        x = [hgtgrid[first-1], hgtgrid[last+1]]
                        y = [svinterp[first-1], svinterp[last+1]]
                    f = interp1d(x, y)
                    svinterp[g] = [f(h) for h in hgtgrid[g]]

            elif ngaps == 1 and nanidx[-1] != svinterp.size-1:
                first = nanidx[0]
                last = nanidx[-1]
                if last+1 == svinterp.size:
                    x = [hgtgrid[first-1], hgtgrid[last]]
                    y = [svinterp[first-1], svinterp[last]]
                elif first == 0:
                    x = [hgtgrid[first], hgtgrid[last+1]]
                    y = [svinterp[first], svinterp[last+1]]
                else:
                    x = [hgtgrid[first-1], hgtgrid[last+1]]
                    y = [svinterp[first-1], svinterp[last+1]]
                f = interp1d(x, y)
                svinterp[nanidx] = [f(h) for h in hgtgrid[nanidx]]
            var.append(svinterp)
        else:
            ''' add column to list '''
            var.append(svinterp)

    ''' time grid with 20 minute spacing'''
    t1 = timestamps[0]-np.timedelta64(20, 'm')
    t2 = timestamps[-1]+np.timedelta64(20, 'm')
    dates = np.arange(t1, t2, dtype='datetime64[20m]')

    ''' get index of each sounding date '''
    tidx = []
    for t in timestamps:
        tidx.append(np.where(dates == t)[0][0])

    ''' assign soundings to 2D array '''
    soundarray = np.full((hgtgrid.size, dates.size), np.nan)
    for n, i in enumerate(tidx):
        soundarray[:, i] = var[n]

    return soundarray, var, tidx, hgtgrid, dates, timestamps
Esempio n. 6
0
nobs = ('n=7', 'n=11')

infiles3, _ = so.get_sounding_files('3', homedir='/localdata')
infiles7, _ = so.get_sounding_files('7', homedir='/localdata')

cmap = discrete_cmap(7, base_cmap='Set1')
color = (cmap(0), cmap(1))

infiles = (infiles3, infiles7)

for n, ax in enumerate(axes):

    first = True
    for f in infiles[n]:
        df = mf.parse_sounding2(f)
        x = np.expand_dims(df.bvf_moist.values, axis=1) * 10000
        y = np.expand_dims(df.index.values, axis=1)
        ax.plot(x, y, color=color[n], lw=0.5)
        top = 2000  # [m]
        top_idx = np.where(y == top)[0]
        if first is True:
            prof = x[:top_idx]
            first = False
        else:
            prof = np.hstack((prof, x[:top_idx]))
    meanx = np.expand_dims(np.nanmean(prof, axis=1), axis=1)
    y2 = y[:top_idx]
    ax.plot(meanx, y2, color=color[n], lw=3)
    xpos = 0.08
    ax.text(
nobs=('n=7','n=11')

infiles3,_ = so.get_sounding_files('3', homedir='/localdata')
infiles7,_ = so.get_sounding_files('7', homedir='/localdata')

cmap = discrete_cmap(7, base_cmap='Set1')
color=(cmap(0),cmap(1))

infiles=(infiles3,infiles7)


for n,ax in enumerate(axes):

    first = True
    for f in infiles[n]:
        df = mf.parse_sounding2(f)
        x = np.expand_dims(df.bvf_moist.values,axis=1)*10000
        y = np.expand_dims(df.index.values,axis=1)
        ax.plot(x,y,color=color[n],lw=0.5)
        top = 2000 # [m]
        top_idx = np.where(y == top)[0]
        if first is True:    
            prof = x[:top_idx]
            first = False
        else:
            prof = np.hstack((prof,x[:top_idx]))
    meanx = np.expand_dims(np.nanmean(prof,axis=1),axis=1)
    y2 = y[:top_idx]
    ax.plot(meanx,y2,color=color[n],lw=3)
    xpos = 0.08
    ax.text(xpos,0.9,ax.get_gid(),