% program mymggreadx.m treats output from the mysimp0.vi and MicrOptics
% uses mgg0.m mami0.m
% reads : 'On Date at Time [R=] Ohm T= C' and imports 'tms,tmp;T,0;'  
% outputs : time  R T T0  
% default ofilen='T'+ifilen;
% use mymggread0(ifilen[,savf[,ofilen[,logf[,comf[,skpo[,lapo[,nnn]]]]]]])
% ifilen ... input filename (must be entered!)
% savf   ... flag savf=1 to save [savf=0]
% ofilen ... output filename [ofilen='tempSee00.txt'] fiom ifilen
% logf   ... logf=[1] use log R 
% comf   ... comf=[1] use commas as separators 
% skpo   ... start from the point skpo of the input file [skpo=0]
% lapo   ... stop at the point lapo [intmax] of the input file
% nscn   ... nr of readings in a scan
% example :
% mymggreadx('./ddata/RT_030625_GST225_100nm_test.txt'); reads and plots interactively log
% mymggreadx('./ddata/RT_030625_GST225_100nm_test.txt',1,[],0); + saves default ofilen lin
% mymggreadx('./ddata/RT_030625_GST225_100nm_test.txt',1,[],1); % or log
% input and corresponding output line example old/nw alld=0/1 in L65
%    Date         Time     R                 T 
% On 04.11.2020 at 9:34:44 R=2.147484E+9 Ohm T=23.865492E+0 C
% On 03.06.2025 at 12:48:24  2.333194E+05 Ohm T=27,231155E+0 C
% ? dbstop in myseeread0 at 53
% natural directory cd F:\Mysa\MGorylova
% 09. 11. 2020 revisited 06. 06. 2025 
% Milos Steinhart, Milky Way 13,
function myseereadx(varargin)
global qtu0 xyz skpo tipo fiti xyz0
global AX H1 H2
%qtu0=[...
%0 25.9492 -0.21317  0.790187   0.455278   0.133045    0.0202414  0.00126682;...
%0 25.928  -0.760296 0.0463779 -0.00216539 6.04814e-5 -7.29342e-7 0]';         
%ttest=local_u2t(-0.00310771);
if and(nargin==1,varargin{1}=='$')
%if nargin==1 && varargin{1}=='$'
  locf=get(gcbo,'tag');
  switch locf
    case 'gcheck'
      cval=[];
    otherwise
      cval=eval(get(gcbo,'string')); 
  end
%  cval=eval(get(gcbo,'string'));
  local_f(locf,cval)
else
if nargin>0 ifilen=varargin{1}; else ifilen='blelog.txt'; end
if nargin>1 savf=varargin{2}; else savf=0; end      % save flag
if nargin>2 ofilen=varargin{3}; else ofilen=[]; end % or default
if nargin>3 logf=varargin{4}; else logf=1; end      % log(R) lfag
if nargin>4 comf=varargin{5}; else comf=1; end      % comma lfag
if nargin>5 skpo=varargin{6}; else skpo=0; end      % skip skpo points
if nargin>6 lapo=varargin{7}; else lapo=intmax; end % flintmax
if nargin>7 nscn=varargin{8}; else nscn=11; end     % readings in a scan
%
% *** creates iifilen and possibly ofilen from ifilen
if nargout('fileparts')==3
  [indir infile inext] = fileparts(ifilen);
 else %if nargout('fileparts')==4
  [indir infile inext inver] = fileparts(ifilen);
end
iifilen=strcat(indir,'/',infile(2:end),inext);
if isempty(ofilen) ofilen=strcat(indir,'/T',infile(2:end),inext); end
% 1 new data; 0 old format %0 fake T new format stop at 126 plot xyz 141
% *** mgg0 reads T0 -> xyzz ***
alld=1; % alld=1; new alld=0; old
if alld>=0  [xyzz, strstr, endstr]=mgg0(iifilen,1);
  else
%  strstr='03-Jun-2025 12:48:20';
  strstr='05-Nov-2020 08:35:30';
end
%load('bleble.mat'); % loads data when tuning
%
% *** read time R, T -> xyz ***
kk=0;
fid=fopen(ifilen);
tline=deblank(fgetl(fid)); % skip the first line
while ~feof(fid)
  kk=kk+1;
  tline=deblank(fgetl(fid));
  tline(tline==',')='.';      % delimiter ',' -> '.'  
if alld==0
  ble=textscan(tline,'%s %s %s %s R=%f %s T=%f %s'); % old
 else
  ble=textscan(tline,'%s %s %s %s %f %s T=%f %s'); % final
end
 %                     1  2  3  4  5  6    7   8
 %                        D  at Ti R  Ohm  Tmp C
  cdatv=datevec(ble{2},'dd.mm.yyyy');
  cuhrv=datevec(ble{4}).*[0 0 0 1 1 1];
  ctimv=cdatv+cuhrv;
  ble{1}=cdatv; ble{3}=ctimv; % ctimv->at
  if kk==1
%        date    hour    R       T     
    bla={ble{1}, ble{3}, ble{5}, ble{7}};
    fiti=ble{3}; % !!!
    xyz(kk,1)=0; xyz(kk,2)=ble{5}; xyz(kk,3)=ble{7}; % t R T
    deltat=max(datenum(fiti-datevec(strstr)),1);     % Dt MicrOptics
   else % 92 if kk==1 
    bla=[bla; {ble{1}, ble{3}, ble{5}, ble{7}}];     % Dat tim R T
    cdtim=etime(bla{kk,2},bla{1,2});                 % Dt from beg   
    xyz(kk,1)=cdtim; xyz(kk,2)=ble{5}; xyz(kk,3)=ble{7}; 
  end % 92 if kk==1
end % 77 while ~feof(fid)
%
% *** t-scale from the beginning of T0 measurements ***
veltat=datevec(deltat);
deltat=3600*veltat(4)+60*veltat(5)+veltat(6);
xyz(:,1)=xyz(:,1)+deltat;
xyz=xyz(and(xyz(:,3)>20,xyz(:,3)<500),:); % get rid of most of bad T values
% and can be roughly plotted ...[mmx,imx]=max(xyz0(:,3));
%if mmx>100 xyz=xyz([1:imx-1 imx+1:end],:); end;
[mal mac]=size(xyz);
fipo=max(skpo+1,1);
lapo=min(lapo,mal);
tipo=xyz(fipo,1);                     % time zero
xyz=xyz(fipo:lapo,:);                 % start from skpo = skip skpo-1 points  
disp([int2str(skpo),' readings were skipped!']);
% here time has the same beginning both in xyz and xyzz
%
% *** resample T0 in xyzz into xyz(:,1) times ***
xyz(:,4)=interp1(xyzz(:,1),xyzz(:,2),xyz(:,1)); 
% *** test plot 
if 1<2
  figure
  plot(xyz(:,1),xyz(:,3),'b.-');
  hold on
  plot(xyz(:,1),xyz(:,4),'r.-');
  xlabel('Time [s]');
  ylabel('Temperature [C]');
  legend('Sample\_T','Plate\_T0');
  title('T vs resampled T0');
end
%  1    2   3   4   5     6          7  8  9
%% time R   T  % standard version
%xyz=xyz(abs(xyz(:,2)-xyz(:,3))>1,:); % choses suffitiend dt
% ***** plot r and T in the course of time *****
tfh=figure;
%plot(xyz(:,1),50*log10(xyz(:,2)),'.-');
xyz0=xyz;                  % original to save
if logf==1 xyz(:,2)=log10(xyz(:,2)); end % log if logf
%hold on
%plot(xyz(:,1),xyz(:,3),'m.-');
%plot(xyzz(:,1),xyzz(:,2),'r.-');
h2d=1;
[AX,H1,H2]=plotyy(xyz(:,1)*h2d,xyz(:,2),xyz(:,1)*h2d,xyz(:,3));
set(H1,'marker','.','markersize',5,'linestyle','none');
set(H2,'marker','.','markersize',14);
%set(AX(1),'ylim',[wmin wmax]); % propper Dose range
%set(AX(2),'ylim',[imin imax]); % propper INR range
%set(AX(2),'ygrid','on','ytick',[2 3],'linewidth',1);
%set(AX(2),'ytick',[2 3])
%ylabel('Warfarin Dose [mg]','fontsize',12);
%ylabel(AX(2),'INR [1]','fontsize',12);
%title(['Warfarin Doses and INR: ',datestr(blv),' -> ',datestr(bll)]);
%plot(xyz(:,1),xyz(:,6),'g.-');
if logf==1
  ylabel('R [lgOhm]','fontsize',12); xlabel('Time [s]');
 else
  ylabel('R [Ohm]','fontsize',12); xlabel('Time [s]');
end
ylabel(AX(2),'Temperature [C]','fontsize',12); %xlabel('Time [s]');
title('Sample Temperature and R in the Course of Time');
supf=0;
if supf>0
% ***** UCu1 vs average T *****
sfh=figure;                           % 
plot(xyz(:,6),xyz(:,4),'r.-');
ylabel('UCu1 [V]'); xlabel('Average T [C]');
title('UCu1 vs <T>');
% ***** UCu2 vs average T *****
ufh=figure;                           % 
plot(xyz(:,6),xyz(:,9),'g.-');
ylabel('UCu2 [V]'); xlabel('Average T [C]');
title('UCu2 vs <T>');
end % supf>0

if savf==1
  if comf==1
    [nlr ncr]=size(xyz);
    fod=fopen(ofilen,'w');
    for ii=1:nlr
      if ~isnan(xyz(ii,:))
        tline=sprintf('%0.5g ',xyz(ii,:));
        tline(tline=='.')=',';
%        tline(tline==' ')=';';
        fprintf(fod,'%s\n',tline);
      end
    end
    fclose(fod);
   else
    save(ofilen,'xyz','-ascii');
  end %if comf==1
disp(['File ',ofilen,' has been created!!!']);
end % if savf==1

if 1<2
% tag is also used as the name of subfunction
gchk=uicontrol('Parent',tfh,'units','normalized',...
    'position',[0.01 0.012 0.1 0.05], ...
    'BackgroundColor',[0.8 0.8 0.8], ...
	'Callback','mymggreadx $', ...
	'buttondownfcn','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','check', ...
	'Tag','gcheck');
gxmi=uicontrol('Parent',tfh,'units','normalized',...
    'position',[0.12 0.012 0.1 0.05], ...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','xmin', ...
	'Tag','gxmi');
%	'buttondownfcn','mymggreadx rcheck', ...
gxma=uicontrol('Parent',tfh,'units','normalized',...
    'position',[0.88 0.012 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','xmax', ...
	'Tag','gxma');
grmi=uicontrol('Parent',tfh,'units','normalized',...
    'position',[0.01 0.14 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','rmin', ...
	'Tag','grmi');
grma=uicontrol('Parent',tfh,'units','normalized',...
    'position',[0.01 0.84 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','rmax', ...
	'Tag','grma');
gtmi=uicontrol('Parent',tfh,'units','normalized',...
    'position',[0.92 0.14 0.08 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','tmin', ...
	'Tag','gtmi');
gtma=uicontrol('Parent',tfh,'units','normalized',...
    'position',[0.92 0.84 0.08 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','tmax', ...
	'Tag','gtma');
if supf>0
%b1h=uicontrol('Parent',sfh,'units','normalized',...
%    'position',[0.01 0.012 0.1 0.05], ...
%    'BackgroundColor',[0.8 0.8 0.8], ...
%	'Callback','mymggreadx $', ...
%	'buttondownfcn','mymggreadx $', ...
%	'ListboxTop',0, ...
%	'String','check', ...
%	'Tag','scheck');
b1h=uicontrol('Parent',sfh,'units','normalized',...
    'position',[0.12 0.012 0.1 0.05], ...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','xmin', ...
	'Tag','ssxmi');
%	'buttondownfcn','mymggreadx rcheck', ...
b1h=uicontrol('Parent',sfh,'units','normalized',...
    'position',[0.88 0.012 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','xmax', ...
	'Tag','ssxma');
b1h=uicontrol('Parent',sfh,'units','normalized',...
    'position',[0.01 0.14 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','ymin', ...
	'Tag','ssymi');
b1h=uicontrol('Parent',sfh,'units','normalized',...
    'position',[0.01 0.84 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','ymax', ...
	'Tag','ssyma');
%b1h=uicontrol('Parent',ufh,'units','normalized',...
%    'position',[0.01 0.012 0.1 0.05], ...
%    'BackgroundColor',[0.8 0.8 0.8], ...
%	'Callback','mymggreadx $', ...
%	'buttondownfcn','mymggreadx $', ...
%	'ListboxTop',0, ...
%	'String','check', ...
%	'Tag','ucheck');
b1h=uicontrol('Parent',ufh,'units','normalized',...
    'position',[0.12 0.012 0.1 0.05], ...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','xmin', ...
	'Tag','ssxmi');
%	'buttondownfcn','mymggreadx rcheck', ...
b1h=uicontrol('Parent',ufh,'units','normalized',...
    'position',[0.88 0.012 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','xmax', ...
	'Tag','ssxma');
b1h=uicontrol('Parent',ufh,'units','normalized',...
    'position',[0.01 0.14 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','ymin', ...
	'Tag','ssymi');
b1h=uicontrol('Parent',ufh,'units','normalized',...
    'position',[0.01 0.84 0.1 0.05],...
    'style','edit', ...
    'BackgroundColor',[1 1 1], ...
	'Callback','mymggreadx $', ...
	'ListboxTop',0, ...
	'String','ymax', ...
	'Tag','ssyma');
end
figure(tfh);
oxlim=get(AX(1),'xlim');
orlim=get(AX(1),'ylim');
otlim=get(AX(2),'ylim');
set(gxmi,'string',oxlim(1)); set(gxma,'string',oxlim(2));
set(grmi,'string',orlim(1)); set(grma,'string',orlim(2));
set(gtmi,'string',otlim(1)); set(gtma,'string',otlim(2));

%set(findobj(tfh,'tag','gxmi'),'string',oxlim(1));
%set(findobj(tfh,'tag','gxma'),'string',oxlim(2));
%set(findobj(tfh,'tag','grmi'),'string',oylim(1));
%set(findobj(tfh,'tag','grma'),'string',oylim(2));
if supf>0
figure(sfh);
oxlim=xlim; oylim=ylim;
set(findobj(sfh,'tag','ssxmi'),'string',oxlim(1));
set(findobj(sfh,'tag','ssxma'),'string',oxlim(2));
set(findobj(sfh,'tag','ssymi'),'string',oylim(1));
set(findobj(sfh,'tag','ssyma'),'string',oylim(2));
figure(ufh);
oxlim=xlim; oylim=ylim;
set(findobj(ufh,'tag','ssxmi'),'string',oxlim(1));
set(findobj(ufh,'tag','ssxma'),'string',oxlim(2));
set(findobj(ufh,'tag','ssymi'),'string',oylim(1));
set(findobj(ufh,'tag','ssyma'),'string',oylim(2));
end
end % 125 if 1<2
figure(tfh)
end % 40 if and(nargin==1,varargin{1}=='$')

function local_f(locf,cval)
global xyz skpo tipo fiti xyz0
global AX H1 H2
oxlim=get(AX(1),'xlim');
orlim=get(AX(1),'ylim');
oiiml=get(AX(2),'xlim');
otlim=get(AX(2),'ylim');
axis manual
switch(locf)
  case 'gxmi'
    set(AX(1),'xlim',[cval oxlim(2)]);
    set(AX(2),'xlim',[cval oxlim(2)]);

    case 'gxma'
    set(AX(1),'xlim',[oxlim(1) cval]);
    set(AX(2),'xlim',[oxlim(1) cval]);      

    case 'grmi'
    set(AX(1),'ylim',[cval orlim(2)]);

    case 'grma'
    set(AX(1),'ylim',[orlim(1) cval]);
    
    case 'gtmi'
    set(AX(2),'ylim',[cval otlim(2)]);

    case 'gtma'
    set(AX(2),'ylim',[otlim(1) cval]);
    
    case 'gcheck'
    xp=0; xp=0; konec=0;
    disp('index time[s]  R[Ohm]  T1[C]   T0[C]');
    while konec==0
      kanec=0;
      while kanec==0
        [cx,cy,bt]=ginput(1);
%        disp([num2str(cx),' ',num2str(cy),' ',int2str(bt)]);
        if bt==28|bt==29|bt==30|bt==31|bt==32 % arrow key or space pressed?
          pp=get(0,'PointerLocation'); xp=pp(1); yp=pp(2); % abs location
          switch bt
            case 28
              xp=xp-1;
            case 29
              xp=xp+1;
            case 30 
              yp=yp+1;
            case 31 
              yp=yp-1;
            case 32 
              xp=xp+0;
          end % switch bt
          set(0,'PointerLocation',[xp yp]); % set abs location
        end % 294 if
    % possition is confirmed by left or right mouse button, space or 'q' 
        if bt==1 | bt==3 | bt==32 | bt==113 kanec=1; end
    % the scan loop is ended by right mouse button or 'q'
        if bt==3 | bt==113 konec=1; end
      end % 291 while kanec==0 - inner loop - position cx cy confirmed
      xbla=find(xyz(:,1)-cx>0); % find the hour h
      if numel(xbla)~=0 in=xbla(1); else in=1; end
disp([num2str(in),' ',num2str(xyz0(in,1)),' ',num2str(xyz0(in,2)),...
    ' ',num2str(xyz0(in,3)),' ',num2str(xyz0(in,4))]);
%%disp(['cx=',num2str(round(cx)),' cy=',int2str(cy),' in=',num2str(in)]);
%absin=in+skpo-1; curts=xyz(in,1)+tipo;
%curdt=datenum(fiti+[0 0 0 0 0 curts]); % time arithmetics also in warf03
%disp([datestr(curdt),' ',num2str(xyz(in,2:end),'%0.4g '),int2str(in),...
%       ' ',int2str(absin)]);

    end % 298 while konec==0
end % 274 switch
% % Tc stuff
function outvar=local_u2t(invar)
global qtu0
roomo=1.61;
mvolts=invar*1000+roomo;
  if invar<=0 qtu=qtu0(:,1); else qtu=qtu0(:,2); end 
  outvar=polyval(flipud(qtu),mvolts);

