Extract daily time series NetCDF to specific location

4 次查看(过去 30 天)
Hi,
I want to extarct daily time series but it doesn't work. The reasons are; (1) it seems like time matrix is minus and (2) it seems like time matrix exceeds array bounds (must not exceed 365) and (3) empty metrix after extraction. Link for data is ftp://ftp.cdc.noaa.gov/Datasets/cpc_global_precip/precip.2019.nc
Code is as folliwings;
clc
close all
clear all
filename = ('precip.2019.nc');
info = ncinfo(filename);
unitsTime = info.Variables(3).Attributes(2);
unitsPrep = info.Variables(4).Attributes(2).Value;
validRangePrep = info.Variables(4).Attributes(9).Value;
labelPrep = info.Variables(4).Attributes(7).Value;
time = ncread(filename,'time');
prep = ncread(filename,'precip');
lon = double(ncread(filename,'lon'));
lat = double(ncread(filename,'lat'));
location_lat = knnsearch(lat,18.5);
location_lon = knnsearch(lon,102.5);
tDatenum = datenum('01-01-2019 00:00:00','dd-mm-yyyy HH:MM:SS');
from_date = '01-01-2019 00:00:00'; to_date = '31-12-2019 00:00:00';
idxDatenum = from_date <= tDatenum & tDatenum <= to_date;
myPrepData = squeeze(prep(location_lon,location_lat,idxDatenum));
Thanks in advance.
Phat

回答(1 个)

Meg Noah
Meg Noah 2020-1-9
Here's a solution:
clc
close all
clear all
filename = ('precip.2019.nc');
info = ncinfo(filename);
% Variable 1
lat = double(ncread(filename,'lat'));
% Variable 2
lon = double(ncread(filename,'lon'));
% Variable 3
% data are for each day of the year (2019)
time = ncread(filename,'time');
unitsTime = info.Variables(3).Attributes(2).Value;
fprintf(1,'Time Units: %s\n',unitsTime);
tDatenum = datenum(1900,1,1,time,0,0);
% Variable 4
prep = ncread(filename,'precip');
prepMissingValue = info.Variables(4).Attributes(1).Value;
prepUnits = info.Variables(4).Attributes(2).Value;
prepDescription = info.Variables(4).Attributes(3).Value;
% validRangePrep = info.Variables(5).Attributes(8).Value;
labelPrep = info.Variables(4).Attributes(7).Value;
validRangePrep = info.Variables(4).Attributes(10).Value;
% location_lat = knnsearch(lat,18.5);
% location_lon = knnsearch(lon,102.5);
dist = abs(lat - 18.5);
idxLat = find(dist == min(dist));
dist = abs(lon - 102.5);
idxLon = find(dist == min(dist));
% print out locations found
nsites = length(idxLat)*length(idxLon);
fprintf(1,'Found %d locations near site\n',nsites);
for kLat = 1:length(idxLat)
for kLon = 1:length(idxLon)
fprintf(1,'Location: Lat = %f Lon = %f\n', lat(idxLat(kLat)), lon(idxLon(kLon)));
end
end
% hardwiring getting all 4 sites
DOY = 1:365; DOY = DOY';
GlobalDaily = table(DOY);
GlobalDaily.Site1 = squeeze(prep(idxLon(1),idxLat(1),:));
GlobalDaily.Site2 = squeeze(prep(idxLon(1),idxLat(2),:));
GlobalDaily.Site3 = squeeze(prep(idxLon(2),idxLat(1),:));
GlobalDaily.Site4 = squeeze(prep(idxLon(2),idxLat(2),:));
GlobalDaily.Properties.VariableUnits = {'Day Of Year','mm','mm','mm','mm'};
GlobalDaily.Properties.UserData.Site1 = [lat(idxLat(1)), lon(idxLon(1))];
GlobalDaily.Properties.UserData.Site2 = [lat(idxLat(2)), lon(idxLon(1))];
GlobalDaily.Properties.UserData.Site3 = [lat(idxLat(1)), lon(idxLon(2))];
GlobalDaily.Properties.UserData.Site4 = [lat(idxLat(2)), lon(idxLon(2))];
GlobalDaily.Properties.UserData.Description = {info.Variables(4).Attributes.Value}';
strSite1 = ['Site1 = ' ...
num2str(GlobalDaily.Properties.UserData.Site1(1)) ...
' \circN, ' ...
num2str(GlobalDaily.Properties.UserData.Site1(2)) ' \circE'];
strSite2 = ['Site2 = ' ...
num2str(GlobalDaily.Properties.UserData.Site2(1)) ...
' \circN, ' ...
num2str(GlobalDaily.Properties.UserData.Site2(2)) ' \circE'];
strSite3 = ['Site3 = ' ...
num2str(GlobalDaily.Properties.UserData.Site3(1)) ...
' \circN, ' ...
num2str(GlobalDaily.Properties.UserData.Site3(2)) ' \circE'];
strSite4 = ['Site4 = ' ...
num2str(GlobalDaily.Properties.UserData.Site4(1)) ...
' \circN, ' ...
num2str(GlobalDaily.Properties.UserData.Site4(2)) ' \circE'];
figure('color','white','Position',[70 500 1200 450]);
set(gca,'fontname','Ariel','fontsize',14,'fontweight','bold');
hold on
box on
plot(GlobalDaily.DOY,GlobalDaily.Site1,'DisplayName',strSite1);
plot(GlobalDaily.DOY,GlobalDaily.Site2,'DisplayName',strSite2);
plot(GlobalDaily.DOY,GlobalDaily.Site3,'DisplayName',strSite3);
plot(GlobalDaily.DOY,GlobalDaily.Site4,'DisplayName',strSite4);
legend('location','best');
ylabel([labelPrep ' [' prepUnits ']']);
title({'CPC Global Precip RT: Daily total of precipitation'; ...
'Surface Measurements'});
xlim([0 365]);
% tickmarks at first day of each month
ttt = datenum(2020,1:12,1,0,0,0);
ttt = ttt - ttt(1) + 1;
set(gca,'xtick',ttt);
set(gca,'xticklabel',{'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug', ...
'Sep','Oct','Nov','Dec'});
AnnualDailyMean2019.png
  1 个评论
Augusto Gabriel da Costa Pereira
78 / 5.000
Resultados de tradução
this error appears on mine:
The index exceeds the number of array elements (7).

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by