卫星数据处理示例

下面这些示例演示如何把真实(卫星、模式等)数据读入 M_Map 并成图。这些示例涉及 HDF、netCDF 等格式的读取函数,代码主要来自原作者的实际工作脚本,其中的文件路径需要 按你的本地环境修改。

1. 全球海表温度(或任意经纬度网格上的变量)

 % NOAA/NASA Pathfinder AVHRR SST product
 % https://podaac.jpl.nasa.gov/sst/
 
 [P,map]=imread('../m_mapWK/199911h54ma-gdm.hdf');
 
 % Documentation for the 54km dataset gives
 % this formula for temperature
 P=0.15*double(P)-3; % deg C
 
 %...and defines this Lat/Long grid for the data
 Plat=90-.25-[0:359]*.5;Plon=-180+.25+[0:719]*.5;
 
 % Since the grid is rectangular in lat/long (i.e. not
 % really a projection at all, although it is included in
 % m_map under the name 'equidistant cyldindrical'), we 
 % don't want to use the 'image' technique. Instead... 
 % Create a grid, offsetting by half a grid point to account
 % for the flat pcolor
 [Plg,Plt]=meshgrid(Plon-0.25,Plat+0.25);
 
 m_proj('hammer-aitoff','clongitude',-150);
  
 % Rather than rearranging the data so its limits match the
 % plot I just draw it twice (you can see the join at 180W
 % because of the quirks of flat pcolor) (Note that 
 % all the global projections have 360 deg ambiguities)
 m_pcolor(Plg,Plt,P);shading flat;colormap(map);
 hold on;
 m_pcolor(Plg-360,Plt,P);shading flat;colormap(map);
 
 m_coast('patch',[.6 1 .6]);
 m_grid('xaxis','middle');

 % add a standard colorbar.
 h=colorbar('h');
 set(get(h,'xlabel'),'string','AVHRR SST Nov 1999');

全球海表温度(或任意经纬度网格上的变量)

2. SSM/I 冰盖(固定网格数据,HDF 格式)

%Near-Real-Time SSM/I-SSMIS EASE-Grid Daily Global Ice Concentration and 
% Snow Extent, Version 5
%
% Brodzik, M. J. and J. S. Stewart. 2016. Near-Real-Time SSM/I-SSMIS EASE-Grid 
% Daily Global Ice Concentration and Snow Extent, Version 5. Boulder, Colorado 
% USA. NASA National Snow and Ice Data Center Distributed Active Archive Center. 
% doi: https://dx.doi.org/10.5067/3KB2JPLFPK3R.

Q=hdfinfo('NISE_SSMISF18_20171124.HDFEOS')
%  Q.Attributes(2).Value says
%                UpperLeftPointMtrs=(-9036842.762500,9036842.762500)
%                LowerRightMtrs=(9036842.762500,-9036842.762500)
%                 ProjParams=(6371228,0,0,0,0,90000000,0,0,0,0,0,0,0)
%  and
%  Q.Attributes(3).Value says
%  'Data Value     Parameter
%     0              snow-free land
%     1-100          sea ice concentration percentage
%     101            permanent ice (Greenland, Antarctica)
%     102            not used
%     103            dry snow
%     104            wet snow
%     105-251        not used
%     252            mixed pixels at coastlines (unable to reliably apply microwave algorithm)
%     253            suspect ice value
%     254            corners(undefined)
%     255            ocean


P=hdfread('NISE_SSMISF18_20171124.HDFEOS','Northern Hemisphere','fields','Extent');
P(P==255)=105;   % Put ocean at top of indices
P(P>105)=0;

% According to web site this is is the projection info. I make te radius
% of my map less than the actual data field though.
m_proj('azimuthal equal-area','latitude',90,'radius',47,'rectbox','on');

 clf
% Plot data as an image
offs=9036842.762500/6371228; % Convert projection coords to units of earth radii
image([-offs offs],[offs -offs],P); set(gca,'ydir','normal');
colormap([.2 .5 .2;  % 0
           jet(100); % 1-100
           1 1 1;    % Greenland
           0 0 0  ;
           .9 .9 .9; % dry snow
           .8 .8 .8; % wet snow
           0 0 .5]); % 105 - now ocean
caxis([0 105]);

m_coast('color','k');
m_grid('linewi',2,'tickdir','out');
title({'SSM/I Ice cover Nov 24, 2017',''},'fontsize',14,'fontweight','bold');

hh=colorbar('h');
set(hh,'tickdir','out');
xlabel(hh,'% Ice cover');

SSM/I 冰盖(固定网格数据,HDF 格式)

3. UTM 网格上的航拍照片

 % This image comes from the TerraServer
 % (http://terraserver.microsoft.com/ - now greatly altered)
 % and has been georeferenced to UTM coords. The UTM projection
 % uses UTM coordinates on the screen (as long as the ellipse
 % parameter is set to something other than the default),
 [P,map]=imread('../m_mapWK/oncehome.jpeg');
 
 % Set the projection limits to the lat/long of image
 % corners.
 m_proj('UTM','long',[-71-6/60-30/3600 -71-4/60-43/3600],...
             'lat',[42+21/60+13/3600  42+22/60+7/3600],'ellipse','wgs84');
 
 clf;
 image([326400 328800],[4692800 4691200],P);set(gca,'ydir','normal');
 m_grid('tickdir','out','linewi',2,'fontsize',14);
 title('A home for certain nerds','fontsize',16);

UTM 网格上的航拍照片

4. 全球数据集的子区域(HDF 格式)

  % Ocean colour data from http://seawifs.gsfc.nasa.gov/SEAWIFS.html
  %
  % Take a 4km weakly average dataset and plot a map for the Strait of
  % Georgia and outer coast. Note that most of this code is used
  % for reading in and subsetting the data.
  
  LATLIMS=[47 51];
  LONLIMS=[-130 -121];
 
  % Note - This is probably not the most efficient way to read and
  % handle HDF data, but I don't usually do this...
  %
  % First, get the attribute data
  PI=hdfinfo('../m_mapWK/A20040972004104.L3m_8D_CHLO_4KM');
  % And write it into a structure
  pin=[];
  for k=1:59
     nm=PI.Attributes(k).Name;nm(nm==' ')='_';
     if isstr(PI.Attributes(k).Value),
          pin=setfield(pin,nm,PI.Attributes(k).Value);
      else
         pin=setfield(pin,nm,double(PI.Attributes(k).Value));
      end
  end;
  
  % lon/lat of grid corners
  lon=[pin.Westernmost_Longitude:pin.Longitude_Step:pin.Easternmost_Longitude];
  lat=[pin.Northernmost_Latitude:-pin.Latitude_Step:pin.Southernmost_Latitude];
  
  % Get the indices needed for the area of interest
  [mn,ilt]=min(abs(lat-max(LATLIMS)));
  [mn,ilg]=min(abs(lon-min(LONLIMS)));
  ltlm=fix(diff(LATLIMS)/pin.Latitude_Step);
  lglm=fix(diff(LONLIMS)/pin.Longitude_Step);
  
  % load the subset of data needed for the map limits given  
  P=hdfread('../m_map/WKA20040972004104.L3m_8D_CHLO_4KM','l3m_data',...
            'Index',{[ilt ilg],[],[ltlm lglm]});
  
  % Convert data into log(Chla) using the equations given. Blank no-data.
  P=double(P);
  P(P==255)=NaN;
  P=(pin.Slope*P+pin.Intercept);   % log_10 of chla
 
  LT=lat(ilt+[0:ltlm-1]);LG=lon(ilg+[0:lglm-1]);
  [Plg,Plt]=meshgrid(LG,LT);
 
  % FINALLY....Draw the map...
  
  clf;
  m_proj('lambert','lon',LONLIMS,'lat',LATLIMS);
  m_pcolor(Plg,Plt,P);shading flat;
  m_gshhs_i('color','k');;
  m_grid('linewi',2,'tickdir','out');;
  h=colorbar;
  set(get(h,'ylabel'),'String','Chla (\mug/l)');
  set(h,'ytick',log10([.5 1 2 3 5 10 20 30]),'yticklabel',[.5 1 2 3 5 10 20 30],...
        'tickdir','out');
  title(['MODIS Chla ' ...
         datestr(datenum(pin.Period_Start_Year,1,0)+pin.Period_Start_Day) ...
         ' -> ' ...
         datestr(datenum(pin.Period_Start_Year,1,0)+pin.Period_End_Day) ],...
         'fontsize',14,'fontweight','bold');

全球数据集的子区域(HDF 格式)

5. 气象数据(netCDF 格式)

iday=156;   % the day to show

% use ncdisp(filename) to discover file contents...

lat=ncread('uwnd.10m.gauss.2017.nc','lat');
lon=ncread('uwnd.10m.gauss.2017.nc','lon');
[LN,LT]=meshgrid(lon,lat);

mtime=ncread('uwnd.10m.gauss.2017.nc','time')/24+datenum(1800,1,1,0,0,0);
u=ncread('uwnd.10m.gauss.2017.nc','uwnd',[1,1,iday],[192,94,1]);
v=ncread('vwnd.10m.gauss.2017.nc','vwnd',[1,1,iday],[192,94,1]);
prate=ncread('prate.sfc.gauss.2017.nc','prate',[1,1,iday],[192,94,1]);

m_proj('miller','lon',[100 260],'lat',[0 65]);
m_coast('patch',[.8 .8 .8]);
hold on 
[CS,CH]=m_contourf(LN,LT,prate'*1e3,[0.05:.05:.7],'edgecolor','none');
m_windbarb(LN,LT,u' ,v',2,'units','m/s','linewi',1,'color','r');
hold off;
m_grid('box','fancy','tickdir','out');

ax=m_contfbar([.3 .7],.05,CS,CH);
set(ax,'fontsize',12)
xlabel(ax,'Mean Daily Precipitation Rate/(kg/m^2/s)');

title(['North Pacific Surface Winds : ' datestr(mtime(iday))],'fontsize',16);

colormap(flipud(m_colmap('Blues')))

气象数据(netCDF 格式)

6. ARGO 漂流浮标轨迹(netCDF 格式)

% Argo stuff
% Go to:
%         https://www.usgodae.org/cgi-bin/argo_select.pl
% Select area you want and date range, download all profiles
% tar -xvzf them into directory...
basname='./argo';

% base map
m_proj('lambert','lons',[-150 -124],'lat',[40 60],'rectbox','on');
[cs,h]=m_etopo2('contourf',[-7000:500:0],'edgecolor','none');
m_gshhs_l('patch',[.5 .8 0],'edgecolor','none');
m_grid('linewi',2,'layer','top');
caxis([-7000 000]);
m_contfbar(.92,[.2 .5],cs,h,'endpiece','no','axfrac',.02);
colormap(m_colmap('blue'));   
title('Argo float trajectories NE Pacific (2017)');

% Add ARGO float trajectories
%  1) draw a scale arrow
vecscl=0.015;
m_vec(vecscl ,-126,58,-0.01,0,'r','shaftwidth',2,'headlength',10,...
      'key',{'1 cm/s','Mean Drift'},'centered','yes');
 
dirs=dir(basname);
m=0;
for k=3:length(dirs)
   profname=dir([dirs(k).folder '/' dirs(k).name]);
   for l=3:length(profname)
       fname=[profname(l).folder '/' profname(l).name '/' profname(l).name '_Rtraj.nc'];
       %ncdisp(fname);
       
       argo.id=ncread(fname,'PLATFORM_NUMBER');
       argo.mtime=ncread(fname,'JULD')+datenum(1950,1,1);
       argo.lat=ncread(fname,'LATITUDE');
       argo.lon=ncread(fname,'LONGITUDE');
        
       % Long are stored between -180 and +180; this removes artificial
       % jumps which might happen just left of the map limits.
       argo.lon(argo.lon>0)=argo.lon(argo.lon>0)-360;
       
        
       ii=find(isfinite(argo.lon));
       if any(ii )
          m_line(argo.lon(ii),argo.lat(ii),'color',[0 0 0]);
          
          % Sometimes first point is an error of some sort so skip it
          Dlon=argo.lon(ii([2 end]));
          Dlat=argo.lat(ii([2 end]));
          t=diff(argo.mtime(ii([2 end]))); % time between the points
         
          % Distance between the points
          [d,a12]=m_idist(Dlon(1),Dlat(1),Dlon(2),Dlat(2));
          
          % Store stuff
          m=m+1;
          spd(m)=d/(t*86400) ;% m/s 
                   
          % Find midpoint on geodesic and store as well
          [Clon(m),Clat(m),a21(m)]=m_fdist(Dlon(1),Dlat(1),a12,d/2);
        end
   end
          
end
Clon=rem(Clon-360,360); % Get it into the right range
a21=rem(a21-180,360);   % I need the opposite direction

% Draw all the 'mean speed' arrows, centered at the midpoint of the
% geodesic between first and last points.
m_vec(vecscl ,Clon,Clat,spd.*sind(a21),spd.*cosd(a21),'r',...
           'centered','yes','shaftwidth',2,'headlength',10);

ARGO 漂流浮标轨迹(netCDF 格式)

7. SAR 内波影像(HDF-5 格式)

 
fname='SAR_IMP_1PNESA19920724_190439_00000018C086_00199_05354_0000.h5';
%h5disp(fname)   % See the structure

titlestr=h5readatt(fname,'/metadata/MPH','STATE_VECTOR_TIME');
datsize=double([ h5readatt(fname,'/bands/Amplitude','raster_width') ...
                 h5readatt(fname,'/bands/Amplitude','raster_height')]);

tielat=h5read(fname,'/tie_point_grids/latitude');
tielon=h5read(fname,'/tie_point_grids/longitude');
stp=[h5readatt(fname,'/tie_point_grids/latitude','sub_sampling_x') ...
     h5readatt(fname,'/tie_point_grids/latitude','sub_sampling_y') ];

% Pull out a subsection in by [2000 2500] from one corner and in 
%  [600 2000] from the opposite corner
istart=[2000 2500];
strd=[3 3];
cnt=fix([(datsize(1)-istart(1)-600)/strd(1) (datsize(2)-istart(2)-2000)/strd(2)]);
% ...and read.
subimg=h5read(fname,'/bands/Amplitude',istart,cnt,strd);

% ....smooth it a bit...
subf=filter2(ones(3,3)/9,subimg);

% Now generate lat/lon for all pixels by interpolating from
% the tie points.
Ty=[0:size(tielat,2)-1]*stp(2)+1;
Tx=[0:size(tielat,1)-1]*stp(1)+1;
Iy=istart(2)+[0:size(subimg,2)-1]*strd(2);
Ix=istart(1)+[0:size(subimg,1)-1]*strd(1);
sublat=interp2(Ty',Tx,tielat,Iy',Ix);
sublon=interp2(Ty',Tx,tielon,Iy',Ix);


% Now make the map

m_proj('lambert','lon',[-123-25/60 -122-40/60],'lat',[48+42/60 49+9/60]);
m_pcolor(sublon,sublat,subf);shading flat;
m_grid('box','fancy','tickdir','out');
m_ruler(1.03,[.15 .5],'ticklen',[.01]);
caxis([0 450]);
colormap(gray);
title(titlestr)

SAR 内波影像(HDF-5 格式)

8. 由高分辨率地形生成晕渲图(netCDF 格式)

clat=[48+46/60 49+5/60];clon=[-125-15/60 -124-55/60]; % Barkley Sound

%% Read data from a netcdf file
fname='/ocean/rich/more/mmapbase/noaa_bc3/barkley_sound_1_navd88_2016.nc';
lat=ncread(fname,'lat');
lon=ncread(fname,'lon');
ilat=lat>=clat(1) & lat=clon(1) & lon<=clon(2);
Z=ncread(fname,'Band1',[ find((ilon),1,'first') find((ilat),1,'first')],...
                       [ sum(ilon) sum(ilat)],[1 1]);


m_proj('equidistant','lon',clon,'lat',clat);   % Projection

%   The shaded relief version (right hand plot))

subplot(1,2,2);
caxis([-300 1210]);  % 1210 chosen by manual adjustment
                     % since  'waterline" appears to be at about Z=2 (vertical datum for
                     % bathymetry is 'lowest normal tide')
colormap([m_colmap('blues',32);m_colmap('gland',128)]);   % Colormap sizes chosen because...
                                                          % ... 32/128 = (300+2)/(1210-2)

m_shadedrelief(lon(ilon),lat(ilat),Z');   

m_grid('box','fancy','grid','none','fontsize',14);

m_contfbar( [.3 .7],.98, Z',[-300:1210],...
            'axfrac',.02,'endpiece','no','levels','match','edgecolor','none');          

% The normal contour version (left hand plot))

subplot(1,2,1);
m_contourf(lon(ilon),lat(ilat),Z',[-300:25:-25 2 50:50:200 300:100:1200 1210]);

caxis([-300 1210]);   
colormap([m_colmap('blues',32);m_colmap('gland',128)]);

m_grid('box','fancy','tickdir','out','grid','none','fontsize',14);

m_contfbar( [.3 .7],.98, Z',[-300:25:-25 2 50:50:200 300:100:1200 1210],...
            'axfrac',.02,'endpiece','no','levels','match');

由高分辨率地形生成晕渲图(netCDF 格式)

9. Google Maps 静态 API

 
% Axis limits 
lms=[ -123-[14 8]/60  49+[10 13.5]/60 ];   

% plot_google_maps.m from the mathworks user contributed files database:
% https://www.mathworks.com/matlabcentral/fileexchange/27627-zoharby-plot_google_map
% accesses the static google maps map tiles, returning a lat/long gridded
% image that is just bigger than the given limits.
clf;
axis(lms);
[Glon,Glat,Gimg]=plot_google_map('maptype','satellite','refresh',0,'autoaxis',0);
clf;

% Now draw it!
 
m_proj('utm','lat',lms(3:4),'lon',lms(1:2));
m_image(Glon,Glat,Gimg); 
m_grid('tickdir','out','box','fancy');
m_ruler([.4 .9],1.05,6,'tickdir','in','ticklen',.01);

Google Maps 静态 API