function namCon = EHY_getConstituentNames(fname)
%% namCon = EHY_getConstituentNames(fname)
%
% This function returns the names of the constituents in a hydrodynamic simulation
%
% modelType can be:
% dfm       Delft3D-Flexible Mesh
% d3d       Delft3D 4
% simona    SIMONA (WAQUA/TRIWAQ)
%
% support function of the EHY_tools
% Julien Groenenboom - E: Julien.Groenenboom@deltares.nl

%%
namCon                     = {};
modelType                  = EHY_getModelType(fname);

%%
switch modelType
    
    case 'dfm'
        %% Delft3D FM constituents (determine constituents by elimination of everything NOT a constituent, not very elegant! (and prone to error...))
        param_hyd = {'station_x_coordinate'         , 'station_y_coordinate'              , 'station_id'               , 'station_name'              , 'waterlevel'  , 'bedlevel'   , ...
            'x_velocity'                   , 'y_velocity'                        , 'z_velocity'               , 'R'                         , 'hwav'        , ...
            'twav'                         , 'phiwav'                            , 'rlabda'                   , 'uorb'                      , 'tauwav'      , ...
            'density'                      , 'sediment_concentration'            , 'windx'                    , 'windy'                     , 'rain'        , ...
            'source_sink_name'             , 'source_sink_x_coordinate'          , 'source_sink_y_coordinate'                                               , ...
            'source_sink_prescribed_discharge'                                   , 'source_sink_prescribed_salinity_increment'                              , ...
            'source_sink_prescribed_temperature_increment'                       , 'source_sink_current_discharge'                                          , ...
            'source_sink_cumulative_volume'                                      , 'source_sink_discharge_average'                                          , ...
            'general_structure_name'       , 'general_structure_discharge'       , 'general_structure_crest_level'                                          , ...
            'general_structure_crest_width', 'general_structure_lower_edge_level', 'general_structure_opening_width'                                        , ...
            'general_structure_s1up'       , 'general_structure_s1dn'                                                                                       , ...
            'general_structure_gate_lower_edge_level'                            , 'general_structure_gate_opening_width'                                   , ...
            'pump_name'                    , 'pump_xmid'                         , 'pump_ymid'                                                              , ...
            'pump_discharge'               , 'pump_capacity'                     , 'pump_s1up'                , 'pump_s1dn'                 , 'gate_name'   , ...
            'gate_discharge'               , 'gate_lower_edge_level'             , 'gate_s1up'                , 'gate_s1dn'                 , 'gategen_name', ...
            'gategen_discharge'            , 'gategen_sill_level'                , 'gategen_sill_width'       , 'gategen_lower_edge_level'  , 'gategen_flow_through_height', ...
            'gategen_opening_width'        , 'gategen_s1up'                      , 'gategen_s1dn'             , 'cdam_name'                 , 'cdam_discharge'             , ...
            'cdam_crest_level'             , 'cdam_s1up'                         , 'cdam_s1dn'                , 'weirgen_name'              , 'weirgen_discharge'          , ...
            'weirgen_crest_level'          , 'weirgen_crest_width'               , 'weirgen_s1up'             , 'weirgen_s1dn'              , 'dambreak_name'              , ...
            'dambreak_s1up'                , 'dambreak_s1dn'                     , 'dambreak_breach_depth'    , 'dambreak_breach_width'     , 'dambreak_discharge'         , ...
            'dambreak_cumulative_discharge', 'dambreak_breach_width_derivative'  , 'dambreak_water_level_jump', 'dambreak_normal_velocity'  , 'dredge_area_name'           , ...
            'dump_area_name'               , 'sedfrac_name'                      , 'dred_link_discharge'      , 'dred_discharge'            , 'dump_discharge'             , ...
            'dred_discharge_frac'          , 'dump_discharge_frac'               , 'dred_time_frac'           , 'plough_time_frac'                          ,  ...
            'Waterdepth'                   , 'tke'                               , 'eps'                      , 'vicww'                                     ,  ...
            'wind'                         , 'tair'                              , 'rhum'                     , 'clou'                                     ,  ...
            'qsun'                         , 'qeva'                              , 'qcon'                     , 'qlong'                                     ,  ...
            'qfreva'                       , 'qfrcon'                            , 'qtot'                     , 'patm'                                      } ;
        
        more_param_hyd={'flowelem_xcc','flowelem_ycc','flowelem_zcc','flowelem_bac','flowelem_xzw','flowelem_yzw','flowelemcontour_x','flowelemcontour_y','flowelem_bl','elemlink',...
            'flowlink','flowlinktype','flowlink_xu','flowlink_yu','wgs84','flowelemdomain','flowlinkdomain','flowelemglobalnr','station_geom','station_geom_node_count',...
            'station_geom_node_coordx','station_geom_node_coordy','depth-averaged_x_velocity','depth-averaged_y_velocity','projected_coordinate_system','flowlink_lonu','flowlink_latu','taus','rich',...
            'source_sink_geom','source_sink_geom_node_count','source_sink_geom_node_coordx','source_sink_geom_node_coordy',...
            'velocity_magnitude','discharge_magnitude','tausx','tausy','potential_density','brunt_vaisala_n2'};
        param_hyd=cat(2,param_hyd,more_param_hyd);
         
        
        handle  = ncinfo(fname);
        Vars    = lower({handle.Variables.Name});
        no_vars = length(Vars);
        index (1:no_vars) = false;
        for i_var = 1: no_vars
            if ~ismember(Vars{i_var},lower(param_hyd))
                if isempty(strfind(Vars{i_var},'water_quality_output')) && isempty(strfind(Vars{i_var},'cross_section')) && ...
                        isempty(strfind(Vars{i_var},'water_balance'       )) && isempty(strfind(Vars{i_var},'zcoordinate'  )) && ...
                        isempty(strfind(Vars{i_var},'time'                ))
                    index(i_var) = true;
                end
            end
        end
        namCon = Vars(index);
        
    case 'd3d'
        %% Delft3D 4 constituents
        d3d  = vs_use (fname,'quiet');
        grp = char(vs_find(vs_use(fname,'quiet'), 'NAMCON'));
        namCon = strtrim(cellstr(vs_get(d3d,grp,'NAMCON','quiet')));
        logi = ~cellfun(@isempty,strfind(lower(namCon),'energy'));
        namCon(logi) = [];
        
    case 'simona'
        %% Simona constituents
        handle = qpfopen(fname);
        namCon = waquaio(handle,'','substances');
        
end
