% Read in the subset of SSTs, and write them in ASCII to a file
fname = 'EPIC_oi_ascii.dat'

% read from netcdf, data has cubic interp-filled coastlines
nc = netcdf('sst_fill.nc');
time = nc{'time'}(:);
lon = nc{'lon'}(:);
lat = nc{'lat'}(:);
sst = nc{'sst'}(:);
%sst(sst==nc{'sst'}.missing_value(:)) = NaN;
am = nc{'available_mask'}(1,:,:); % FYI the same every day

da = 1;
mo = 9;
year = 2001;
base_time = datenum('31-aug-2001');

dummy_int = 0;

fid = fopen(fname,'w');

%write the header
fprintf(fid,'% 6.2i',da);
fprintf(fid,'% 6.2i',mo);
fprintf(fid,'% 6.4i',year);
fprintf(fid,'\n');

fprintf(fid,'% 6i',length(time)); % number of days
fprintf(fid,'% 6i',dummy_int);
fprintf(fid,'% 6i',dummy_int);
fprintf(fid,'\n');

fprintf(fid,'% 8.3f',lat(1));
fprintf(fid,'% 8.3f',lon(1));
fprintf(fid,'% 8.3f',lat(2)-lat(1)); % dy  > OK if the same, but
fprintf(fid,'% 8.3f',lon(2)-lon(1)); % dx  > maybe reversed!
fprintf(fid,'\n');

% write the sst in the order (lon,lat,day)
for i = 1:length(time);
  fprintf(fid,'% 8.05g\n',sst(i,:,:));
end

fid = fclose(fid);
nc = close(nc);