function [output] = readVectorValues(sampleTypeData, firstSampleIndex, lastSampleIndex, numTS, channel_handle, maxDim)
%Funtion to read in samples for one channel of type vector - scalar and
%comples vecor
%   Blockwise reading of data
%   sampleTypeData          type of data to whether reading out scalar or
%                               complex vector
%   firstSampleIndex        index of first sample for this reading
%   numTS                   number of samples of this sweep
%   maxDim                  maximum dimension of data per timestamp
%   output                  total sweep with timestamp

[~,sys] = memory;

if sampleTypeData == 100
        
        block = 1e5;
        maxDim = uint64(maxDim);
        
        if (block * maxDim) > sys.PhysicalMemory.Available* 0.9
            block = 1e2;
        end
        
        numBlocks = max(1, uint64(ceil(double(numTS)/double(block))));
        numValidTS = uint64(0);
        nextSample = uint64(0);

        offset = 1;
        clear data_temp
        clear timestamp
        timestamp = zeros(1);
        data_temp = zeros(1);
%         timestamp = zeros(numTS, 1);
%         data_temp = zeros(numTS * maxDim, 1);
        
        firstSample = firstSampleIndex;
        
        for blockIdx = 0:numBlocks - 1
            
            blockData = zeros(1, block * maxDim);
            blockTS = zeros(1, block);
            sampleDimensions_out = zeros(1, numTS); 

            [DMDReader_ErrorCode, channel_handle, blockData, blockTS, ~ , numValidTS, nextSample] = calllib('dmdlib', 'DMDReader_GetSamplesAndTS_ScalarVector_Seconds', ...
                channel_handle, firstSample, block, maxDim, blockData, blockTS, sampleDimensions_out, numValidTS, nextSample);

            
            if (DMDReader_ErrorCode ~= 0)
               close(f)
               error(['Error: ', num2str(DMDReader_ErrorCode), ' DMDReader_GetSamplesAndTS_ScalarVector_Seconds'])
            end

            if (sys.PhysicalMemory.Available < sys.PhysicalMemory.Total * 0.2)
               close(f)
               error('Error: Not enough RAM available.')
            end
            if numValidTS > 0
                blockTS = blockTS(1:numValidTS)';
                blockData = blockData(1:numValidTS * maxDim)';

            timestamp(offset:numValidTS + offset - 1) = blockTS(1:numValidTS);
            data_temp(offset:numValidTS * maxDim + offset - 1) = blockData(1:numValidTS * maxDim);
            offset = offset + numValidTS;
            end % if numValidTS > 0

            firstSample = nextSample;
            if nextSample > lastSampleIndex
                break
            end
            
        end % for blockIdx
        if(any((data_temp) ~= 0))
            data_temp = reshape(data_temp, [maxDim, numValidTS])';
        end
            output.time = timestamp;
            output.data = data_temp;
            
elseif sampleTypeData == 101
    
        block = 1e2;
        maxDim = uint64(maxDim);
        if (maxDim * block) > sys.PhysicalMemory.Available* 0.9
           block = 1e1;
        end
        numBlocks = max(1, uint64(ceil(double(numTS)/double(block))));
        numValidTS = uint64(0);
        nextSample = uint64(0);
        
        data_out.real= [];
        data_out.imag= [];
        
        complexX(maxDim * block).real = 0;
 
        clear timestamp
        timestamp = zeros(numTS, 1);
        offset = 1;
        
        firstSample = firstSampleIndex;
        
        for blockIdx = 0:numBlocks - 1
 
                blockTS = zeros(1, block);
                sampleDimensions_out = zeros(1, numTS); 
                blockData = libstruct('DMDReader_SAMPLEVALUE_COMPLEX', complexX);
                blockDataPtr = libpointer('DMDReader_SAMPLEVALUE_COMPLEX', blockData);

                [DMDReader_ErrorCode, channel_handle, blockData, blockTS, ~, numValidTS, nextSample] = ... 
                    calllib('dmdlib', 'DMDReader_GetSamplesAndTS_ComplexVector_Seconds', channel_handle, firstSample, block, ...
                    maxDim, blockData, blockTS, sampleDimensions_out, numValidTS, nextSample);

                if (DMDReader_ErrorCode ~= 0)
                    close(f)
                    error(['Error: ', num2str(DMDReader_ErrorCode), ' DMDReader_GetSamplesAndTS_ComplexVector_Seconds'])
                end
                [~,sys] = memory;
                if (sys.PhysicalMemory.Available < sys.PhysicalMemory.Total * 0.2)
                   close(f)
                   error('Error: Not enough RAM available.')
                   
                end

                 sampleValueComplexReal = zeros(1, maxDim * numValidTS);
                 sampleValueComplexImag = zeros(1, maxDim * numValidTS);

                 if numValidTS > 0 
                     blockTS = blockTS(1:numValidTS)';

                     timestamp(offset:numValidTS + offset - 1) = blockTS(1:numValidTS);
                       for n = 0 : maxDim * numValidTS - 1
                           dummyPtr = blockDataPtr + n;
                           sampleValueComplexReal(n + 1) = dummyPtr.Value.real;
                           sampleValueComplexImag(n + 1) = dummyPtr.Value.imag;
                        end %for n
                     sampleValueComplexReal = sampleValueComplexReal(1:numValidTS * maxDim)';
                     sampleValueComplexImag = sampleValueComplexImag(1:numValidTS * maxDim)';
                     data_out.real = vertcat(data_out.real, sampleValueComplexReal);
                     data_out.imag = vertcat(data_out.imag, sampleValueComplexImag);
                     offset = offset + numValidTS;
                 end % if numValidTS > 0
                 
                 firstSample = nextSample;
                 if nextSample > lastSampleIndex
                     break
                 end
        end % for blockIdx
    if((any(data_out.real) ~= 0) && (any(data_out.imag)) ~= 0)
        data_out.real = reshape(data_out.real, [maxDim, numValidTS])';
        data_out.imag = reshape(data_out.imag, [maxDim, numValidTS])';
    end
    

    output.time = timestamp;
    output.real = data_out.real;
    output.imag = data_out.imag;
    
end % if SampleTypeData

end % end function
