


PURPOSE: export timeslice of CF-1 conform (climate) data grid to ArcGIS
ASCII format.
-------------------------------------------------------------------
USAGE: nc2ascii(ncin, fout, time, varids)
where: ncin is the path of the netcdf input file(string) or ncID (int),
fout is the output ascii filename (string),
time is the timeslice to read in (int, opional, default 1)
and varids is a vector of variable IDs in the from [varID, latID,lonID]
giving the variable IDs of the variable to export
along with the IDs of aviables storing the latitude and longitude
information. Default is [end,2,4] (end=last var in file).
Note: Quick'n'dirty. Adapt to your need (e.g. using varnames).
Uses: mat2asciigrid(), nc_readvar()
MEXCDF: http://mexcdf.sourceforge.net
and obviously netcdf: http://www.unidata.ucar.edu/s(oftware/netcdf/
Felix Hebeler, Geography Dept., University Zurich, December 2006.
some checking first

0001 function nc2ascii(ncin, fout, time, varids) 0002 % PURPOSE: export timeslice of CF-1 conform (climate) data grid to ArcGIS 0003 % ASCII format. 0004 % ------------------------------------------------------------------- 0005 % USAGE: nc2ascii(ncin, fout, time, varids) 0006 % where: ncin is the path of the netcdf input file(string) or ncID (int), 0007 % fout is the output ascii filename (string), 0008 % time is the timeslice to read in (int, opional, default 1) 0009 % and varids is a vector of variable IDs in the from [varID, latID,lonID] 0010 % giving the variable IDs of the variable to export 0011 % along with the IDs of aviables storing the latitude and longitude 0012 % information. Default is [end,2,4] (end=last var in file). 0013 % 0014 % Note: Quick'n'dirty. Adapt to your need (e.g. using varnames). 0015 % 0016 % Uses: mat2asciigrid(), nc_readvar() 0017 % 0018 % MEXCDF: http://mexcdf.sourceforge.net 0019 % and obviously netcdf: http://www.unidata.ucar.edu/s(oftware/netcdf/ 0020 % 0021 % Felix Hebeler, Geography Dept., University Zurich, December 2006. 0022 %some checking first 0023 if exist(ncin) % check if ncin exists as path or id 0024 if isstr(ncin) 0025 if ~exist(ncin, 'file') 0026 error(['Netcdf file not found: ',ncin]) 0027 else 0028 ncid=mexcdf('OPEN',ncin,'r'); % open existing file 0029 end 0030 elseif isinteger(ncin) 0031 [a,a,a,a,a] = mexnc('inq', ncin ) 0032 if a~=0 % if status a is anything but 0, there's an error: 0033 error(mexnc('strerror',a)) 0034 end 0035 end 0036 else 0037 error('No valid Netcdf input file referrer specified') 0038 end 0039 % some more checking for defaults 0040 if ~exist('time') 0041 time=1; 0042 end 0043 if ~exist('varids') 0044 [ndims, nvars] = mexcdf('INQUIRE',ncid); % retrieve info 0045 varids=[nvars-1,2,4]; 0046 end 0047 [varname, datatype] = mexnc('inq_var', ncid, varids(1)) ; 0048 %mexcdf('CLOSE',ncid); 0049 data=nc_readvar(ncin,varname,time); 0050 data=rot90(data,3); 0051 data=fliplr(data); 0052 tlat=mexnc('get_var_float',ncid,varids(2)); %read in lat and lon as float 0053 tlon=mexnc('get_var_float',ncid,varids(3)); 0054 0055 mat2asciigrid(fout, tlon,tlat,data); 0056 %figure, imagesc(tlon(end:-1:1),tlat,fliplr(data)) % 0057 %set(gca,'ydir','normal') 0058 %title('NetCDF grid successfully exported to ArcGrid ASCII Format:')