Convert sveral netCDF files to ASCII or CSV
Show older comments
HI all, I have 2000 files of netCDF (.nc). These files are for precipitation of my study area. I have the script which included below. I want to make this script to call all netCDF and output them as ASCII. Some netCDF files have attached too.. I really appreciate your help and time.
Thanks, Majid
10 Comments
Jan
on 2 Jun 2017
What is your question? What have you tried so far? Which problems occur?
KSSV
on 2 Jun 2017
netCDF is a good format..which are easy to access..why you want to change them to csv/ ascii?
Majid Mohamod
on 4 Jun 2017
per isakson
on 4 Jun 2017
Edited: per isakson
on 4 Jun 2017
Have you verified that NetCDFreader together with NetCDF2ASCII work for one of your nc-file ?
The help says:
NetCDF2ASCII(header,PositionOfFile,PositionOfVariable)
How do one find the values for   PositionOfFile, PositionOfVariable ?
per isakson
on 4 Jun 2017
The file you attached are from the FEX-submission: NetCDF reader and export to ASCII version 1.1 (2.17 KB) by Michael Pikridas. You should have informed us.
Majid Mohamod
on 4 Jun 2017
Majid Mohamod
on 4 Jun 2017
per isakson
on 5 Jun 2017
Edited: per isakson
on 5 Jun 2017
"... work with multiple netCDF files!"   The first step must be to verify that the functions works as expected with one file. So far, I failed with one file. Did you succeed? Do you know that the FEX-submission is intended to work with your files?
Majid Mohamod
on 5 Jun 2017
per isakson
on 5 Jun 2017
Edited: per isakson
on 5 Jun 2017
- What makes you think that this FEX-submission can read and convert your files?
- "ArcSWAT"   is that The Soil and Water Assessment Tool (SWAT)?
- What makes you think that the FEX-submission will write a csv-file that is appropriate for ArcSWAT ?
- NetCDFreader requires that the extension is .nc. Yours is .nc4.
Accepted Answer
More Answers (2)
shobhit pipil
on 17 Dec 2018
Edited: shobhit pipil
on 17 Dec 2018
0 votes
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,'w+');
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
fclose all
1 Comment
shobhit pipil
on 17 Dec 2018
Would anybody please reply to me. I am trying to extract the NetCDF pricipitation data into SWAT format. The current script is not able to write data from NetCDF to txt file from each iteration. Please help.
javeed hamad
on 27 Mar 2022
0 votes
HI every one!
I want to extract GHI (Global horizontal irradiation) data from NC file which include almost 3500 files for my study area, can someone please guide which script should use to extract multiple files simultaneously.
Thanks a lot, Javeed
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!