# Metview Macro

# **************************** LICENSE START ***********************************
#
# Copyright 2012 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 ************************************


t2_csv = read_table(
        table_filename  :  "../t2_20120304_1200.csv"
        )

print(type(t2_csv))
print(count(t2_csv))

lats = values(t2_csv, 'Lat')
lons = values(t2_csv, 'Lon')
vals = values(t2_csv, 'T2m')


# create the geopoints variable we will populate
# - we could create a full, 6-column, geopoints variable,
#   which is the default if we do not supply a second
#   parameter to create_geo(). But here we do not require any
#   intelligent date-matching, so xyz is enough. 

t2_geo = create_geo (count(lats),'xyv')


# put the vectors of values into the geopoints variable.
# - if we went for the 6-column type, then we would also call
#   set_level(), set_date() and set_time().

t2_geo = set_longitudes(t2_geo, lons)
t2_geo = set_latitudes (t2_geo, lats)
t2_geo = set_values    (t2_geo, vals)



# read the GRIB file of 2T analysis
t2_grib = read("../t2_an.grib")


# Compute the difference
diff = t2_geo - t2_grib

# plotting visdef
diff_symb = msymb(
    symbol_type                          : "marker",
    symbol_table_mode                    : "advanced",
    symbol_advanced_table_selection_type : "list",
    symbol_advanced_table_level_list     : [-1000,-1,1,1000],
    symbol_advanced_table_colour_method  : "list",
    symbol_advanced_table_colour_list    : ["blue","grey","red"]
    )

# plot the data
plot(diff, diff_symb)


# print out some statistics
print('Count: ', count(diff))
print('Min:   ', minvalue(diff))
print('Mean:  ', mean(diff))
print('Max:   ', maxvalue(diff))
