常用程序
2024年11月25日
常用程序
数据结构
% 设计数据结构 years = 2003:2018; codg = struct('TEC', cell(numel(years), 1)); for i = 1:numel(years) codg(i).TEC = nan(71, 73, 25, yeardays(years(i))); end % 重构数组 % 四维转三维(71×73×24×365 -> 71×73×8760) tec = reshape(tec, 71, 73, []); % 三维转四维(71×73×8760 -> 71×73×24×365) tec = reshape(tec, 71, 73, 24, 365); % 高维转置(71×73×8760 -> 73×71×8760) tec = permute(tec, [2, 1, 3]);时间相关
% 根据年月日计算年积日 mdoy = doy(datetime(2021, 3, 21)); % mdoy = day(datetime(2021, 3, 21), 'dayofyear'); % 根据年积日计算年月日 ymd = datevec(datetime(2021, 1, 80)); % 计算月份天数 days = eomday(2021, 3); % 世界时转地方时 dh = round(lon(j)/15); dmin = round((lon(j)/15-dh)*60); T = datevec(datetime(2021, 3, 21, ut+dh, 0+dmin, 0));构建网格点经纬度
lon_1 = -180:5:180; lat_1 = 87.5:-2.5:-87.5; lon_2 = repelem((lon_1)', numel(lat_1)); lat_2 = reshape(repmat((lat_1)', 1, numel(lon_1)), [], 1);
NetCDF文件
读取文件file_path = fullfile(pwd, 'TEC.nc'); % 读取变量 lon = ncread(file_path, 'lon'); lat = ncread(file_path, 'lat'); tec = ncread(file_path, 'TEC')'; % 此处需要转置 % 读取属性 year = ncreadatt(file_path, '/', 'year');输出文件% 写入文件 lon = -180:5:180; lat = 87.5:-2.5:-87.5; tec = zeros(numel(lat), numel(lon)); name = 'TEC.nc'; nc(lon, lat, tec, name); % 函数定义 function nc(lon, lat, tec, name) cid = netcdf.create(name, 'NC_WRITE'); dimidlon = netcdf.defDim(cid, 'lon', numel(lon)); dimidlat = netcdf.defDim(cid, 'lat', numel(lat)); varid1 = netcdf.defVar(cid, 'lon', 'double', dimidlon); varid2 = netcdf.defVar(cid, 'lat', 'double', dimidlat); varid3 = netcdf.defVar(cid, 'TEC', 'double', [dimidlon dimidlat]); netcdf.endDef(cid); netcdf.putVar(cid, varid1, lon); netcdf.putVar(cid, varid2, lat); netcdf.putVar(cid, varid3, tec'); % netcdf.reDef(cid); % netcdf.putAtt(cid, netcdf.getConstant("NC_GLOBAL"), 'year', 2021); netcdf.close(cid); end创建保存目录
dir_path = fullfile(pwd, num2str(years(i))); if ~exist(dir_path,'dir') mkdir(dir_path); end导出图片
ax = gca; fg = gcf; set(ax, 'LineWidth', 1.0); set(fg, 'Units', 'centimeters','Position', [2, 2, 15, 8]); annotation('line', [ax.Position(1), ax.Position(1)+ax.Position(3)], [1, 1].*(ax.Position(2)+ax.Position(4)), 'LineStyle', '-', 'LineWidth', ax.LineWidth, 'Color', ax.XColor); annotation('line', [1, 1].*(ax.Position(1)+ax.Position(3)), [ax.Position(2), ax.Position(2)+ax.Position(4)], 'LineStyle', '-', 'LineWidth', ax.LineWidth, 'Color', ax.XColor); exportgraphics(gcf, 'Test_1.png', 'Resolution', 600);