function Monthly_pps(year, month, channel, outfilename, binfilepath)
%
% function Monthly_pps(year, month, channel, outfilename, binfilepath)
% Calculates and prints a file of maser offsets for one month. Currently
% the binfilepath is '/home/ftp/pub/hmaser/bin/'.
% by kaj.wiik@hut.fi (5 Mar 1996)
% 21 Jun 1996 Changed to allow calculation of non-full months (kwi)
% 2 Sep 1996 Default outfilename /tmp/GPS.MH, default binfilepath 
% /u2/home/ftp/pub/hmaser/bin/.
% 21 Oct 1996 Added the 'channel' argument. Currently the QDATA channels
% are: 1 -- Geod. GPS (GPSMH3)
%      2 -- Control room PPS (rubbish) (GPSMH0)
%      3 -- Formsync (now HP GPS) (GPSMH2)
%      4 -- TV sync (GPSMH0)
%      5 -- Magnavox GPS (GPSMH1)
% Default is channel 5 (GPSMH1).
% The resulting files can be joined using 'cat GPS.MH* | sort +0 -n > GPS.MH'.
% 5 Nov 1996 Added channel argument also to help, improved the output format.
% 27 Nov 1996 If invoked without argument, function calculates now the offset
%             file for the current month (due to mixup in VT005..).
% 02 Nov 1996 Accepts also 'last' and 'current' as arguments (meaning last and
%             current months)
% 02 Jan 1997 New default binfilepath
% 12 Mar 1997 New default binfilepath /home/ftp/pub/hmaser/bin

gpsno = [3 0 2 0 1];

[dum, date]=unix('date +%m%Y');

if nargin < 5
	binfilepath = '/home/ftp/pub/hmaser/bin/'
	if nargin < 4
		outfilename = '/tmp/GPS.MH'
		if nargin < 3
			if nargin == 0
				month = str2num(date(1:2));
				year = str2num(date(3:6));
			elseif nargin == 1
 				if strcmp(year,'last') % Year is the first arg..
					month = str2num(date(1:2)) - 1;
                                	year = str2num(date(3:6));
 				elseif strcmp(year,'current') % Year is the first arg..
                                        month = str2num(date(1:2));
                                        year = str2num(date(3:6));
				else
					exit('Bad argument:');
				end
			end
			channel = 5;
		end
	end
end

std_limit = 0.2; % Max 200 ns standard deviation (values greater will be cut)
delay_limit = 0.5*1e6; % Max 0.5 s delay offset (values greater will be cut)

fd = fopen(outfilename,'w');
fprintf(1,'Start date (M/Y): %d/%d\n', month, year);
start_mjd = mjd(year, month, 1);
stop_mjd = mjd(year, month+1, 1);

start_mjd_file = 10*fix(start_mjd/10);

data = [];

for i = 0:10:30
	mjdstring = sprintf('%5d', start_mjd_file + i);
	binfilename = [binfilepath 'q' mjdstring '.bin'];
	if exist(binfilename)
		data = [data; load_bin_floats(binfilename, channel, 1+5, 1, 'float')];
	end
end

time = data(:,1);
pps = data(:,2);
clear data

last_time = max(time);
if last_time < stop_mjd
	stop_mjd = last_time;
end

current_mjd = start_mjd;

while current_mjd < stop_mjd
	next_mjd = current_mjd + 1;
	index = find((time >= current_mjd) & (time < next_mjd)...
                     &(pps > -1*delay_limit) & (pps < delay_limit));

	if length(index) > 10
		[epoch, std, delay, rate] = fit_one_day(time(index), pps(index));
			if std <= std_limit
			print_huib_format(fd, epoch, std, delay, rate, gpsno(channel));
			end
	end
	current_mjd = next_mjd;	
end

fclose(fd);
