Split .nc file into multiple .nc files

조회 수: 25 (최근 30일)
Jake_K
Jake_K 2018년 3월 8일
답변: KSSV 2022년 6월 26일
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
anjula marasinghe
anjula marasinghe 2022년 6월 26일
did you find the answer. I have same question

댓글을 달려면 로그인하십시오.

답변 (1개)

KSSV
KSSV 2022년 6월 26일
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

카테고리

Help CenterFile Exchange에서 Predictive Maintenance Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by