dataDir = '/data/projects/CHILL/';
addpath('/home/katherine/CRONJOBS/');

%==========================================
% Break CONUS grid into regional subsets. The more powerful the machine, the
% larger the regional subsets can be.
LAT_START = [1 301];
LAT_END = [300 585];
LON_START = [1 301 601 901 1101];
LON_END = [300 600 900 1100 1386];
N_LAT = 585;
N_LON = 1386;
N_DAY = 366;

YEARS = 1979:2026;

YR_START=1979;
YR_END=YEAR; 
N_YRS = YR_END-YR_START+1;
YR_INDEX = 1:N_YRS;
years =YR_START: YR_END;
n_yr = length(YEARS);

%==========================================
%  GET GRID
%==========================================
%new grid
m=matfile(['alt_gridmetlonlat.mat']);
lat = double(m.lat);
lon =double(m.lon);
%==========================================
%  SET UP NEW .MAT FILE
%==========================================
	yr_index = [1:N_YRS];
	n_lat = length(lat);
	n_lon = length(lon);
	n_yr = length(yr_index);

	mwrite = matfile('/data/projects/CHILL/historicalchill_alldays_allyears.mat','Writable',true);
yearmin = min(years);

for yearInFilename= 1980:2024;
	yearInFilename
	mread = matfile(['/data/projects/CHILL/historicalchill_alldays_',num2str(yearInFilename),'.mat'],'Writable',false);
	% 585        1386         212  this is data from Oct 1 I believe

	if(yearInFilename == 2024);
		months = 1:4;
	else;
		months = 1:7;
	end;

		if(mo<=3);
			year = yearInFilename - 1;
		else;
			year = yearInFilename ;
		end;
		yrindex2write = year  -yearmin+1; 
		yr = year-yearmin+1;

		if(isleapyear(year));
			daysInMonths  =[31 30 31 31 29 31 30]; % 31 30 31 31 30];
		else;
			daysInMonths  =[31 30 31 31 28 31 30]; % 31 30 31 31 30];
		end;

		%get nan mask
		t= mread.chillhours_all(:,:,1)';
		f_nan=find(isnan(t));

		temp = double(mread.chillhours_all(:,:,dayRange));
		temp = temp';
		temp(f_nan) = NaN;
		mwrite.chillhours(:,:,yr,mo) = single(temp);

		temp = mread.chillportion_all(:,:,dayRange);
		temp = nansum(double(mread.utahchill_all(:,:,dayRange)),3);


		%make the daily netcdf file for the year
			
	

end; %yr

%==========================================
%  CREATE NETCDF SUMMARY FILE
REF_YEAR=1900;
YR_REF=1900;
%===============================
%   MAKE NETCDF
%=============================
MONTHS ={'Oct';'Nov';'Dec';'Jan';'Feb';'Mar';'Apr';};

units='';
for mo=1:7;
	clear time;
	if(mo <=3);
		monthAdjusted = mo+9;
	else;
		monthAdjusted = mo-3;

	end;
	for yr=1:length(years);
		year =years(yr);
		time(yr) = datenum(year,monthAdjusted,15)  -datenum(REF_YEAR,1,1);
	end;
	[Y,M,D]=datevec(time+datenum(YR_REF,1,1));


	varname = 'chillhours';
	filename_chill = ['metdata_',varname,'_monthlyTimeSeries_',char(MONTHS{mo}),'.nc'];
	chill =double(file_chill.(varname)(:,:,:,mo));
	chill = permute(chill,[3 1 2]); %change dimensions to time,lon,lat
	netcdfFilename = [filename_chill]; 
	  system(['rm ',netcdfFilename])
	make_monthlynetcdf(chill,time,lat,lon,varname,units,1,netcdfFilename,REF_YEAR);

	varname = 'chillportion';
	filename_chill = ['metdata_',varname,'_monthlyTimeSeries_',char(MONTHS{mo}),'.nc'];
	chill =double(file_chill.(varname)(:,:,:,mo));
	chill = permute(chill,[3 1 2]); %change dimensions to time,lon,lat
	netcdfFilename = [filename_chill]; 
	  system(['rm ',netcdfFilename])
	make_monthlynetcdf(chill,time,lat,lon,varname,units,2,netcdfFilename,REF_YEAR);

	varname = 'chillunits';
	filename_chill = ['metdata_',varname,'_monthlyTimeSeries_',char(MONTHS{mo}),'.nc'];
	chill =double(file_chill.('utahchill')(:,:,:,mo));
	chill = permute(chill,[3 1 2]); %change dimensions to time,lon,lat
	netcdfFilename = [filename_chill]; 
	  system(['rm ',netcdfFilename])
	make_monthlynetcdf(chill,time,lat,lon,varname,units,3,netcdfFilename,REF_YEAR);

end;
%==========================================
%  TRANSFER NETCDF SUMMARY FILES
%==========================================
%this part isn't working on santaana... due to not having this connection set up
if(0);
	NKN_PATH = 'khegewisch@climate-utils.nkn.uidaho.edu://reacch-data/MET/summaries'
	for mo=1:7;
		filename_chill = ['metdata_chillhours_monthlyTimeSeries_',char(MONTHS{mo}),'.nc'];
		outputfilename = filename_chill;
		scpstatus = system(['scp ',outputfilename,' ',NKN_PATH]);

		filename_chill = ['metdata_chillportion_monthlyTimeSeries_',char(MONTHS{mo}),'.nc'];
		outputfilename = filename_chill;
		scpstatus = system(['scp ',outputfilename,' ',NKN_PATH]);

		filename_chill = ['metdata_chillunits_monthlyTimeSeries_',char(MONTHS{mo}),'.nc'];
		outputfilename = filename_chill;
		scpstatus = system(['scp ',outputfilename,' ',NKN_PATH]);

	end;
end;
%==========================================
% RESET FOR NEXT YEAR
%==========================================
if(0);
	system('mv oldchill.mat bkupchill.mat');
	system('cp historicalchill_alldays_allyears.mat oldchill.mat')
end;
