Download all daily data (.nc) directly from the command window

6 views (last 30 days)
The script below collects data for a specific latitude and longitude of precipitation for all days in January 2000 at this link: https://www.ncei.noaa.gov/data/cmorph-high-resolution-global-precipitation-estimates/ access /daily/0.25deg/2000/01/
However, my goal is not just to pull data from specific locations and turn it into .xlsx for just one month.
I want to download every day of the year directly from this link without specifying latitude and longitude and without having to do the month-by-month process: https://www.ncei.noaa.gov/data/cmorph-high-resolution -global-precipitation-estimates /access /daily /0.25 degrees/
% Download the CPC data used in the script below
year_start = 20000101; % first year to download
year_stop = 20000131; % last year to download
nb_of_years = year_stop - year_start +1;
httpsUrl = "https://www.ncei.noaa.gov/data/cmorph-high-resolution-global-precipitation-estimates/access/daily/0.25deg/2000/01/";
filename_tmp="temp.nc"; % temporary filename (for loop); data will be always stored under this temp name
LG=[0.125]; % longitude (NB range is 0 : 360°)
LT=[-55.625]; % latitude
% export results to excel
filename_export='PRP_CMORPH.xlsx';
%% main loop
for ci = 1:nb_of_years
clear Cmorph
filename = strcat("CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_", num2str(year_start+ci-1), ".nc"); % create filename according to list above
disp(strcat(filename, " is processed"));
dataUrl = strcat(httpsUrl, filename);
data = webread(dataUrl);
filename_out = websave(filename_tmp,dataUrl); % save downloaded data
% ncdisp(filename_tmp,'/','min'); % commented, just to avoid filling the command window
precip=ncread(filename_tmp,'cmorph'); % Dimensions: lon,lat,time
long=ncread(filename_tmp,'lon');
lat=ncread(filename_tmp,'lat');
time=ncread(filename_tmp,'time');
for i=1:3
LGG=find(long==LG(i));
LTT=find(lat==LT(i));
if isempty(LGG)||isempty(LTT)
disp('LOCATION NOT FOUND IN THIS NetCDF FILE')
break
end
% Step 5 :Convert 3 dimensional array to column vector array for one station
Precip(:,i)=precip(LGG,LTT,:); % Unrecognized function or variable 'precip'.
X(i)={'Precipitation(mm/day)'};% label for variable
end
latt2=[LG ; LT];
time=double(time);
AA=time/24+datenum('1900-01-01 00:00:0.0');
[yy,mm,dd,~,~,~] = datevec(AA);
date1=[yy mm dd];
DD={'Year','Month','Day'};
sheet=ci; % NB : every year on a new sheet - same file
C={'LOCATION: Cities','','','Longitude';'DATE FROM JAN-DEZ 2021','','','Latitude'};
writecell(C,filename_export,"Sheet",sheet,"Range",'A1');
writecell(DD,filename_export,"Sheet",sheet,"Range",'A3');
writematrix(latt2,filename_export,"Sheet",sheet,"Range",'E1');
writecell(X,filename_export,"Sheet",sheet,"Range",'E3');
writematrix(date1,filename_export,"Sheet",sheet,"Range",'A4');
writematrix(Precip,filename_export,"Sheet",sheet,"Range",'E4');
end

Accepted Answer

Mathieu NOE
Mathieu NOE on 9 Dec 2022
hello Augusto
this code will loop over all specified monthes (it determines automatically how many days are in the current month)
remains now what you want to do will the daily data ...
% Download the CPC data used in the script below
which_year = 2000; % year to download
httpsUrl = "https://www.ncei.noaa.gov/data/cmorph-high-resolution-global-precipitation-estimates/access/daily/0.25deg/";
% filename_tmp="temp.nc"; % temporary filename (for loop); data will be always stored under this temp name
%% main loop
mm = (1:12); % all monthes in year
% mm = [1 6]; % process only some specific monthes in year
for ci = 1:numel(mm) % loop over monthes
days_per_month = datenum(which_year, mm(ci)+1, 1) - datenum(which_year, mm(ci), 1); % compute how many days this month/year
for ck = 1:days_per_month % loop over days
ymd_str = strcat(sprintf('%04d', which_year), sprintf('%02d', mm(ci)), sprintf('%02d', ck)); % year month day string
Url = strcat(httpsUrl, sprintf('%04d', which_year), "/", sprintf('%02d', mm(ci)),"/");
filename = strcat("CMORPH_V1.0_ADJ_0.25deg-DLY_00Z_", ymd_str ,".nc"); % create filename according to list above
disp(strcat(filename, " is processed"));
dataUrl = strcat(Url, filename);
data = webread(dataUrl);
filename_tmp=strcat("data_", ymd_str ,".nc"); % temporary filename (for loop); data will be always stored under this temp name
filename_out = websave(filename_tmp,dataUrl); % save downloaded data
% ncdisp(filename_tmp,'/','min'); % commented, just to avoid filling the command window
precip=ncread(filename_tmp,'cmorph'); % Dimensions: lon,lat,time
long=ncread(filename_tmp,'lon');
lat=ncread(filename_tmp,'lat');
time=ncread(filename_tmp,'time');
end
end
  12 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!