#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, par: string, time_dim_index: number)
    
    if par = "time_dim_index" then
	    return thermo_data_info(nc, time_dim_index)
    else
        fail("thermo_data_info: invalid parameter: " & par)
    end if
    
end thermo_data_info

function thermo_data_info(nc: netcdf)
    
    time_dim_index = 1
    if base_language = 'python' then 
	   time_dim_index = 0
	end if
	return thermo_data_info(nc, time_dim_index)

end thermo_data_info

function thermo_data_info(nc: netcdf, time_dim_index: number)
   
   	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
            idx = time_dim_index
            if base_language = 'python' then 
	           idx = idx+1
	        end if           
            dt = dt[idx]
            dt_len = length(dt)
            if dt_len >= 12 then 
                res.date = substring(dt,1,8)
                res.time = substring(dt,9,12)
                s = substring(dt,1,4) & "-" &  substring(dt,5,6) &
                           "-" &  substring(dt,7,8) & " " & substring(dt,9,10) & ":" &
                           substring(dt,11,12)
                res.base_date = date(s)
                if dt_len >= 16 then
                    res.step = substring(dt,13,16)
                    res.valid_date = res.base_date + hour(number(substring(dt,13,16)))
                end if
            end if           
        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
	#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