Split .nc file into multiple .nc files

27 views (last 30 days)
Jake_K
Jake_K on 8 Mar 2018
Answered: KSSV on 26 Jun 2022
Hallo everybody.
I'm looking for some help regarding: How to split a .nc-file into multiple .nc-files.
I downloaded satellite (different variables) data from ERA-Interim for a specific area (lat x lon: 16 x 34) for nearly 20 years (1992-2009). But I only received one single file. With ncdisp I can look at it, e.g. for the variable total rainfall:
tp
Size: 16x34x6575
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
scale_factor = 3.1821e-06
add_offset = 0.10426
_FillValue = -32767
missing_value = -32767
units = 'm'
long_name = 'Total precipitation'
The size of this 3D array is now 16x34x6575. The last number is a time vector equal to the days -> 6575 days. My question now: how can I 'split' this file into 6575 new files?
So at the moment I have this file: data.nc, but I want for every day a new file: data_19920101.nc to data_20091231.nc in my folder. One file should look like this:
tp
Size: 16x34x1
Dimensions: longitude,latitude,time
Datatype: int16
Attributes:
scale_factor = 3.1821e-06
add_offset = 0.10426
_FillValue = -32767
missing_value = -32767
units = 'm'
long_name = 'Total precipitation'
I would be very thankful for any and all help.
Best
  1 Comment
anjula marasinghe
anjula marasinghe on 26 Jun 2022
did you find the answer. I have same question

Sign in to comment.

Answers (1)

KSSV
KSSV on 26 Jun 2022
There is no necessity to split the single ncfile into multiple ncfiles for each time step. It is waste of time, waste of task and not required. You can read the ncfile, at the required time step you want from the single file and use the data. You have two options, first read the file step by step in a loop. Second, load the entire data and pick what timestep you want.
Option 1
ncfile=myfile.nc ;
lon = ncread(ncfile,'longitude') ; nx = length(lon) ;
lat = ncread(ncfile,'latitude') ; ny = length(lat) ;
time = ncread(ncfile,'time') ;
for i = 1:length(time)
tp = ncread(ncfile,'tp',[1 1 i ],[nx ny 1]) ;
pcolor(lon,lat,tp') ;
title(sprintf("Time step = %d",i)) ;
shading interp
drawnow
end
Option 2
ncfile=myfile.nc ;
lon = ncread(ncfile,'longitude') ; nx = length(lon) ;
lat = ncread(ncfile,'latitude') ; ny = length(lat) ;
time = ncread(ncfile,'time') ;
tp = ncread(ncfile.'tp') ;
for i = 1:length(time)
R = tp(:,:,i) ;
pcolor(lon,lat,R')
shading interp
drawnow
end

Tags

Community Treasure Hunt

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

Start Hunting!