#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 information from a thermo data object
#
# Description   : Extracts information from a thermo data object
#
# Parameters    : nc - the thermo data object
# 
# Return Value  : a definition
#
# Dependencies  : none
#
#==============================================================================


function thermo_data_info(nc: netcdf)
   
   	res = ()
   	 	
    att = global_attributes(nc)
    dim = 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
        s = parse(att.Coordinates,"/")
    end if
   
    if "time" in vars 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 = 1
	if base_language = 'python' then 
	   first_dim_val = 0
	end if
   
   	# 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'])
     
    # get pressure in hPa
    setcurrent(nc, "pres")
    p = values(nc,[first_dim_val,'all'])
    
    if count(p) > 0 then
    
        eps = 1E-5
    	num = count(p)
    	
    	# the profile goes downwards
    	if p[1] < p[num] then
            idx = num
            has_surf = 0
            while has_surf = 0 do
    	   
    	       if abs(t[idx] - missing_val) > eps and  abs(td[idx] - missing_val) > eps then
    	           has_surf = 1
    	           res.bottom_t = t[idx]
    	           res.bottom_td = td[idx]
    	           res.bottom_p = p[idx]
    	       end if
    	       idx = idx - 1    
    	
    	   end while
    	
    	# the profile goes upwards    	
    	else
    	    idx = 1
            has_surf = 0
            while has_surf = 0 do
    	   
    	       if abs(t[idx] - missing_val) > eps and  abs(td[idx] - missing_val) > eps then
    	           has_surf = 1
    	           res.bottom_t = t[idx]
    	           res.bottom_td = td[idx]
    	           res.bottom_p = p[idx]
    	       end if
    	       idx = idx + 1    
    	
    	   end while
        end if
    
    end if	
    
    return res
     
end thermo_data_info