Skip to content

Commit

Permalink
Version 2.0. Rewrite the structure of how requests are made. Closes 1…
Browse files Browse the repository at this point in the history
…. Closes 2.
  • Loading branch information
rpurser47 committed Jun 19, 2022
1 parent e2e6134 commit a8bf8a7
Show file tree
Hide file tree
Showing 40 changed files with 804 additions and 123 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
*.mltbx
doc/html/*.html
**/*.nc
**/*.csv
**/*.zip
**/*.grib
2 changes: 1 addition & 1 deletion CONTRIBUTING.MD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Thank you for your interest in contributing to a MathWorks repository! We encou

## Guidelines

We don't have best practices for writing MATLAB code, but we do have some recommendations:
We don't have best practices for writing MATLAB® code, but we do have some recommendations:

* You should not have any warnings or errors in the [code analyzer report](http://www.mathworks.com/help/matlab/matlab_prog/matlab-code-analyzer-report.html)
* [Loren Shure's blog](https://blogs.mathworks.com/loren) has [great advice on improving your MATLAB code](https://blogs.mathworks.com/loren/category/best-practice/)
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ MATLAB® Tools to access [The Climate Data Store](https://cds.climate.coperni
| Function | Description |
| ------ | ------ |
| `climateDataStoreDownload` | Get data from Copernicus Climate Data Store |
| `climateDataStoreDownloadASync` | Queue a request data from Copernicus Climate Data Store and continue working in MATLAB. |

## Usage

1. See the notes below for information on first time install
2. type `help climateDataStoreDownload` for help on using the function
2. type `help climateDataStoreDownload` or `help climateDataStoreDownloadAsync` for help on using the functions
3. Find your dataset at [Climate Data Store](https://cds.climate.copernicus.eu/#!/home) and click on the "download data" tab. Make your selections for the subset of data you want. Click "show API request" at the bottom.
4. Use `climateDataStoreDownload` to get the data. The first parameter is the name of the data set to retrieve, and will be used as the name of the directory to put the downloaded files in. The second parameter is a MATLAB version of the python structure that selects what subset of the data to download. `climateDataStoreDownload` downloads the files, and returns a list of files that were downloaded. Typically, these are NetCDF files with an .nu extension, which are read using the [ncinfo](https://www.mathworks.com/help/matlab/ref/ncinfo.html) and [ncread](https://www.mathworks.com/help/matlab/ref/ncread.html) functions. Note that downloading the files can take some time, depending on how large they are.

4. Use `climateDataStoreDownload` to get the data. The first parameter is the name of the data set to retrieve. The second parameter is a MATLAB version of the python structure that selects what subset of the data to download. `climateDataStoreDownload` downloads the files, and returns a list of files that were downloaded. Note that downloading the files can take some time, depending on how large they are. If you have really large files, `climateDataStoreDownloadAsync can be helpful.

Typically, files returned are:

| File Type | Extension | MATLAB Functions |
|-----------|-----------|------------------|
| NetCDF | `.nu` | [`ncinfo`](https://www.mathworks.com/help/matlab/ref/ncinfo.html) , [`ncread`](https://www.mathworks.com/help/matlab/ref/ncread.html) |
| GRIB | `.grib` | [`ncinfo`](https://www.mathworks.com/help/matlab/ref/ncinfo.html) , [`ncread`](https://www.mathworks.com/help/matlab/ref/ncread.html) |
| text | `.txt` , `.csv` | [`readtable`](https://www.mathworks.com/help/matlab/ref/readtable.html)
## First time Install

* Requires MATLAB release R2019a or newer
Expand All @@ -33,7 +41,10 @@ This demonstrates a number of MATLAB features, including:
* [Toolbox Packaging](https://www.mathworks.com/help/matlab/matlab_prog/create-and-share-custom-matlab-toolboxes.html)
* [MATLAB Projects](https://www.mathworks.com/help/matlab/projects.html)
* [Argument validation](https://www.mathworks.com/help/matlab/matlab_prog/function-argument-validation-1.html)

* [MATLAB Classes](https://www.mathworks.com/help/matlab/object-oriented-programming.html)
* [Static Methods](https://www.mathworks.com/help/matlab/matlab_oop/static-methods.html)
* [Property Access Methods](https://www.mathworks.com/help/matlab/matlab_oop/property-access-methods.html)

## Example: Getting Started with Copernicus Climate Data Store Toolbox

[The sea ice thickness dataset](https://cds.climate.copernicus.eu/cdsapp#!/dataset/satellite-sea-ice-thickness) provides monthly gridded data of sea ice thickness for the Arctic region based on satellite radar altimetry observations. Sea ice is an important component of our climate system and a sensitive indicator of climate change. Its presence or its retreat has a strong impact on air-sea interactions, the Earth’s energy budget as well as marine ecosystems. It is recognized by the Global Climate Observing System as an Essential Climate Variable. Sea ice thickness is one of the parameters commonly used to characterize sea ice, alongside sea ice concentration, sea ice edge, and sea ice type, also available in the Climate Data Store.
Expand Down
117 changes: 47 additions & 70 deletions climateDataStoreDownload.m
Original file line number Diff line number Diff line change
@@ -1,82 +1,59 @@
function [filePaths, citation] = climateDataStoreDownload(name,options)
function [filePaths, citation] = climateDataStoreDownload(datasetName,datasetOptions,options)
%climateDataStoreDownload Get data from Copernicus Climate Data Store
% Download a data set from the Copernicus Climate Data Store.
% (https://cds.climate.copernicus.eu/cdsapp)
% Download a data set from the Copernicus Climate Data Store. (https://cds.climate.copernicus.eu/cdsapp)
%
% Find your dataset at Climate Data Store and click on the "download data"
% tab. Make your selections for the subset of data you want. Click "show
% API request" at the bottom.
% Find your dataset at Climate Data Store and click on the "download data" tab. Make your selections for the subset of data you want. Click "show
% API request" at the bottom.
%
% NAME is the name of the data set to retrieve, and will also be used as
% the name of the directory to put the downloaded files in. OPTIONS is a
% MATLAB structure matching the python structure shown when you choose
% "Show API request". The data files are downloaded, and the function
% returns FILEPATHS, a list of files downloaded. In addition, the function
% provides CITATION, which is the correct citiation to use with the data
% retrieved.
% [filePaths, citation] = climateDataStoreDownload(datasetName,datasetOptions) retrieves the data set with the name datasetName, and will also be used
% as the name of the directory or file downloaded files, with a date/time stamp added. datasetOptions is a MATLAB structure matching the python
% structure shown when you choose "Show API request". filePaths is string array of the files downloaded. The function returns FILEPATHS, a list of
% files downloaded. The function also returns citation, which is the correct citiation to use with the data retrieved.
%
% climateDataStoreDownload(...,Timeout Sets the maximum time in seconds to wait for a response.
% climateDataStoreDownload(...,DontExpandZIP=true Results that are ZIP files are not automatically expanded.
% climateDataStoreDownload(...,DontPromptForCredentials=true If no credentials are present, don't request them (intended for tests)
%
% Downloading the files can take some time, depending on how large they are.
% Downloading the files can take some time, depending on how large they are (I've had it take 30 minutes!). You can check on the status of your
% request by visiting https://cds.climate.copernicus.eu/cdsapp#!/yourrequests. For large requests, you may want to consider using
% climateDataStoreDownloadAsync, which queues a request and returns, allowing you to keep working.
%
% Notes:
% * You must have:
% * python 3.8 installed
% (https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe)
% You must have:
% * python 3.8 installed (https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe)
% * the cdsapi python package (pip3 install cdsapi)
% * Your credentials need to be in a .cdsapirc file in your user
% directory. See https://cds.climate.copernicus.eu/api-how-to for more info
% * This function relies on the Python API to access the Copernicus Climate
% Data Store (CDS) (https://github.com/ecmwf/cdsapi) by the European Centre
% for Medium-Range Weather Forecasts (ECMWF)
% * Your credentials need to be in a .cdsapirc file in your user directory. See https://cds.climate.copernicus.eu/api-how-to for more info
% * This function relies on the Python API to access the Copernicus Climate Data Store (CDS) (https://github.com/ecmwf/cdsapi) by the European
% Centre for Medium-Range Weather Forecasts (ECMWF)
%
% Example: Sea Ice thickness
% (https://cds.climate.copernicus.eu/cdsapp#!/dataset/satellite-sea-ice-thickness)
% options.version = "1_0";
% options.variable = "all";
% options.satellite = "cryosat_2";
% options.cdr_type = ["cdr","icdr"];
% options.year = ["2011","2021"];
% options.month = "03";
% downloadedFilePaths = climateDataStoreDownload('satellite-sea-ice-thickness',options);

% Copyright 2021 The MathWorks, Inc.
% Example: Sea Ice thickness (https://cds.climate.copernicus.eu/cdsapp#!/dataset/satellite-sea-ice-thickness)
% datasetOptions.version = "1_0";
% datasetOptions.variable = "all";
% datasetOptions.satellite = "cryosat_2";
% datasetOptions.cdr_type = ["cdr","icdr"];
% datasetOptions.year = ["2011","2021"];
% datasetOptions.month = "03";
% downloadedFilePaths = climateDataStoreDownload('satellite-sea-ice-thickness',datasetOptions);
%
% See Also: climateDataStoreDownloadAsync

validateattributes(name,{'string','char'},{'scalartext'});
validateattributes(options,'struct',{'scalar'});
% Copyright 2022 The MathWorks, Inc.

% Diagnostics
setupPythonIfNeeded();
setupCDSAPIIfNeeded();
arguments
datasetName (1,1) string {mustBeNonzeroLengthText}
datasetOptions (1,1) struct
options.DontExpandZIP (1,1) logical = false;
options.DontPromptForCredentials (1,1) logical = false;
options.Timeout (1,1) double {mustBePositive} = Inf;
end



% Convert the structure from strings to char and cellstr.
options = makeStringsChars(options);

% Force download option to ZIP
options.format = 'zip';

zipfilePath = string(tempname) + ".zip";

retrieveFromCDS(name,options,zipfilePath);

filePaths = string(unzip(zipfilePath,name)');
citation = "Generated using Copernicus Climate Change Service information " + string(datetime('now','Format','y'));

% Delete the temporary ZIP file
delete(zipfilePath);
end

function theStruct = makeStringsChars(theStruct)
fields = string(fieldnames(theStruct));
for iField = 1:numel(fields)
if isstring(theStruct.(fields(iField)))
if isscalar(theStruct.(fields(iField)))
% Convert scalar strings to char
theStruct.(fields(iField)) = char(theStruct.(fields(iField)));
else
% Convert string arrays to cell array of chars
theStruct.(fields(iField)) = cellstr(theStruct.(fields(iField)));
end
end
f = climateDataStoreDownloadFuture(datasetName, datasetOptions,options);
f.wait(options.Timeout);
if f.State == "failed"
throwAsCaller(f.Error)
elseif f.State ~= "completed"
throwAsCaller(MException("climateDataStore:UnexpectedState","cdsRequestState should be complete, it's %s",f.State))
end

filePaths = f.OutputArguments{1};
citation = f.OutputArguments{2};
end
61 changes: 61 additions & 0 deletions climateDataStoreDownloadAsync.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function F = climateDataStoreDownloadAsync(datasetName,datasetOptions,options)
%climateDataStoreDownload Queue a data request from Copernicus Climate Data Store
% Queue a data set request with the Copernicus Climate Data Store. (https://cds.climate.copernicus.eu/cdsapp)
%
% Find your dataset at Climate Data Store and click on the "download data" tab. Make your selections for the subset of data you want. Click "show
% API request" at the bottom.
%
% F = climateDataStoreDownloadAsync(datasetName,datasetOptions) queues a request for the data set with the name datasetName, and will also be used
% as the name of the directory or file downloaded files, with a date/time stamp added. datasetOptions is a MATLAB structure matching the python
% structure shown when you choose "Show API request". F is an object you can use to query the state of your request, and cancel the request. Note
% that if you delete F, or allow it to be cleared, your request with CDS will be cancelled. Note that the operation does not take place on a
% seperate thread -- download of files will not take place until you interact with F, either through checking Status or using wait().
%
% climateDataStoreDownloadAsync(...,DontExpandZIP=true Results that are ZIP files are not automatically expanded.
% climateDataStoreDownloadAsync(...,DontPromptForCredentials=true If no credentials are present, don't request them (intended for tests)
%
% Downloading the files can take some time, depending on how large they are (I've had it take 30 minutes!). You can check on the status of your
% request by visiting https://cds.climate.copernicus.eu/cdsapp#!/yourrequests. For simple/quick requests, you may want to consider using
% climateDataStoreDownload, which is easier to use, but blocks MATLAB.
%
% Note that many requests return immediately. You should check Status property right away. Results may be available.
%
% Notes:
% You must have:
% * python 3.8 installed (https://www.python.org/ftp/python/3.8.10/python-3.8.10-amd64.exe)
% * the cdsapi python package (pip3 install cdsapi)
% * Your credentials need to be in a .cdsapirc file in your user directory. See https://cds.climate.copernicus.eu/api-how-to for more info
% * This function relies on the Python API to access the Copernicus Climate Data Store (CDS) (https://github.com/ecmwf/cdsapi) by the European
% Centre for Medium-Range Weather Forecasts (ECMWF)
%
% Example: ERA5 hourly data on pressure levels (https://cds.climate.copernicus.eu/cdsapp#!/dataset/reanalysis-era5-pressure-levels)
% datasetName ="reanalysis-era5-pressure-levels";
% datasetOptions.product_type = "reanalysis";
% datasetOptions.format = "grib";
% datasetOptions.year = "2020";
% datasetOptions.month = "01";
% datasetOptions.day = "01";
% datasetOptions.pressure_level = "1";
% datasetOptions.variable = "divergence";
% datasetOptions.time = "06:00";
% F = climateDataStoreDownloadAsync(datasetName, datasetOptions);
% % Run whatever MATLAB code you want in here.
% F.wait();
% if F.state == "completed"
% downloadedFilePaths = OutputArguments{1};
% citation = OutputArguments{2};
% end
%
% See Also: climateDataStoreDownload, climateDataStoreDownloadFuture

% Copyright 2022 The MathWorks, Inc.

arguments
datasetName (1,1) string {mustBeNonzeroLengthText}
datasetOptions (1,1) struct
options.DontExpandZIP (1,1) logical = false;
options.DontPromptForCredentials (1,1) logical = false;
end

F = climateDataStoreDownloadFuture(datasetName,datasetOptions, options);
end
Loading

0 comments on commit a8bf8a7

Please sign in to comment.