%
% Plotting gaps in CNMCA-UGM data set
%

gap=zeros(57,3,4);
% 54 anni, 3 variabili, 4 stazioni

for nfiles=1:4

switch nfiles,
case 1,
fname='venezia_av.dat';
case 2,
fname='puntamarina_av.dat';
case 3,
fname='rimini_av.dat';
case 4,
fname='brindisi_av.dat';
end

fid=fopen(fname);
dati=fscanf(fid,'%d/%d/%d %d.%d.%d;%f;%f;%f', [9 inf]);
dati=dati';

null=find(dati==3.4028235E+38);
dati(null)=NaN;

aa=dati(:,3);       % anno
MM=dati(:,2);       % mese
dd=dati(:,1);       % giorno
hh=dati(:,4);       % ore
mm=dati(:,5);       % minuti
ss=dati(:,6);       % secondi
dir=dati(:,7);      % Wind direction °N
wind=dati(:,8);       % wind kn
mslp=dati(:,9);       % mslp mB
jd=julian([aa MM dd hh mm ss]);
fclose(fid);

ind=find(wind>10000); wind(ind)=NaN;
ind=find(dir >360  | dir==0); dir(ind)=NaN;
ind=find(mslp>2000 | mslp==0); mslp(ind)=NaN;

% this is to compute total number of gaps
% start=julian([1951  1  1  0 0 0])-2440000;
start=julian([1951  1  1  0 0 0]);
% stop =julian([2008 12 31 24 0 0])-2440000;
stop =julian([2008 12 31 24 0 0]);
aaa=find(jd>=start & jd<= stop);
ind_1a(nfiles)=length(aaa);
bbb=find(isnan(wind(aaa)));
ind_2a(nfiles)=length(bbb);
ind_3a(nfiles)=ind_1a(nfiles)-ind_2a(nfiles);
% end


for i=1951:2008
start=julian([i  1  1  0 0 0]);
stop =julian([i 12 31 24 0 0]);
ind =find(jd>=start & jd<= stop);
bad1=find(isnan(wind(ind)));
bad2=find(isnan(dir(ind)));
bad3=find(isnan(mslp(ind)));
gap(i-1950,1,nfiles)=100*(length(bad1)/length(ind));
gap(i-1950,2,nfiles)=100*(length(bad2)/length(ind));
gap(i-1950,3,nfiles)=100*(length(bad3)/length(ind));
end

end     % end of nfiles

% now plotting

subplot(4,1,1)
bar([1951:1:2008],gap(:,1,1)); axis([1950 2010 0 100]);
set(gca,'xtick',1950:5:2010,'ytick',0:25.:100.); grid on;
ylabel('Venezia (%)','fontsize',12);
subplot(4,1,2)
bar([1951:1:2008],gap(:,1,2)); axis([1950 2010 0 100]);
set(gca,'xtick',1950:5:2010,'ytick',0:25.:100.); grid on;
ylabel('Ravenna (%)','fontsize',12);
subplot(4,1,3)
bar([1951:1:2008],gap(:,1,3)); axis([1950 2010 0 100]);
set(gca,'xtick',1950:5:2010,'ytick',0:25.:100.); grid on;
ylabel('Rimini (%)','fontsize',12);
subplot(4,1,4)
bar([1951:1:2008],gap(:,1,4)); axis([1950 2010 0 100]);
set(gca,'xtick',1950:5:2010,'ytick',0:25.:100.); grid on;
ylabel('Brindisi (%)','fontsize',12);



% printing plot

plotname='gaps.ps';
print('-painters','-dpsc','-cmyk','-r300',plotname);

% end
