Home > rest_20090422 > rest_spm5_files > nic_spm_read_netcdf.m

nic_spm_read_netcdf

PURPOSE ^

Read the header information from a NetCDF file into a data structure.

SYNOPSIS ^

function cdf = nic_spm_read_netcdf(fname)

DESCRIPTION ^

 Read the header information from a NetCDF file into a data structure.
 FORMAT cdf = spm_read_netcdf(fname)
 fname - name of NetCDF file
 cdf   - data structure

 See: http://www.unidata.ucar.edu/packages/netcdf/
 _______________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

nic_spm_read_netcdf.m

SOURCE CODE ^

0001 function cdf = nic_spm_read_netcdf(fname)
0002 % Read the header information from a NetCDF file into a data structure.
0003 % FORMAT cdf = spm_read_netcdf(fname)
0004 % fname - name of NetCDF file
0005 % cdf   - data structure
0006 %
0007 % See: http://www.unidata.ucar.edu/packages/netcdf/
0008 % _______________________________________________________________________
0009 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscienic_e
0010 
0011 % John Ashburner
0012 % $Id: spm_read_netcdf.m 112 2005-05-04 18:20:52Z john $
0013 
0014 
0015 dsiz     = [1 1 2 4 4 8];
0016 fp=fopen(fname,'r','ieee-be');
0017 if fp==-1,
0018     cdf = [];
0019     return;
0020 end;
0021 
0022 % Return null if not a CDF file.
0023 %-----------------------------------------------------------------------
0024 mgc = fread(fp,4,'uchar')';
0025 if ~all(['CDF' 1] == mgc),
0026     cdf = [];
0027     fclose(fp);
0028     return;
0029 end
0030 
0031 % I've no idea what this is for
0032 numrecs = fread(fp,1,'uint32');
0033 
0034 cdf = struct('numrecs',numrecs,'dim_array',[], 'gatt_array',[], 'var_array', []);
0035 
0036 dt = fread(fp,1,'uint32');
0037 if dt == 10,
0038     % Dimensions
0039     nelem = fread(fp,1,'uint32');
0040     for j=1:nelem,
0041         str   = readname(fp);
0042         dim_length = fread(fp,1,'uint32');
0043         cdf.dim_array(j).name       = str;
0044         cdf.dim_array(j).dim_length = dim_length;
0045     end;
0046     dt   = fread(fp,1,'uint32');
0047 end
0048 
0049 while ~dt, dt   = fread(fp,1,'uint32'); end;
0050 
0051 if dt == 12,
0052     % Attributes
0053     nelem = fread(fp,1,'uint32');
0054     for j=1:nelem,
0055         str    = readname(fp);
0056         nic_type= fread(fp,1,'uint32');
0057         nnelem = fread(fp,1,'uint32');
0058         val    = fread(fp,nnelem,dtypestr(nic_type));
0059         if nic_type == 2, val = deblank([val' ' ']); end
0060         padding= fread(fp,ceil(nnelem*dsiz(nic_type)/4)*4-nnelem*dsiz(nic_type),'uchar');
0061         cdf.gatt_array(j).name    = str;
0062         cdf.gatt_array(j).nic_type = nic_type;
0063         cdf.gatt_array(j).val     = val;
0064     end;
0065     dt   = fread(fp,1,'uint32');
0066 end
0067 
0068 while ~dt, dt   = fread(fp,1,'uint32'); end;
0069 
0070 if dt == 11,
0071     % Variables
0072     nelem = fread(fp,1,'uint32');
0073     for j=1:nelem,
0074         str    = readname(fp);
0075         nnelem = fread(fp,1,'uint32');
0076         val    = fread(fp,nnelem,'uint32');
0077         cdf.var_array(j).name    = str;
0078         cdf.var_array(j).dimid   = val+1;
0079         cdf.var_array(j).nic_type = 0;
0080         cdf.var_array(j).vsize   = 0;
0081         cdf.var_array(j).begin   = 0;
0082         dt0    = fread(fp,1,'uint32');
0083         if dt0 == 12,
0084             nelem0 = fread(fp,1,'uint32');
0085             for jj=1:nelem0,
0086                 str    = readname(fp);
0087                 nic_type= fread(fp,1,'uint32');
0088                 nnelem = fread(fp,1,'uint32');
0089                 val    = fread(fp,nnelem,dtypestr(nic_type));
0090                 if nic_type == 2, val = deblank([val' ' ']); end
0091                 padding= fread(fp,...
0092                     ceil(nnelem*dsiz(nic_type)/4)*4-nnelem*dsiz(nic_type),'uchar');
0093                 cdf.var_array(j).vatt_array(jj).name    = str;
0094                 cdf.var_array(j).vatt_array(jj).nic_type = nic_type;
0095                 cdf.var_array(j).vatt_array(jj).val     = val;
0096             end;
0097             dt0    = fread(fp,1,'uint32');
0098         end;
0099         cdf.var_array(j).nic_type  = dt0;
0100         cdf.var_array(j).vsize = fread(fp,1,'uint32');
0101         cdf.var_array(j).begin = fread(fp,1,'uint32');
0102     end;
0103     dt   = fread(fp,1,'uint32');
0104 end;
0105 
0106 fclose(fp);
0107 return;
0108 %_______________________________________________________________________
0109 
0110 %_______________________________________________________________________
0111 function str = dtypestr(i)
0112 % Returns a string appropriate for reading or writing the CDF data-type.
0113 types = char('uint8','uint8','int16','int32','float32','float64');
0114 str   = deblank(types(i,:));
0115 return;
0116 %_______________________________________________________________________
0117 
0118 %_______________________________________________________________________
0119 function name = readname(fp)
0120 % Extracts a name from a CDF file pointed to at the right location by
0121 % fp.
0122 stlen  = fread(fp,1,'uint32');
0123 name   = deblank([fread(fp,stlen,'uchar')' ' ']);
0124 padding= fread(fp,ceil(stlen/4)*4-stlen,'uchar');
0125 return;
0126 %_______________________________________________________________________

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005