Skip to content

Commit e13337f

Browse files
authored
Merge pull request #169 from magnusuMET/netcdfreqsdocs
Add docs on installing netcdf
2 parents e3a981e + 2ba5e89 commit e13337f

File tree

5 files changed

+64
-26
lines changed

5 files changed

+64
-26
lines changed

.typos.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ extend-exclude = ["netcdf-src/source", "netcdf-sys"]
55
extend-ignore-identifiers-re = [
66
"Dout",
77
"is_ambigous", # Remove when deprecated item is removed
8+
"typ",
89
]

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ All variable data is read into a contiguous buffer, or into an [ndarray](https:/
3131

3232
## Building
3333

34-
This crate depends on `libnetcdf`, but a static build from source is also supported, which can be enabled using the `static` feature.
34+
This crate depends on the C library [`netcdf-c`](https://www.unidata.ucar.edu/netcdf/) which must be installed on the machine, along with libraries such as `hdf5` which `netcdf-c` depends on. For some platforms you might have to set the environment variable `NETCDF_DIR` which needs to point at the installation of `netcdf-c` you wish to use.
3535

36-
The crate is built on several platforms using github actions, and is currently known to build form from source on all major platforms (linux, macos, windows (gnu+msvc)), and through the package installers `conda` and `apt`.
36+
An alternative to using the system libraries is to enable `static` feature of this crate (`cargo add netcdf --features static`), which compiles `libnetcdf` from source. The `static` feature requires `cmake`, a `c++` compiler and more to be installed on the build machine.
3737

38-
If during compilation there is an error in building the `hdf5` crate, consider using the `static` feature which will include a compatible version of both `netcdf` and `hdf5`. This is likely to be an issue [upstream](https://github.com/aldanor/hdf5-rust/issues/262).
38+
The crate is built on several platforms using github actions, and is currently known to build form from source on all major platforms (linux, macos, windows (gnu+msvc)), and through the package installers `conda` and `apt`. Please see the github workflows for tips on how to install `netcdf`.
3939

40-
### Building without `libnetcdf` or building statically
40+
41+
### Building `libnetcdf` statically
4142
1. `git clone https://github.com/georust/netcdf`
4243
2. `git submodule update --init --recursive`
4344
3. `cargo build --features static`

netcdf-sys/build.rs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,18 @@ fn from_utf8_to_trimmed_string(bytes: &[u8]) -> String {
172172
}
173173

174174
impl NcInfo {
175-
fn guess() -> Self {
176-
todo!()
177-
}
178-
fn from_path(path: &Path) -> Self {
179-
Self {
175+
fn from_path(path: &Path) -> Option<Self> {
176+
let includedir = path.join("include");
177+
if !includedir.exists() {
178+
return None;
179+
}
180+
181+
Some(Self {
180182
version: None,
181183
includedir: path.join("include"),
182184
libdir: path.join("lib"),
183185
libname: "netcdf".to_owned(),
184-
}
186+
})
185187
}
186188
fn gather_from_ncconfig(search_path: Option<&Path>) -> Option<Self> {
187189
let path = if let Some(search_path) = search_path {
@@ -232,6 +234,40 @@ fn _check_consistent_version_linked() {
232234
todo!()
233235
}
234236

237+
fn determine_ncinfo() -> NcInfo {
238+
let nc_dir = std::env::var_os("NETCDF_DIR")
239+
.or_else(|| std::env::var_os("NetCDF_DIR"))
240+
.map(PathBuf::from);
241+
242+
if let Some(nc_dir) = nc_dir.as_ref() {
243+
#[allow(unused_mut)]
244+
if let Some(info) = NcInfo::gather_from_ncconfig(Some(nc_dir)) {
245+
return info;
246+
}
247+
if let Some(info) = NcInfo::from_path(nc_dir) {
248+
return info;
249+
}
250+
#[cfg(windows)]
251+
{
252+
// Conda requires Library prefix
253+
let nc_dir = nc_dir.join("Library");
254+
if let Some(info) = NcInfo::gather_from_ncconfig(Some(&nc_dir)) {
255+
return info;
256+
}
257+
if let Some(info) = NcInfo::from_path(&nc_dir) {
258+
return info;
259+
}
260+
}
261+
}
262+
263+
if let Some(info) = NcInfo::gather_from_ncconfig(None) {
264+
return info;
265+
}
266+
267+
panic!("A system version of libnetcdf could not be found. Consider installing to some default location, use the environment variable NETCDF_DIR (remember to restart the shell), or prefer building the static version of libnetcdf by enabling the `static` feature on `netcdf-sys` or `netcdf`"
268+
);
269+
}
270+
235271
fn main() {
236272
println!("cargo::rerun-if-changed=build.rs");
237273

@@ -240,26 +276,19 @@ fn main() {
240276
let netcdf_lib = std::env::var("DEP_NETCDFSRC_LIB").unwrap();
241277
let netcdf_path = PathBuf::from(std::env::var_os("DEP_NETCDFSRC_SEARCH").unwrap());
242278

243-
info = NcInfo::gather_from_ncconfig(Some(&netcdf_path.join("..")))
244-
.unwrap_or_else(|| NcInfo::from_path(&netcdf_path.join("..")));
279+
let mut linfo = NcInfo::gather_from_ncconfig(Some(&netcdf_path.join("..")));
280+
if linfo.is_none() {
281+
linfo = NcInfo::from_path(&netcdf_path.join(".."))
282+
}
283+
284+
info = linfo.expect("The library path should be determined by this point");
245285

246286
println!("cargo::rustc-link-search=native={}", netcdf_path.display());
247287
println!("cargo::rustc-link-lib=static={netcdf_lib}");
248288
} else {
249289
println!("cargo::rerun-if-env-changed=NETCDF_DIR");
250290

251-
let nc_dir = std::env::var_os("NETCDF_DIR")
252-
.or_else(|| std::env::var_os("NetCDF_DIR"))
253-
.map(PathBuf::from);
254-
255-
#[cfg(windows)]
256-
let nc_dir = nc_dir.map(|d| d.join("Library"));
257-
258-
info = if let Some(nc_dir) = nc_dir.as_ref() {
259-
NcInfo::gather_from_ncconfig(Some(nc_dir)).unwrap_or_else(|| NcInfo::from_path(nc_dir))
260-
} else {
261-
NcInfo::gather_from_ncconfig(None).unwrap_or_else(NcInfo::guess)
262-
};
291+
info = determine_ncinfo();
263292

264293
println!("cargo::rustc-link-search={}", info.libdir.display());
265294
println!("cargo::rustc-link-lib={}", &info.libname);

netcdf/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@
1414
//!
1515
//! For more information see:
1616
//! * [The official introduction to `netCDF`](https://docs.unidata.ucar.edu/nug/current/netcdf_introduction.html)
17-
//! * [The `netCDF-c` repository](https://github.com/Unidata/netcdf-c)
17+
//! * [The `NetCDF-c` repository](https://github.com/Unidata/netcdf-c)
18+
//!
19+
//! # Installing netcdf-c
20+
//!
21+
//! This crate depends on [Unidata NetCDF-c](https://github.com/Unidata/netcdf-c) and dependencies
22+
//! of this library (such as hdf5).
23+
//! An alternative to the system libraries is to use the bundled sources of netcdf by using the `static` feature of this crate. This will require build utilities such as `cmake`, `c++` compiler and more.
24+
//!
1825
//!
1926
//! # Examples
2027
//!

netcdf/src/variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::utils::{checked_with_lock, with_lock};
1919
///
2020
/// This type is used for retrieving data from a variable.
2121
/// Metadata on the `netCDF`-level can be retrieved using e.g.
22-
/// [`fill_value`](Self::fill_value), [`endinanness`](Self::endianness).
22+
/// [`fill_value`](Self::fill_value), [`endianness`](Self::endianness).
2323
/// Use [`attributes`](Self::attribute) to get additional metadata assigned
2424
/// by the data producer. This crate will not apply any of the transformations
2525
/// given by such attributes (e.g. `add_offset` and `scale_factor` are NOT considered).

0 commit comments

Comments
 (0)