Clear Filters
Clear Filters

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

7 views (last 30 days)
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 Comments
Peter Perkins
Peter Perkins on 30 Nov 2023
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
or
>> 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
bhakti on 1 Dec 2023
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!

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 1 Dec 2023
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 :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
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;
end
  4 Comments
bhakti
bhakti on 5 Dec 2023
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 on 8 Dec 2023
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
whos
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!