用Matlab读取NetCDF文件部分地区的数据

NetCDF文件的数据读取网上也有很多相应的教程,可以参考

然而在我想提取 某个地区或者是某个范围(已知经纬度的情况下) 的数据时,网上的教程还挺少的。

然而还是给我在找一篇用R语言处理NetCDF文件的博客的评论找到了方法(怪我懒得用matlab的help来研究)

结合两篇博客,我们知道可以用ncread来提取部分地区的数据

>> help ncread
 ncread Read variable data from a NetCDF source.
 
     VARDATA = ncread(FILENAME,VARNAME) reads data from the variable
     VARNAME in the NetCDF file FILENAME.
 
     VARDATA = ncread(OPENDAP_URL,VARNAME) reads data from the variable
     VARNAME from an OPeNDAP NetCDF data source.
 
     VARDATA = ncread(SOURCE,VARNAME,START, COUNT) 
     VARDATA = ncread(SOURCE,VARNAME,START, COUNT, STRIDE) reads data from
     VARNAME beginning at the location given by START from SOURCE, which
     can either be a filename or an OPeNDAP URL. For an N-dimensional
     variable START is a vector of 1-based indices of length N specifying
     the starting location. COUNT is also a vector of length N specifying
     the number of elements to read along corresponding dimensions. If a
     particular element of COUNT is Inf, data is read until the end of that
     dimension. The optional argument STRIDE specifies the inter-element
     spacing along each dimension. STRIDE defaults to a vector of ones.
     
     The MATLAB datatype of VARDATA will be the closest type to the
     corresponding NetCDF datatype. VARDATA will be of type double, if at
     least one of '_FillValue', 'scale_offset' and 'add_offset' variable
     attribute is present. The following attribute conventions are applied
     in sequence to VARDATA if the corresponding attribute exists for this
     variable:
 
        1. Values in VARDATA equal to the '_FillValue' attribute value are
           replaced with NaNs. If '_FillValue' attribute does not exist,
           ncread will query the library for the variable's fill value.
        2. VARDATA is multiplied by the value of 'scale_factor' attribute.
        3. The value of the 'add_offset' attribute is added to VARDATA.
     
 
     Example: Read and display the 'peaks' data in the example file.
        ncdisp('example.nc','peaks');
        peaksData  = ncread('example.nc','peaks');
        peaksDesc  = ncreadatt('example.nc','peaks','description');
        surf(double(peaksData));
        title(peaksDesc);
 
     Example: Subsample the 'peaks' data by a factor of 2.
        subsetdata = ncread('example.nc','peaks',...
                            [1 1], [Inf Inf], [2 2]);
        surf(double(subsetdata));
 
 
    See also ncdisp, ncreadatt, ncinfo, ncwrite, netcdf.

    ncread 的参考页

1. 用法

VARDATA = ncread(SOURCE,VARNAME,START, COUNT)
  • SOURCE:文件名
  • VARNAME:变量名
  • START:从第几个开始数,具体看nc文件的数据而定
  • COUNT:一共要数多少个

2. 例子

>> filename='sst.nc';
>> ncdisp(filename)
Source:
           D:\test\sst.nc
Format:
           classic
Global Attributes:
           Title                     = 'Monthly version of HadISST sea surface temperature component'
           description               = 'HadISST 1.1 monthly average sea surface temperature'
           institution               = 'Met Office Hadley Centre'
           source                    = 'HadISST'
           reference                 = 'Rayner, N. A., Parker, D. E., Horton, E. B., Folland, C. K., Alexander, L. V., Rowell, D. P., Kent, E. C., Kaplan, A.  Global analyses of sea surface temperature, sea ice, and night marine air temperature since the late nineteenth century J. Geophys. Res.Vol. 108, No. D14, 4407 10.1029/2002JD002670'
           Conventions               = 'CF-1.0'
           history                   = '7/2/2020 converted to netcdf from pp format'
           supplementary_information = 'Updates and supplementary information will be available from http://www.metoffice.gov.uk/hadobs/hadisst'
           comment                   = 'Data restrictions: for academic research use only. Data are Crown copyright see (http://www.opsi.gov.uk/advice/crown-copyright/copyright-guidance/index.htm)'
Dimensions:
           time      = 1800  (UNLIMITED)
           latitude  = 180
           longitude = 360
           nv        = 2
Variables:
    time     
           Size:       1800x1
           Dimensions: time
           Datatype:   single
           Attributes:
                       units         = 'days since 1870-1-1 0:0:0'
                       calendar      = 'gregorian'
                       long_name     = 'Time'
                       standard_name = 'time'
    time_bnds
           Size:       2x1800
           Dimensions: nv,time
           Datatype:   single
    latitude 
           Size:       180x1
           Dimensions: latitude
           Datatype:   single
           Attributes:
                       units         = 'degrees_north'
                       long_name     = 'Latitude'
                       standard_name = 'latitude'
    longitude
           Size:       360x1
           Dimensions: longitude
           Datatype:   single
           Attributes:
                       units         = 'degrees_east'
                       long_name     = 'Longitude'
                       standard_name = 'longitude'
    sst      
           Size:       360x180x1800
           Dimensions: longitude,latitude,time
           Datatype:   single
           Attributes:
                       _FillValue    = -1.000000015047466e+30
                       standard_name = 'sea_surface_temperature'
                       long_name     = 'sst'
                       units         = 'C'
                       cell_methods  = 'time: lat: lon: mean'
                       missing_value = -1.000000015047466e+30
test=ncread(filename,'sst',[90,1,1],[90,60,1])
  • filename:文件名
  • 'sst':变量名
  • [90,1,1]:计数起始位置。分别对应longitude、latitude、time的起始位置,即经度从第90个数据开始,纬度从第1个数据开始,时间从第1个时间开始
  • [90,60,1]:一共要数多少个。分别对应longitude、latitude、time的要提取的个数,即经度从第90个数据开始读90个,纬度从第1个数据开始读60个,时间从第1个数据开始读1个,即一个月的数据
  • 4
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值