- Initialize an empty matrix to store the time series data.
- Loop through each .nc file in the directory.
- For each file, read the time series data for the specific location. You can use "ncread" function.
- Append this data as a new column in the matrix.
- Repeat steps 2-4 for all .nc files.
Write Netcdf files from directory to matrix, based on location
6 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
I have a directory with a lot of different .nc files, all of them have the same lattitude, longitude and time (both in value as in lenght). They are different output from different river discharge models. I want to substract all .nc files from my directory in a loop. But, then I also want to substract for each file a timeserie for a certain location (lat, lon combination). Preferably I put all these timeseries as a column next to each other in a matrix so I can all plot them in a graph.
Does anyone know how to substract all these different files into either stand alone arrays or all together in a table.
I also know how to get all different files read and but in a multi dimensional table, maybe it it possible to go from there...:
ncvars = {'latitude', 'longitude', 'mmflow', 'time'};
projectdir = 'C:\Users\User\Documents\Gisdata_Bram\Rainfalldata_nigeria\Riverdischarge\RCP85\monthly_data';
dinfo = dir( fullfile(projectdir, '*.nc') );
num_files = length(dinfo);
filenames = fullfile( projectdir, {dinfo.name} );
% lats = cell(num_files, 1);
% lons = cell(num_files, 1);
mmflow = cell(num_files, 1);
% times = cell(num_files, 1);
for K = 1 : num_files
this_file = filenames{K};
% lats{K} = ncread(this_file, ncvars{1});
% lons{K} = ncread(this_file, ncvars{2});
mmflow{K} = ncread(this_file, ncvars{3});
% times{K} = ncread(this_file, ncvars{4});
end
Below is my code to do it for one file with the squeeze function:
lat = ncread("RCP85\HMF-WA_AMFLOW_BC_ACCESS1.0_RCP85_2006-2099.nc","latitude");
lon = ncread("RCP85\HMF-WA_AMFLOW_BC_ACCESS1.0_RCP85_2006-2099.nc","longitude");
time = ncread("RCP85\HMF-WA_AMFLOW_BC_ACCESS1.0_RCP85_2006-2099.nc", "time"); %yearly
time_monthly= ncread("RCP85\HMF-WA_MMFLOW_BC_ACCESS1.0_RCP85_200601-209912.nc", "time"); %yearly
flow_monthly= ncread("RCP85\HMF-WA_MMFLOW_BC_BNU-ESM_RCP85_200601-209912.nc", "mmflow");
flowNiger_monthly8=squeeze(flow_monthly(248,189,:));
plot(time_monthly, flowNiger_monthly)
0 个评论
回答(1 个)
Himanshu
2023-9-26
Hello Bram,
I understand that you are trying to extract data from multiple ".nc" files in a directory and organize them into a matrix based on location. This task can be accomplished by utilizing MATLAB's ability to handle multidimensional arrays and netCDF files.
The issue you are encountering lies in extracting the time series data for a specific location from each ".nc" file and then organizing this data into a matrix. You should loop through each ".nc" file, read the specific data, and store it in a matrix.
You can follow the below steps:
You can refer to the below documentations to understand more about the "ncread" function, the "ncdisp" function and Multidimensional Arrays in MATLAB.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!