How to calculate an area-weighted average in GRASS

# set the computational region to the value raster
g.region rast=rast_val -ap
 
# convert the value raster to area vector
r.to.vect rast_val out=vect_val type=area
 
# calculate the areas of the area vector polygons
v.to.db vect_val op=area col=area_m2
 
# overlay the value vector OR the area vector
v.overlay ainput=vect_val binput=vect_area oper=or output=vect_val_area
 
# add a new column for the area-weighted average
v.db.addcolumn vect_area col="avg_val double"
 
# populate the new column with the area-weighted average
v.db.update vect_area col=avg_val \
  qcol="(select sum(a_value*a_area_m2)/sum(a_area_m2) from vect_val_area \
         where vect_val_area.b_cat=vect_area.cat group by b_cat)"