odb_wind_profiler.tar.gz




#Metview Macro

#  **************************** LICENSE START ***********************************
# 
#  Copyright 2019 ECMWF. This software is distributed under the terms
#  of the Apache License version 2.0. In applying this license, ECMWF does not
#  waive the privileges and immunities granted to it by virtue of its status as
#  an Intergovernmental Organization or submit itself to any jurisdiction.
# 
#  ***************************** LICENSE END ************************************
# 

# define station id
statid="95759"

# read db
db = read("wprof.odb")

# define query for u wind component
q_u="select obsvalue as val, " &
      "vertco_reference_1 as p, " &
      "date@hdr as date," &
      "time@hdr as time" &        
      "where varno=3 and statid='" & statid & "'"  

# define query for v wind component
q_v="select obsvalue as val, " &
      "vertco_reference_1 as p, " &
      "date as d," &
      "time as t"  &  
      "where varno=4 and statid='" & statid & "'"  
 
# define query for metadata 
q_meta="select DISTINCT lat@hdr as lat, lon@hdr as lon, " &
     "andate, antime where statid='" & statid & "'" 
      
# filter u
f_u = odb_filter(
	odb_query	:	q_u,
	odb_data	:	db
	)
	
# filter v
f_v = odb_filter(
	odb_query	:	q_v,
	odb_data	:	db
	)

# filter metadata
f_m = odb_filter(
	odb_query	:	q_meta,
	odb_data	:	db
	)
	
# read the odb columns into vectors 
u = values(f_u, "val")
d = values(f_u, "date")
t = values(f_u, "time")
p = values(f_u, "p")/100   # pressure is converted to hPa
v = values(f_v, "val")

# read values for the title
lat = values(f_m, "lat")[1]
lon = values(f_m, "lon")[1]
andate = values(f_m, "andate")[1]
antime = values(f_m, "antime")[1]

# build date list
dLst=nil
for i=1 to count(d) do
    hh = t[i]/10000
    mm = (t[i]-(hh*10000))/100.
    dLst=dLst & [date(d[i]) + hour(hh) + minute(mm)]
end for

# define an input visualiser
vis = input_visualiser(
	input_plot_type	:	"xy_vectors",
	input_x_type	:	"date",
	input_y_type	:	"number",
	input_date_x_values	:	dLst,
	input_y_values	:	tolist(p),
	input_x_component_values	:	tolist(u),
	input_y_component_values	:	tolist(v)
    )


# wind plotting style
wp = mwind(
    wind_thinning_factor                  : 1.0,
    legend                                : "on",
    wind_advanced_method                  : "on",
    wind_advanced_colour_selection_type   : "interval",
    wind_advanced_colour_min_value        : 1,
    wind_advanced_colour_level_interval   : 1,
    wind_advanced_colour_max_level_colour : "RGB(1,0.05373,0.003922)",
    wind_advanced_colour_min_level_colour : "RGB(0.003922,0.2031,1)",
    wind_arrow_thickness                  : 2,
    wind_arrow_unit_velocity              : 10
    )
    

# define title
title = mtext(text_font_size : 0.4,
            text_line_1 : "WIND PROFILER AN date=" & andate & " time=" & antime & 
            " station=" & statid &
            " lat=" & lat & " lon=" & lon)
                   
# horizontal axis
h_axis = maxis(
	axis_position	:	"left",
	axis_title_text	:	"Time",
	axis_minor_tick	:	"on",
	axis_minor_tick_count	:	5,
	axis_date_type : "hours",
	axis_hours_label: "on",
	axis_hours_label_height: 0.4,
	axis_grid: "on",
	axis_grid_colour: "charcoal",
	axis_grid_line_style: "dot"
	
	)
	
# vertical axis	
v_axis = maxis(
	axis_orientation	:	"vertical",
	axis_title_text	:	"Presssure (hPa)",
	axis_grid: "on",
	axis_grid_colour: "charcoal",
	axis_grid_line_style: "dot"
	)

# the view
view = cartesianview(
	x_automatic	:	"on",
	x_axis_type : "date",
	#x_date_min: 2017-01-05,
	#x_date_max: 2017-01-06,
	y_automatic	:	"off",
	y_min: 1000,
	y_max: 400,
	y_automatic_reverse	:	"on",
	horizontal_axis	:	h_axis,
	vertical_axis	:	v_axis,
	subpage_background_colour: "RGB(0.95,0.95,0.95)"
	)

# define the output plot file
setoutput(pdf_output(output_name : 'odb_wind_profiler'))

# generate the plot
plot(view, vis, wp, title)





#  **************************** LICENSE START ***********************************
# 
#  Copyright 2019 ECMWF. This software is distributed under the terms
#  of the Apache License version 2.0. In applying this license, ECMWF does not
#  waive the privileges and immunities granted to it by virtue of its status as
#  an Intergovernmental Organization or submit itself to any jurisdiction.
# 
#  ***************************** LICENSE END ************************************
# 

import metview as mv
from datetime import datetime

# define station id
statid = "95759"

# read db
db = mv.read("wprof.odb")

# define query for u wind component
q_u = """select obsvalue as val,
      vertco_reference_1 as p,
      date@hdr as date,
      time@hdr as time        
      where varno=3 and statid='{}'""".format(statid)  

# define query for v wind component
q_v = """select obsvalue as val, 
      vertco_reference_1 as p,
      date as d,
      time as t 
      where varno=4 and statid='{}'""".format(statid) 
 
# define query for metadata 
q_meta = """select DISTINCT lat@hdr as lat, lon@hdr as lon, andate, 
      antime where statid='{}'""".format(statid) 
      
# filter u
f_u = mv.odb_filter(
	odb_query	=	q_u,
	odb_data	=	db 
	)
	
# filter v
f_v = mv.odb_filter(
	odb_query	=	q_v,
	odb_data    =	db
	)

# filter metadata
f_m = mv.odb_filter(
	odb_query	=	q_meta,
	odb_data	=	db
	)
	
# read the odb columns into vectors 
u = mv.values(f_u, "val")
d = mv.values(f_u, "date")
t = mv.values(f_u, "time")
p = mv.values(f_u, "p") / 100   # pressure is converted to hPa
v = mv.values(f_v, "val")

# read values for the title
lat = mv.values(f_m, "lat")[0]
lon = mv.values(f_m, "lon")[0]
andate = mv.values(f_m, "andate")[0]
antime = mv.values(f_m, "antime")[0]

# build datetime list
d_lst=[]
for i,d_val in enumerate(d):       
    tt = str(int(t[i]))
    if len(tt) == 5:
        tt = '0' + tt
    
    d_lst.append(datetime.strptime(str(int(d_val)) + tt,"%Y%m%d%H%M%S"))
 

# define an input visualiser
vis = mv.input_visualiser(
	input_plot_type	=	"xy_vectors",
	input_x_type	=	"date",
	input_y_type	=	"number",
	input_date_x_values =	d_lst,
	input_y_values	    =	list(p),
	input_x_component_values  =	list(u),
	input_y_component_values  =	list(v)
    )

# wind plotting style
wp = mv.mwind(
    wind_thinning_factor                  = 1.0,
    legend                                = "on",
    wind_advanced_method                  = "on",
    wind_advanced_colour_selection_type   = "interval",
    wind_advanced_colour_min_value        = 1,
    wind_advanced_colour_level_interval   = 1,
    wind_advanced_colour_max_level_colour = "red",
    wind_advanced_colour_min_level_colour = "blue",
    wind_arrow_thickness                  = 2,
    wind_arrow_unit_velocity              = 10
    )
    
# define title
title = mv.mtext(text_font_size = 0.4,
    text_line_1 = """WIND PROFILER AN date={:.0f} time={:.0f} station={} lat={:.2f} lon={:.2f}""".
    format(andate, antime, statid, lat, lon)
    )
                   
# horizontal axis
h_axis = mv.maxis(
	axis_position	         = "left",
	axis_title_text	         = "Time",
	axis_minor_tick          = "on",
	axis_minor_tick_count	 = 5,
	axis_date_type           = "hours",
	axis_hours_label         = "on",
	axis_hours_label_height  = 0.4,
	axis_grid                = "on",
	axis_grid_colour         = "charcoal",
	axis_grid_line_style     = "dot"	
	)
	
# vertical axis	
v_axis = mv.maxis(
	axis_orientation       = "vertical",
	axis_title_text	       = "Presssure (hPa)",
	axis_grid              = "on",
	axis_grid_colour       = "charcoal",
	axis_grid_line_style   = "dot"
	)

# the view
view = mv.cartesianview(
	x_automatic	   =   "on",
	x_axis_type    =   "date",	
	y_automatic	   =   "off",
	y_min          =   1000,
	y_max          =   400,
	y_automatic_reverse	 =	"on",
	horizontal_axis	     =	h_axis,
	vertical_axis	     =	v_axis,
	subpage_background_colour = "RGB(0.95,0.95,0.95)"
	)

# define the output plot file
mv.setoutput(mv.pdf_output(output_name = 'odb_wind_profiler'))

# generate the plot
mv.plot(view, vis, wp, title)