How can I merge different .nc files related to different time steps?

조회 수: 7 (최근 30일)
bhakti 2023년 11월 29일
댓글: Mathieu NOE 2023년 12월 8일
Data source: HYCOM
Context: Listed below are the attributes of data for a day at time step 12 am. Let's say I have data of Jan at different timesteps. And I have to merge them all into a single file to get variables velocity u and velocity_v sizes as [176,239,40,248] (different files data of a single month merged) as variables are captured for every 3 hours/temporal resolution is 3 hours. Please help with the code?
  댓글 수: 6
Peter Perkins
Peter Perkins 2023년 11월 30일
Do you ultimately want two 176-by-239-by-40-by-248 arrays (or maybe one 176-by-239-by-40-by-248-by-2 array), or do you want one array that is (176*239*40*248)-by-6, where columns 1:4 of the latter are values for time, depth, lat, and lon, and columns 5:6 are values of velocity at the corresponding time/depth/lat/lon combination for each row?
I.e., in fewer dimensions,
>> x
x =
0.81472 0.91338 0.2785
0.90579 0.63236 0.54688
0.12699 0.09754 0.95751
>> y
y =
1 1 0.81472
2 1 0.90579
3 1 0.12699
1 2 0.91338
2 2 0.63236
3 2 0.09754
1 3 0.2785
2 3 0.54688
3 3 0.95751
Your answer almost certainly depends on what you need to do with your result.
bhakti 2023년 12월 1일
Hello Peter,
Thanks for your response.
I will use the results to make hdf5 files.
I need the velocities at their corresponding lon/lat/depth/time combination for each row. That is 2 arrays namely, u_velocity[176,239,40,248] and v_velocity[176,239,40,248].
Thanks in advance!

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

채택된 답변

Mathieu NOE
Mathieu NOE 2023년 12월 1일
hello @bhakti
here you have your u and v velcocity data stored in 4D arrays u_out and v_out as requested
I tried my code with the 7 days data nc files and it seems to work fine (size is in this case [176,239,40,56] )
also I often use this Fex submission to be sure the files are sorted correctly (what the regular dir does not do well)
fileDir = pwd; % current directory (or specify which one is the working directory)
S = dir(fullfile(fileDir,'*.nc')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order , see :
for k = 1:numel(S)
filename = S(k).name;
% Load in a few selected variables
u = ncread(filename,'water_u');
v = ncread(filename,'water_v');
% concatenate data
u_out(:,:,:,k) = u;
v_out(:,:,:,k) = v;
  댓글 수: 4
bhakti 2023년 12월 5일
Hello Mathieu,
Thanks again.
I did size(u_out) and size(v_out).
But the code achieved the mandate. Thank you for it.
Mathieu NOE
Mathieu NOE 2023년 12월 8일
hello again
according to your screenshot the [1,5] is the dimension of the vector size(u_out) not size(u_out) itself
but on my side size(u_out) has only dimension 1 by 4 and not 1 by five
size_u = size(u_out)
size_u = 176 239 40 56
Name Size Bytes Class Attributes
N 1x1 8 double
S 56x1 45344 struct
ans 1x4 32 double
fileDir 1x45 90 char
filename 1x14 28 char
k 1x1 8 double
size_u 1x4 32 double
u 176x239x40 13460480 double
u_out 176x239x40x56 753786880 double
v 176x239x40 13460480 double
v_out 176x239x40x56 753786880 double

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

추가 답변 (0개)


Help CenterFile Exchange에서 NetCDF에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by