how do I extract time series?
조회 수: 23 (최근 30일)
이전 댓글 표시
Hi all, I am trying to extract the data from the nc list of files using the following loop.
1- extract the whole parameter
2- set them in a 3d matrix
3- calculate the mean
4- extract the data in a given domain
5- calculate the mean to have the time series
I am not convinced with the final result (useries)
any help would be appreciated.
Thank you
useries = [];
for day=1:length(datenum_array);
filename=strcat('./MERRA2_400.tavg1_2d_ocn_Nx.2019',num2str(datevec_array(day,2),'%02i'),num2str(datevec_array(day,3),'%02i'),'.SUB.nc');
% disp(filename)
time = ncread(filename,'time');
lon = ncread(filename,'lon');
lat = ncread(filename,'lat');
%this box domain
lat_range=find(lat<= 28 & lat >= 26);
lon_range=find(lon<= 36.5 & lon >= 33);
time_range = datenum_array(32:12);
%read U10
ucom = ncread(filename,'U10M');
umean = nanmean(ucom,3);
U10 (:,:,day) = umean;
Ubox = U10(lon_range,lat_range,time_range);
umean2 = nanmean(nanmean(nanmean(Ubox)));
useries = [useries,umean2];
end
댓글 수: 0
채택된 답변
dpb
2020년 3월 31일
>> which time
C:\ML_R2019b\toolbox\matlab\bigdata\@tall\time.m % tall method
>> which day
C:\ML_R2019b\toolbox\matlab\timefun\@datetime\datetime.m % datetime method
>>
Better to not alias builtin/ML-supplied functions...changed names below.
useries = [];
lat_low=26; lat_up=28; % don't bury data in code--use variables so can change easily
lon_low=33; lon_up=36.5; % ditto
t_low=???; t_up=???; % dunno here but datenum_array(32:12) will return empty array reference
No way to tell what you really want for the time ranges but
time_range = datenum_array(32:12);
will return a null array as the indexing colon expression is equivalent to 32:1:12 which is an increasing difference of +1 over a decreasing interval which is empty. That is undoubtedly the biggest cause of your problems.
Set a time value and use logical indexing within in similar as demonstrated for lat, lon except using datetime
for d=1:size(datenum_array,1); % length unreliable as is max(size()), not specific dimension
filename=sprintf('./MERRA2_400.tavg1_2d_ocn_Nx.2019%02d%02d.SUB.nc',datevec_array(d,2:3)); % somewhat simpler
t=ncread(filename,'time'); % above comment re: builtin function names
lon = ncread(filename,'lon');
lat = ncread(filename,'lat');
%this box domain
lat_range=iswithin(lat,lat_low,lat_up);
lon_range=iswithin(lon,lon_low,lon_up);
time_range=iswithin(datenum_array,t_low,t_up); % t_low, t_up datetime() values begin, end desired
%read U10
ucom = ncread(filename,'U10M');
umean = nanmean(ucom,3);
U10(:,:,d) = umean;
Ubox = U10(lon_range,lat_range,time_range);
umean2 = nanmean(nanmean(nanmean(Ubox)));
useries = [useries,umean2];
end
Above presumes all files have same lat, lon and time data so the result of the locations will be same length--otherwise, the concatenation will fail on mismatched sizes.
Would be more efficient to determine that number first and then preallocate and store, but presuming the number of files is relatively small, the extra overhead of dynamic allocation may not be excessive and worth the extra coding.
iswithin is my "syntactic sugar" utility routine:
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
which just moves the logical into a function for somewhat cleaner code at the user level.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!