Export netCDF data based on latitude and longitude

4 ビュー (過去 30 日間)
Majid Mohamod
Majid Mohamod 2017 年 7 月 17 日
コメント済み: Majid Mohamod 2017 年 7 月 22 日
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

採用された回答

Walter Roberson
Walter Roberson 2017 年 7 月 18 日
You have the advantage of not having to make a correction for the odd way netcdf encodes time.
  14 件のコメント
Walter Roberson
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)
Majid Mohamod
Majid Mohamod 2017 年 7 月 22 日
Thank you so much man. I will try it and keep you updated. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNetCDF についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by