Export netCDF data based on latitude and longitude

6 views (last 30 days)
Hello,
I have the following code which coded by KSSV This code is to read netCDF and export them to excel file. The problems are:
  1. I want to export a specific long, lat
  2. export the data to several csv files instead one excel file
I appreciate your help and time!
files = dir('*.nc') ;
nfiles = length(files) ;
P = cell(nfiles,1) ; % precipitation of all files
date = cell(nfiles,2) ;
time = cell(nfiles,2) ;
for i = 1:nfiles
%%Read dat
date{i,1} = ncreadatt(files(i).name,'/','HDF5_GLOBAL.BeginDate') ;
date{i,2} = ncreadatt(files(i).name,'/','HDF5_GLOBAL.EndDate') ;
%%Read time
time{i,1} = ncreadatt(files(i).name,'/','HDF5_GLOBAL.BeginTime') ;
time{i,2} = ncreadatt(files(i).name,'/','HDF5_GLOBAL.EndTime') ;
%%REad precipitation
P{i} = ncread(files(i).name,'precipitation') ;
end
myfile = 'myfile.xlsx' ;
for i = 1:nfiles
xlswrite(myfile,P{i},i)
end

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jul 2017
You have the advantage of not having to make a correction for the odd way netcdf encodes time.
  14 Comments
Walter Roberson
Walter Roberson on 21 Jul 2017
%setup done once
lat_vec = linspace(-50, 50, 400);
long_vec = linspace(-180, 180, 1440);
lat_res = 0.25;
long_res = 0.25;
latlim = [5 -5]; %5N 5S
lonlim = [-50 -45]; %-50W -45W
matching_lat = lat_vec > min(latlim) - lat_res/2 & lat_vec < max(latlim) + lat_res/2;
matching_long = long_vec > min(lonlim) - long_res/2 & long_vec < max(lonlim) + long_res/2;
%and for each file,
data_subset = P{i}(matching_lat, matching_long);
The lat_res and long_res are there to take into account that the coordinates are the centers of the grid cells, so the given limits might happen to be within the logical confines of a grid cell and yet not happen to match based upon the given limit. For example, if the limit given were -5.1 then you need to include the grid entry that is nominally at -5 because that grid entry really runs from -5.25 to -4.75 (I do not know whether that is semi-open or semi-closed)
Majid Mohamod
Majid Mohamod on 22 Jul 2017
Thank you so much man. I will try it and keep you updated. :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!