function [exportVisu, readonly_list]=bruker_genVisu(data, genmode, path_to_imagewrite, importVisu, Template, Subject, HeaderInformation, PackageParameters, FrameGroupParams, varargin)
% [exportVisu, readonly_list, template_error_list]=bruker_genVisu(data, genmode, path_to_imagewrite, importVisu, Template, Subject, PackageParameters, FrameGroupParams, ['importVisuCorePosition'], ['importVisuCoreOrientation'], ['noVisuCoreTransposition'])
%
% Input:
%   data: Datamatrix
%   gemnode: string with 'Visu', 'Template', 'Subject' or 'auto' -> default set is auto
%           Defines the type of parameterstruct which will be used to generate the exportVisu.
%           'auto' will use the best value from all existing parameterstructs
%   path_to_imagewrite: path to the directory where you wanna save the visu_pars file. This will be used to extract expno and procno an set a better
%           value, can also be an empty variable. It's recommanded to set the path in follwowing syntax: '/yourstudyname/expno/pdata/procno' where expno and procno are
%           numbers
%   importVisu: the visu-parameterstruct of this dataset or of a very similar dataset, can also be an empty struct
%   Template: if it exist the template parameter-struct generated by PV6 for a better usage with Matlab, can also be an empty struct
%   Subject: the subject-parameterstruct of this dataset, can also be an empty struct
%   PackageParameters: struct hwoch contains PackageParameters.MatlabPackageVersion and PackageParameters.MatlabPackageName, can also be an empty
%           struct
%   FrameGroupParams: the FrameGroup-Parameterstruct generated by the object by reading a image-dataset, can also be an empty struct
%
%   Optionals:
%   'importVisuCorePosition': imports the VisuCorePosition from FrameGroupParams and if that doesn't contain it, it tries to import it from visu or
%           template
%   'importVisuCoreOrientation': imports the importVisuCoreOrientation from FrameGroupParams and if that doesn't contain it, it tries to import it from visu or
%           template
%   'importVisuCoreTransposition': if set, the function tries to import the VisuCoreTrnaspoisition form template or visu.
%   'importAll': tries all optinals 
%
% Out:
%   exportVisu: the VisuParamStruct which can be written to file
%
%   readonly_list: a list of variables that are not allowed to change because an change if this variables may cause inconsitancys in the ParaVision
%           database.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Copyright (c) 2012
% Bruker BioSpin MRI GmbH
% D-76275 Ettlingen, Germany
%
% All Rights Reserved
%
% $Id: bruker_genVisu.m,v 1.1 2012/09/11 14:22:10 pfre Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Variables
readonly_list={};
exportVisu=struct();

%% Calculate Variables

% Expno and Procno if path exist:
if ~isempty(path_to_imagewrite)
    expno=[];
    procno=[];
    pos1=strfind(path_to_imagewrite, 'pdata');
    if ~isempty(pos1)
       prev_pdata=strfind(path_to_imagewrite(1:pos1), '/');
       after_pdata=strfind(path_to_imagewrite(pos1:end), '/')+pos1;

       % Check if e.g. bla/blub/5/pdata/4/acqp
       pos2=strfind(path_to_imagewrite(after_pdata:end), '/');
       if ~isempty(pos2) % / after 4 found -> only take space between
            procno=path_to_imagewrite(after_pdata:after_pdata+pos2(1)-2);
       else
           procno=path_to_imagewrite(after_pdata:end);
       end
       expno=path_to_imagewrite(prev_pdata(end-1)+1:prev_pdata(end)-1);   
       
       % used for generating new path:
       % target: bla/blub/VisuSubjectId_VisuStudyId/5/pdata/4
       first_path_part=path_to_imagewrite(1:prev_pdata(end-1)); % bla/blub/
       second_path_part=['/', expno, '/pdata/', procno]; %e.g. /5/pdata/4
    else
       error('Your path does not contain .../procno/pdata/expno'); 
    end
else
    error('You have to insert the imagewrite-path first')
end





%% Start generating the variables

%% writepath
exportVisu.imagewrite_path=''; % dummy -> will be at first position
% value will generated at the end of the function because at first VisuSubjectId and VisuStudyId have to be generated

%% Owner
pos=find(strcmpi(HeaderInformation(:,1), 'OWNER')==1);
if ~isempty(pos) && length(pos)==1
    exportVisu.OWNER=HeaderInformation{pos,2};
else
    exportVisu.OWNER='Matlab';
end
clear pos;

%% VisuUid
tmp=ver;
if (strcmpi(computer('arch'), 'glnx86') || strcmpi(computer('arch'), 'glnxa64')) || sum(strfind(computer, 'linux'))>=1
    % Linux
    [status, result]=system('/sbin/ifconfig eth0'); %linux
    mac_pos=strfind(result, 'HWaddr');
    mac=result(mac_pos+7:mac_pos+8+17);
    point_pos=strfind(mac, ':');
    mac(point_pos)='';
    host_identifier=num2str(hex2dec(mac));
else
    % Windows/Mac
    host_identifier=hostid;
    host_identifier=dec2bin(host_identifier{1});
    host_identifier=strcat(host_identifier(:)');
    if length(host_identifier) > 52
        host_identifier=host_identifier(1:52);
    end
    host_identifier=bin2dec(host_identifier);
    host_identifier=num2str(host_identifier);
end
license_identifier=license;
time_identifier=num2str(round(etime(clock, [1970 01 01 00 00 00])));
for i=1:5
    counter(i)=num2str(fix(rand(1,1)*10));
end
if host_identifier(1)=='0',    host_identifier(1)=1; end
if license_identifier(1)=='0', license_identifier(1)='2'; end
if time_identifier(1)=='0',    time_identifier(1)='3'; end
if counter(1)=='0',   counter(1)='4'; end
exportVisu.VisuUid=['2.16.756.5.104.', host_identifier, '.', license_identifier, '.', time_identifier, '.',  counter];
readonly_list=[readonly_list, {'VisuUid'}];


%% VisuVersion
% written directly

%% VisuCreator
if isfield(PackageParameters, 'MatlabPackageName')
    exportVisu.VisuCreator=PackageParameters.MatlabPackageName;
else
    exportVisu.VisuCreator='MatlabPackage';
end
readonly_list=[readonly_list, {'VisuCreator'}];

%% VisuCreatorVersion
if isfield(PackageParameters, 'MatlabPackageVersion')
    exportVisu.VisuCreatorVersion=PackageParameters.MatlabPackageVersion;
else
    exportVisu.VisuCreatorVersion='-----';
end
readonly_list=[readonly_list, {'VisuCreatorVersion'}];

%% VisuCreationDate
exportVisu.VisuCreationDate=[datestr(now, 'yyyy-mm-ddTHH:MM:SS'), ',493+0020'];
readonly_list=[readonly_list, {'VisuCreationDate'}];

%% VisuInstanceModality
% written directly

%% VisuCoreFrameCount
dims=size(data);
if length(data)>5
    exportVisu.VisuCoreFrameCount=prod(dims(5:end));
else
    exportVisu.VisuCoreFrameCount=size(data,5);
end
if ~isreal(data)
    exportVisu.VisuCoreFrameCount=exportVisu.VisuCoreFrameCount*2;
end
clear dims;

%% VisuCoreDim
VisuCoreDim=sum([size(data,1) size(data,2) size(data,3) size(data,4)]>[0 1 1 1]);
exportVisu.VisuCoreDim=VisuCoreDim;

%% VisuCoreSize
[d(1) d(2) d(3) d(4)]=size(data);
exportVisu.VisuCoreSize=d(1:VisuCoreDim);
clear d;

%% VisuCoreDimDesc
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuCoreDimDesc') && ~isempty(Template.VisuCoreDimDesc)
            exportVisu.VisuCoreDimDesc=Template.VisuCoreDimDesc;
        elseif isfield(importVisu, 'VisuCoreDimDesc') && ~isempty(importVisu.VisuCoreDimDesc) && size(importVisu.VisuCoreDimDesc,1)==VisuCoreDim
            exportVisu.VisuCoreDimDesc=importVisu.VisuCoreDimDesc;
        elseif (VisuCoreDim==4)
            exportVisu.VisuCoreDimDesc={'spectroscopic', 'spatial', 'spatial', 'spatial'}';
        else
            exportVisu.VisuCoreDimDesc=repmat({'spatial'}, VisuCoreDim,1);
        end
    case {'Template'} 
        if isfield(Template, 'VisuCoreDimDesc') && ~isempty(Template.VisuCoreDimDesc)
            exportVisu.VisuCoreDimDesc=Template.VisuCoreDimDesc;
        elseif (VisuCoreDim==4)
            exportVisu.VisuCoreDimDesc={'spectroscopic', 'spatial', 'spatial', 'spatial'}';
        else
            exportVisu.VisuCoreDimDesc=repmat({'spatial'}, VisuCoreDim,1);
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuCoreDimDesc') && ~isempty(importVisu.VisuCoreDimDesc) && size(importVisu.VisuCoreDimDesc,1)==VisuCoreDim
            exportVisu.VisuCoreDimDesc=importVisu.VisuCoreDimDesc;
        elseif (VisuCoreDim==4)
            exportVisu.VisuCoreDimDesc={'spectroscopic', 'spatial', 'spatial', 'spatial'}';
        else
            exportVisu.VisuCoreDimDesc=repmat({'spatial'}, VisuCoreDim,1);
        end
    case {'Subject'}
        if (VisuCoreDim==4)
            exportVisu.VisuCoreDimDesc={'spectroscopic', 'spatial', 'spatial', 'spatial'}';
        else
            exportVisu.VisuCoreDimDesc=repmat({'spatial'}, VisuCoreDim,1);
        end
end    


%% VisuCoreExtent
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuCoreExtent') && ~isempty(Template.VisuCoreExtent)
            exportVisu.VisuCoreExtent=Template.VisuCoreExtent;
        elseif isfield(importVisu, 'VisuCoreExtent') && ~isempty(importVisu.VisuCoreExtent)
            exportVisu.VisuCoreExtent=importVisu.VisuCoreExtent;
        else
            switch VisuCoreDim
                case 1
                    exportVisu.VisuCoreExtent=num2str(size(data,1));
                case 2
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2)]);
                case 3
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3)]);
                case 4
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3), size(data,4)]);
            end
        end
    case {'Template'} 
        if isfield(Template, 'VisuCoreExtent') && ~isempty(Template.VisuCoreExtent)
            exportVisu.VisuCoreExtent=Template.VisuCoreExtent;
        else
            switch VisuCoreDim
                case 1
                    exportVisu.VisuCoreExtent=num2str(size(data,1));
                case 2
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2)]);
                case 3
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3)]);
                case 4
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3), size(data,4)]);
            end
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuCoreExtent') && ~isempty(importVisu.VisuCoreExtent)
            exportVisu.VisuCoreExtent=importVisu.VisuCoreExtent;
        else
            switch VisuCoreDim
                case 1
                    exportVisu.VisuCoreExtent=num2str(size(data,1));
                case 2
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2)]);
                case 3
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3)]);
                case 4
                    exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3), size(data,4)]);
            end
        end
    case {'Subject'}
        switch VisuCoreDim
            case 1
                exportVisu.VisuCoreExtent=num2str(size(data,1));
            case 2
                exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2)]);
            case 3
                exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3)]);
            case 4
                exportVisu.VisuCoreExtent=num2str([size(data,1), size(data,2), size(data,3), size(data,4)]);
        end
end    

%% VisuCoreFrameThickness
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuCoreFrameThickness') && ~isempty(Template.VisuCoreFrameThickness)
            exportVisu.VisuCoreFrameThickness=Template.VisuCoreFrameThickness;
        elseif isfield(importVisu, 'VisuCoreFrameThickness') && ~isempty(importVisu.VisuCoreFrameThickness)
            exportVisu.VisuCoreFrameThickness=importVisu.VisuCoreFrameThickness;
        end
    case {'Template'} 
        if isfield(Template, 'VisuCoreFrameThickness') && ~isempty(Template.VisuCoreFrameThickness)
            exportVisu.VisuCoreFrameThickness=Template.VisuCoreFrameThickness;
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuCoreFrameThickness') && ~isempty(importVisu.VisuCoreFrameThickness)
            exportVisu.VisuCoreFrameThickness=importVisu.VisuCoreFrameThickness;
        end
end

%% VisuCoreUnits
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuCoreUnits') && ~isempty(Template.VisuCoreUnits)
            if ~size(Template.VisuCoreUnits,2)==VisuCoreDim % Error -> Try importVisu
                if isfield(importVisu, 'VisuCoreUnits') && ~isempty(importVisu.VisuCoreUnits) && size(importVisu.VisuCoreUnits,2)==VisuCoreDim
                    exportVisu.VisuCoreUnits=importVisu.VisuCoreUnits;
                else
                    exportVisu.VisuCoreUnits=repmat({'mm'}, 1, VisuCoreDim);
                end
            else
                exportVisu.VisuCoreUnits=Template.VisuCoreUnits;
            end
        elseif isfield(importVisu, 'VisuCoreUnits') && ~isempty(importVisu.VisuCoreUnits)
            exportVisu.VisuCoreUnits=importVisu.VisuCoreUnits;
        else
            exportVisu.VisuCoreUnits=repmat({'mm'}, 1, VisuCoreDim);
        end
    case {'Template'} 
        if isfield(Template, 'VisuCoreUnits') && ~isempty(Template.VisuCoreUnits) && size(Template.VisuCoreUnits,2)==VisuCoreDim
            exportVisu.VisuCoreUnits=Template.VisuCoreUnits;
        else
            exportVisu.VisuCoreUnits=repmat({'mm'}, 1, VisuCoreDim);
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuCoreUnits') && ~isempty(importVisu.VisuCoreUnits) && size(importVisu.VisuCoreUnits,2)==VisuCoreDim
            exportVisu.VisuCoreUnits=importVisu.VisuCoreUnits;
        else
            exportVisu.VisuCoreUnits=repmat({'mm'}, 1, VisuCoreDim);
        end
    case {'Subject'}
        exportVisu.VisuCoreUnits=repmat({'mm'}, 1, VisuCoreDim);
end    

%% VisuCoreOrientation
if sum(strcmpi(varargin, 'importVisuCoreOrientation'))>=1  || sum(strcmpi(varargin, 'importAll'))>=1 
    switch genmode
        case {'auto'}
            if isfield(Template, 'VisuCoreOrientation') && ~isempty(Template.VisuCoreOrientation)
                VisuCoreOrientation=Template.VisuCoreOrientation;
            elseif isfield(importVisu, 'VisuCoreOrientation') && ~isempty(importVisu.VisuCoreOrientation)
                VisuCoreOrientation=importVisu.VisuCoreOrientation;
            end
        case {'Template'} 
            if isfield(Template, 'VisuCoreOrientation') && ~isempty(Template.VisuCoreOrientation)
                VisuCoreOrientation=Template.VisuCoreOrientation;
            end
        case {'Visu'}
            if isfield(importVisu, 'VisuCoreOrientation') && ~isempty(importVisu.VisuCoreOrientation)
                VisuCoreOrientation=importVisu.VisuCoreOrientation;
            end
    end   
    if exist('FrameGroupParams', 'var') && isfield(FrameGroupParams, 'VisuCoreOrientation')
        exportVisu.VisuCoreOrientation=[FrameGroupParams.VisuCoreOrientation];        
    elseif exist('VisuCoreOrientation', 'var') && (numel(VisuCoreOrientation)==9 || numel(VisuCoreOrientation)==9*exportVisu.VisuCoreFrameCount)
        exportVisu.VisuCoreOrientation=VisuCoreOrientation;
    end
    clear VisuCoreOrientation;        
end

%% VisuCorePosition
if sum(strcmpi(varargin, 'importVisuCorePosition'))>=1  || sum(strcmpi(varargin, 'importAll'))>=1
    switch genmode
        case {'auto'}
            if isfield(Template, 'VisuCorePosition') && ~isempty(Template.VisuCorePosition)
                VisuCorePosition=Template.VisuCorePosition;
            elseif isfield(importVisu, 'VisuCorePosition') && ~isempty(importVisu.VisuCorePosition)
                VisuCorePosition=importVisu.VisuCorePosition;
            end
        case {'Template'} 
            if isfield(Template, 'VisuCorePosition') && ~isempty(Template.VisuCorePosition)
                VisuCorePosition=Template.VisuCorePosition;
            end
        case {'Visu'}
            if isfield(importVisu, 'VisuCorePosition') && ~isempty(importVisu.VisuCorePosition)
                VisuCorePosition=importVisu.VisuCorePosition;
            end
    end
    if exist('FrameGroupParams', 'var') && isfield(FrameGroupParams, 'VisuCorePosition')
        exportVisu.VisuCorePosition=[FrameGroupParams.VisuCorePosition];
    elseif exist('VisuCorePosition', 'var') && (numel(VisuCorePosition)==3 || numel(VisuCorePosition)==3*exportVisu.VisuCoreFrameCount)
        exportVisu.VisuCorePosition=VisuCorePosition;
    end
    clear VisuCorePosition;
end
    
%% VisuInstanceType
if (~isfield(exportVisu, 'VisuCorePosition')) || (~isfield(exportVisu, 'VisuCoreOrientation'))
    exportVisu.VisuInstanceType='MINIMAL_INSTANCE';
end

%% VisuCoreDataSlope, VisuCoreDataOffs, VisuCoreDataMax, VisuCoreDataMin
VisuCore=bruker_genSlopeOffsMinMax('_16BIT_SGN_INT', data, 1, 0);
exportVisu.VisuCoreDataSlope=VisuCore.VisuCoreDataSlope;
exportVisu.VisuCoreDataOffs=VisuCore.VisuCoreDataOffs;
exportVisu.VisuCoreDataMin=VisuCore.VisuCoreDataMin;
exportVisu.VisuCoreDataMax=VisuCore.VisuCoreDataMax;
readonly_list=[readonly_list, {'VisuCoreDataMin'}];
readonly_list=[readonly_list, {'VisuCoreDataMax'}];

%% VisuCoreFrameType
if isreal(data)
    exportVisu.VisuCoreFrameType='MAGNITUDE_IMAGE';
else
    exportVisu.VisuCoreFrameType={'REAL_IMAGE', 'IMAGINARY_IMAGE'}';  
end

%% VisuCoreSlicePacksDef
% not supported by Matlab

%% VisuCoreSlicePacksSlices
% not supported by Matlab

%% VisuCoreSlicePacksSliceDist
% not supported by Matlab

%% VisuCoreWordType
exportVisu.VisuCoreWordType='_16BIT_SGN_INT';

%% VisuCoreByteOrder
exportVisu.VisuCoreByteOrder='littleEndian';

%% VisuCoreTransposition
if ( sum(strcmpi(varargin, 'importVisuCoreTransposition'))>=1 || sum(strcmpi(varargin, 'importAll'))>=1 ) && ( isfield(importVisu, 'VisuCoreTransposition') || isfield(Template, 'VisuCoreTransposition') )
    % search but don't write VisuCoreTransposition to exportVisu
    switch genmode
        case {'auto'}
            if isfield(Template, 'VisuCoreTransposition') && ~isempty(Template.VisuCoreTransposition)
                VisuCoreTransposition=Template.VisuCoreTransposition;
            elseif isfield(importVisu, 'VisuCoreTransposition') && ~isempty(importVisu.VisuCoreTransposition)
                VisuCoreTransposition=importVisu.VisuCoreTransposition;
            end
        case {'Template'} 
            if isfield(Template, 'VisuCoreTransposition') && ~isempty(Template.VisuCoreTransposition)
                VisuCoreTransposition=Template.VisuCoreTransposition;
            end
        case {'Visu'}
            if isfield(importVisu, 'VisuCoreTransposition') && ~isempty(importVisu.VisuCoreTransposition)
                VisuCoreTransposition=importVisu.VisuCoreTransposition;
            end
    end
    
    % VisuCoreTransposition exist:
    if exist('VisuCoreTransposition', 'var')
        TranspositionNeeded=false;
        for i=1:length(VisuCoreTransposition)
            if ~(VisuCoreTransposition(i)==0) % if 0 no transposition of that frame
                ch_dim1=mod(VisuCoreTransposition(i), exportVisu.VisuCoreDim) +1;
                ch_dim2=VisuCoreTransposition(i)-1 +1;
                if ~( size(data,ch_dim1)==size(data, ch_dim2) )
                    TranspositionNeeded=true;
                end
            end
        end
        if TranspositionNeeded    
            if numel(VisuCoreTransposition)==exportVisu.VisuCoreFrameCount
                exportVisu.VisuCoreTransposition=VisuCoreTransposition;
                warning('MATLAB:bruker_warning', 'Your dataset seams to use transposition please check if the values are correct');
            else
               warning('MATLAB:bruker_warning', 'Your dataset seams to use transposition, but size does not match - please insert the value yourself');
            end
        end
        clear VisuCoreTransposition;     
    end
end
    
%% VisuCoreDiskSliceOrder
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuCoreDiskSliceOrder') && ~isempty(Template.VisuCoreDiskSliceOrder)
            VisuCoreDiskSliceOrder=Template.VisuCoreDiskSliceOrder;
        elseif isfield(importVisu, 'VisuCoreDiskSliceOrder') && ~isempty(importVisu.VisuCoreDiskSliceOrder)
            VisuCoreDiskSliceOrder=importVisu.VisuCoreDiskSliceOrder;
        end
    case {'Template'} 
        if isfield(Template, 'VisuCoreDiskSliceOrder') && ~isempty(Template.VisuCoreDiskSliceOrder)
            VisuCoreDiskSliceOrder=Template.VisuCoreDiskSliceOrder;
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuCoreDiskSliceOrder') && ~isempty(importVisu.VisuCoreDiskSliceOrder)
            VisuCoreDiskSliceOrder=importVisu.VisuCoreDiskSliceOrder;
        end
end
if exist('VisuCoreDiskSliceOrder', 'var') && strcmp(VisuCoreDiskSliceOrder, 'disk_reverse_slice_order')
    exportVisu.VisuCoreDiskSliceOrder=VisuCoreDiskSliceOrder;
end
clear VisuCoreDiskSliceOrder;

%% VisuFGOrderDescDim, VisuFGOrderDesc, VisuGroupDepVals
% only written with complex-frames
if ~isreal(data)
    exportVisu.VisuFGOrderDescDim=2;
    exportVisu.VisuFGOrderDesc={exportVisu.VisuCoreFrameCount/2, 'object', '', 0, 0; 2, 'FG_COMPLEX', '', 0, 1;}';
    exportVisu.VisuGroupDepVals={'VisuCoreFrameType', 0}';
end


%% VisuSubjectName
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuSubjectName') && ~isempty(Template.VisuSubjectName)
            exportVisu.VisuSubjectName=Template.VisuSubjectId;
        elseif isfield(importVisu, 'VisuSubjectId') && ~isempty(importVisu.VisuSubjectId)
            exportVisu.VisuSubjectName=importVisu.VisuSubjectId;
        else
            exportVisu.VisuSubjectId='anonymus';
        end
    case {'Template'} 
        if isfield(Template, 'VisuSubjectName') && ~isempty(Template.VisuSubjectName)
            exportVisu.VisuSubjectName=Template.VisuSubjectId;
        else
            exportVisu.VisuSubjectName='anonymus';
        end
    case 'Visu'
        if isfield(importVisu, 'VisuSubjectId') && ~isempty(importVisu.VisuSubjectId)
            exportVisu.VisuSubjectName=importVisu.VisuSubjectId;
        else
            exportVisu.VisuSubjectName='anonymus';
        end
    case 'Subject'
        exportVisu.VisuSubjectName='anonymus';
end

%% VisuSubjectID
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuSubjectId') && ~isempty(Template.VisuSubjectId)
            exportVisu.VisuSubjectId=Template.VisuSubjectId;
        elseif isfield(importVisu, 'VisuSubjectId') && ~isempty(importVisu.VisuSubjectId)
            exportVisu.VisuSubjectId=importVisu.VisuSubjectId;
        else
            exportVisu.VisuSubjectId='anonymus';
        end
    case {'Template'} 
        if isfield(Template, 'VisuSubjectId') && ~isempty(Template.VisuSubjectId)
            exportVisu.VisuSubjectId=Template.VisuSubjectId;
        else
            exportVisu.VisuSubjectId='anonymus';
        end
    case 'Visu'
        if isfield(importVisu, 'VisuSubjectId') && ~isempty(importVisu.VisuSubjectId)
            exportVisu.VisuSubjectId=importVisu.VisuSubjectId;
        else
            exportVisu.VisuSubjectId='anonymus';
        end
    case 'Subject'
        exportVisu.VisuSubjectId='anonymus';
end

%% VisuSubjectBirthDate
% look at the template

%% VisuSubjectSex
% look at the template

%% VisuStudyUid
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuStudyUid') && ~isempty(Template.VisuStudyUid)
            exportVisu.VisuStudyUid=Template.VisuStudyUid;
        elseif isfield(importVisu, 'VisuStudyUid') && ~isempty(importVisu.VisuStudyUid)
            exportVisu.VisuStudyUid=importVisu.VisuStudyUid;
        elseif isfield(Subject, 'SUBJECT_study_instance_uid') && ~isempty(Subject.SUBJECT_study_instance_uid)
            exportVisu.VisuStudyUid=Subject.SUBJECT_study_instance_uid;
        else
            error('Neither Template.VisuStudyUid, Visu.VisuStudyUid nor Subject.SUBJECT_study_instance_uid does exist') 
        end
    case {'Template'} 
        if isfield(Template, 'VisuStudyUid') && ~isempty(Template.VisuStudyUid)
            exportVisu.VisuStudyUid=Template.VisuStudyUid;
        else
            error('VisuStudyUid does not exist in Template') 
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuStudyUid') && ~isempty(importVisu.VisuStudyUid)
            exportVisu.VisuStudyUid=importVisu.VisuStudyUid;
        else
            error('Visu.VisuStudyUid does not exist') 
        end
    case {'Subject'}
        if isfield(Subject, 'SUBJECT_study_instance_uid') && ~isempty(Subject.SUBJECT_study_instance_uid)
           exportVisu.VisuStudyUid=Subject.SUBJECT_study_instance_uid;
        else
           error('Subject.SUBJECT_study_instance_uid does not exist') 
        end
end    

%% VisuStudyId
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuStudyId') && ~isempty(Template.VisuStudyId)
            exportVisu.VisuStudyId=Template.VisuStudyId;
        elseif isfield(importVisu, 'VisuStudyId') && ~isempty(importVisu.VisuStudyId)
            exportVisu.VisuStudyId=importVisu.VisuStudyId;
        elseif isfield(Subject, 'SUBJECT_study_name') && ~isempty(Subject.SUBJECT_study_name)
            exportVisu.VisuStudyId=Subject.SUBJECT_study_name;
        else
            warning('MATLAB:bruker_warning', 'VisuStudyId does not exist');
        end
    case {'Template'} 
        if isfield(Template, 'VisuStudyId') && ~isempty(Template.VisuStudyId)
            exportVisu.VisuStudyId=Template.VisuStudyId;
        else
            warning('MATLAB:bruker_warning', 'VisuStudyId does not exist');
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuStudyId') && ~isempty(importVisu.VisuStudyId)
            exportVisu.VisuStudyId=importVisu.VisuStudyId;
        else
            warning('MATLAB:bruker_warning', 'VisuStudyId does not exist');
        end
    case {'Subject'}
        if isfield(Subject, 'SUBJECT_study_name') && ~isempty(Subject.SUBJECT_study_name)
            exportVisu.VisuStudyId=Subject.SUBJECT_study_name;
        else
            warning('MATLAB:bruker_warning', 'VisuStudyId does not exist');
        end
        
end

%% VisuStudyNumber
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuStudyNumber') && ~isempty(Template.VisuStudyNumber)
            exportVisu.VisuStudyNumber=Template.VisuStudyNumber;
        elseif isfield(importVisu, 'VisuStudyNumber') && ~isempty(importVisu.VisuStudyNumber)
            exportVisu.VisuStudyNumber=importVisu.VisuStudyNumber;
        elseif isfield(Subject, 'SUBJECT_study_nr') && ~isempty(Subject.SUBJECT_study_nr)
            exportVisu.VisuStudyNumber=Subject.SUBJECT_study_nr;
        end
    case {'Template'} 
        if isfield(Template, 'VisuStudyNumber') && ~isempty(Template.VisuStudyNumber)
            exportVisu.VisuStudyNumber=Template.VisuStudyNumber;
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuStudyNumber') && ~isempty(importVisu.VisuStudyNumber)
            exportVisu.VisuStudyNumber=importVisu.VisuStudyNumber;
        end
    case {'Subject'}
        if isfield(Subject, 'SUBJECT_study_nr') && ~isempty(Subject.SUBJECT_study_nr)
            exportVisu.VisuStudyNumber=Subject.SUBJECT_study_nr;
        end
end

%% VisuSubjectWeight
% look at the template

%% VisuStudyReferringPhysician
% look at the template

%% VisuStudyDescription
% look at the template

%% VisuExperimentNumber
switch genmode
    case {'auto'}
        if exist('expno', 'var') && ~isempty(expno)
            exportVisu.VisuExperimentNumber=expno;
        elseif isfield(Template, 'VisuExperimentNumber') && ~isempty(Template.VisuExperimentNumber)
            exportVisu.VisuExperimentNumber=Template.VisuExperimentNumber;
        elseif isfield(importVisu, 'VisuExperimentNumber') && ~isempty(importVisu.VisuExperimentNumber)
            exportVisu.VisuExperimentNumber=importVisu.VisuExperimentNumber;
        else
            exportVisu.VisuExperimentNumber=99;
        end
    case {'Template'} 
        if exist('expno', 'var') && ~isempty(expno)
            exportVisu.VisuExperimentNumber=expno;
        elseif isfield(Template, 'VisuExperimentNumber') && ~isempty(Template.VisuExperimentNumber)
            exportVisu.VisuExperimentNumber=Template.VisuExperimentNumber;
        else
            exportVisu.VisuExperimentNumber=99;
        end
    case {'Visu'}
        if exist('expno', 'var') && ~isempty(expno)
            exportVisu.VisuExperimentNumber=expno;
        elseif isfield(importVisu, 'VisuExperimentNumber') && ~isempty(importVisu.VisuExperimentNumber)
            exportVisu.VisuExperimentNumber=importVisu.VisuExperimentNumber;
        else
            exportVisu.VisuExperimentNumber=99;
        end
    case {'Subject'}
        if exist('expno', 'var') && ~isempty(expno)
            exportVisu.VisuExperimentNumber=expno;
        else
            exportVisu.VisuExperimentNumber=99;
        end
end

%% VisuProcessingNumber
switch genmode
    case {'auto'}
        if exist('procno', 'var') && ~isempty(procno)
            exportVisu.VisuProcessingNumber=expno;
        elseif isfield(Template, 'VisuProcessingNumber') && ~isempty(Template.VisuProcessingNumber)
            exportVisu.VisuProcessingNumber=Template.VisuProcessingNumber;
        elseif isfield(importVisu, 'VisuProcessingNumber') && ~isempty(importVisu.VisuProcessingNumber)
            exportVisu.VisuProcessingNumber=importVisu.VisuProcessingNumber;
        else
            exportVisu.VisuProcessingNumber=99;
        end
    case {'Template'} 
        if exist('procno', 'var') && ~isempty(procno)
            exportVisu.VisuProcessingNumber=expno;
        elseif isfield(Template, 'VisuProcessingNumber') && ~isempty(Template.VisuProcessingNumber)
            exportVisu.VisuProcessingNumber=Template.VisuProcessingNumber;
        else
            exportVisu.VisuProcessingNumber=99;
        end
    case {'Visu'}
        if exist('procno', 'var') && ~isempty(procno)
            exportVisu.VisuProcessingNumber=expno;
        elseif isfield(importVisu, 'VisuProcessingNumber') && ~isempty(importVisu.VisuProcessingNumber)
            exportVisu.VisuProcessingNumber=importVisu.VisuProcessingNumber;
        else
            exportVisu.VisuProcessingNumber=99;
        end
    case {'Subject'}
        if exist('procno', 'var') && ~isempty(procno)
            exportVisu.VisuProcessingNumber=expno;
        else
            exportVisu.VisuProcessingNumber=99;
        end
end

%% VisuSubjectPosition
switch genmode
    case {'auto'}
        if isfield(Template, 'VisuSubjectPosition') && ~isempty(Template.VisuSubjectPosition)
            exportVisu.VisuSubjectPosition=Template.VisuSubjectPosition;
        elseif isfield(importVisu, 'VisuSubjectPosition') && ~isempty(importVisu.VisuSubjectPosition)
            exportVisu.VisuSubjectPosition=importVisu.VisuSubjectPosition;
        elseif isfield(Subject, 'SUBJECT_entry') && ~isempty(Subject.SUBJECT_entry) && isfield(Subject, 'SUBJECT_position') && ~isempty(Subject.SUBJECT_position)
            exportVisu.VisuSubjectPosition=[Subject.SUBJECT_entry(12:15), '_', Subject.SUBJECT_position(10:end)];
        else
            error('The variables necessary for calculating VisuSubjectPosition don''t exist')
        end
    case {'Template'} 
        if isfield(Template, 'VisuSubjectPosition') && ~isempty(Template.VisuSubjectPosition)
            exportVisu.VisuSubjectPosition=Template.VisuSubjectPosition;
        else
            error('VisuSubjectPosition doesn''t exist in Template')
        end
    case {'Visu'}
        if isfield(importVisu, 'VisuSubjectPosition') && ~isempty(importVisu.VisuSubjectPosition)
            exportVisu.VisuSubjectPosition=importVisu.VisuSubjectPosition;
        else
            error('VisuSubjectPosition doesn''t exist in Visu')
        end
    case {'Subject'}
        if isfield(Subject, 'SUBJECT_entry') && ~isempty(Subject.SUBJECT_entry) && isfield(Subject, 'SUBJECT_position') && ~isempty(Subject.SUBJECT_position)
            exportVisu.VisuSubjectPosition=[Subject.SUBJECT_entry(12:15), '_', Subject.SUBJECT_position(10:end)];
        else
            error('The variables necessary for calculating VisuSubjectPosition from Subject don''t exist')
        end
end    

%% VisuSeriesTypeId
exportVisu.VisuSeriesTypeId='DERIVED_OTHER';
readonly_list=[readonly_list, {'VisuSeriesTypeId'}];

%% VisuManufacturer
% look at the template

%...

%% writepath
exportVisu.imagewrite_path=[first_path_part, exportVisu.VisuSubjectId, '_',  exportVisu.VisuStudyId, second_path_part];
end
