Blame view

readme.md 3 KB
5ab287f2   Nathanael Jourdane   First commit
1
2
# CDF tools

ef9a8767   Nathanael Jourdane   Add the CDF specs
3
## NetCDF to CDF converter for AMDADB
5ab287f2   Nathanael Jourdane   First commit
4
5
6
7
8
9
10
11
12
13

- File: [nc2cdf.py](./nc2cdf.py)
- Python interpreter: 3.6

### CLI usage

#### Converting a Net-CDF file:

Convert the NetCDF file, save it in a temp directory, then display its path:

ef9a8767   Nathanael Jourdane   Add the CDF specs
14
15
16
```bash
./nc2cdf.py path/to/input_file.nc.gz
```
5ab287f2   Nathanael Jourdane   First commit
17
18
19

Convert a Net-CDF file and save it in the specified path.

ef9a8767   Nathanael Jourdane   Add the CDF specs
20
21
22
```bash
./nc2cdf.py path/to/input_file.nc.gz path/to/output_file.cdf
```
5ab287f2   Nathanael Jourdane   First commit
23

ef9a8767   Nathanael Jourdane   Add the CDF specs
24
**Note:** If the specified input file is a gzip archive, it will be automatically extracted in a temp directory before the conversion.
5ab287f2   Nathanael Jourdane   First commit
25
26
27

#### Describing a NetCDf file:

ef9a8767   Nathanael Jourdane   Add the CDF specs
28
29
30
```bash
./nc2cdf.py  -i path/to/file.nc.gz
```
5ab287f2   Nathanael Jourdane   First commit
31
32
33

This display information about a Net-CDF file (such as global attributes and variables information).

ef9a8767   Nathanael Jourdane   Add the CDF specs
34
### Python usage
5ab287f2   Nathanael Jourdane   First commit
35

ef9a8767   Nathanael Jourdane   Add the CDF specs
36
37
```python
import nc2cdf
5ab287f2   Nathanael Jourdane   First commit
38

ef9a8767   Nathanael Jourdane   Add the CDF specs
39
netcdf = NetCdf('path/to/input_file.nc.gz')
5ab287f2   Nathanael Jourdane   First commit
40

ef9a8767   Nathanael Jourdane   Add the CDF specs
41
netcdf.describe()
5ab287f2   Nathanael Jourdane   First commit
42

ef9a8767   Nathanael Jourdane   Add the CDF specs
43
44
netcdf.get_cdf()
print('CDF path: ' + netcdf.get_cdf_path())
5ab287f2   Nathanael Jourdane   First commit
45

ef9a8767   Nathanael Jourdane   Add the CDF specs
46
47
netcdf.get_cdf('path/to/output_file.cdf')
```
5ab287f2   Nathanael Jourdane   First commit
48

ef9a8767   Nathanael Jourdane   Add the CDF specs
49
## Dependencies
5ab287f2   Nathanael Jourdane   First commit
50

ef9a8767   Nathanael Jourdane   Add the CDF specs
51
- NetCDF4
5ab287f2   Nathanael Jourdane   First commit
52
53
54

[NetCDF](https://www.unidata.ucar.edu/software/netcdf/) is C library to read and edit NetCDF files.

ef9a8767   Nathanael Jourdane   Add the CDF specs
55
[NetCDF4](https://github.com/Unidata/netcdf4-python) is a Python wrapper for NetCDF, which requires the NetCDF library, used here to read NetCDF files.
5ab287f2   Nathanael Jourdane   First commit
56
57
58

Documentation is available [here](http://unidata.github.io/netcdf4-python/).

ef9a8767   Nathanael Jourdane   Add the CDF specs
59
- pycdf
5ab287f2   Nathanael Jourdane   First commit
60
61
62
63
64

[SpacePy](http://pythonhosted.org/SpacePy/index.html) is a python package for space sciences, used here to write CDF files.

Documentation of the package spacepy.pycdf is available [here](http://pythonhosted.org/SpacePy/pycdf.htm).

ef9a8767   Nathanael Jourdane   Add the CDF specs
65
### Installing the Python environment and dependencies
5ab287f2   Nathanael Jourdane   First commit
66
67
68

We will install dependencies in Python environments.

ef9a8767   Nathanael Jourdane   Add the CDF specs
69
#### Case 1: If you have NetCDF installed on your machine
5ab287f2   Nathanael Jourdane   First commit
70
71
72

You can use pip and virtualenv:

ef9a8767   Nathanael Jourdane   Add the CDF specs
73
74
75
76
77
78
```bash
pip install virtualenv
virtualenv -p python3 nc2cdf
source nc2cdf/bin/activate # Or ". nc2cdf/bin/activate.fish" on Fish terms
pip install -r pip_req_nc2cdf.txt
```
5ab287f2   Nathanael Jourdane   First commit
79

ef9a8767   Nathanael Jourdane   Add the CDF specs
80
#### Case 2: If you don't have NetCDF installed on your machine
5ab287f2   Nathanael Jourdane   First commit
81
82
83

The easier way is to use [Anaconda](https://docs.continuum.io/), which is a tool to install compiled Python dependencies in environments.

ef9a8767   Nathanael Jourdane   Add the CDF specs
84
85
86
First, [install Anaconda3](https://docs.continuum.io/anaconda/install).

Then edit your system startup file:
5ab287f2   Nathanael Jourdane   First commit
87
88
89

I recommend to add an alias which set the Python path. In this way the Anaconda Python will not be used by default and you can easily deal with multiple Anaconda versions.

ef9a8767   Nathanael Jourdane   Add the CDF specs
90
Add at the end of your `~/.bashrc`:
5ab287f2   Nathanael Jourdane   First commit
91

ef9a8767   Nathanael Jourdane   Add the CDF specs
92
93
94
```bash
alias conda3="set PATH $HOME/.anaconda3/bin/ $PATH"
```
5ab287f2   Nathanael Jourdane   First commit
95
96
97

Or on Fish terms (`~/.config/omf/init.fish`):

ef9a8767   Nathanael Jourdane   Add the CDF specs
98
99
100
```bash
alias conda3="set PATH $HOME/.anaconda3/bin/ $PATH; and source $HOME/.anaconda3/etc/fish/conf.d/conda.fish
```
5ab287f2   Nathanael Jourdane   First commit
101

ef9a8767   Nathanael Jourdane   Add the CDF specs
102
Now create the environment:
5ab287f2   Nathanael Jourdane   First commit
103

ef9a8767   Nathanael Jourdane   Add the CDF specs
104
```bash
5ab287f2   Nathanael Jourdane   First commit
105
106
conda3
conda create -f conda_env_nc2cdf.yml
ef9a8767   Nathanael Jourdane   Add the CDF specs
107
```
5ab287f2   Nathanael Jourdane   First commit
108

ef9a8767   Nathanael Jourdane   Add the CDF specs
109
To finish, activate the Conda environment:
5ab287f2   Nathanael Jourdane   First commit
110

ef9a8767   Nathanael Jourdane   Add the CDF specs
111
```bash
5ab287f2   Nathanael Jourdane   First commit
112
source activate nc2cdf # or only "activate nc2cdf" on Fish terms
ef9a8767   Nathanael Jourdane   Add the CDF specs
113
114
115
```

You can now use the converter.
5ab287f2   Nathanael Jourdane   First commit
116
117
118
119
120
121

### Licence

- License: [GPLv3](https://www.gnu.org/licenses/gpl-3.0.html);
- Credits: CNRS/IRAP;
- contact: nathanael.jourdane@irap.omp.eu