function nc2ascii(fileTxt, fileCdf) % NC2ASCII - quick function to turn nc data into ascii files % Note: IT will crash with NC file's with more than 2d arrays. % % Use as: nc2ascii(fileTxt) % or nc2ascii(fileTxt,fileCdf) % % Inputs: fileTxt = name of file to write ascii data to % fileCdf = name fo netCDF file to read % Brian Schlining % 10 May 1999 % 10 Dec 1999; B. Schlining; Modifications %============== % Get FileName %============== if nargin < 2 [infile inpath] = uigetfile('*.nc','Select netCDF file to read',0,0); if infile == 0 return end fileCdf = [inpath infile]; end f = netcdf(fileCdf,'nowrite'); % open the netCDF file %================== % Write the header %================== ncdump(fileCdf, fileTxt); % Do ncdump -h to a file % Read the ncdump into memory fid = fopen(fileTxt,'rt'); i = 1; while ~feof(fid) s{i} = fgetl(fid); i = i + 1; end fclose(fid); % Write the ncdump back out with '%' prepended to each line fid = fopen(fileTxt,'wt'); fprintf(fid,'%% ASCII dump of netCDF files from NC2ASCII.M\n'); fprintf(fid,'%% Missing values and fill values have been replaced with NaN''s\n%%\n'); for i = 1:length(s) fprintf(fid,'%%%s\n',s{i}); end %fclose(fid); %fid = fopen(fileTxt,'a'); %===================================== % Get information about the variables %===================================== % Dimension information d = dim(f); for i = 1:length(d) isRecordDim(i) = isrecdim(d{i}); end RecordDim = find(isRecordDim); % Locate the recored dimension % Variable information v = var(f); % Get the variable objects for i = 1:length(v) n{i} = name(v{i}); % Get the names of each variable vDim = dim(f{n{i}}); % Get the dimensions of each variable for j = 1:length(vDim) isRecordVDim(j) = isrecdim(vDim{j}); end isRecordVarDim{i} = isRecordVDim; % Store for later use when writing the file end %================== % Extract the data %================== Dat = []; next = 0; for i = 1:length(v) Buf = f{n{i}}(:); % Read the data into a variable fillValue = f{n{i}}.FillValue_(:); % Replace fill values with NaN if ~isempty(fillValue) BAD = find(Buf == fillValue); Buf(BAD) = NaN; end missingValue = f{n{i}}.missing_value(:); % replace missing values with NaN if ~isempty(missingValue) BAD = find(Buf == missingValue); Buf(BAD) = NaN; end isRecordVDim = isRecordVarDim{i}; RecordVDim = find(isRecordVDim); if isempty(RecordVDim) % Print variables without a record dimension in the header fprintf(fid,'%%%s = \n',n{i}); [r c] = size(Buf); for ir = 1:r for ic = 1:c fprintf(fid,'%%%12.5f', Buf(ir,ic)); end fprintf(fid,'\n'); end else % Get the header name for each variable rc = size(v{i}); r = rc(1); c = rc(2); if c > 1 for ii = 1:c %<=need to get the number of columns next = next + 1; varName{next} = [n{i} num2str(ii)]; end else next = next + 1; varName{next} = n{i}; end Dat = [Dat Buf]; end end fprintf(fid,'%%'); formatString = []; for i = 1:length(varName) fprintf(fid,'%s\t',varName{i}); if i < length(varName) formatString = [formatString '%12.5f\t']; else formatString = [formatString '%12.5f\n']; end end fprintf(fid,'\n'); fprintf(fid,formatString,Dat'); fclose(fid);