function y=sigfilt(cmet,tc,fl,r,nn,c,dtt,mstd);
%# flag=sigfilt(var,time,flag,r);
%# var=variable data
%# time=time variable
%# flag=flag array of data (checks if data out of bounds).
%# r= denotes pass number through program.
%# nn= multiplier time standard deviation
%# c=biweight parameter.
%# dtt=1/2 time window 
%# mstd=minimum standard deviation to that will be used if biweight falls below this.
%# Program applies a standard deviation filter to a time limited data array
%#lz=size(fl,1);
%#ll=zeros(lz,1);
%#######################################
%# Set parameters which may be changed
   dtt=dtt/24;	% time interval to compute standard deviation over
%#   nn=3;	% multiplier to standard deviation for rejection
%#   c=9;		% number for biweighting of data.
%#
%#######################################
%# First find all data not marked as bad or nonexistant
%# On a first pass we only use marked bad or nonexistant for
%# a second pass we exclude those already marked as bad.
if r==1,
	ll=((fl == 1)|(fl==3));
else,
	ll=(fl > 0);
end
%# Now set up two arrays.  1 of data flaged bad or nonexistant and 
%# the other of data that is to be screened.
%# idn= data to be screened
idn=find(ll==0);
%# ido=data to be removed from filter
ido=find(ll==1);
%# mmet is data not to be filtered because it is either bad or
%# blank.
mmet=cmet(ll);
%# cmet is now changed to be only the data we want to filter
%# set up similar arrays for the other data.
omet=cmet(~ll);
tc=tc(~ll);
fln=fl(ll);
fly=fl(~ll);
%#sf=size(fly,1);
%#fly=zeros(sf,1);
%# Now we do not care about new data.  We want to run the filter
%# on all the data recieved.

%# So now we should only worry about points which
%# have passed previous attempts at flag checking.
%# find out size of good data in array.
maxm=length(omet);
%# If no good data (omet=[]) then, no filter necessary.
%# and all values are already flagged as bad or missing.
if maxm==0,
	y=fln;
	return;
end
%# What do we do if we have only 1 good value.
%# There are two schools of thought:
%# 1) All the other values are bad so it should be also.
%# 2) It is good so it may be the only good value.
%# We will go with the first option.
if maxm==1,
	y(ido(:))=fln;
	y(idn(:))=2; % value set bad by this program
	return;
end
%# mmet=bad data don't filter or include in filter
%# omet=data to filter.
%# cmet=all data.
%# Find the maximum time in the file
maxt=max(tc);
mint=min(tc);
%# We compute standard deviations until we get within the bounds 
%# of the end of the file.
tmax=maxt-dtt;
tmin=mint+dtt;
%# So we can compute run our filter from tmin to tmax.
%# Now we get an index of data we can preform the filter on in the array.
[index]=find((tc >= tmin)&(tc <= tmax));
li=length(index);
%# Let us use the method outlined in by Lanzante in Int. Journal of Clim.
%# Vol 16 pp1197-1266 (1996)
%# So we loop through the data 
for i=1:li,
%# define our first data interval
	tmin=tc(index(i))-dtt;
	tmax=tc(index(i))+dtt;
	zo=((tc >= tmin)&(tc <= tmax));
	zmet=omet(zo);
%# we now have our truncated array.  Compute the median of the values (scalar)
	mm=median(zmet);
	lz=length(zmet);
%# Now compute the median absolute deviation. (vector)
	mad=median(abs(zmet-mm));
%# Now compute the weights for the data.
	wu=zmet-mm;
	if mad~=0,
		wu=wu./(c*mad);
%# Set weight greater than or equal to one to zero.
		lu=abs(wu) >= 1;
		wu(lu)=0;
%# Now compute the biweight mean
		d1=(zmet-mm);
		d2=wu.^2;
		d2=1-d2;
		d2=d2.^2;
		b1=d1.*d2;
		b2=d2;
		b3=sum(b1);
		b4=sum(b2);
		bim=mm+b3/b4;
%# Now to compute the biweight standard deviation
		d3=d1.^2;
		d4=d2.^2;
		s1=d3.*d4;
		d5=wu.^2;
		d6=1-d5;
		d7=5.*d5;
		d7=1-d7;
		s2=d6.*d7;
		s3=sqrt(lz*sum(s1));
		s4=abs(sum(s2));
		bis=s3/s4;
%# Now that we have the biweight standard deviation and mean
%# Time to eliminate points.
%# compute bounds for elimination of data.
%# Add line to find if bis less than a minimum value fed in by the
%# user.
		if((bis < mstd)&(mstd~=0)),
			bis=mstd;
		end
		blow=bim-nn*bis;
		bhig=bim+nn*bis;
		if(omet(index(i))< blow)|(omet(index(i)) > bhig),
			fly(index(i))=2;
		else,
			fly(index(i))=0;
		end
	else,
		fly(index(i))=0;
	end
end
%# Again we are now passing the flag as the result and not
%# changing the data values at all.
y(idn(:))=fly(:);
y(ido(:))=fln(:);
return;

