Home > custom > io > ipcc2grid.m

ipcc2grid

PURPOSE ^

PURPOSE: Convert a IPCC dat climate file to ESRI ascii grid format

SYNOPSIS ^

function ipcc2grid(fin,fout,m)

DESCRIPTION ^

 PURPOSE: Convert a IPCC dat climate file to ESRI ascii grid format
 -------------------------------------------------------------------
   fin,fout are IPCC input .dat file and ascii grid output file, resp.
   m = month (timeslice) to convert (month 1..12) - give m = s for 
       summing across all months or m = m for mean, default is s!

 Example: ipcc2grid('ctmp8190.dat','ctmp8190.asc','m');

 Felix Hebeler, Geography Dept., University Zurich, May 2007.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ipcc2grid(fin,fout,m)
0002 % PURPOSE: Convert a IPCC dat climate file to ESRI ascii grid format
0003 % -------------------------------------------------------------------
0004 %   fin,fout are IPCC input .dat file and ascii grid output file, resp.
0005 %   m = month (timeslice) to convert (month 1..12) - give m = s for
0006 %       summing across all months or m = m for mean, default is s!
0007 %
0008 % Example: ipcc2grid('ctmp8190.dat','ctmp8190.asc','m');
0009 %
0010 % Felix Hebeler, Geography Dept., University Zurich, May 2007.
0011 
0012 if ~exist('m','var')
0013     m='s';
0014 end
0015 nodata = -9999;
0016 
0017 %% open file
0018 fid = fopen(fin);
0019 if fid==-1
0020   error('File not found or permission denied');
0021 end
0022 
0023 %% read header info and data
0024 display(['Reading in data from file ',fin])
0025 h1= fgetl(fid); % this should be text as in
0026 h2= fgetl(fid); % this is the info
0027 data = [];
0028 data = [data; fscanf(fid, '%f')];
0029 fclose(fid);
0030 
0031 %% make a nice header
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 %% replace NaN and reshape data
0040 data(data==nodata)=NaN;
0041 data=reshape(data,header.n_cols,header.n_rows,header.n_months);
0042 
0043 %% rotate (only works for 2D matrices)
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 %% export to ascii grid using mat2grid
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); % shift half a cell left
0054 yrange=header.ymin-(header.grd_sz/2):header.grd_sz:header.ymax-(header.grd_sz/2); % shift half a cell down;
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

Generated on Tue 24-Feb-2009 19:14:50 by m2html © 2003