Skip to content

Instantly share code, notes, and snippets.

@NeutralKaon
Last active August 3, 2023 06:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeutralKaon/b3075becba9e12c606048f0218e2658a to your computer and use it in GitHub Desktop.
Save NeutralKaon/b3075becba9e12c606048f0218e2658a to your computer and use it in GitHub Desktop.
A matlab routine for reading Siemens IDEA / POET .dsv pulse sequence directories / files
function out=read_dsv(pathname)
% Read a Siemens IDEA DSV folder as simulated by POET, for subsequent plotting.
% This just parses a pre-existing text file and plonks it into a struct.
%
% Input: a POET-simulated folder containing .dsv files (check your temp
% files in an IDEA VM)
%
% Output: a returned structure containing the parsed contents present.
%
% Should you wish to plot a pulse sequence diagram, you probably want to
% have a look at the .values, .horizontal_unit_name, and .horizontal_delta
% members, and plot to taste. NB: the values are NOT monotonically increasing
% y-ordinates: they're 'wrapped' so that a duration is specified for a certain
% length of time.
%
% Enjoy!
%
% (c) Jack J. Miller, University of Oxford, 2020; jack.miller@physics.org
% GPL v2
if ~isdir(pathname)
error('Check that %s is actually a directory',pathname);
end
fnames = dir(pathname);
out=[];
fprintf(1,'Parsing. Please wait.\n\n');
for ix=1:length(fnames)
current_fname=fnames(ix).name;
returned_results = extractBetween(current_fname, 'SimulationProtocol_', '.dsv');
if length(returned_results) == 1
filename = [fnames(ix).folder '/' fnames(ix).name];
out.(returned_results{1}) = readDSV(filename); %The struct.('fieldname') trick
fprintf(1,'Loaded %s...\n',fnames(ix).name);
end
end
end
function ret = readDSV(filename)
fid=fopen(filename,'r');
content=textscan(fid,'%s','delimiter','\n');
fclose(fid);
no_lines=size(content{1});
readNumbersFlag=0;
nComments = 0;
jx=0;
if no_lines <= 7
warning('File %s is probably empty!',filename);
ret=struct();
return
end
if strcmp(filename(end-6:end-4),'ERR')
ret.errorStatus=content{1};
return
end
if strcmp(filename(end-6:end-4),'INF')
ret.Info=content{1};
return
end
for ix=1:no_lines
line=content{1}{ix};
%I probably could have done this better.
tmp.description = extractAfter(line, 'TITLE=');
if ~isempty(tmp.description)
ret.description=tmp.description;
end
tmp.samples= extractAfter(line, 'SAMPLES=');
if ~isempty(tmp.samples)
ret.samples=str2double(extractAfter(line, 'SAMPLES='));
end
tmp.vertical_unit_name = extractAfter(line,'VERTUNITNAME=');
if ~isempty(tmp.vertical_unit_name )
ret.vertical_unit_name =tmp.vertical_unit_name ;
end
tmp.horizontal_unit_name = extractAfter(line,'HORIUNITNAME=');
if ~isempty(tmp.horizontal_unit_name )
ret.horizontal_unit_name =tmp.horizontal_unit_name ;
end
tmp.horizontal_delta = extractAfter(line,'HORIDELTA=');
if ~isempty(tmp.horizontal_delta )
ret.horizontal_delta =str2double(tmp.horizontal_delta) ;
end
tmp.vertical_factor= extractAfter(line,'VERTFACTOR=');
if ~isempty(tmp.vertical_factor)
ret.vertical_factor=str2double(tmp.vertical_factor);
end
tmp.max_limit = extractAfter(line,'MAXLIMIT=');
if ~isempty(tmp.max_limit )
ret.max_limit =str2double(tmp.max_limit);
end
tmp.min_limit = extractAfter(line,'MINLIMIT=');
if ~isempty(tmp.min_limit )
ret.min_limit =str2double(tmp.min_limit);
end
tmp.version = extractAfter(line, 'FORMAT=');
if ~isempty(tmp.version)
ret.version =tmp.version ;
end
tmp.comment = extractAfter(line, '; ');
if ~isempty(tmp.comment)
nComments=nComments+1;
ret.Comment{nComments} = tmp.comment;
end
if (readNumbersFlag && ~isempty(line))
ret.values(jx)=str2double(line);
jx=jx+1;
end
if strcmp(line,'[VALUES]')
readNumbersFlag=1;
ret.values=zeros(ret.samples,1);
jx=1;
end
end
if (length(ret.values) ~= ret.samples)
warning('The expected and measured length of samples and values differed');
end
if ~strcmp(ret.version, 'DSV_V0100')
warning('Unknown DSV version "%s"!',tmp.version);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment