Home > custom > io > ipcc2mat.m

ipcc2mat

PURPOSE ^

PURPOSE: Import IPCC climate observation data into Matlab (3D) matrix

SYNOPSIS ^

function [data,header] = ipcc2mat(fname,r)

DESCRIPTION ^

 PURPOSE: Import IPCC climate observation data into Matlab (3D) matrix
 -------------------------------------------------------------------
 USAGE: [data,header] = ipcc2mat(fname,r)
 where: fname = filename of ipcc climate observation file in ascii .dat
                format as provided at http://www.ipcc-data.org
        r = optional flag to rotate& flip data matrix for pretty printing in
            mat format (1) or not (0). Default is 1 (rotate), 
 Output:
        data = 3D matrix in format (rows/cols/month) usually (360,720,12)

 Note: 

 Example: [data,header]=ipcc2mat('ctmp8190.dat',1);
          figure, imagesc(data)

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [data,header] = ipcc2mat(fname,r)
0002 % PURPOSE: Import IPCC climate observation data into Matlab (3D) matrix
0003 % -------------------------------------------------------------------
0004 % USAGE: [data,header] = ipcc2mat(fname,r)
0005 % where: fname = filename of ipcc climate observation file in ascii .dat
0006 %                format as provided at http://www.ipcc-data.org
0007 %        r = optional flag to rotate& flip data matrix for pretty printing in
0008 %            mat format (1) or not (0). Default is 1 (rotate),
0009 % Output:
0010 %        data = 3D matrix in format (rows/cols/month) usually (360,720,12)
0011 %
0012 % Note:
0013 %
0014 % Example: [data,header]=ipcc2mat('ctmp8190.dat',1);
0015 %          figure, imagesc(data)
0016 %
0017 % Felix Hebeler, Geography Dept., University Zurich, May 2007.
0018 
0019 if ~exist('r','var')
0020     r=1;
0021 end
0022 nodata = -9999;
0023 
0024 %% open file
0025 fid = fopen(fname);
0026 if fid==-1
0027   error('File not found or permission denied');
0028 end
0029 
0030 %% read header ifno and data
0031 h1= fgetl(fid); % this should be the header text
0032 h2= fgetl(fid); % this is the header info
0033 data = [];
0034 data = [data; fscanf(fid, '%f')];
0035 fclose(fid);
0036 
0037 %% make a nice header
0038 header={};
0039 while ~isempty(h1)
0040     [s,h1]=strtok(h1);
0041     [t,h2]=strtok(h2);
0042     eval(['header.',s,'=str2num(t);'])
0043 end
0044 
0045 %% replace NaN and reshape data
0046 data(data==nodata)=NaN;
0047 data=reshape(data,header.n_cols,header.n_rows,header.n_months);
0048 
0049 %% rotate (only works for 2D matrices)
0050 if r==1
0051     t=nan(header.n_rows,header.n_cols,header.n_months);
0052     for d = 1:size(data,3)
0053         t(:,:,d)=fliplr(rot90(data(:,:,d),3));
0054     end
0055 end
0056 data=t;
0057 clear t;

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