Plot for Month and year
조회 수: 4 (최근 30일)
이전 댓글 표시
Dear All,
I have data with format of NETCDF file. I can read one of them and plot it. For each day I have many data so I wish to read all of them to provide result for one day and then expand it to provide it for month and year. Also, I would like to have a data in .mat format, for example for each month. Would you please help me in this regard?
Best,
Ara
댓글 수: 3
채택된 답변
Manikanta Aditya
2024년 5월 29일
편집: Manikanta Aditya
2024년 5월 29일
Based on the understanding from your explaination in the question, you can use some MATLAB functions and script for it.
Use can use the 'ncread' function: https://in.mathworks.com/help/matlab/ref/ncread.html
Check it out:
% Define paths to your NETCDF files
netcdfPath = 'path/to/netcdf/files/';
% Define output directory for processed data
outputDir = 'path/to/output/directory/';
% Loop through each NETCDF file
netcdfFiles = dir(fullfile(netcdfPath, '*.nc'));
for fileIdx = 1:numel(netcdfFiles)
% Read NETCDF file
ncFile = fullfile(netcdfPath, netcdfFiles(fileIdx).name);
% Read necessary data from NETCDF file (use appropriate commands for your data)
data = ncread(ncFile, 'variable_name');
% Process daily data (e.g., calculate daily averages)
dailyAverage = mean(data, 'all');
dailyDataFileName = fullfile(outputDir, ['daily_data_', datestr(netcdfFiles(fileIdx).datenum, 'yyyymmdd'), '.mat']);
save(dailyDataFileName, 'dailyAverage');
end
% Aggregate daily data to monthly
monthlyDataFileName = fullfile(outputDir, ['monthly_data_', datestr(netcdfFiles(fileIdx).datenum, 'yyyymm'), '.mat']);
save(monthlyDataFileName, 'monthlyData');
% Aggregate monthly data to yearly
yearlyDataFileName = fullfile(outputDir, ['yearly_data_', datestr(netcdfFiles(fileIdx).datenum, 'yyyy'), '.mat']);
save(yearlyDataFileName, 'yearlyData');
- Consider this workaround script as a base and it should help you to start off.
I hope this gives you some help with your query!
댓글 수: 7
Manikanta Aditya
2024년 5월 29일
@Ara,
To create a contour plot of Vertical Total Electron Content (VTEC) from the daily .mat files, you'll need to follow these steps:
- Load Data from .mat Files
- Interpolate and Contour Plot
% Define the path to your .mat files
matPath = 'path/to/output/directory/';
matFiles = dir(fullfile(matPath, 'daily_data_*.mat'));
% Initialize arrays to store the combined data
allLongitudes = [];
allLatitudes = [];
allVTEC = [];
% Loop through each .mat file and load the data
for fileIdx = 1:numel(matFiles)
% Load the daily data file
matFile = fullfile(matPath, matFiles(fileIdx).name);
data = load(matFile);
% Assuming the data structure contains longitudes, latitudes, and VTEC
longitudes = data.longitudes;
latitudes = data.latitudes;
VTEC = data.VTEC;
% Append the data to the combined arrays
allLongitudes = [allLongitudes; longitudes];
allLatitudes = [allLatitudes; latitudes];
allVTEC = [allVTEC; VTEC];
end
% Ensure all three arrays have the same length
minLength = min([length(allLongitudes), length(allLatitudes), length(allVTEC)]);
allLongitudes = allLongitudes(1:minLength);
allLatitudes = allLatitudes(1:minLength);
allVTEC = allVTEC(1:minLength);
% Create a meshgrid for longitude and latitude
[lonGrid, latGrid] = meshgrid(unique(allLongitudes), unique(allLatitudes));
% Interpolate VTEC onto the grid
VTEC_grid = griddata(allLongitudes, allLatitudes, allVTEC, lonGrid, latGrid, 'natural');
% Create the contour plot
figure;
contourf(lonGrid, latGrid, VTEC_grid);
colorbar;
title('Contour Plot of VTEC');
xlabel('Longitude (degrees)');
ylabel('Latitude (degrees)');
c = colorbar;
c.Label.String = 'VTEC (TECU)';
추가 답변 (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!