function [gcmdataFinetemp] = interp_curvilinear_to_rectangular(FINE_XX,FINE_YY,COARSE_XX,COARSE_YY,gcmdataCoarse,mask,DAYS);

%troubleshoot
%gcmdataCoarse = gcmdataCoarse(:,:,:,y);
	FINE_XX=FINE_XX';
	FINE_YY=FINE_YY';
	
	[XX,YY]=meshgrid(FINE_XX',FINE_YY');

	gcmdataFinetemp = NaN(length(FINE_YY),length(FINE_XX),DAYS);
	for k=1:DAYS;
		temp = double(gcmdataCoarse(:,:,k));   %needs to be double
%	tic
%		for j=1:length(FINE_XX);
%		       for i=1:length(FINE_YY);
%				 if(mask(i,j));
%				      gcmdataFinetemp(i,j,k) = griddata(COARSE_XX, COARSE_YY,temp, FINE_XX(j), FINE_YY(i));
%				 end
%		       end %i
%		end %j
%	toc

        	gcmdataFinetemp(:,:,k) = griddata(COARSE_XX, COARSE_YY,temp, XX,YY);
	end %k


	for j=1:length(FINE_XX);
		for i = 1:length(FINE_YY);
			if(mask(i,j)==0);
				gcmdataFinetemp(i,j,:) = NaN;
			end
		end
	end

	%gcmdataFinetemp = single(gcmdataFinetemp);


%test this is working right
%figure(1)
%temp = gcmdataCoarse(:,:,1);
%makemap(COARSE_XX-360,COARSE_YY,temp,[min(temp(:)) max(temp(:))]);
%colorbar;m_grid;
%title('NARCCAP curvilinear grid data for tasmax, Year 1');
%figure(2)
%temp2 = gcmdataFinetemp(:,:,1);
% [X Y]=meshgrid(FINE_XX-360,FINE_YY);
%makemap(X,Y,temp2,[min(temp(:)) max(temp(:))]);
%colorbar;m_grid;
%title('Interpolation to rectangular grid for tasmax, Year 1');


end
