Download NetCDF data via command window
8 views (last 30 days)
Show older comments
Augusto Gabriel da Costa Pereira
on 14 Oct 2022
Commented: Walter Roberson
on 19 Oct 2022
How do I download NetCDF data through my 'comand window' from my matlab?
Website with the data below:
0 Comments
Accepted Answer
Mathieu NOE
on 17 Oct 2022
hello (again) Augusto
try this code.
The results are saved in one single excel file (attached FYI), each year on a new sheet. Of course you can change that logic and save each results separately in a specific file
hope it helps
% Download the CPC data used in the script below
year_start = 1990; % first year to download
year_stop = 2019; % last year to download
nb_of_years = year_stop - year_start +1;
httpsUrl = "https://psl.noaa.gov/thredds/fileServer/Datasets/cpc_global_precip/";
filename_tmp="temp.nc"; % temporary filename (for loop); data will be always stored under this temp name
LG=[-48.25 -48.75 -49.25] + 360; % longitude (NB range is 0 : 360°)
LT=[-1.25 -1.25 -1.25]; % latitude
% export results to excel
filename_export='PRP_CPC.xlsx';
%% main loop
for ci = 1:nb_of_years
clear Precip
filename = strcat("precip.", 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,'precip'); % 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
5 Comments
Mathieu NOE
on 19 Oct 2022
hello
I don't think there is a problem with the code itself as I tested it sevaral times without an inch
maybe your internet connection had a glitch that caused matlab to fail during the data loading
Walter Roberson
on 19 Oct 2022
- your system might have interrupted the connection, such as if you ran out of system memory or operating system resources
- your system security software or local network security might have terminated the connection for various reasons such as mistaken belief that the data holds a virus
- your network service provider might have terminated the connection because it believes that you have used more total data than you contracted for
- your network service provider might have terminated the connection as a way of rate-limiting the connection
- there might be a network glitch or network overload somewhere on the path that resulted in the connection being terminated. With that particular transmission technology, a single corrupted packet would not lead to that: if the corruption was noticed then the protocol would end up asking for retransmission. But if someone dug up the network cable with a backhoe then the connection is going to fail
More Answers (0)
See Also
Categories
Find more on NetCDF in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!