data = [];
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');
data2= [v];
data2(isnan(data2)) = -99.0 ; %cleanup in case the data is a NaN
data = [data;data2];
end
end
end
fprintf(fid, '18900101\n');%#write the climate data starting date to header of outfile. Change '20200101' to the starting date of your dataset. 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