Effacer les filtres
Effacer les filtres

Write Netcdf files from directory to matrix, based on location

4 vues (au cours des 30 derniers jours)
Bram Loef
Bram Loef le 15 Sep 2022
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)

Réponses (1)

Himanshu
Himanshu le 26 Sep 2023
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:
  1. Initialize an empty matrix to store the time series data.
  2. Loop through each .nc file in the directory.
  3. For each file, read the time series data for the specific location. You can use "ncread" function.
  4. Append this data as a new column in the matrix.
  5. Repeat steps 2-4 for all .nc files.
You can refer to the below documentations to understand more about the "ncread" function, the "ncdisp" function and Multidimensional Arrays in MATLAB.

Community Treasure Hunt

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

Start Hunting!

Translated by