%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%	Create daily data file raedable by grads
%		z=stations and t=time
%	Put a line on the exact onset date
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
close all;
load /beluga/data/ousmane/data/kergi/station/daily/onset/onsetfreqStart_.txt
%mn0=onsetfreqStart_(1:59,1); 
%ms0=onsetfreqStart_(1:59,2);
mw0=onsetfreqStart_(1:59,3);
%fn0=onsetfreqStart_(1:59,4);
%fs0=onsetfreqStart_(1:59,5);
fw0=onsetfreqStart_(1:59,6);
%fn1=onsetfreqStart_(1:59,7);
%fs1=onsetfreqStart_(1:59,8);
fw1=onsetfreqStart_(1:59,9);

iwhole=[1:20];
load xamjjaso20stat5008;
xamjjaso(20,1:18,:)=-999.;

f1 = fopen('test.bin','wb');
for iy=1950:2008
tt=sprintf('datenum(''31.03.%4d'',''dd.mm.yyyy'')-datenum(''1.1.%4d'',''dd.mm.yyyy'')+1',iy,iy);
n1=eval(tt);
hh=-999.;
    	for i=1:n1  % 01Jan to 31Mar
		for iz=1:20
	a=fwrite(f1,hh,'float32');
		end
		for iz=1:20
        a=fwrite(f1,hh,'float32');
		end
                for iz=1:20
        a=fwrite(f1,hh,'float32');
                end
                for iz=1:20
        a=fwrite(f1,hh,'float32');
                end
	end

        for i=1:214  % 01Apr to 31Oct
		for iz=1:20
	a=fwrite(f1,squeeze(xamjjaso(iz,iy-1949,i)),'float32');
		end
	if(i==mw0(iy-1949,1)+30)
		for iz=1:20
		a=fwrite(f1,i,'float32');
		end
	else
		for iz=1:20
		a=fwrite(f1,hh,'float32');
		end
	end
        if(i==fw0(iy-1949,1)+30)
                for iz=1:20
                a=fwrite(f1,i,'float32');
                end
        else
                for iz=1:20
                a=fwrite(f1,hh,'float32');
                end
        end
        if(i==fw1(iy-1949,1)+30)
                for iz=1:20
                a=fwrite(f1,i,'float32');
                end
        else
                for iz=1:20
                a=fwrite(f1,hh,'float32');
                end
        end

        end

        for i=1:61  % 01Jan to 31Mar
		for iz=1:20
		a=fwrite(f1,hh,'float32');
		end
		for iz=1:20
		a=fwrite(f1,hh,'float32');
		end
                for iz=1:20
                a=fwrite(f1,hh,'float32');
                end
                for iz=1:20
                a=fwrite(f1,hh,'float32');
                end

        end
end

fclose(f1);
%====
%dset /beluga/data/ousmane/data/kergi/station/daily/data/test.bin
%undef -999
%title rainfall, onset(Min0, Freq0 and Freq1) from 01Jan50 to 31Dec08
%xdef 1 linear -15 2.5
%ydef 1 linear 15 2.5
%zdef 20 linear 1 1
%tdef 21550 linear 01Jan1950 1dy
%vars 4
%dly 20 99 Daily rainfall
%mn0 20 99 First onset at any station
%fr0 20 99 Large scale onset
%fr1 20 99 Large scale onset w/t dry spells
%endvars
%====
