#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 ************************************
# 

#=============================================================================
# Function      : thermo_data_info
#
# Syntax        : thermo_data_info(nc: netcdf)
#                                          
# Category      : THERMODYNAMICS
#
# OneLineDesc   : extracts data and metadata from a thermo data object
#
# Description   : Extracts data and metadata from a thermo data object
#
# Parameters    : nc - the thermo data object
#                 time_dim_index - in Macro starts at 1, in Python at 0
# 
# Return Value  : a definition
#
# Dependencies  : none
#
#==============================================================================


function thermo_data_values(nc: netcdf, time_dim_index)
   
   	res = (
   	    bottom_t:  vector_missing_value,
        bottom_td: vector_missing_value,
        bottom_p: vector_missing_value,
        lat:  "",
        lon: "",
        station: "",
        date: ""
    )
   	 	
    att = global_attributes(nc)
    dims = dimensions(nc)
    vars = variables(nc)
    
    if att.Coordinates <> nil then
        s =parse(att.Coordinates,"/")
        if count(s) = 2 then
            res.lat = s[1]
            res.lon = s[2]
        end if
    end if
   
    if att.Station <> nil then
        res.station = att.Station
    end if
   
    if find(vars,"time") then
        setcurrent(nc, "time")
        dt = values(nc)
        if dt <> nil then 
            dt = dt[1]
            res.date = substring(dt,1,8)
            res.time = substring(dt,9,12)
            res.step = substring(dt,13,18)
        end if
    end if
    
    missing_val = 1E+30
    if att._FILL_VALUE <> nil then
        missing_val = att._FILL_VALUE
    end if    
   
   	first_dim_val = time_dim_index
   
    # get pressure for t and td in hPa
    setcurrent(nc, "pres")
    p = values(nc,[first_dim_val,'all'])
   
   	# get temperature in C
    setcurrent(nc, "t")
    t = values(nc,[first_dim_val,'all'])
    
    # get dewpoint in C
    setcurrent(nc, "td")
    td = values(nc,[first_dim_val,'all'])
    
    if find(dims,"pwind") then 
        # get pressure for wind in hPa
        setcurrent(nc, "pwind")
        p_wind = values(nc,[first_dim_val,'all'])
    else
        p_wind = p
    end if
       
    # get u in m/s
    setcurrent(nc, "u")
    u = values(nc,[first_dim_val,'all'])
     
    # get v in m/s
    setcurrent(nc, "v")
    v = values(nc,[first_dim_val,'all'])
    
    # replace nc missing vals with vector missing vals
    res.p = bitmap(p, missing_val)
    res.t = bitmap(t, missing_val)
    res.td = bitmap(td, missing_val)
    res.p_wind = bitmap(p_wind, missing_val)
    res.u = bitmap(u, missing_val)
    res.v = bitmap(v, missing_val)
    
    if count(res.p) > 0 then  
        eps = 1E-5
    	num = count(p)
    	
        # the profile goes downwards
        if res.p[1] < res.p[num] then
            idx_start = num
            idx_end = 1
            idx_step = -1 
        else
            idx_start = 1
            idx_end = num
            idx_step = 1 
        end if
    
        for idx = idx_start to idx_end by idx_step do
            if res.p[idx] <> vector_missing_value and 
               res.t[idx] <> vector_missing_value and
               res.td[idx] <> vector_missing_value then
        	       res.bottom_t = res.t[idx]
        	       res.bottom_td = res.td[idx]
        	       res.bottom_p = res.p[idx]
        	       return res
            end if 
        end for
    end if  
    
    return res 
     
end thermo_data_values