%MAKE SURE THE DIRECTORY IS CHANGED TO THE DIRECTORY THAT CONTAINS THE NETCDF FILES
clear
clc
% introduction of the loop
Files=dir('*.nc');
for k=1:length(Files)
FileNames=Files(k).name;
pr=ncread(FileNames,'rainfall_amount');%name of netCDF file; 'pr' is the precip variable in the netCDF file
long=ncread(FileNames,'x'); %'longitude' is the longitude variable in the netCDF file
lat=ncread(FileNames,'y'); %'latitude' is the latitude variable in the netCDF file
for j= 716:746(lat); %1:length(lat);
for i= 328:368(long); %1:length(long);
v=pr(i,j,:); %read through all lat,longs in netCDF
outfile=sprintf('%d_%d_PCP.txt',lat(j),long(i));%name of outputfile; format is 'LATITUDE_LONGITUDE_PCP.txt'
fid=fopen(outfile,'wt');
m=1;
for i = 1:2
new(m)=v(i);
m =m+1;
data= [v];
data(isnan(data)) = -99.0 ; %cleanup in case the data is a NaN
%fprintf(fid, '18910101\n');%#write the climate data starting date to header of outfile. For example, Jan. 1, 1950 is 19500101
fprintf(fid,'%5.1f\n',data); %formatting the data for SWAT specifications
fclose(fid);
disp([outfile 'created'])%display created file
clearvars data
end
end
end
end
Best Answer