Download NetCDF data via command window

12 views (last 30 days)
How do I download NetCDF data through my 'comand window' from my matlab?
Website with the data below:
precip.1990.nc61.38 Mbytes2020-04-13T21:00:26.700Z
precip.1991.nc57.06 Mbytes2020-04-13T21:00:25.980Z
precip.1992.nc58.23 Mbytes2022-02-28T22:46:21.624Z
precip.1993.nc58.34 Mbytes2020-04-13T21:00:25.892Z
precip.1994.nc56.61 Mbytes2020-04-13T21:00:23.807Z
precip.1995.nc56.91 Mbytes2020-04-13T21:00:24.043Z
precip.1996.nc57.37 Mbytes2020-04-13T21:00:24.821Z
precip.1997.nc56.96 Mbytes2020-04-13T21:00:24.614Z
precip.1998.nc61.25 Mbytes2020-04-13T21:00:22.487Z
precip.1999.nc62.01 Mbytes2020-04-13T21:00:22.363Z
precip.2000.nc65.35 Mbytes2022-02-28T22:46:21.761Z
precip.2001.nc64.10 Mbytes2022-02-28T22:46:21.971Z
precip.2002.nc63.94 Mbytes2022-02-28T22:46:22.539Z
precip.2003.nc64.74 Mbytes2022-02-28T22:46:22.684Z
precip.2004.nc65.01 Mbytes2022-02-28T22:46:23.162Z
precip.2005.nc62.20 Mbytes2022-02-28T22:46:23.485Z
precip.2006.nc64.78 Mbytes2022-02-28T22:46:23.614Z
precip.2007.nc60.43 Mbytes2022-02-28T22:46:23.748Z
precip.2008.nc60.59 Mbytes2022-02-28T22:46:23.896Z
precip.2009.nc62.17 Mbytes2022-02-28T22:46:23.986Z
precip.2010.nc61.36 Mbytes2022-02-28T22:46:24.066Z
precip.2011.nc61.79 Mbytes2022-02-28T22:46:24.199Z
precip.2012.nc62.44 Mbytes2022-02-28T22:46:24.294Z
precip.2013.nc62.15 Mbytes2022-02-28T22:46:24.425Z
precip.2014.nc63.20 Mbytes2022-02-28T22:46:24.560Z
precip.2015.nc62.88 Mbytes2022-02-28T22:46:24.648Z
precip.2016.nc57.85 Mbytes2022-02-28T22:46:24.724Z
precip.2017.nc60.16 Mbytes2022-02-28T22:46:24.848Z
precip.2018.nc58.86 Mbytes2022-02-28T22:46:24.880Z
precip.2019.nc59.79 Mbytes2022-02-28T22:46:24.913Z

Accepted Answer

Mathieu NOE
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
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
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

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!