0001 function ipcc2grid(fin,fout,m)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 if ~exist('m','var')
0013 m='s';
0014 end
0015 nodata = -9999;
0016
0017
0018 fid = fopen(fin);
0019 if fid==-1
0020 error('File not found or permission denied');
0021 end
0022
0023
0024 display(['Reading in data from file ',fin])
0025 h1= fgetl(fid);
0026 h2= fgetl(fid);
0027 data = [];
0028 data = [data; fscanf(fid, '%f')];
0029 fclose(fid);
0030
0031
0032 header={};
0033 while ~isempty(h1)
0034 [s,h1]=strtok(h1);
0035 [t,h2]=strtok(h2);
0036 eval(['header.',s,'=str2num(t);'])
0037 end
0038
0039
0040 data(data==nodata)=NaN;
0041 data=reshape(data,header.n_cols,header.n_rows,header.n_months);
0042
0043
0044 t=nan(header.n_rows,header.n_cols,header.n_months);
0045 for d = 1:size(data,3)
0046 t(:,:,d)=fliplr(rot90(data(:,:,d),3));
0047 end
0048 data=t;
0049 clear t;
0050
0051
0052 display(['Exporting ascii grid to file ',fout])
0053 xrange=header.xmin-(header.grd_sz/2):header.grd_sz:header.xmax-(header.grd_sz/2);
0054 yrange=header.ymin-(header.grd_sz/2):header.grd_sz:header.ymax-(header.grd_sz/2);
0055
0056 if ischar(m)
0057 if m=='m'
0058 mat2grid(fout,xrange,yrange,mean(data,3))
0059 elseif m=='s'
0060 mat2grid(fout,xrange,yrange,sum(data,3))
0061 else
0062 display('Timeslice option not supported. Give timeslice 1..12 for month to convert, m for mean across all month or s for sum.')
0063 end
0064 else
0065 mat2grid(fout,xrange,yrange,data(:,:,m));
0066 end