MATLAB nctoolbox使用

来源:互联网 发布:mac 桌面隐藏硬盘 编辑:程序博客网 时间:2024/06/10 18:31


nctoolbox is a Matlab toolbox that provides read-only access to common data model datasets. Under the hood, nctoolbox uses NetCDF-Javaas the data access layer. This allows nctoolbox to access NetCDF, OPeNDAP, HDF5, GRIB, GRIB2, HDF4 and many (15+) other file formats and services using the same API. It works with Matlab 2008a and later.


下载地址:https://pan.baidu.com/s/1nuMhvbb


之前一直没注意下载的地址是google,国内没法访问,因此重新访问后下载了最新的2013版,并上传到百度云,又需要的可以下载试试。


官方wiki:(来源https://code.google.com/archive/p/nctoolbox/wikis,请翻墙查看)

1.Install.wiki


Download NCTOOLBOX

Most users: standard version:

  1. Download the nctoolbox zip file
  2. Extract the downloaded zip file somewhere
  3. Install NCTOOLBOX (see below)

    Risk takers: development version:

  4. Download a recent zipfile from https://github.com/acrosby/nctoolbox_recent/zipball/master

  5. Extract the downloaded zip file somewhere
  6. Install NCTOOLBOX (see below)or if you want to make sure ensure you have the latest, bleeding edge code:
  7. Get a Mecurial (hg) client from the Mercurial web site
  8. Clone the source code repository:hg clone http://code.google.com/p/nctoolbox/ nctoolbox
  9. After the first time you clone, you can update to the latest source by doing the following:hg pull http://code.google.com/p/nctoolbox/hg update
  10. Install NCTOOLBOX (see below)

    Install NCTOOLBOX

  11. In Matlab, change to the nctoolbox directory. For example:```

    cd ~/Documents/Matlab/nctoolbox ```

  12. In Matlab, run the setup_nctoolbox function```

    setup_nctoolbox ```

  13. If you want nctoolbox to be available each time you start Matlab, edit startup.m:```

    edit startup.m and add these lines: addpath('c:/change-to-your-path/nctoolbox') setup_nctoolbox ```


2.Documention.wiki


Quick Start

  • Run and examine the demos in the nctoolbox/demos directory
  • Tutorial on reading data in NCTOOLBOX using the NCGEODATASET class. This is the way most people will want to read data, for the reasons described below.

The Rest of the Story

There are several dataset classes that can be used to read data. Each provides different types of functionality. All dataset classes provide access to variable data, list of variable names, and netcdf attributes as well as the underlying netcdf-java dataset object. Most users will want to use NCGEODATASET, the most recent class, since this the only class that gives access to thegeovariable class. This allows subsetting by values of lat/lon, by windows of time and the ability to return coordinates that use standard names of time, lat, lon, z for every dataset variable(and even performs coordinate conversions). This class also has a method to pull attributes from a netcdf file into a convenient matlab structure and another to pull the extents of the coordinates. The older NCDATASET and CFDATASET classes are retained mostly for backwards compatibility.

  • nctoolbox Matlab Classes
    • NCDATASET - Provides basic access to data via start/stop indices.
      • Tutorial
      • Overview of methods and functions
    • CFDATASET - Provides additional capabilities for fetching data from CF and COARDS compliant datasets
      • Tutorial
      • Overview of methods and functions
    • NCGEODATASET - Provides a wide variety of higher level functions that make use of the Unidata Common Data Model for interoperability
      • Tutorial
      • Overview of methods and functions


3.UsingNcdataset.wiki

Opening a Dataset

```

ds = ncdataset('http://dods.mbari.org/opendap/data/ssdsdata/deployments/m0/200706/OS_MBARI-M0_20070621_R_TS.nc')

ds =

ncdataset handle

Properties: netcdf: [1x1 ucar.nc2.dataset.NetcdfDataset] variables: {12x1 cell}

Methods, Events, Superclasses```

Q: What's the netcdf property?

A: That's the Java object used to do the IO. If you're familiar with Java you can use this object, via theNetcdf Java API, to do all kinds of crazy stuff. If you don't know Java you can pretty much ignore the netcdf property. One nice thing is that it can be used to dump out theCDL and show you everything about the dataset. For example:

```

ds = ncdataset('http://dods.mbari.org/opendap/data/ssdsdata/deployments/m0/200706/OS_MBARI-M0_20070621_R_TS.nc'); ds.netcdf

ans =

netcdf dods://dods.mbari.org/cgi-bin/nph-nc/data/ssdsdata/deployments/m0/200706/OS_M0_20070621_TS.nc { dimensions: LONGITUDE = 1; LATITUDE = 1; DEPTH = 5; bnds = 2; TIME = UNLIMITED; // (23510 currently) variables: double DEPTH_bnds(DEPTH=5, bnds=2); float PSAL(TIME=23510, DEPTH=5, LATITUDE=1, LONGITUDE=1); :_CoordinateAxes = "TIME DEPTH LATITUDE LONGITUDE "; :missing_value = -1.0E34f; // float :_FillValue = -1.0E34f; // float :long_name = "Hourly sea_water_salinity"; :units = " "; :standard_name = "sea_water_salinity"; :uncertainty = "0.02"; :valid_min = "30"; :valid_max = "36"; :cell_methods = "time: mean"; :history = "From m0_ctd0001_20070621_original";

```

Q: What's the variables property?

A: Thats a cell array of strings. Each cell contains the name of a variable found in thencdataset object. You need those names to get to the data. (more about that later). To quickly see what variables your dataset has, you can do this:

```

ds = ncdataset('http://dods.mbari.org/opendap/data/ssdsdata/deployments/m0/200706/OS_MBARI-M0_20070621_R_TS.nc'); ds.variables

ans =

'DEPTH_bnds''PSAL''PSAL_QC''TEMP''TEMP_QC''TIME_QC''POSITION_QC''DEPTH_QC''LONGITUDE''LATITUDE''DEPTH''TIME'

```


Reading Data

To read data from the dataset you need to supply a variable name to the data method, either explicitly (i.e. you type it in) or by accessing the values in thevariables property (If you don't know what variables property means go back to the top of the page and start reading from there). Here's an example:

```

ds = ncdataset('http://dods.mbari.org/opendap/data/ssdsdata/deployments/m0/200706/OS_MBARI-M0_20070621_R_TS.nc') temp = ds.data('TEMP'); % Explicit psal = ds.data(ds.variables{2}) % Accessing 'PSAL' using variables property ```

Q: What if I don't want to read all the data in a variable? (i.e. How do I fetch a subset of data)

A: That's a great question! Sometimes the data is simply too big to pull in all at once or you might only need the last week of a 10 year time-series. Fortunately, it's easy to retrieve a subset of data. First, you need to know the size (what the NetCDF API calls shape) of the variable. Here's an example of getting the size:

```

ds = ncdataset('http://geoport.whoi.edu/thredds/dodsC/coawst/fmrc/coawst_2_best.ncd'); ds.size('temp')

ans =

    4672          16         336         896

```

Calling size does not fetch the data; it reads the info from the CDL, which you've already fetched. So no matter how big the dataset is callingsize is fast.

Once you know the shape, you can use the information to specify the first data point or slice to retrive. If needed, you can provide alast argument (data from first until last will be fetched). You can also specify astride argument that tells ncdataset to skip data and only grab the nth point along a particular axis. Here's some examples:

```

firstIdx = [4672 16 1 1]; lastIdx = [4672 16 336 896]; t = ds.data('temp', firstIdx, lastIdx); % default stride here is [1 1 1 1] size(t)

ans =

 1     1   336   896

```

Notice the size of t in the above example is 1 1 336 896. Matlabhates those singleton dimensions; they're easy to get rid of though:

```

t2 = squeeze(t); size(t2)

ans =

336 896```

Q: I fetched the data like your example, but Matlab complains with "Warning: CData must be double or uint8"

Matlab prefers data to be double precision and most Matlab functions will complain if you try to use other types, such as uint16, unit32, unit64, int8, int16, int32, int64 and single. However,ncdataset reads the data pretty much as it's stored in the dataset. If the dataset stores data asfloats (32-bit precision, or what Matlab calls single), then ncdataset reads the data as float. It's very easy to convert to thedouble precision data using the Matlab function double. Here's an example:

```

ds = ncdataset('http://geoport.whoi.edu/thredds/dodsC/coawst/fmrc/coawst_2_best.ncd'); tSingle = ds.data('temp', [4672 16 1 1], [4672 16 336 896]); tDouble = double(tSingle); % This converts anything to double ```


Probing a Dataset's Metadata

Attributes

Most dataset's have metadata attached to them. In NetCDF terms, these are known asattributes. A dataset may have both global attributes, that describe the entire dataset, andvariable attributes that describe an individual variable. You can read both with thencdataset.attribute method. This method returns a 2xN (that means 2 columns and any number of rows) cell array. The first column contains the name of the attribute, the second column contains the corresponding value for that attribute. Here's an example of fetching the global attributes:

```

ds = ncdataset('http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/ncep.marine/sst.mean.nc'); ds.attributes

ans =

'platform'       'Ship Observations'    'title'          'NCEP Real-time Marine''history'                   [1x223 char]'Conventions'    'COARDS'

```

In the same vein, if you want to fetch the variable attributes you just use the name of the variable as an argument:

```

ds.attributes('sst')

ans =

'_CoordinateAxes'    'time lat lon '                                  'dataset'                                                [1x23 char  ]'var_desc'                                               [1x25 char  ]'level_desc'                                             [1x9  char  ]'statistic'                                              [1x6  char  ]'parent_stat'                                            [1x16 char  ]'actual_range'                                           [2x1  single]'precision'          [                                              2]'units'              'degC'                                           'long_name'          'Sea Surface Temperature Monthly Mean at Surface

```

If you are searching for a particular attribute, such as units, you can use thevalue4key function to fetch the value:

```

a = ds.attributes('sst'); value4key(a, 'units')

ans =

degC```

Axes

Many variables in a dataset have axes that defines the data's coordinates. Axes are very helpful for knowing things like when and where a particular point of data was measured. For example, a temperature variable will often have a time variable and possibly a latitude and longitude as it's axes. ncdataset provides basic access to these axes names for data files that followCOARDS conventions. The names returned by ncdataset.axes method are variables in thencdataset that represent an axis for you variable (got it). Here's an example to clear things up:

```

ds = ncdataset('http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/ncep.marine/sst.mean.nc'); ds.axes('sst')

ans =

'time''lat''lon'

```

Notice that the axes names above are all variables in the ncdataset. Just to refresh your memory, here's the variables in our dataset.

```

ds.variables

ans =

'sst''lat''lon''time'

```

The axes are returned in order to a dimension of you variable. Notice the size of 'sst':

```

ds.axes('sst')

ans =

'time''lat''lon'

ds.size('sst')

ans =

     230          90         180

```

In this example, the first dimension of sst, with a length of 230, has an axis defined by the 'time' variable. The second axes, with a length of 90, is 'lat'. Finally the 3rd axes, with 180 data points in it, is 'lon'. Let's put it all together:```

sst = ds.data('sst', [230 1 1]); % Grab one day of data sst = squeeze(double(sst)); % Make it a datatype matlab functions like t = ds.time('time', ds.data('time', [230], [230]))); lat = ds.data('lat'); lon = ds.data('lon'); surf(lon, lat, sst); shading('interp');view(2); title(datestr(t)); xlabel(value4key(ds.attributes('lon'), 'units')) ylabel(value4key(ds.attributes('lat'), 'units')) ```


Dealing with Time Variables

Many datasets will contain at least one time variable. Unfortunately, the creators of the dataset you're using often use time units that arecapricious arbitrary http://code.google.com/p/nctoolbox/issues/detail?id=6&can=1'>evil chosen for domain specific reasons. Ideally, since you are in Matlab, you would like all your time units to be inMatlab's native time format.

ncdataset provides a convenience method for converting time to Matlab time. It does this by parsing theunits attribute on a variable. Let's take a look at a dataset with a time variable:

```

ds = ncdataset('http://www.marine.csiro.au/dods/nph-dods/dods-data/ocean_colour/seawifs/global/8_day/netcdf/f_L3m_8D_T865_9_2007.nc') ds.variables

ans =

'flag''t865''lon''lat''time'

value4key(ds.attributes('time'), 'units')

ans =

seconds since 1980-01-01 00:00:0.0```

Aha, let's fetch the time variable, with units of seconds since 1980-01-01 00:00:0.0, as Matlab time ...

```

t = ds.time('time'); datestr(t(1)) % Using Matlabs build in time handling functions

ans =

05-Jan-2007 00:20:11

datestr(t(end))

ans =

29-Dec-2007 12:07:42```

Q: What if I only want to fetch and convert a subset of the time data?

A: It is possible to fetch a subset, but it's a 2 step process where you: 1. Fetch the subset of data 1. Convert the data you fetched

```

t2 = ds.data('time', [1], [4]); % Fetch subset t3 = ds.time('time', t2); % convert fetched data. the variable id 'time' is needed to find the conversion units datestr(t3)

ans =

05-Jan-2007 00:20:1113-Jan-2007 00:48:4620-Jan-2007 23:36:1929-Jan-2007 00:03:26```




0 2
原创粉丝点击