function [data, info] = readNetCDFVariable(ncFile, varName)
% Read specific variable from NetCDF file with detailed information
% ncFile: NetCDF filename
% varName: Variable name to read (optional, if not provided, reads all)

    if ~isfile(ncFile)
        error('NetCDF file not found: %s', ncFile);
    end
    
    info = ncinfo(ncFile);
    
    if nargin < 2 || isempty(varName)
        % Read all variables
        data = struct();
        for i = 1:length(info.Variables)
            currentVar = info.Variables(i).Name;
            data.(currentVar) = ncread(ncFile, currentVar);
        end
    else
        % Read specific variable
        if ~any(strcmp({info.Variables.Name}, varName))
            availableVars = {info.Variables.Name};
            error('Variable "%s" not found. Available variables: %s', ...
                  varName, strjoin(availableVars, ', '));
        end
        data = ncread(ncFile, varName);
    end
end

function varInfo = getNetCDFInfo(ncFile, varName)
% Get detailed information about a variable or the entire file
    info = ncinfo(ncFile);
    
    if nargin < 2
        varInfo = info;
    else
        varFound = false;
        for i = 1:length(info.Variables)
            if strcmp(info.Variables(i).Name, varName)
                varInfo = info.Variables(i);
                varFound = true;
                break;
            end
        end
        if ~varFound
            error('Variable "%s" not found', varName);
        end
    end
end