先搞清楚怎么读数据,怎么处理三维二维一维数组和转换
sst= ncread(source1,'sst'); %温度,三维
time=ncread(source1,'time'); %时间,一维
sst2 =sst(6,17,:); %确定时间的两个维度 (经lon,纬lat,时间从始到终)
sst2=sst2(:); %转换成一维,方便结合时间做出二维图像
%plot(time,sst2); %做出全部时间-温度二维图
plot(time(1:100),sst2(1:100)); %做出区间时间-温度二维图
画出地理范围温度图
note. 注意里面的for循环是把 -1e9的数据赋值为Nan 因为有的nc图sst值为空时的值是-1e9(忘了,反正就是可小)
clc;
clear;
%输入文件
%%%%!boundary = input('输入绘图范围,格式:[起始经度 终止经度 起始纬度 终止纬度]\n');
boundary =[290 360 32 72];
%文件打开与数据载入
source1 = "C:\Users\Administrator\Desktop\ERsst.mnmean.nc"; %打开源文件
%%%%%!ncdisp(source1); %查阅NC文件信息!重要,先清楚包含的数据类型和范围
lon = ncread(source1,'lon'); %查阅经度信息
loncount = length(lon); %查阅经度的精度(有多少格点)
lat = ncread(source1,'lat'); %查阅纬度信息
latcount = length(lat); %查阅纬度精度(有多少格点)
time = ncread(source1,'time'); %查阅时间层数信息
ticount = length(time); %查阅时间层数
disp('时间层数为:')
disp(ticount); %显示时间层数
t = input('输入绘制的时间层:');
%%%!varname = input('输入变量:','s'); %根据ncdisp显示的变量输入绘图
varname = 'sst';
%查找绘制范围对应的所在矩阵的位置,相当于截取一小段矩阵
lon_scope = find(lon >= boundary(1) & lon<=boundary(2));
lat_scope = find(lat >= boundary(3) & lat<=boundary(4));
%绘制范围的数据量
lon_number = length(lon_scope);
lat_number = length(lat_scope);
start = [lon_scope(1),lat_scope(1),1]; %初始位置
count = [lon_number,lat_number,ticount]; %读取范围
stride1 = [1,1,1]; %读取步长
sst1 = ncread(source1,varname ,start,count,stride1);
sst_plot = imrotate(sst1(:,:,t), 90); %旋转矩阵,因为matlab是列优先
[r,c] = size(sst_plot) % 读取行r、列c
for i = 1:r % 建立for循环嵌套
for k = 1:c
if sst_plot(i,k)<-100 % 读取矩阵每个位置数据,先行后列
sst_plot(i,k)=NaN;
end
end
end
%墨卡托投影,规定绘制范围
m_proj('Mercator','lat',[boundary(3) boundary(4)],'lon',[boundary(1) boundary(2)]);
%生成网格
lat_1=linspace(boundary(3),boundary(4),lat_number);
lon_1=linspace(boundary(1),boundary(2),lon_number);
[plon,plat]=meshgrid(lon_1,lat_1);
hold on
%绘制图形
m_pcolor(plon,plat,sst_plot) %添加我们要画的内容
m_coast('color',[0 0 0],'linewidth',2); %绘制海岸线,填充陆地
m_grid('box','fancy') %添加边框
hold on
%添加标题
title('1988-SST','fontsize',15) %标题
%添加色标
h = colorbar('h');
set(get(h,'title'),'string','degC');
hold on