Export netCDF data based on latitude and longitude
6 次查看(过去 30 天)
显示 更早的评论
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:
- I want to export a specific long, lat
- 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
0 个评论
采纳的回答
Walter Roberson
2017-7-18
You can use the same kind of masking techniques I talk about in https://www.mathworks.com/matlabcentral/answers/347505-extracting-netcdf-data-based-on-time#answer_272996
You have the advantage of not having to make a correction for the odd way netcdf encodes time.
14 个评论
Walter Roberson
2017-7-21
%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)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!