Automatic Downloading and automatic unzip of tarred and gunzipped files.

2 views (last 30 days)
So I the code I have currently can download several .tar.gz files within a loop, the amount of files downloaded can be altered by changing the variable year and the day within the file. After this succession of the loop, the code automatically progress onto the unzipping part, there is something at which I can't seem to crack is the fact that the code that is written only unzips the last file it downloads rather than all the files downloaded, what do I need to change to the code so that it would unzip all the files downloaded?
Many Thanks
url_https='https://data.cosmic.ucar.edu';
for year = 2022:2022
for day = 001:001
dataUrl = sprintf('%s/gnss-ro/cosmic2/nrt/level1b/%d/%03d/podTc2_nrt_%d_%03d.tar.gz',url_https, year, day, year, day);
dataFile = sprintf('podTc2_nrt_%d_%03d.tar.gz', year, day);
FileFullPath = websave(dataFile, dataUrl);
end
end
DownloadedFile=sprintf('podTc2_nrt_%d_%03d', year, day);
unzip_Files=untar(FileFullPath,DownloadedFile);
ncvars={'TEC','elevation','time','x_LEO','y_LEO','z_LEO','x_GPS','y_GPS','z_GPS'};
projectdir=(DownloadedFile);
dinfo=dir(fullfile(projectdir,'*.0001_nc'));
num_files=length(dinfo);
filenames=fullfile(projectdir,{dinfo.name});
TECs=cell(num_files,1);
Elevations=cell(num_files,1);
times=cell(num_files,1);
x_LEOs=cell(num_files,1);
y_LEOs=cell(num_files,1);
z_LEOs=cell(num_files,1);
x_GPSs=cell(num_files,1);
y_GPSs=cell(num_files,1);
z_GPSs=cell(num_files,1);
for i=1 : num_files
this_file=filenames{i};
TECs{i}=ncread(this_file,ncvars{1});
Elevations{i}=ncread(this_file,ncvars{2});
times{i}=ncread(this_file,ncvars{3});
x_LEOs{i}=ncread(this_file,ncvars{4});
y_LEOs{i}=ncread(this_file,ncvars{5});
z_LEOs{i}=ncread(this_file,ncvars{6});
x_GPSs{i}=ncread(this_file,ncvars{7});
y_GPSs{i}=ncread(this_file,ncvars{8});
z_GPSs{i}=ncread(this_file,ncvars{9});
delete(this_file)
end
  1 Comment
Tianchu Lu
Tianchu Lu on 12 Sep 2022
So this code automatically downloads a file from a data website between the first day of the calendar year, and after downloading it would unzip that file perfectly. But if i want to download four days within a calendar year, the downloading part would download the specific days without any problem. The problem occurs when the downloading part occurs such that it would only unzip the last file it has downloaded. Hopefully I have explained that a bit better.

Sign in to comment.

Accepted Answer

Saffan
Saffan on 7 Sep 2023
Hi Tianchu,
To unzip all the downloaded files instead of just the last one, you need to move the unzipping part inside the loop that downloads the files as shown in the following code snippet:
for year = 2022:2022
for day = 001:004
dataUrl = sprintf('%s/gnss-ro/cosmic2/nrt/level1b/%d/%03d/podTc2_nrt_%d_%03d.tar.gz',url_https, year, day, year, day);
dataFile = sprintf('podTc2_nrt_%d_%03d.tar.gz', year, day);
FileFullPath = websave(dataFile, dataUrl);
DownloadedFile=sprintf('podTc2_nrt_%d_%03d', year, day);
unzip_Files=untar(FileFullPath,DownloadedFile);
end
end
Please refer to this for more information:

More Answers (0)

Categories

Find more on Downloads 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!