PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/ATF2/FlightSim/trustedApps/extDispCalc/extDispCalc.m

http://atf2flightsim.googlecode.com/
MATLAB | 479 lines | 218 code | 92 blank | 169 comment | 28 complexity | c72030287ab9fe752cd71b2971125b18 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  1. function varargout = extDispCalc(varargin)
  2. % EXTDISPCALC M-file for extDispCalc.fig
  3. % EXTDISPCALC, by itself, creates a new EXTDISPCALC or raises the existing
  4. % singleton*.
  5. %
  6. % H = EXTDISPCALC returns the handle to a new EXTDISPCALC or the handle to
  7. % the existing singleton*.
  8. %
  9. % EXTDISPCALC('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in EXTDISPCALC.M with the given input arguments.
  11. %
  12. % EXTDISPCALC('Property','Value',...) creates a new EXTDISPCALC or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before extDispCalc_OpeningFcn gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to extDispCalc_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Edit the above text to modify the response to help extDispCalc
  23. % Last Modified by GUIDE v2.5 16-May-2008 01:43:40
  24. % Begin initialization code - DO NOT EDIT
  25. gui_Singleton = 1;
  26. gui_State = struct('gui_Name', mfilename, ...
  27. 'gui_Singleton', gui_Singleton, ...
  28. 'gui_OpeningFcn', @extDispCalc_OpeningFcn, ...
  29. 'gui_OutputFcn', @extDispCalc_OutputFcn, ...
  30. 'gui_LayoutFcn', [] , ...
  31. 'gui_Callback', []);
  32. if nargin && ischar(varargin{1})
  33. gui_State.gui_Callback = str2func(varargin{1});
  34. end
  35. if nargout
  36. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  37. else
  38. gui_mainfcn(gui_State, varargin{:});
  39. end
  40. % End initialization code - DO NOT EDIT
  41. % --- Executes just before extDispCalc is made visible.
  42. function extDispCalc_OpeningFcn(hObject, eventdata, handles, varargin)
  43. % This function has no output args, see OutputFcn.
  44. % hObject handle to figure
  45. % eventdata reserved - to be defined in a future version of MATLAB
  46. % handles structure with handles and user data (see GUIDATA)
  47. % varargin command line arguments to extDispCalc (see VARARGIN)
  48. global FL BEAMLINE INSTR
  49. % Choose default command line output for extDispCalc
  50. handles.output = handles;
  51. % Update handles structure
  52. guidata(hObject, handles);
  53. % Get bpm names for EXT
  54. nbpm=0;
  55. for ibpm=find(cellfun(@(x) x.Index>FL.SimModel.extStart,INSTR))
  56. nbpm=nbpm+1;
  57. UserData{1}{nbpm}=BEAMLINE{INSTR{ibpm}.Index}.Name;
  58. end % for ibpm
  59. % Fill popupmenu with names
  60. set(handles.popupmenu2,'String',UserData{1});
  61. % Fill userdata field
  62. set(handles.figure1,'UserData',UserData);
  63. % Update twiss parameters from online model
  64. [stat,FL.SimModel.Twiss] = GetTwiss(1,length(BEAMLINE),FL.SimModel.Initial.x.Twiss,FL.SimModel.Initial.y.Twiss) ;
  65. if stat{1}~=1
  66. errordlg(['Error updating model Twiss parameters:' stat{2}],'extDispCalc error');
  67. end % if err
  68. % --- Outputs from this function are returned to the command line.
  69. function varargout = extDispCalc_OutputFcn(hObject, eventdata, handles)
  70. % varargout cell array for returning output args (see VARARGOUT);
  71. % hObject handle to figure
  72. % eventdata reserved - to be defined in a future version of MATLAB
  73. % handles structure with handles and user data (see GUIDATA)
  74. % Get default command line output from handles structure
  75. varargout{1} = handles.output;
  76. % --- Executes on button press in pushbutton2.
  77. function pushbutton2_Callback(hObject, eventdata, handles)
  78. % hObject handle to pushbutton2 (see GCBO)
  79. % eventdata reserved - to be defined in a future version of MATLAB
  80. % handles structure with handles and user data (see GUIDATA)
  81. guiCloseFn('extDispCalc',handles);
  82. function edit1_Callback(hObject, eventdata, handles)
  83. % hObject handle to edit1 (see GCBO)
  84. % eventdata reserved - to be defined in a future version of MATLAB
  85. % handles structure with handles and user data (see GUIDATA)
  86. % Hints: get(hObject,'String') returns contents of edit1 as text
  87. % str2double(get(hObject,'String')) returns contents of edit1 as a double
  88. val=str2double(get(hObject,'String'));
  89. if isnan(val) || ~isnumeric(val)
  90. errordlg('Must pass number in Npulse field','extDispCalc Error');
  91. return
  92. end % if not num
  93. [stat,output]=FlHwUpdate('buffersize');
  94. if stat{1}~=1
  95. errordlg('Not able to receive bpm buffer size','FlHwUpdate error');
  96. return
  97. end % if err
  98. set(hObject,'String',num2str(min(output,val)));
  99. % --- Executes during object creation, after setting all properties.
  100. function edit1_CreateFcn(hObject, eventdata, handles)
  101. % hObject handle to edit1 (see GCBO)
  102. % eventdata reserved - to be defined in a future version of MATLAB
  103. % handles empty - handles not created until after all CreateFcns called
  104. % Hint: edit controls usually have a white background on Windows.
  105. % See ISPC and COMPUTER.
  106. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  107. set(hObject,'BackgroundColor','white');
  108. end
  109. % --- Executes on button press in pushbutton1.
  110. function pushbutton1_Callback(hObject, eventdata, handles)
  111. % hObject handle to pushbutton1 (see GCBO)
  112. % eventdata reserved - to be defined in a future version of MATLAB
  113. % handles structure with handles and user data (see GUIDATA)
  114. global BEAMLINE FL
  115. errHandler('reset',handles);drawnow('expose');
  116. UserData=get(handles.figure1,'UserData');
  117. if length(UserData)>=2 && ~isempty(UserData{2})
  118. [stat, Dmdl] = extDispFit(UserData{2}{1});
  119. if stat{1}~=1; errHandler(['EXT disp fitting routine: ',stat{2}],handles); return; end;
  120. else
  121. errHandler('Need to perform dispersion calculation first',handles);
  122. return
  123. end % if run disp calc
  124. UserData{3}=Dmdl;
  125. % plot fitted functions
  126. try
  127. sp=subplot(1,1,1,'Parent',handles.uipanel3);
  128. svals=arrayfun(@(x) BEAMLINE{x}.S,FL.SimModel.extStart+1:length(BEAMLINE))-BEAMLINE{FL.SimModel.extStart+1}.S;
  129. hold on
  130. plot(sp,svals,Dmdl(1,:),'r');
  131. plot(sp,svals,Dmdl(2,:),'b');
  132. hold off
  133. axis tight
  134. catch
  135. errHandler(['Error plotting fitted dispersion values: ',lasterr],handles)
  136. return
  137. end % try/catch
  138. set(handles.figure1,'UserData',UserData);
  139. % --- Executes on button press in pushbutton3.
  140. function pushbutton3_Callback(hObject, eventdata, handles)
  141. % hObject handle to pushbutton3 (see GCBO)
  142. % eventdata reserved - to be defined in a future version of MATLAB
  143. % handles structure with handles and user data (see GUIDATA)
  144. global INSTR BEAMLINE FL
  145. UserData=get(handles.figure1,'UserData');
  146. % Run Disp calculationn routine
  147. errHandler('reset',handles);
  148. nave=str2double(get(handles.edit2,'String'));
  149. if isnan(nave) || ~isnumeric(nave)
  150. errHandler('Not a number in ave box');
  151. return
  152. end % ave check
  153. Dx=[];Dy=[];
  154. for iave=1:nave
  155. set(handles.text4,'String',sprintf('Running Dispersion calculation routine, calc %d of %d...',iave,nave)); drawnow('expose');
  156. [stat, D, D_err, CHI2, dEmean, xbpm_e, ybpm_e] = bpmDispCalc(round(str2double(get(handles.edit1,'String'))));
  157. if stat{1}~=1; errHandler(['Error In Dispersion calculation routine: ',stat{2}],handles); return; end;
  158. Dx=[Dx;D(1,:)]; Dy=[Dy;D(2,:)];
  159. end % for iave
  160. D=[mean(Dx); mean(Dy)];
  161. D_err=[std(Dx); std(Dy)];
  162. % Put data in UserData
  163. UserData{2}={D, D_err, CHI2, dEmean, xbpm_e, ybpm_e};
  164. % Get INSTR s vals
  165. instlist=cellfun(@(x) x.Index,INSTR);
  166. svals=arrayfun(@(x) BEAMLINE{x}.S,instlist(instlist>FL.SimModel.extStart));
  167. if length(svals)~=length(D(1,:))
  168. errHandler('length mismatch between EXT INSTR model and dispersion values',handles);
  169. return
  170. end % check lengths match
  171. set(handles.text4,'String','Plotting results...'); drawnow('expose');
  172. % Plot dispersion values
  173. sp=subplot(1,1,1,'Parent',handles.uipanel3);
  174. errorbar(sp,svals,D(1,:)*1e3,D_err(1,:)*1e3,'r*-')
  175. hold on
  176. errorbar(sp,svals,D(2,:)*1e3,D_err(2,:)*1e3,'b*-')
  177. hold off
  178. legend('x','y');
  179. % Populate dispersion and chi2 listboxes
  180. if length(D(1,:))~=length(UserData{1})
  181. errHandler('Mismatch between number of expected bpms and those returned from bpmDispCalc',handles);
  182. return;
  183. end % if err
  184. lb1str={};
  185. lb2str={};
  186. for ibpm=1:length(UserData{1})
  187. lb1str{end+1}=[UserData{1}{ibpm} ' (x): ' sprintf('%.3e',D(1,ibpm))];
  188. lb1str{end+1}=[UserData{1}{ibpm} ' (y): ' sprintf('%.3e',D(2,ibpm))];
  189. lb2str{end+1}=[UserData{1}{ibpm} ' (x): ' sprintf('%.3e',CHI2(1,ibpm))];
  190. lb2str{end+1}=[UserData{1}{ibpm} ' (y): ' sprintf('%.3e',CHI2(2,ibpm))];
  191. end
  192. set(handles.listbox1,'String',lb1str);
  193. set(handles.listbox2,'String',lb2str);
  194. set(handles.text4,'String','Completed DispCalc routine'); drawnow('expose');
  195. % Write-out UserData
  196. set(handles.figure1,'UserData',UserData);
  197. % --- Executes on button press in togglebutton1.
  198. function togglebutton1_Callback(hObject, eventdata, handles)
  199. % hObject handle to togglebutton1 (see GCBO)
  200. % eventdata reserved - to be defined in a future version of MATLAB
  201. % handles structure with handles and user data (see GUIDATA)
  202. % Hint: get(hObject,'Value') returns toggle state of togglebutton1
  203. global FL
  204. if ~isfield(FL,'t_extDispCalc')
  205. FL.t_extDispCalc=timer('StartDelay',1,'Period',1/FL.accRate,...
  206. 'ExecutionMode','FixedRate','BusyMode','drop');
  207. FL.t_extDispCalc.TimerFcn = ['extDispCalc(''pushbutton3_Callback'',',num2str(hObject,100),',[],',num2str(handles,100),')'];
  208. end % if need to create timer object
  209. if get(hObject,'Value')
  210. if ~isequal(FL.t_extDispCalc.running,'on')
  211. start(FL.t_extDispCalc);
  212. end % if not already running
  213. else
  214. stop(FL.t_extDispCalc);
  215. end % if autorun toggled
  216. % --- Executes on selection change in listbox1.
  217. function listbox1_Callback(hObject, eventdata, handles)
  218. % hObject handle to listbox1 (see GCBO)
  219. % eventdata reserved - to be defined in a future version of MATLAB
  220. % handles structure with handles and user data (see GUIDATA)
  221. % Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
  222. % contents{get(hObject,'Value')} returns selected item from listbox1
  223. % --- Executes during object creation, after setting all properties.
  224. function listbox1_CreateFcn(hObject, eventdata, handles)
  225. % hObject handle to listbox1 (see GCBO)
  226. % eventdata reserved - to be defined in a future version of MATLAB
  227. % handles empty - handles not created until after all CreateFcns called
  228. % Hint: listbox controls usually have a white background on Windows.
  229. % See ISPC and COMPUTER.
  230. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  231. set(hObject,'BackgroundColor','white');
  232. end
  233. % --- Executes on selection change in listbox2.
  234. function listbox2_Callback(hObject, eventdata, handles)
  235. % hObject handle to listbox2 (see GCBO)
  236. % eventdata reserved - to be defined in a future version of MATLAB
  237. % handles structure with handles and user data (see GUIDATA)
  238. % Hints: contents = get(hObject,'String') returns listbox2 contents as cell array
  239. % contents{get(hObject,'Value')} returns selected item from listbox2
  240. % --- Executes during object creation, after setting all properties.
  241. function listbox2_CreateFcn(hObject, eventdata, handles)
  242. % hObject handle to listbox2 (see GCBO)
  243. % eventdata reserved - to be defined in a future version of MATLAB
  244. % handles empty - handles not created until after all CreateFcns called
  245. % Hint: listbox controls usually have a white background on Windows.
  246. % See ISPC and COMPUTER.
  247. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  248. set(hObject,'BackgroundColor','white');
  249. end
  250. % --- Executes on button press in pushbutton5.
  251. function pushbutton5_Callback(hObject, eventdata, handles)
  252. % hObject handle to pushbutton5 (see GCBO)
  253. % eventdata reserved - to be defined in a future version of MATLAB
  254. % handles structure with handles and user data (see GUIDATA)
  255. global FL BEAMLINE
  256. % Update twiss parameters from online model
  257. [stat,FL.SimModel.Twiss] = GetTwiss(1,length(BEAMLINE),FL.SimModel.Initial.x.Twiss,FL.SimModel.Initial.y.Twiss) ;
  258. if stat{1}~=1
  259. errordlg(['Error updating model Twiss parameters:' stat{2}],'extDispCalc error');
  260. end % if err
  261. % --- Executes on selection change in popupmenu2.
  262. function popupmenu2_Callback(hObject, eventdata, handles)
  263. % hObject handle to popupmenu2 (see GCBO)
  264. % eventdata reserved - to be defined in a future version of MATLAB
  265. % handles structure with handles and user data (see GUIDATA)
  266. % Hints: contents = get(hObject,'String') returns popupmenu2 contents as cell array
  267. % contents{get(hObject,'Value')} returns selected item from popupmenu2
  268. % --- Executes during object creation, after setting all properties.
  269. function popupmenu2_CreateFcn(hObject, eventdata, handles)
  270. % hObject handle to popupmenu2 (see GCBO)
  271. % eventdata reserved - to be defined in a future version of MATLAB
  272. % handles empty - handles not created until after all CreateFcns called
  273. % Hint: popupmenu controls usually have a white background on Windows.
  274. % See ISPC and COMPUTER.
  275. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  276. set(hObject,'BackgroundColor','white');
  277. end
  278. % --- Executes on button press in pushbutton6.
  279. function pushbutton6_Callback(hObject, eventdata, handles)
  280. % hObject handle to pushbutton6 (see GCBO)
  281. % eventdata reserved - to be defined in a future version of MATLAB
  282. % handles structure with handles and user data (see GUIDATA)
  283. UserData=get(handles.figure1,'UserData');
  284. % get BPM to plot (x & y)
  285. bpm=get(handles.popupmenu2,'Value');
  286. Px=polyfit(UserData{2}{4}*1e3,UserData{2}{5}(:,bpm)'*1e3,1);
  287. Py=polyfit(UserData{2}{4}*1e3,UserData{2}{6}(:,bpm)'*1e3,1);
  288. % plot energy correlations
  289. sp=subplot(1,2,1,'Parent',handles.uipanel3);
  290. plot(sp,UserData{2}{4}*1e3,UserData{2}{5}(:,bpm)*1e3,'r.')
  291. hold on
  292. plot(sp,UserData{2}{4}*1e3,polyval(Px,UserData{2}{4}*1e3),'r')
  293. hold off
  294. sp=subplot(1,2,2,'Parent',handles.uipanel3);
  295. plot(sp,UserData{2}{4}*1e3,UserData{2}{6}(:,bpm)*1e3,'b.')
  296. hold on
  297. plot(sp,UserData{2}{4}*1e3,polyval(Py,UserData{2}{4}*1e3),'b')
  298. hold off
  299. % --- Executes on button press in pushbutton7.
  300. function pushbutton7_Callback(hObject, eventdata, handles)
  301. % hObject handle to pushbutton7 (see GCBO)
  302. % eventdata reserved - to be defined in a future version of MATLAB
  303. % handles structure with handles and user data (see GUIDATA)
  304. global BEAMLINE FL INSTR
  305. UserData=get(handles.figure1,'UserData');
  306. % Get INSTR s vals
  307. instlist=cellfun(@(x) x.Index,INSTR);
  308. svals=arrayfun(@(x) BEAMLINE{x}.S,instlist(instlist>FL.SimModel.extStart));
  309. if length(svals)~=length(UserData{1}(1,:))
  310. errordlg('length mismatch between EXT INSTR model and dispersion values','dispCalc GUI error')
  311. return
  312. end % check lengths match
  313. sp=subplot(1,1,1,'Parent',handles.uipanel3);
  314. % Plot dispersion values
  315. errorbar(sp,svals,UserData{2}{1}(1,:)*1e3,UserData{2}{2}(1,:)*1e3,'r*-')
  316. hold on
  317. errorbar(sp,svals,UserData{2}{1}(2,:)*1e3,UserData{2}{2}(2,:)*1e3,'b*-')
  318. hold off
  319. legend('x','y');
  320. % --- Executes on key press with focus on togglebutton1 and none of its controls.
  321. function togglebutton1_KeyPressFcn(hObject, eventdata, handles)
  322. % hObject handle to togglebutton1 (see GCBO)
  323. % eventdata structure with the following fields (see UICONTROL)
  324. % Key: name of the key that was pressed, in lower case
  325. % Character: character interpretation of the key(s) that was pressed
  326. % Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
  327. % handles structure with handles and user data (see GUIDATA)
  328. % --- Executes on key press with focus on pushbutton3 and none of its controls.
  329. function pushbutton3_KeyPressFcn(hObject, eventdata, handles)
  330. % hObject handle to pushbutton3 (see GCBO)
  331. % eventdata structure with the following fields (see UICONTROL)
  332. % Key: name of the key that was pressed, in lower case
  333. % Character: character interpretation of the key(s) that was pressed
  334. % Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
  335. % handles structure with handles and user data (see GUIDATA)
  336. % --- If Enable == 'on', executes on mouse press in 5 pixel border.
  337. % --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton1.
  338. function pushbutton1_ButtonDownFcn(hObject, eventdata, handles)
  339. % hObject handle to pushbutton1 (see GCBO)
  340. % eventdata reserved - to be defined in a future version of MATLAB
  341. % handles structure with handles and user data (see GUIDATA)
  342. % --- Executes when user attempts to close figure1.
  343. function figure1_CloseRequestFcn(hObject, eventdata, handles)
  344. % hObject handle to figure1 (see GCBO)
  345. % eventdata reserved - to be defined in a future version of MATLAB
  346. % handles structure with handles and user data (see GUIDATA)
  347. try
  348. guiCloseFn('extDispCalc',handles);
  349. catch
  350. delete(hObject)
  351. end
  352. % --- Executes on button press in pushbutton8.
  353. function pushbutton8_Callback(hObject, eventdata, handles)
  354. % hObject handle to pushbutton8 (see GCBO)
  355. % eventdata reserved - to be defined in a future version of MATLAB
  356. % handles structure with handles and user data (see GUIDATA)
  357. [stat,out]=FlHwUpdate('buffersize');
  358. if stat{1}~=1; errHandler(['Error getting buffer data: ',stat{2}],handles); return; end;
  359. set(handles.edit1,'String',num2str(out));
  360. % --- Error Handler----
  361. function errHandler(str,handles)
  362. if isequal(str,'reset')
  363. set(handles.text4,'BackgroundColor','White');
  364. set(handles.text4,'ForegroundColor','Black');
  365. set(handles.text4,'String',[]);
  366. else
  367. set(handles.text4,'String',str);
  368. set(handles.text4,'BackgroundColor','Red')
  369. set(handles.text4,'ForegroundColor','White')
  370. return
  371. end
  372. function edit2_Callback(hObject, eventdata, handles)
  373. % hObject handle to edit2 (see GCBO)
  374. % eventdata reserved - to be defined in a future version of MATLAB
  375. % handles structure with handles and user data (see GUIDATA)
  376. % Hints: get(hObject,'String') returns contents of edit2 as text
  377. % str2double(get(hObject,'String')) returns contents of edit2 as a double
  378. % --- Executes during object creation, after setting all properties.
  379. function edit2_CreateFcn(hObject, eventdata, handles)
  380. % hObject handle to edit2 (see GCBO)
  381. % eventdata reserved - to be defined in a future version of MATLAB
  382. % handles empty - handles not created until after all CreateFcns called
  383. % Hint: edit controls usually have a white background on Windows.
  384. % See ISPC and COMPUTER.
  385. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  386. set(hObject,'BackgroundColor','white');
  387. end