Ejemplo n.º 1
0
def get_data(usr_case=None):

    file_met, usr_case = get_files(usr_case)
    index_field, name_field = get_index_field(usr_case)
    dfBBY = []
    dfCZD = []
    dfFRS = []
    # period = get_request_dates(usr_case)

    for f in file_met:
        loc = f[-12:-9]
        if loc == "bby":
            #            elev = 15
            #            dfBBY.append(mf.parse_surface(
            #                f, index_field[loc], name_field, elev))
            dfBBY.append(mf.parse_surface(f))
        elif loc == "czc":
            #            elev = 462
            #            dfCZD.append(mf.parse_surface(
            #                f, index_field[loc], name_field, elev))
            dfCZD.append(mf.parse_surface(f))
        elif loc == "frs":
            #            elev = 462
            #            dfFRS.append(mf.parse_surface(
            #                f, index_field[loc], name_field, elev))
            dfFRS.append(mf.parse_surface(f))

    if len(dfBBY) > 1:
        meteoBBY = pd.concat(dfBBY)
    else:
        meteoBBY = dfBBY[0]

    if len(dfCZD) > 1:
        meteoCZD = pd.concat(dfCZD)
    else:
        meteoCZD = dfCZD[0]

    if len(dfFRS) > 1:
        meteoFRS = pd.concat(dfFRS)
    else:
        try:
            meteoFRS = dfFRS[0]
        except IndexError:
            index = meteoBBY.index
            columns = meteoBBY.columns
            meteoFRS = pd.DataFrame(index=index, columns=columns)

    return meteoBBY, meteoCZD, meteoFRS, usr_case
Ejemplo n.º 2
0
def get_data(usr_case=None):

    file_met, usr_case = get_files(usr_case)
    index_field, name_field = get_index_field(usr_case)
    dfBBY = []
    dfCZD = []
    dfFRS = []
    # period = get_request_dates(usr_case)

    for f in file_met:
        loc = f[-12:-9]
        if loc == 'bby':
#            elev = 15
#            dfBBY.append(mf.parse_surface(
#                f, index_field[loc], name_field, elev))
            dfBBY.append(mf.parse_surface(f))
        elif loc == 'czc':
#            elev = 462
#            dfCZD.append(mf.parse_surface(
#                f, index_field[loc], name_field, elev))
            dfCZD.append(mf.parse_surface(f))            
        elif loc == 'frs':
#            elev = 462
#            dfFRS.append(mf.parse_surface(
#                f, index_field[loc], name_field, elev))
            dfFRS.append(mf.parse_surface(f))

    if len(dfBBY) > 1:
        meteoBBY = pd.concat(dfBBY)
    else:
        meteoBBY = dfBBY[0]

    if len(dfCZD) > 1:
        meteoCZD = pd.concat(dfCZD)
    else:
        meteoCZD = dfCZD[0]

    if len(dfFRS) > 1:
        meteoFRS = pd.concat(dfFRS)
    else:
        try:
            meteoFRS = dfFRS[0]
        except IndexError:
            index = meteoBBY.index
            columns = meteoBBY.columns
            meteoFRS = pd.DataFrame(index=index, columns=columns)

    return meteoBBY, meteoCZD, meteoFRS, usr_case
Ejemplo n.º 3
0
def get_surface_data(usr_case, homedir=None):
    ''' set directory and input files '''
    case = 'case' + usr_case.zfill(2)
    casedir = homedir + '/' + case
    out = os.listdir(casedir)
    out.sort()
    files = []

    for f in out:
        if f[-3:] in ['met', 'urf']:
            files.append(f)

    file_met = []
    for f in files:
        if f[:3] == 'bby':
            file_met.append(casedir + '/' + f)

    df = []
    for f in file_met:
        meteo = mf.parse_surface(f)
        df.append(meteo)

    if len(df) > 1:
        surface = pd.concat(df)
    else:
        surface = df[0]

    return surface
def get_buoy(f=None, target_index=None):
    buoy = mf.parse_buoy(f)
    bucomp = -buoy.SPD * np.sin(np.radians(buoy.DIR))
    bvcomp = -buoy.SPD * np.cos(np.radians(buoy.DIR))
    buoy['U'] = bucomp
    buoy['V'] = bvcomp
    buoy.drop(buoy.columns[[0, 1, 2, 3, 4]], axis=1, inplace=True)
    buoy = buoy.loc[target_index]

    return buoy
Ejemplo n.º 5
0
def get_buoy(f=None, target_index=None):
    buoy = mf.parse_buoy(f)
    bucomp = -buoy.SPD * np.sin(np.radians(buoy.DIR))
    bvcomp = -buoy.SPD * np.cos(np.radians(buoy.DIR))
    buoy['U'] = bucomp
    buoy['V'] = bvcomp
    buoy.drop(buoy.columns[[0, 1, 2, 3, 4]], axis=1, inplace=True)
    buoy = buoy.loc[target_index]

    return buoy
Ejemplo n.º 6
0
def main(option):

    if option == 'compare':
        dfBBY = []
        dfCZD = []
        for f in file_met:
            loc = f[-12:-9]
            if loc == 'bby':
                dfBBY.append(mf.parse_surface(f))
            elif loc == 'czc':
                dfCZD.append(mf.parse_surface(f))

        if len(dfBBY) > 1:
            meteoBBY = pd.concat(dfBBY)
        else:
            meteoBBY = dfBBY[0]

        if len(dfCZD) > 1:
            meteoCZD = pd.concat(dfCZD)
        else:
            meteoCZD = dfCZD[0]

        make_compare(BBY=meteoBBY, CZD=meteoCZD)

    else:

        df = []
        for f in file_met:
            # df.append(mf.parse_surface(f,index_field[usr_loc],name_field,elev[usr_loc]))
            df.append(mf.parse_surface(f))

        if len(df) > 1:
            meteo = pd.concat(df)
        else:
            meteo = df[0]

        make_meteo(meteo)
        make_thermo(meteo)

    plt.show()
def get_surf(f=None):
    surf = mf.parse_surface(f)
    sucomp = -surf.wspd * np.sin(np.radians(surf.wdir))
    svcomp = -surf.wspd * np.cos(np.radians(surf.wdir))
    surf['U'] = sucomp
    surf['V'] = svcomp
    surf.drop(surf.columns[[0, 1, 2, 5, 6, 7, 8, 9, 10]], axis=1, inplace=True)
    g = pd.TimeGrouper('10T')
    surfU = surf.U.groupby(g).mean()
    surfV = surf.V.groupby(g).mean()
    surf10 = pd.DataFrame(data={'U': surfU, 'V': surfV})

    return surf10
Ejemplo n.º 8
0
def get_surf(f=None):
    surf = mf.parse_surface(f)
    sucomp = -surf.wspd * np.sin(np.radians(surf.wdir))
    svcomp = -surf.wspd * np.cos(np.radians(surf.wdir))
    surf['U'] = sucomp
    surf['V'] = svcomp
    surf.drop(surf.columns[[0, 1, 2, 5, 6, 7, 8, 9, 10]], axis=1, inplace=True)
    g = pd.TimeGrouper('10T')
    surfU = surf.U.groupby(g).mean()
    surfV = surf.V.groupby(g).mean()
    surf10 = pd.DataFrame(data={'U': surfU, 'V': surfV})

    return surf10
Ejemplo n.º 9
0
def make_quiver(ax):
    """ make quiver plot for each file """
    for i, f in enumerate(buoyfiles):
        df = mf.parse_buoy(f)
        df2 = df[st:en]
        time = df2.index[::2]
        wspd = df2.SPD[::2]
        wdir = df2.DIR[::2]

        X = np.asarray(range(len(wspd)))
        Y = np.zeros(len(wspd))
        U = np.asarray(-wspd*np.sin(wdir*np.pi/180.))
        V = np.asarray(-wspd*np.cos(wdir*np.pi/180.))

        if len(time) > 0:
            ntime = len(time)
            xticks = range(0, ntime, 18)
            xticklabels = time[xticks]
            date_fmt = '%d\n%H'
            xtlabels = [t.strftime(date_fmt) for t in xticklabels]

            Q = ax[i].quiver(
                X, Y, U, V, width=0.002, facecolor='b', edgecolor='k', headwidth=3)
            if i == 0:
                ax[i].quiverkey(Q, 0.9, 0.1, 10,
                                r'$10 \frac{m}{s}$',
                                fontproperties={'weight': 'bold', 'size': 14})

            ax[i].set_xticks(xticks)
            ax[i].set_xticklabels(xtlabels)
            ax[i].invert_xaxis()
            ax[i].set_xlim([ntime+5, -5])
            ax[i].set_ylim([-0.02, 0.02])
            filename = os.path.basename(f)
            buoyname = 'B'+filename[3:5]
            degree_sign = u'\N{DEGREE SIGN}'
            atext = buoyname+' - '+buoylats[buoyname]+degree_sign+'N'
            ax[i].text(0.05, 0.1, atext, transform=ax[i].transAxes, fontsize=14,
                       verticalalignment='bottom')
        else:
            ax[i].text(
                0.5, 0.5, 'NO DATA', weight='bold', transform=ax[i].transAxes)
            filename = os.path.basename(f)
            buoyname = 'B'+filename[3:5]
            atext = buoyname+' - '+buoylats[buoyname]+degree_sign+'N'
            ax[i].text(0.05, 0.1, atext, transform=ax[i].transAxes, fontsize=14,
                       verticalalignment='bottom')

        ax[i].set_yticklabels([''])

        plt.draw()
Ejemplo n.º 10
0
def main():
    ''' raf files are created using matlab function 
	W:/MATLAB/p3_matlab/convert_to_raf_ascii.m '''
    f = '/home/rvalenzuela/P3_stdtape/ascii/010123I.std.ascii.raf'
    ini, end = get_sounding_times()

    m = 0
    for i, e in zip(ini, end):

        if m >= 0:
            sound = mf.parse_acft_sounding(f, i, e, return_interp=True)
            # print sound
            temp = sound.AIR_TEMP.values  #[C]
            dewp = sound.DEW_POINT.values  #[C]
            wspd = sound.WIND_SPD.values
            wdir = sound.WIND_DIR.values
            u, v = get_components(wspd, wdir)
            pres = sound.AIR_PRESS.values
            lats = sound.LAT.values
            lons = sound.LON.values
            hgt = sound.index.values
            theta = sound.theta.values
            thetaeq = sound.thetaeq.values
            bvf_dry = sound.bvf_dry.values
            bvf_moist = sound.bvf_moist.values
            location = [np.average(lats), np.average(lons)]

            # plot_skew1(temp=temp, dewp=dewp, u=u, v=v, press=pres, date=[i, e],	loc=location)
            plot_skew2(temp=temp,
                       dewp=dewp,
                       u=u,
                       v=v,
                       press=pres,
                       date=[i, e],
                       loc=location,
                       hgt=hgt)
            # plot_thermo(temp=temp, dewp=dewp, u=u, v=v, hgt=hgt,theta=theta, thetaeq=thetaeq,
            # 			 bvf_dry=bvf_dry, bvf_moist=bvf_moist,press=pres, date=[i, e], loc=location,top=5000)

        m += 1

    # compare_potential_temp(df,date)
    # break

    plt.show()
Ejemplo n.º 11
0
def get_pressure(case=None, homedir=None):
    from glob import glob

    fpath = homedir + '/SURFACE/case{}/bby*'
    surf_files = glob(fpath.format(str(case).zfill(2)))
    surf_files.sort()
    df_list = []
    for f in surf_files:
        df_list.append(mf.parse_surface(f))

    if len(df_list) > 1:
        df = pd.concat(df_list)
    else:
        df = df_list[0]

    g = pd.TimeGrouper('60T')
    dfg = df['press'].groupby(g).mean()
    return dfg
Ejemplo n.º 12
0
def main():

	case=7
	mesocase=get_mesocase(case)
	mesofiles=mesocase['files']
	st = mesocase['dates'][0]
	en = mesocase['dates'][1]
	stations=mesocase['stations']
	var=['TMP','RELH','PMSL','DRCT']
	hl=[]
	xticks = pd.date_range(start=st,end=en, freq='3H')
	fig,ax = plt.subplots(len(var),1,figsize=(13,10),sharex=True)
	for i,f in enumerate(mesofiles):
		df=mf.parse_mesowest_excel(f)
		df2=df[st:en]
		for j in range(len(var)):
			l=ax[j].plot(df2.index,df2[var[j]],'-o')
			ax[j].invert_xaxis()
			ax[j].set_xticks(xticks)
			ax[j].set_xlim([en,st])
			if j == 0: hl.append(l)

	deg_sign= u'\N{DEGREE SIGN}'
	lns = [line[0] for line in hl]
	ax[0].legend(lns, stations, loc=0)
	ax[0].set_ylabel('Temperature [' +deg_sign+'C]')
	ax[1].set_ylim([40,105])
	ax[1].set_ylabel('Relative humidity [%]')
	ax[2].set_ylim([1015,1025])
	ax[2].set_ylabel('Pressure MSL [hPa]')
	
	ax[3].set_yticks(range(0,360+60,60))
	ax[3].set_ylabel('Wind direction [deg]')
	datefmt = dates.DateFormatter('%d\n%H')
	ax[3].xaxis.set_major_formatter(datefmt)
	ax[3].set_xlabel(r'$\Leftarrow$'+' Time [UTC]')
	
	t1='METAR surface observations (source: Mesowest)'
	t2='\nDate: ' + st.strftime('%b-%Y')
	plt.suptitle(t1+t2)
	plt.show(block=False)
Ejemplo n.º 13
0
def main():

	''' raf files are created using matlab function 
	W:/MATLAB/p3_matlab/convert_to_raf_ascii.m '''
	f='/home/rvalenzuela/P3_stdtape/ascii/010123I.std.ascii.raf'
	ini,end = get_sounding_times()

	m=0
	for i,e in zip(ini,end):

		if m>=0:
			sound=mf.parse_acft_sounding(f, i, e, return_interp=True)
			# print sound
			temp=sound.AIR_TEMP.values #[C]
			dewp=sound.DEW_POINT.values #[C]
			wspd=sound.WIND_SPD.values
			wdir=sound.WIND_DIR.values
			u,v = get_components(wspd,wdir)
			pres=sound.AIR_PRESS.values
			lats=sound.LAT.values
			lons=sound.LON.values
			hgt=sound.index.values
			theta=sound.theta.values
			thetaeq=sound.thetaeq.values
			bvf_dry=sound.bvf_dry.values
			bvf_moist=sound.bvf_moist.values
			location=[np.average(lats),np.average(lons)]

			# plot_skew1(temp=temp, dewp=dewp, u=u, v=v, press=pres, date=[i, e],	loc=location)
			plot_skew2(temp=temp, dewp=dewp, u=u, v=v, press=pres, date=[i, e],	loc=location, hgt=hgt)
			# plot_thermo(temp=temp, dewp=dewp, u=u, v=v, hgt=hgt,theta=theta, thetaeq=thetaeq,
			# 			 bvf_dry=bvf_dry, bvf_moist=bvf_moist,press=pres, date=[i, e], loc=location,top=5000)


		m+=1

	
	# compare_potential_temp(df,date)
	# break

	plt.show()
Ejemplo n.º 14
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()
Ejemplo n.º 15
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()
Ejemplo n.º 16
0
def run(case, tta=None, plot_theory=False, grid=True, ax=None, homedir=None,
        color_surf=(0, 0, 0.5, 0.5),color_wp=(0, 0.5, 0, 0.5),
        add_date=False, wprof_hgt=None):

    " process gapflow analysis"

    " first index is mesowest file, second index is \
    BBY (1 or more files) "
    f = get_filenames(case,homedir)

    " parse mesowest data "
    meso = mf.parse_mesowest_excel(f[0])
    t = get_times(case)
    mpress = meso.loc[t[0]: t[1]]['PMSL'].values
    mesoidx = meso.loc[t[0]: t[1]].index

    " parse surface BBY data "
    if len(f[1]) > 1:
        '  more than one day of obs '
        surf = mf.parse_surface(f[1][0])
        for ff in f[1][1:]:
            surf = surf.append(mf.parse_surface(ff))
    else:
        ' only one day '
        surf = mf.parse_surface(f[1][0])

    " resample to 1min so we can find \
    mesowest index "
    surf = surf.resample('1T').interpolate()

    " adjust bias is mesowest in case 1 and 2 "
    if case in [1, 2]:
        bias = 9
    else:
        bias = 0
    spress = surf.loc[mesoidx]['press'].values - bias

    " BBY and mesowest pressure difference "
    pressDiff = spress - mpress

    " BBY surface winds "
    swspd = surf.loc[mesoidx]['wspd'].values
    swdir = surf.loc[mesoidx]['wdir'].values
    ucomp = -swspd*np.sin(np.radians(swdir))

    " gapflow dataframe "
    d = {'ucomp': ucomp, 'wspd': swspd, 'wdir': swdir,
         'pdiff': pressDiff, 'Bpress': spress, 'Kpress': mpress}
    gapflow = pd.DataFrame(data=d, index=mesoidx)

    " removes rows with NaN "
    gapflow = gapflow[np.isfinite(gapflow['Kpress'])]

    " add wind profiler data at target altitude "
    out = get_windprof(case,
                       gapflow_time=gapflow.index,
                       top_hgt_km=wprof_hgt,
                       homedir=homedir)
    wp_wspd, wp_wdir = out
    wp_ucomp = -wp_wspd*np.sin(np.radians(wp_wdir))
    gapflow['wp_ws'] = wp_wspd
    gapflow['wp_wd'] = wp_wdir
    gapflow['wp_ucomp'] = wp_ucomp

    " Mass etal 95 equation "
    # blh = get_BLH(case)
    blh = 500  #[m]
    massPa, massU = mass_eq(air_density=1.24, BLH=blh)
    path = make_polygon(massPa, massU)

    gapflow = check_polygon(gapflow, path)
    # gapflow['gapflow'] = ((gapflow.poly is True) & (gapflow.wdir <= 120))
#    sub = gapflow[(gapflow.poly is True) & (gapflow.wdir <= 120)]

    " theoretical lines "
    if plot_theory:
        if ax is None:
            fig, ax = plt.subplots(figsize=(8, 7))
            ax.set_xlabel('Pressure difference, BBY-SCK [hPa]')
            ax.set_ylabel('BBY zonal wind [m s-1]')
        # ax.scatter(gapflow['pdiff'], gapflow['ucomp'],
        #            color=color_surf, label='surf')
        # ax.scatter(gapflow['pdiff'], gapflow['wp_ucomp'],
        #            color=color_wp, label='wp')
        # ax.scatter(sub['pdiff'], sub['ucomp'], color='r')
        ax.plot(massPa/100, massU[0], marker=None)
        ax.plot(massPa/100, massU[1], linestyle='--', color='r')
        ax.plot(massPa/100, massU[2], linestyle='--', color='r')
        if grid:
            ax.grid(True)
        ax.set_xlim([-12, 1])
        ax.set_ylim([-20, 15])
        ini = mesoidx[0]
        end = mesoidx[-1]
        date = ini.strftime('%b-%Y ')
        beg = ini.strftime('%d')
        end = end.strftime('%d')
#        ax.text(0.03, 0.76, timetxt.format(str(case).zfill(2),
#                                           date, beg, end),
        if add_date is True:
            if beg == end:
                ax.text(0.03, 0.85,'{} {}'.format(beg,date),
                        fontsize=12,
                        transform=ax.transAxes)
            else:
                ax.text(0.03, 0.85,'{}-{} {}'.format(beg,end,date),
                        fontsize=12,
                        transform=ax.transAxes)

    return gapflow
Ejemplo n.º 17
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
Ejemplo n.º 18
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
Ejemplo n.º 19
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(
name = {"bby": "BodegaBay", "czc": "Cazadero"}
elev = {"bby": 15, "czc": 462}
if usr_case in ["1", "2"]:
    index_field = {"bby": [3, 4, 10, 5, 6, 11, 13], "czc": [3, 4, 10, 5, 6, 11, 13]}
elif usr_case in ["3", "4", "5", "6", "7"]:
    index_field = {"bby": [3, 6, 9, 10, 12, 17, 26], "czc": [3, 4, 5, 6, 8, 13, 22]}
else:
    index_field = {"bby": [3, 4, 5, 6, 8, 13, 15], "czc": [3, 4, 5, 6, 8, 13, 15]}

""" make dataframe for each station """
dfBBY = []
dfCZD = []
for f in file_met:
    loc = f[-12:-9]
    if loc == "bby":
        dfBBY.append(mf.parse_surface(f, index_field[loc], name_field, elev[loc]))
    elif loc == "czc":
        dfCZD.append(mf.parse_surface(f, index_field[loc], name_field, elev[loc]))

""" concatenate dataframes when there is
more than one day of data """
if len(dfBBY) > 1:
    meteoBBY = pd.concat(dfBBY)
else:
    meteoBBY = dfBBY[0]

if len(dfCZD) > 1:
    meteoCZD = pd.concat(dfCZD)
else:
    meteoCZD = dfCZD[0]
rcParams['mathtext.default'] = 'sf'


def cosd(array):
    return np.cos(np.radians(array))


homedir = '/localdata'
topdf = False

case = range(13, 14)
res = 'coarse'
o = 'case{}_total_wind_{}.pdf'

surf_file = '/Users/raulvalenzuela/Data/SURFACE/case13/czc04047.met'
czd = mf.parse_surface(surf_file)

surf_file = '/Users/raulvalenzuela/Data/SURFACE/case13/bby04047.met'
bby = mf.parse_surface(surf_file)

czdh = czd.preciph[~czd.preciph.isnull()]
bbyh = bby.preciph[~bby.preciph.isnull()]
''' creates plot with seaborn style '''
# with sns.axes_style("white"):
#     sns.set_style('ticks',
#               {'xtick.direction': u'in',
#                'ytick.direction': u'in'}
#               )

scale = 1.3
plt.figure(figsize=(8 * scale, 6 * scale))
Ejemplo n.º 22
0
        ax=None,
        homedir=None,
        color_surf=(0, 0, 0.5, 0.5),
        color_wp=(0, 0.5, 0, 0.5),
        add_date=False,
        wprof_hgt=None):

    " process gapflow analysis"

    " first index is mesowest file, second index is \
    BBY (1 or more files) "

    f = get_filenames(case, homedir)

    " parse mesowest data "
    meso = mf.parse_mesowest_excel(f[0])
    t = get_times(case)
    mpress = meso.loc[t[0]:t[1]]['PMSL'].values
    mesoidx = meso.loc[t[0]:t[1]].index

    " parse surface BBY data "
    if len(f[1]) > 1:
        '  more than one day of obs '
        surf = mf.parse_surface(f[1][0])
        for ff in f[1][1:]:
            surf = surf.append(mf.parse_surface(ff))
    else:
        ' only one day '
        surf = mf.parse_surface(f[1][0])

    " resample to 1min so we can find \
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(),
Ejemplo n.º 24
0
import Meteoframes as mf 
import matplotlib.pyplot as plt 
from matplotlib import dates
import datetime

import seaborn as sns 
import pandas as pd

f='/home/rvalenzuela/GPS_IWV/case03/bby_gpsiwv_010123-24.dat'

df = mf.parse_gps_iwv(f)

st = datetime.datetime(2001, 1, 23, 0, 0)
en = datetime.datetime(2001, 1, 25, 0, 0)
xticks = pd.date_range(start=st,end=en, freq='3H')

fig,ax=plt.subplots()
ax.plot(df.index, df['IPW'],'-o')
ax.set_xticks(xticks)
ax.invert_xaxis()
datefmt = dates.DateFormatter('%H\n%d')
ax.xaxis.set_major_formatter(datefmt)
ax.set_xlabel(r'$\Leftarrow$'+' Time [UTC]')
ax.set_ylabel('IWV [cm]')
t1='GPS-IWV at Bodega Bay'
t2='\nDate: ' + st.strftime('%b-%Y')
plt.title(t1+t2)

plt.show(block=False)
rcParams['mathtext.default'] = 'sf'

def cosd(array):
    return np.cos(np.radians(array))


homedir = '/localdata'
topdf = False

case = range(13,14)
res = 'coarse'
o = 'case{}_total_wind_{}.pdf'

# surf_file = '/Users/raulvalenzuela/Data/SURFACE/case13/czc04047.met'
surf_file = '/localdata/SURFACE/case13/czc04047.met'
czd = mf.parse_surface(surf_file)
czdh = czd.preciph[~czd.preciph.isnull()]
surf_file = '/localdata/SURFACE/case13/bby04047.met'
bby = mf.parse_surface(surf_file)
bbyh = bby.preciph[~czd.preciph.isnull()]

''' creates plot with seaborn style '''
# with sns.axes_style("white"):
#     sns.set_style('ticks',
#               {'xtick.direction': u'in',
#                'ytick.direction': u'in'}
#               )

scale=1.3
plt.figure(figsize=(8*scale, 6*scale))
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]
Ejemplo n.º 27
0
    return surface


def make_arrays(resolution='coarse',
                surface=False,
                case=None,
                period=False,
                homedir=None):

    wpfiles = get_filenames(case, homedir=homedir + '/WINDPROF')

    wp = []
    ncols = 0  # number of timestamps
    for f in wpfiles:
        if resolution == 'fine':
            wp.append(mf.parse_windprof(f, 'fine'))
        elif resolution == 'coarse':
            wp.append(mf.parse_windprof(f, 'coarse'))
        else:
            print 'Error: resolution has to be "fine" or "coarse"'
        ncols += 1
    ''' creates 2D arrays with spd and dir '''
    nrows = len(
        wp[0].HT.values)  # number of altitude gates (fine same as coarse)
    hgt = wp[0].HT.values
    #    print len(hgt)
    wspd = np.empty([nrows, ncols])
    wdir = np.empty([nrows, ncols])
    timestamp = []
    for i, p in enumerate(wp):
        timestamp.append(p.timestamp)
Ejemplo n.º 28
0
from rass_thetav import get_thetav
from glob import glob
from rv_utilities import add_colorbar, format_xaxis, format_yaxis

homedir = os.path.expanduser('~')


for r in range(8, 14):
    ''' case 14 does not have RASS obs '''
    print 'case '+str(r)
    print '-----------------'
    fs = glob(homedir+'/RASS/c'+str(r).zfill(2)+'/*.cns')
    fs.sort()

    for n, f in enumerate(fs):
        T, Tc, Hgt, timestamp = mf.parse_rass(f)
        if n == 0:
            Tstack = T
            Hgtstack = Hgt
            ts = np.array(timestamp)
        else:
            Tstack = np.hstack((Tstack, T))
            Hgtstack = np.hstack((Hgtstack, Hgt))
            ts = np.hstack((ts, np.array(timestamp)))

    thetav = get_thetav(case=r, Tv_array=Tstack,
                        hgt_array=Hgtstack, homedir=homedir)
    titletxt = 'Case{} ({})'
    fig, ax = plt.subplots(2, 1, sharex=True)
    im = ax[0].imshow(Tstack, interpolation='none', origin='lower')
    ax[0].set_title(titletxt.format(str(r), ts[0].strftime('%Y-%b')))
Ejemplo n.º 29
0
        'bby': [3, 6, 9, 10, 12, 17, 26],
        'czc': [3, 4, 5, 6, 8, 13, 22]
    }
else:
    index_field = {
        'bby': [3, 4, 5, 6, 8, 13, 15],
        'czc': [3, 4, 5, 6, 8, 13, 15]
    }
''' make dataframe for each station '''
dfBBY = []
dfCZD = []
for f in file_met:
    loc = f[-12:-9]
    if loc == 'bby':
        dfBBY.append(
            mf.parse_surface(f, index_field[loc], name_field, elev[loc]))
    elif loc == 'czc':
        dfCZD.append(
            mf.parse_surface(f, index_field[loc], name_field, elev[loc]))
''' concatenate dataframes when there is
more than one day of data '''
if len(dfBBY) > 1:
    meteoBBY = pd.concat(dfBBY)
else:
    meteoBBY = dfBBY[0]

if len(dfCZD) > 1:
    meteoCZD = pd.concat(dfCZD)
else:
    meteoCZD = dfCZD[0]