

function data = gemreader0;

%
% function data = gemreader0;
%
% read in BAGO pwv data from *.gem files
%

!grep BAGO *0.gem > bago.gem
[s,w] = unix('wc -l bago.gem');
ind=findstr(w,'bago')-1;
eval(['npts = ' w(1:ind) ';'])

data.year = zeros(npts,1);
data.month = zeros(npts,1);
data.day = zeros(npts,1);
data.hour = zeros(npts,1);
data.minute = zeros(npts,1);
data.pwv = zeros(npts,1);
data.err = zeros(npts,1);
data.zwd = zeros(npts,1);
data.palt = zeros(npts,1);
data.tmpc = zeros(npts,1);
data.relh = zeros(npts,1);
data.nss1970 = zeros(npts,1);
data.jd = zeros(npts,1);
x=[];
y=[];

%              STN   YYMMDD/HHMM  PWV[mm]  ERR[mm]  ZWD[mm]  PALT    TMPC   RELH 
%plt_2180.gem: BAGO  980806/0030    53.5      2.3   325.8  1012.4    29.3    68.0
% 12345678901234567890123456789012345678901234567890123456789012345678901234567890
%          1         2         3         4         5         6         7         8

fid = fopen('bago.gem','r');

eof = feof(fid);
i=0;
while eof == 0
  eof = feof(fid);
  if eof == 0
    nextline = fgetl(fid);
    i = i + 1;
    eval(['data.year(' num2str(i) ')=' nextline(21:22) '+1900;'])
    eval(['data.month(' num2str(i) ')=' nextline(23:24) ';'])
    eval(['data.day(' num2str(i) ')=' nextline(25:26) ';'])
    eval(['data.hour(' num2str(i) ')=' nextline(28:29) ';'])
    eval(['data.minute(' num2str(i) ')=' nextline(30:31) ';'])
    eval(['data.pwv(' num2str(i) ')=' nextline(34:39) ';'])
    eval(['data.err(' num2str(i) ')=' nextline(44:48) ';'])
    eval(['data.zwd(' num2str(i) ')=' nextline(51:56) ';'])
    eval(['data.palt(' num2str(i) ')=' nextline(58:64) ';'])
    eval(['data.tmpc(' num2str(i) ')=' nextline(68:72) ';'])
    eval(['data.relh(' num2str(i) ')=' nextline(75:80) ';'])

    data.nss1970(i)=nosecsince1970(data.year(i),data.month(i),data.day(i),...
                   data.hour(i),data.minute(i),0);
    data.jd(i)=julday(data.year(i),data.month(i),data.day(i));
	       
    x=[x;data.nss1970(i);        data.nss1970(i);        data.nss1970(i)];
    y=[y;data.pwv(i)+data.err(i);data.pwv(i)-data.err(i);NaN];
    end
end

fclose(fid)

plot(dn(x),y,dn(data.nss1970),data.pwv,'d-');
blowup
dates

data.x=x;
data.y=y;
