-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save and load lattices in JSON format (#766)
* Handle JSON files in python and Matlab * Added tests for save and load
- Loading branch information
Showing
15 changed files
with
857 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
function varargout=atwritejson(ring, varargin) | ||
%ATWRITEJSON Create a JSON file to store an AT lattice | ||
% | ||
%JS=ATWRITEJSON(RING) | ||
% Return the JSON representation of RING as a character array | ||
% | ||
%ATWRITEJSON(RING, FILENAME) | ||
% Write the JSON representation of RING to the file FILENAME | ||
% | ||
%ATWRITEJSON(RING, ..., 'compact', true) | ||
% If compact is true, write a compact JSON file (no linefeeds) | ||
% | ||
%see also atloadlattice | ||
|
||
[compact, varargs]=getoption(varargin, 'compact', false); | ||
[filename, ~]=getargs(varargs,[]); | ||
|
||
if ~isempty(filename) | ||
%get filename information | ||
[pname,fname,ext]=fileparts(filename); | ||
|
||
%Check file extension | ||
if isempty(ext), ext='.json'; end | ||
|
||
% Open file to be written | ||
[fid,mess]=fopen(fullfile(pname,[fname ext]),'wt'); | ||
|
||
if fid==-1 | ||
error('AT:FileErr','Cannot Create file %s\n%s',fn,mess); | ||
else | ||
fprintf(fid, sjson(ring)); | ||
fclose(fid); | ||
end | ||
varargout={}; | ||
else | ||
varargout={sjson(ring)}; | ||
end | ||
|
||
function jsondata=sjson(ring) | ||
ok=~atgetcells(ring, 'Class', 'RingParam'); | ||
data.atjson= 1; | ||
data.elements=ring(ok); | ||
data.properties=get_params(ring); | ||
jsondata=jsonencode(data, 'PrettyPrint', ~compact); | ||
end | ||
|
||
function prms=get_params(ring) | ||
% Get "standard" properties | ||
[name, energy, part, periodicity, harmonic_number]=... | ||
atGetRingProperties(ring,'FamName', 'Energy', 'Particle',... | ||
'Periodicity', 'HarmNumber'); | ||
prms=struct('name', name, 'energy', energy, 'periodicity', periodicity,... | ||
'particle', saveobj(part), 'harmonic_number', harmonic_number); | ||
% Add user-defined properties | ||
idx=atlocateparam(ring); | ||
if ~isempty(idx) | ||
flist={'FamName','PassMethod','Length','Class',... | ||
'Energy', 'Particle','Periodicity','cell_harmnumber'}; | ||
present=isfield(ring{idx}, flist); | ||
p2=rmfield(ring{idx},flist(present)); | ||
for nm=fieldnames(p2)' | ||
na=nm{1}; | ||
prms.(na)=p2.(na); | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ | |
from .reprfile import * | ||
from .tracy import * | ||
from .elegant import * | ||
from .json import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.