Plot for Month and year

5 views (last 30 days)
Ara
Ara on 29 May 2024
Commented: Ara on 29 May 2024
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 Comments
Ara
Ara on 29 May 2024
I could not share the data as it mentioned the format is not accepted. Here is the link for data to provide a better view of it.
https://data.cosmic.ucar.edu/gnss-ro/cosmic2/provisional/spaceWeather/level2/2023/

Sign in to comment.

Accepted Answer

Manikanta Aditya
Manikanta Aditya on 29 May 2024
Edited: Manikanta Aditya on 29 May 2024
Hi, @Ara,
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 Comments
Manikanta Aditya
Manikanta Aditya on 29 May 2024
To create a contour plot of Vertical Total Electron Content (VTEC) from the daily .mat files, you'll need to follow these steps:
  1. Load Data from .mat Files
  2. 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)';
Ara
Ara on 29 May 2024
Thank you for the code and explanation.
I have to calculate VTEC from STEC. I have electron density that I need to read from NETCDF file and then convert it to VTEC. And then plot it as a contourplot.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!