From 2cd4311b008f2abf17eb41b157cd23c04d6acbb8 Mon Sep 17 00:00:00 2001 From: Mike Hiley Date: Sat, 27 Jun 2015 12:01:55 -0500 Subject: [PATCH] Initializing from scratch repo --- .gitignore | 4 + .travis.yml | 11 + COPYING | 12 + Cargo.toml | 15 + LICENSE | 9 +- README.md | 56 + THIRD_PARTY | 11 + netcdf-sys/Cargo.toml | 24 + netcdf-sys/LICENSE-MIT | 25 + netcdf-sys/build.rs | 32 + netcdf-sys/src/lib.rs | 291 +++ netcdf-sys/src/ncconst.c | 180 ++ netcdf-sys/src/netcdf_v4.3.3.1.h | 1935 +++++++++++++++++ netcdf-sys/testdata/simple_xy.nc | Bin 0 -> 384 bytes src/attribute.rs | 163 ++ src/dimension.rs | 40 + src/file.rs | 78 + src/group.rs | 367 ++++ src/lib.rs | 108 + src/variable.rs | 144 ++ ...iminary_NOAA-19_asc_d20130630_c20140325.nc | Bin 0 -> 16409 bytes testdata/pres_temp_4D.nc | Bin 0 -> 2784 bytes testdata/sfc_pres_temp.nc | Bin 0 -> 1012 bytes testdata/simple_nc4.nc | Bin 0 -> 8402 bytes testdata/simple_xy.nc | Bin 0 -> 384 bytes tests/lib.rs | 487 +++++ 26 files changed, 3988 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 COPYING create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 THIRD_PARTY create mode 100644 netcdf-sys/Cargo.toml create mode 100644 netcdf-sys/LICENSE-MIT create mode 100644 netcdf-sys/build.rs create mode 100644 netcdf-sys/src/lib.rs create mode 100644 netcdf-sys/src/ncconst.c create mode 100644 netcdf-sys/src/netcdf_v4.3.3.1.h create mode 100644 netcdf-sys/testdata/simple_xy.nc create mode 100644 src/attribute.rs create mode 100644 src/dimension.rs create mode 100644 src/file.rs create mode 100644 src/group.rs create mode 100644 src/lib.rs create mode 100644 src/variable.rs create mode 100644 testdata/patmosx_v05r03-preliminary_NOAA-19_asc_d20130630_c20140325.nc create mode 100644 testdata/pres_temp_4D.nc create mode 100644 testdata/sfc_pres_temp.nc create mode 100644 testdata/simple_nc4.nc create mode 100644 testdata/simple_xy.nc create mode 100644 tests/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..70867c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +target +testout +Cargo.lock +.*.swp diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7fde4a6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: rust +rust: + - 1.0.0 + - beta + - nightly +os: + - linux + - osx +before_install: + - (test $TRAVIS_OS_NAME == "osx" || (brew update && brew install netcdf)) + - (test $TRAVIS_OS_NAME == "linux" || (sudo apt-get update -qq && sudo apt-get install -y libnetcdf-dev && sudo apt-get install -y libclang-dev)) diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..1a5fd67 --- /dev/null +++ b/COPYING @@ -0,0 +1,12 @@ +rust-netcdf is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..859c58f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "netcdf" +version = "0.1.0" +authors = ["Michael Hiley "] +license = "GPL-3.0" +description = "High-level NetCDF bindings for Rust" +repository = "https://github.com/mhiley/rust-netcdf" +documentation = "https://github.com/mhiley/rust-netcdf" +keywords = ["netcdf", "hdf", "hdf4", "hdf5", "cdm", "libnetcdf", "netcdf4"] + +[dependencies.netcdf-sys] +path = "netcdf-sys" + +[dependencies.lazy_static] +git = "https://github.com/Kimundi/lazy-static.rs" diff --git a/LICENSE b/LICENSE index 733c072..c39ed31 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,5 @@ +Copyright (c) 2015 Michael Hiley + GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 @@ -631,8 +633,8 @@ to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. - {one line to give the program's name and a brief idea of what it does.} - Copyright (C) {year} {name of author} + + Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -652,7 +654,7 @@ Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: - {project} Copyright (C) {year} {fullname} + Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. @@ -672,4 +674,3 @@ may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . - diff --git a/README.md b/README.md new file mode 100644 index 0000000..7417fe0 --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# rust-netcdf + +[![Build Status](https://travis-ci.org/mhiley/rust-netcdf.svg?branch=master)](https://travis-ci.org/mhiley/rust-netcdf) + +High-level [NetCDF](http://www.unidata.ucar.edu/software/netcdf/) bindings for Rust + +## Status + +Not (yet) supported: appending to existing files (using unlimited dimensions), user defined types, string variables, multi-valued attributes, strided/subsetted reads. All variable data is read into a 1-dimensional Vec with the last variable dimension varying fastest. + +## Building + +rust-netcdf depends on libnetcdf. You also need libclang installed because netcdf-sys uses [rust-bindgen](https://github.com/crabtw/rust-bindgen) to generate netcdf function signatures. + +## Read Example + +```Rust +// Open file simple_xy.nc: +let file = netcdf::open(&path_to_simple_xy).unwrap(); + +// Access any variable, attribute, or dimension through simple HashMap's: +let var = file.root.variables.get("data").unwrap(); + +// Read variable as any NC_TYPE, optionally failing if doing so would +// force a cast: +let data : Vec = var.get_int(false).unwrap(); + +// All variable data is read into 1-dimensional Vec. +for x in 0..(6*12) { + assert_eq!(data[x], x as i32); +} +``` + +## Write Example + +```Rust +let f = netcdf::test_file_new("crabs.nc"); // just gets a path inside repo + +let mut file = netcdf::create(&f).unwrap(); + +let dim_name = "ncrabs"; +file.root.add_dimension(dim_name, 10).unwrap(); + +let var_name = "crab_coolness_level"; +let data : Vec = vec![42; 10]; +// Variable type written to file is inferred from Vec type: +file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); +``` + +## Documentation + +I intend to improve documentation soon. For now, check out tests/lib.rs for quite a few usage examples. diff --git a/THIRD_PARTY b/THIRD_PARTY new file mode 100644 index 0000000..a787fbd --- /dev/null +++ b/THIRD_PARTY @@ -0,0 +1,11 @@ +rust-netcdf contains code from Unidata's NetCDF, under the following license: + +NetCDF License (http://www.unidata.ucar.edu/software/netcdf/copyright.html) + --------------- +Copyright 1993-2014 University Corporation for Atmospheric Research/Unidata + +Portions of this software were developed by the Unidata Program at the University Corporation for Atmospheric Research. + +Access and use of this software shall impose the following obligations and understandings on the user. The user is granted the right, without any fee or cost, to use, copy, modify, alter, enhance and distribute this software, and any derivative works thereof, and its supporting documentation for any purpose whatsoever, provided that this entire notice appears in all copies of the software, derivative works and supporting documentation. Further, UCAR requests that the user credit UCAR/Unidata in any publications that result from the use of this software or in any product that includes this software, although this is not an obligation. The names UCAR and/or Unidata, however, may not be used in any advertising or publicity to endorse or promote any products or commercial entity unless specific written permission is obtained from UCAR/Unidata. The user also understands that UCAR/Unidata is not obligated to provide the user with any support, consulting, training or assistance of any kind with regard to the use, operation and performance of this software nor to provide the user with any updates, revisions, new versions or "bug fixes." + +THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/netcdf-sys/Cargo.toml b/netcdf-sys/Cargo.toml new file mode 100644 index 0000000..7c19244 --- /dev/null +++ b/netcdf-sys/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "netcdf-sys" +version = "0.1.0" +authors = ["Mike Hiley "] +license = "MIT" +description = "FFI bindings to NetCDF" +repository = "https://github.com/mhiley/rust-netcdf" +documentation = "https://github.com/mhiley/rust-netcdf" +keywords = ["netcdf", "hdf", "hdf4", "hdf5", "cdm"] + +links = "netcdf" +build = "build.rs" + +[build-dependencies] +gcc = "0.3.8" + +[build-dependencies.bindgen] +git = "https://github.com/crabtw/rust-bindgen.git" + +[dependencies.lazy_static] +git = "https://github.com/Kimundi/lazy-static.rs" + +[dependencies] +libc = "0.1.8" diff --git a/netcdf-sys/LICENSE-MIT b/netcdf-sys/LICENSE-MIT new file mode 100644 index 0000000..4555762 --- /dev/null +++ b/netcdf-sys/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2015 Michael Hiley + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/netcdf-sys/build.rs b/netcdf-sys/build.rs new file mode 100644 index 0000000..b44bb77 --- /dev/null +++ b/netcdf-sys/build.rs @@ -0,0 +1,32 @@ +extern crate gcc; +extern crate bindgen; + +fn main() { + let out_dir = std::env::var("OUT_DIR").unwrap(); + let out_dir = std::path::Path::new(&out_dir); + + // This is a workaround to avoid using bindgen! macro. Cleaner solution + // may be available if https://github.com/crabtw/rust-bindgen/issues/201 + // is resolved. + let rs_path = std::path::Path::new(&out_dir).join("netcdf_bindings.rs"); + + let mut bindings = bindgen::builder(); + bindings.forbid_unknown_types(); + // hack for Arch Linux 2015-06-24: + bindings.clang_arg("-I/usr/lib/clang/3.6.1/include"); + + // XXX why do usual clang search paths work for lib but not for include? + let mnf_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let netcdf_h = std::path::Path::new(&mnf_dir).join( + "src").join("netcdf_v4.3.3.1.h"); + let netcdf_h = netcdf_h.to_str().unwrap(); + bindings.header(netcdf_h); + bindings.link("netcdf"); + + let bindings = bindings.generate(); + let bindings = bindings.unwrap(); + bindings.write_to_file(rs_path).unwrap(); + + // compile c wrapper to convert CPP constants into proper C types+values + gcc::compile_library("libncconst.a", &["src/ncconst.c"]); +} diff --git a/netcdf-sys/src/lib.rs b/netcdf-sys/src/lib.rs new file mode 100644 index 0000000..e9fac9c --- /dev/null +++ b/netcdf-sys/src/lib.rs @@ -0,0 +1,291 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] + +#[macro_use] +extern crate lazy_static; + +extern crate libc; +use std::sync::Mutex; + +include!(concat!(env!("OUT_DIR"), "/netcdf_bindings.rs")); + +extern "C" { + pub static nc_nat: ::libc::c_int; + pub static nc_byte: ::libc::c_int; + pub static nc_char: ::libc::c_int; + pub static nc_short: ::libc::c_int; + pub static nc_int: ::libc::c_int; + pub static nc_long: ::libc::c_int; + pub static nc_float: ::libc::c_int; + pub static nc_double: ::libc::c_int; + pub static nc_ubyte: ::libc::c_int; + pub static nc_ushort: ::libc::c_int; + pub static nc_uint: ::libc::c_int; + pub static nc_int64: ::libc::c_int; + pub static nc_uint64: ::libc::c_int; + pub static nc_string: ::libc::c_int; + pub static nc_max_atomic_type: ::libc::c_int; + pub static nc_vlen : ::libc::c_int; + pub static nc_opaque : ::libc::c_int; + pub static nc_enum : ::libc::c_int; + pub static nc_compound : ::libc::c_int; + pub static nc_nowrite : ::libc::c_int; + pub static nc_write : ::libc::c_int; + pub static nc_clobber : ::libc::c_int; + pub static nc_noclobber : ::libc::c_int; + pub static nc_diskless : ::libc::c_int; + pub static nc_mmap : ::libc::c_int; + pub static nc_classic_model: ::libc::c_int; + pub static nc_64bit_offset : ::libc::c_int; + pub static nc_lock : ::libc::c_int; + pub static nc_share : ::libc::c_int; + pub static nc_netcdf4 : ::libc::c_int; + pub static nc_mpiio : ::libc::c_int; + pub static nc_mpiposix : ::libc::c_int; + pub static nc_pnetcdf : ::libc::c_int; + pub static nc_format_classic: ::libc::c_int; + pub static nc_format_64bit : ::libc::c_int; + pub static nc_format_netcdf4: ::libc::c_int; + pub static nc_format_netcdf4_classic : ::libc::c_int; + pub static nc_format_nc3 : ::libc::c_int; + pub static nc_format_nc_hdf5: ::libc::c_int; + pub static nc_format_nc_hdf4: ::libc::c_int; + pub static nc_format_pnetcdf: ::libc::c_int; + pub static nc_format_dap2 : ::libc::c_int; + pub static nc_format_dap4 : ::libc::c_int; + pub static nc_format_undefined: ::libc::c_int; + pub static nc_sizehint_default : ::libc::c_int; + pub static nc_global : ::libc::c_int; + pub static nc_max_dims : ::libc::c_int; + pub static nc_max_attrs : ::libc::c_int; + pub static nc_max_vars : ::libc::c_int; + pub static nc_max_name : ::libc::c_int; + pub static nc_max_var_dims : ::libc::c_int; + pub static nc_max_hdf4_name : ::libc::c_int; + pub static nc_endian_native : ::libc::c_int; + pub static nc_endian_little : ::libc::c_int; + pub static nc_endian_big : ::libc::c_int; + pub static nc_chunked : ::libc::c_int; + pub static nc_contiguous : ::libc::c_int; + pub static nc_nochecksum : ::libc::c_int; + pub static nc_fletcher32 : ::libc::c_int; + pub static nc_noshuffle : ::libc::c_int; + pub static nc_shuffle : ::libc::c_int; + pub static nc_noerr : ::libc::c_int; + pub static nc2_err : ::libc::c_int; + pub static nc_ebadid : ::libc::c_int; + pub static nc_enfile : ::libc::c_int; + pub static nc_eexist : ::libc::c_int; + pub static nc_einval : ::libc::c_int; + pub static nc_eperm : ::libc::c_int; + pub static nc_enotindefine : ::libc::c_int; + pub static nc_eindefine : ::libc::c_int; + pub static nc_einvalcoords : ::libc::c_int; + pub static nc_emaxdims : ::libc::c_int; + pub static nc_enameinuse : ::libc::c_int; + pub static nc_enotatt : ::libc::c_int; + pub static nc_emaxatts : ::libc::c_int; + pub static nc_ebadtype : ::libc::c_int; + pub static nc_ebaddim : ::libc::c_int; + pub static nc_eunlimpos : ::libc::c_int; + pub static nc_emaxvars : ::libc::c_int; + pub static nc_enotvar : ::libc::c_int; + pub static nc_eglobal : ::libc::c_int; + pub static nc_enotnc : ::libc::c_int; + pub static nc_ests : ::libc::c_int; + pub static nc_emaxname : ::libc::c_int; + pub static nc_eunlimit : ::libc::c_int; + pub static nc_enorecvars : ::libc::c_int; + pub static nc_echar : ::libc::c_int; + pub static nc_eedge : ::libc::c_int; + pub static nc_estride : ::libc::c_int; + pub static nc_ebadname : ::libc::c_int; + pub static nc_erange : ::libc::c_int; + pub static nc_enomem : ::libc::c_int; + pub static nc_evarsize : ::libc::c_int; + pub static nc_edimsize : ::libc::c_int; + pub static nc_etrunc : ::libc::c_int; + pub static nc_eaxistype : ::libc::c_int; + pub static nc_edap : ::libc::c_int; + pub static nc_ecurl : ::libc::c_int; + pub static nc_eio : ::libc::c_int; + pub static nc_enodata : ::libc::c_int; + pub static nc_edapsvc : ::libc::c_int; + pub static nc_edas : ::libc::c_int; + pub static nc_edds : ::libc::c_int; + pub static nc_edatadds : ::libc::c_int; + pub static nc_edapurl : ::libc::c_int; + pub static nc_edapconstraint : ::libc::c_int; + pub static nc_etranslation : ::libc::c_int; + pub static nc_eaccess : ::libc::c_int; + pub static nc_eauth : ::libc::c_int; + pub static nc_enotfound : ::libc::c_int; + pub static nc_ecantremove : ::libc::c_int; + pub static nc4_first_error : ::libc::c_int; + pub static nc_ehdferr : ::libc::c_int; + pub static nc_ecantread : ::libc::c_int; + pub static nc_ecantwrite : ::libc::c_int; + pub static nc_ecantcreate : ::libc::c_int; + pub static nc_efilemeta : ::libc::c_int; + pub static nc_edimmeta : ::libc::c_int; + pub static nc_eattmeta : ::libc::c_int; + pub static nc_evarmeta : ::libc::c_int; + pub static nc_enocompound : ::libc::c_int; + pub static nc_eattexists : ::libc::c_int; + pub static nc_enotnc4 : ::libc::c_int; + pub static nc_estrictnc3 : ::libc::c_int; + pub static nc_enotnc3 : ::libc::c_int; + pub static nc_enopar : ::libc::c_int; + pub static nc_eparinit : ::libc::c_int; + pub static nc_ebadgrpid : ::libc::c_int; + pub static nc_ebadtypid : ::libc::c_int; + pub static nc_etypdefined : ::libc::c_int; + pub static nc_ebadfield : ::libc::c_int; + pub static nc_ebadclass : ::libc::c_int; + pub static nc_emaptype : ::libc::c_int; + pub static nc_elatefill : ::libc::c_int; + pub static nc_elatedef : ::libc::c_int; + pub static nc_edimscale : ::libc::c_int; + pub static nc_enogrp : ::libc::c_int; + pub static nc_estorage : ::libc::c_int; + pub static nc_ebadchunk : ::libc::c_int; + pub static nc_enotbuilt : ::libc::c_int; + pub static nc_ediskless : ::libc::c_int; + pub static nc_ecantextend : ::libc::c_int; + pub static nc_empi : ::libc::c_int; + pub static nc4_last_error : ::libc::c_int; + pub static nc_have_new_chunking_api : ::libc::c_int; + pub static nc_eurl : ::libc::c_int; + pub static nc_econstraint : ::libc::c_int; + pub static nc_fill : ::libc::c_int; + pub static nc_nofill : ::libc::c_int; + + pub static nc_unlimited: ::libc::c_long; + + pub static nc_fill_byte: i8; + pub static nc_fill_char: u8; + pub static nc_fill_short: i16; + pub static nc_fill_int: i32; + pub static nc_fill_float: f32; + pub static nc_fill_double: f64; + pub static nc_fill_ubyte: u8; + pub static nc_fill_ushort: u16; + pub static nc_fill_uint: u32; + pub static nc_fill_int64: i64; + pub static nc_fill_uint64: u64; + pub static nc_fill_string: *const ::libc::c_char; + + pub static nc_align_chunk: ::libc::size_t; +} + +// NetCDF types map well to Rust types, for for completeness, here are +// definitions of min/max constants: +pub const nc_max_byte : i8 = std::i8::MAX; +pub const nc_min_byte : i8 = std::i8::MIN; +pub const nc_max_char : u8 = std::u8::MAX; +pub const nc_max_short : i16 = std::i16::MAX; +pub const nc_min_short : i16 = std::i16::MIN; +pub const nc_max_int : i32 = std::i32::MAX; +pub const nc_min_int : i32 = std::i32::MIN; +pub const nc_max_float : f32 = std::f32::MAX; +pub const nc_min_float : f32 = std::f32::MIN; +pub const nc_max_double : f64 = std::f64::MAX; +pub const nc_min_double : f64 = std::f64::MIN; +pub const nc_max_ubyte : u8 = std::u8::MAX; +pub const nc_max_ushort : u16 = std::u16::MAX; +pub const nc_max_uint : u32 = std::u32::MAX; +pub const nc_max_int64 : i64 = std::i64::MAX; +pub const nc_min_int64 : i64 = std::i64::MIN; +pub const nc_max_uint64 : u64 = std::u64::MAX; + +// Per the NetCDF FAQ, "THE C-BASED LIBRARIES ARE NOT THREAD-SAFE" +// So, here is our global mutex. +// Use lazy-static dependency to avoid use of static_mutex feature which +// breaks compatibility with stable channel. +lazy_static! { + pub static ref libnetcdf_lock: Mutex<()> = Mutex::new(()); +} + +#[cfg(test)] +mod tests { + use super::*; + use std::path; + use std::env; + use std::ffi; + + #[test] + fn test_nc_open_close() { + let mnf_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let test_data_path = path::Path::new(&mnf_dir).join( + "testdata").join("simple_xy.nc"); + let f = ffi::CString::new(test_data_path.to_str().unwrap()).unwrap(); + + let mut ncid : i32 = -999999i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let err = nc_open(f.as_ptr(), nc_nowrite, &mut ncid); + assert_eq!(err, nc_noerr); + let err = nc_close(ncid); + assert_eq!(err, nc_noerr); + } + } + + #[test] + fn test_inq_varid() { + let mnf_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let test_data_path = path::Path::new(&mnf_dir).join( + "testdata").join("simple_xy.nc"); + let f = ffi::CString::new(test_data_path.to_str().unwrap()).unwrap(); + let varname = ffi::CString::new("data").unwrap(); + + let mut ncid : i32 = -999999i32; + let mut varid : i32 = -999999i32; + let mut nvars : i32 = -999999i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let err = nc_open(f.as_ptr(), nc_nowrite, &mut ncid); + assert_eq!(err, nc_noerr); + let err = nc_inq_nvars(ncid, &mut nvars); + assert_eq!(err, nc_noerr); + assert_eq!(nvars, 1); + let err = nc_inq_varid(ncid, varname.as_ptr(), &mut varid); + assert_eq!(err, nc_noerr); + let err = nc_close(ncid); + assert_eq!(err, nc_noerr); + } + } + + #[test] + fn test_get_var() { + let mnf_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let test_data_path = path::Path::new(&mnf_dir).join( + "testdata").join("simple_xy.nc"); + let f = ffi::CString::new(test_data_path.to_str().unwrap()).unwrap(); + let varname = ffi::CString::new("data").unwrap(); + + let mut ncid : i32 = -999999i32; + let mut varid : i32 = -999999i32; + let mut buf : Vec = Vec::with_capacity(6*12); + unsafe { + buf.set_len(6*12); + + let _g = libnetcdf_lock.lock().unwrap(); + let err = nc_open(f.as_ptr(), nc_nowrite, &mut ncid); + assert_eq!(err, nc_noerr); + + let err = nc_inq_varid(ncid, varname.as_ptr(), &mut varid); + assert_eq!(err, nc_noerr); + + let err = nc_get_var_int(ncid, varid, buf.as_mut_ptr()); + assert_eq!(err, nc_noerr); + + let err = nc_close(ncid); + assert_eq!(err, nc_noerr); + } + + for x in 0..(6*12) { + assert_eq!(buf[x], x as i32); + } + } + +} diff --git a/netcdf-sys/src/ncconst.c b/netcdf-sys/src/ncconst.c new file mode 100644 index 0000000..d2aeba4 --- /dev/null +++ b/netcdf-sys/src/ncconst.c @@ -0,0 +1,180 @@ +/* Here we convert netcdf.h CPP macros into concrete C types and values. + * We assume plain int types unless otherwise obvious. + * Following netcdf.h v4.3.3.1 + */ +#include + +const int nc_nat = NC_NAT; +const int nc_byte = NC_BYTE; +const int nc_char = NC_CHAR; +const int nc_short = NC_SHORT; +const int nc_int = NC_INT; +const int nc_long = NC_LONG; +const int nc_float = NC_FLOAT; +const int nc_double = NC_DOUBLE; +const int nc_ubyte = NC_UBYTE; +const int nc_ushort = NC_USHORT; +const int nc_uint = NC_UINT; +const int nc_int64 = NC_INT64; +const int nc_uint64 = NC_UINT64; +const int nc_string = NC_STRING; +const int nc_max_atomic_type = NC_MAX_ATOMIC_TYPE; +const int nc_vlen = NC_VLEN ; +const int nc_opaque = NC_OPAQUE ; +const int nc_enum = NC_ENUM ; +const int nc_compound = NC_COMPOUND ; +const int nc_nowrite = NC_NOWRITE ; +const int nc_write = NC_WRITE ; +const int nc_clobber = NC_CLOBBER ; +const int nc_noclobber = NC_NOCLOBBER ; +const int nc_diskless = NC_DISKLESS ; +const int nc_mmap = NC_MMAP ; +const int nc_classic_model = NC_CLASSIC_MODEL; +const int nc_64bit_offset = NC_64BIT_OFFSET ; +const int nc_lock = NC_LOCK ; +const int nc_share = NC_SHARE ; +const int nc_netcdf4 = NC_NETCDF4 ; +const int nc_mpiio = NC_MPIIO ; +const int nc_mpiposix = NC_MPIPOSIX ; +const int nc_pnetcdf = NC_PNETCDF ; +const int nc_format_classic = NC_FORMAT_CLASSIC; +const int nc_format_64bit = NC_FORMAT_64BIT ; +const int nc_format_netcdf4 = NC_FORMAT_NETCDF4; +const int nc_format_netcdf4_classic = NC_FORMAT_NETCDF4_CLASSIC ; +const int nc_format_nc3 = NC_FORMAT_NC3 ; +const int nc_format_nc_hdf5 = NC_FORMAT_NC_HDF5; +const int nc_format_nc_hdf4 = NC_FORMAT_NC_HDF4; +const int nc_format_pnetcdf = NC_FORMAT_PNETCDF; +const int nc_format_dap2 = NC_FORMAT_DAP2 ; +const int nc_format_dap4 = NC_FORMAT_DAP4 ; +const int nc_format_undefined = NC_FORMAT_UNDEFINED; +const int nc_sizehint_default = NC_SIZEHINT_DEFAULT ; +const int nc_global = NC_GLOBAL ; +const int nc_max_dims = NC_MAX_DIMS ; +const int nc_max_attrs = NC_MAX_ATTRS ; +const int nc_max_vars = NC_MAX_VARS ; +const int nc_max_name = NC_MAX_NAME ; +const int nc_max_var_dims = NC_MAX_VAR_DIMS ; +const int nc_max_hdf4_name = NC_MAX_HDF4_NAME ; +const int nc_endian_native = NC_ENDIAN_NATIVE ; +const int nc_endian_little = NC_ENDIAN_LITTLE ; +const int nc_endian_big = NC_ENDIAN_BIG ; +const int nc_chunked = NC_CHUNKED ; +const int nc_contiguous = NC_CONTIGUOUS ; +const int nc_nochecksum = NC_NOCHECKSUM ; +const int nc_fletcher32 = NC_FLETCHER32 ; +const int nc_noshuffle = NC_NOSHUFFLE ; +const int nc_shuffle = NC_SHUFFLE ; +const int nc_noerr = NC_NOERR ; +const int nc2_err = NC2_ERR ; +const int nc_ebadid = NC_EBADID ; +const int nc_enfile = NC_ENFILE ; +const int nc_eexist = NC_EEXIST ; +const int nc_einval = NC_EINVAL ; +const int nc_eperm = NC_EPERM ; +const int nc_enotindefine = NC_ENOTINDEFINE ; +const int nc_eindefine = NC_EINDEFINE ; +const int nc_einvalcoords = NC_EINVALCOORDS ; +const int nc_emaxdims = NC_EMAXDIMS ; +const int nc_enameinuse = NC_ENAMEINUSE ; +const int nc_enotatt = NC_ENOTATT ; +const int nc_emaxatts = NC_EMAXATTS ; +const int nc_ebadtype = NC_EBADTYPE ; +const int nc_ebaddim = NC_EBADDIM ; +const int nc_eunlimpos = NC_EUNLIMPOS ; +const int nc_emaxvars = NC_EMAXVARS ; +const int nc_enotvar = NC_ENOTVAR ; +const int nc_eglobal = NC_EGLOBAL ; +const int nc_enotnc = NC_ENOTNC ; +const int nc_ests = NC_ESTS ; +const int nc_emaxname = NC_EMAXNAME ; +const int nc_eunlimit = NC_EUNLIMIT ; +const int nc_enorecvars = NC_ENORECVARS ; +const int nc_echar = NC_ECHAR ; +const int nc_eedge = NC_EEDGE ; +const int nc_estride = NC_ESTRIDE ; +const int nc_ebadname = NC_EBADNAME ; +const int nc_erange = NC_ERANGE ; +const int nc_enomem = NC_ENOMEM ; +const int nc_evarsize = NC_EVARSIZE ; +const int nc_edimsize = NC_EDIMSIZE ; +const int nc_etrunc = NC_ETRUNC ; +const int nc_eaxistype = NC_EAXISTYPE ; +const int nc_edap = NC_EDAP ; +const int nc_ecurl = NC_ECURL ; +const int nc_eio = NC_EIO ; +const int nc_enodata = NC_ENODATA ; +const int nc_edapsvc = NC_EDAPSVC ; +const int nc_edas = NC_EDAS ; +const int nc_edds = NC_EDDS ; +const int nc_edatadds = NC_EDATADDS ; +const int nc_edapurl = NC_EDAPURL ; +const int nc_edapconstraint = NC_EDAPCONSTRAINT ; +const int nc_etranslation = NC_ETRANSLATION ; +const int nc_eaccess = NC_EACCESS ; +const int nc_eauth = NC_EAUTH ; +const int nc_enotfound = NC_ENOTFOUND ; +const int nc_ecantremove = NC_ECANTREMOVE ; +const int nc4_first_error = NC4_FIRST_ERROR ; +const int nc_ehdferr = NC_EHDFERR ; +const int nc_ecantread = NC_ECANTREAD ; +const int nc_ecantwrite = NC_ECANTWRITE ; +const int nc_ecantcreate = NC_ECANTCREATE ; +const int nc_efilemeta = NC_EFILEMETA ; +const int nc_edimmeta = NC_EDIMMETA ; +const int nc_eattmeta = NC_EATTMETA ; +const int nc_evarmeta = NC_EVARMETA ; +const int nc_enocompound = NC_ENOCOMPOUND ; +const int nc_eattexists = NC_EATTEXISTS ; +const int nc_enotnc4 = NC_ENOTNC4 ; +const int nc_estrictnc3 = NC_ESTRICTNC3 ; +const int nc_enotnc3 = NC_ENOTNC3 ; +const int nc_enopar = NC_ENOPAR ; +const int nc_eparinit = NC_EPARINIT ; +const int nc_ebadgrpid = NC_EBADGRPID ; +const int nc_ebadtypid = NC_EBADTYPID ; +const int nc_etypdefined = NC_ETYPDEFINED ; +const int nc_ebadfield = NC_EBADFIELD ; +const int nc_ebadclass = NC_EBADCLASS ; +const int nc_emaptype = NC_EMAPTYPE ; +const int nc_elatefill = NC_ELATEFILL ; +const int nc_elatedef = NC_ELATEDEF ; +const int nc_edimscale = NC_EDIMSCALE ; +const int nc_enogrp = NC_ENOGRP ; +const int nc_estorage = NC_ESTORAGE ; +const int nc_ebadchunk = NC_EBADCHUNK ; +const int nc_enotbuilt = NC_ENOTBUILT ; +const int nc_ediskless = NC_EDISKLESS ; +const int nc_ecantextend = NC_ECANTEXTEND ; +const int nc_empi = NC_EMPI ; +const int nc4_last_error = NC4_LAST_ERROR ; +const int nc_have_new_chunking_api = NC_HAVE_NEW_CHUNKING_API ; +const int nc_eurl = NC_EURL ; +const int nc_econstraint = NC_ECONSTRAINT ; +const int nc_fill = NC_FILL ; +const int nc_nofill = NC_NOFILL ; + +/* deal with non-int types */ +const char * const dim_without_variable = DIM_WITHOUT_VARIABLE; +const char * const nc__FillValue = _FillValue; + +const long nc_unlimited = NC_UNLIMITED ; + +const signed char nc_fill_byte = NC_FILL_BYTE; +const char nc_fill_char = NC_FILL_CHAR; +const short nc_fill_short = NC_FILL_SHORT ; +const long nc_fill_int = NC_FILL_INT; +const float nc_fill_float = NC_FILL_FLOAT; +const double nc_fill_double = NC_FILL_DOUBLE; +const unsigned char nc_fill_ubyte = NC_FILL_UBYTE ; +const unsigned short nc_fill_ushort = NC_FILL_USHORT ; +const unsigned int nc_fill_uint = NC_FILL_UINT ; +const long long nc_fill_int64 = NC_FILL_INT64 ; +const unsigned long long nc_fill_uint64 = NC_FILL_UINT64 ; +const char * const nc_fill_string = ""; + +const size_t nc_align_chunk = NC_ALIGN_CHUNK; + +//#define X_INT64_MAX (9223372036854775807LL) +//#define X_INT64_MIN (-X_INT64_MAX - 1) +//#define X_UINT64_MAX (18446744073709551615ULL) diff --git a/netcdf-sys/src/netcdf_v4.3.3.1.h b/netcdf-sys/src/netcdf_v4.3.3.1.h new file mode 100644 index 0000000..c9ca0af --- /dev/null +++ b/netcdf-sys/src/netcdf_v4.3.3.1.h @@ -0,0 +1,1935 @@ +/*! \file + +Main header file for the C API. + +Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 +University Corporation for Atmospheric Research/Unidata. + +See \ref copyright file for more info. +*/ + +#ifndef _NETCDF_ +#define _NETCDF_ + +#include /* size_t, ptrdiff_t */ +#include /* netcdf functions sometimes return system errors */ + +/* Required for alloca on Windows */ +#if defined(_WIN32) || defined(_WIN64) +#include +#endif + +#ifdef _WIN64 +#include +#endif + +/*! The nc_type type is just an int. */ +typedef int nc_type; + +#if defined(__cplusplus) +extern "C" { +#endif + +/* + * The netcdf external data types + */ +#define NC_NAT 0 /**< Not A Type */ +#define NC_BYTE 1 /**< signed 1 byte integer */ +#define NC_CHAR 2 /**< ISO/ASCII character */ +#define NC_SHORT 3 /**< signed 2 byte integer */ +#define NC_INT 4 /**< signed 4 byte integer */ +#define NC_LONG NC_INT /**< deprecated, but required for backward compatibility. */ +#define NC_FLOAT 5 /**< single precision floating point number */ +#define NC_DOUBLE 6 /**< double precision floating point number */ +#define NC_UBYTE 7 /**< unsigned 1 byte int */ +#define NC_USHORT 8 /**< unsigned 2-byte int */ +#define NC_UINT 9 /**< unsigned 4-byte int */ +#define NC_INT64 10 /**< signed 8-byte int */ +#define NC_UINT64 11 /**< unsigned 8-byte int */ +#define NC_STRING 12 /**< string */ + +#define NC_MAX_ATOMIC_TYPE NC_STRING + +/* The following are use internally in support of user-defines + * types. They are also the class returned by nc_inq_user_type. */ +#define NC_VLEN 13 /**< vlen (variable-length) types */ +#define NC_OPAQUE 14 /**< opaque types */ +#define NC_ENUM 15 /**< enum types */ +#define NC_COMPOUND 16 /**< compound types */ + +/* Define the first user defined type id (leave some room) */ +#define NC_FIRSTUSERTYPEID 32 + +/** Default fill value. This is used unless _FillValue attribute + * is set. These values are stuffed into newly allocated space as + * appropriate. The hope is that one might use these to notice that a + * particular datum has not been set. */ +/**@{*/ +#define NC_FILL_BYTE ((signed char)-127) +#define NC_FILL_CHAR ((char)0) +#define NC_FILL_SHORT ((short)-32767) +#define NC_FILL_INT (-2147483647L) +#define NC_FILL_FLOAT (9.9692099683868690e+36f) /* near 15 * 2^119 */ +#define NC_FILL_DOUBLE (9.9692099683868690e+36) +#define NC_FILL_UBYTE (255) +#define NC_FILL_USHORT (65535) +#define NC_FILL_UINT (4294967295U) +#define NC_FILL_INT64 ((long long)-9223372036854775806LL) +#define NC_FILL_UINT64 ((unsigned long long)18446744073709551614ULL) +#define NC_FILL_STRING ((char *)"") +/**@}*/ + +/*! Max or min values for a type. Nothing greater/smaller can be + * stored in a netCDF file for their associated types. Recall that a C + * compiler may define int to be any length it wants, but a NC_INT is + * *always* a 4 byte signed int. On a platform with 64 bit ints, + * there will be many ints which are outside the range supported by + * NC_INT. But since NC_INT is an external format, it has to mean the + * same thing everywhere. */ +/**@{*/ +#define NC_MAX_BYTE 127 +#define NC_MIN_BYTE (-NC_MAX_BYTE-1) +#define NC_MAX_CHAR 255 +#define NC_MAX_SHORT 32767 +#define NC_MIN_SHORT (-NC_MAX_SHORT - 1) +#define NC_MAX_INT 2147483647 +#define NC_MIN_INT (-NC_MAX_INT - 1) +#define NC_MAX_FLOAT 3.402823466e+38f +#define NC_MIN_FLOAT (-NC_MAX_FLOAT) +#define NC_MAX_DOUBLE 1.7976931348623157e+308 +#define NC_MIN_DOUBLE (-NC_MAX_DOUBLE) +#define NC_MAX_UBYTE NC_MAX_CHAR +#define NC_MAX_USHORT 65535U +#define NC_MAX_UINT 4294967295U +#define NC_MAX_INT64 (9223372036854775807LL) +#define NC_MIN_INT64 (-9223372036854775807LL-1) +#define NC_MAX_UINT64 (18446744073709551615ULL) +#define X_INT64_MAX (9223372036854775807LL) +#define X_INT64_MIN (-X_INT64_MAX - 1) +#define X_UINT64_MAX (18446744073709551615ULL) +/**@}*/ + +/** Name of fill value attribute. If you wish a variable to use a + * different value than the above defaults, create an attribute with + * the same type as the variable and this reserved name. The value you + * give the attribute will be used as the fill value for that + * variable. */ +#define _FillValue "_FillValue" +#define NC_FILL 0 /**< Argument to nc_set_fill() to clear NC_NOFILL */ +#define NC_NOFILL 0x100 /**< Argument to nc_set_fill() to turn off filling of data. */ + +/* Define the ioflags bits for nc_create and nc_open. + currently unused: 0x0010,0x0020,0x0040,0x0080 + and the whole upper 16 bits +*/ + +#define NC_NOWRITE 0x0000 /**< Set read-only access for nc_open(). */ +#define NC_WRITE 0x0001 /**< Set read-write access for nc_open(). */ +/* unused: 0x0002 */ +#define NC_CLOBBER 0x0000 /**< Destroy existing file. Mode flag for nc_create(). */ +#define NC_NOCLOBBER 0x0004 /**< Don't destroy existing file. Mode flag for nc_create(). */ + +#define NC_DISKLESS 0x0008 /**< Use diskless file. Mode flag for nc_open() or nc_create(). */ +#define NC_MMAP 0x0010 /**< Use diskless file with mmap. Mode flag for nc_open() or nc_create(). */ + +#define NC_CLASSIC_MODEL 0x0100 /**< Enforce classic model. Mode flag for nc_create(). */ +#define NC_64BIT_OFFSET 0x0200 /**< Use large (64-bit) file offsets. Mode flag for nc_create(). */ + +/** \deprecated The following flag currently is ignored, but use in + * nc_open() or nc_create() may someday support use of advisory + * locking to prevent multiple writers from clobbering a file + */ +#define NC_LOCK 0x0400 + +/** Share updates, limit cacheing. +Use this in mode flags for both nc_create() and nc_open(). */ +#define NC_SHARE 0x0800 + +#define NC_NETCDF4 0x1000 /**< Use netCDF-4/HDF5 format. Mode flag for nc_create(). */ + +/** Turn on MPI I/O. +Use this in mode flags for both nc_create() and nc_open(). */ +#define NC_MPIIO 0x2000 +/** Turn on MPI POSIX I/O. +Use this in mode flags for both nc_create() and nc_open(). */ +#define NC_MPIPOSIX 0x4000 /**< \deprecated As of libhdf5 1.8.13. */ +#define NC_PNETCDF 0x8000 /**< Use parallel-netcdf library. Mode flag for nc_open(). */ + +/** Format specifier for nc_set_default_format() and returned + * by nc_inq_format. This returns the format as provided by + * the API. See nc_inq_format_extended to see the true file format. + * Starting with version 3.6, there are different format netCDF files. + * 4.0 introduces the third one. \see netcdf_format + */ +/**@{*/ +#define NC_FORMAT_CLASSIC (1) +#define NC_FORMAT_64BIT (2) +#define NC_FORMAT_NETCDF4 (3) +#define NC_FORMAT_NETCDF4_CLASSIC (4) + +/**@}*/ + +/** Extended format specifier returned by nc_inq_format_extended() + * Added in version 4.3.1. This returns the true format of the + * underlying data. + * The function returns two values + * 1. a small integer indicating the underlying source type + * of the data. Note that this may differ from what the user + * sees from nc_inq_format() because this latter function + * returns what the user can expect to see thru the API. + * 2. A mode value indicating what mode flags are effectively + * set for this dataset. This usually will be a superset + * of the mode flags used as the argument to nc_open + * or nc_create. + * More or less, the #1 values track the set of dispatch tables. + * The #1 values are as follows. + */ +/**@{*/ +#define NC_FORMAT_NC3 (1) +#define NC_FORMAT_NC_HDF5 (2) /* netCDF-4 subset of HDF5 */ +#define NC_FORMAT_NC_HDF4 (3) /* netCDF-4 subset of HDF4 */ +#define NC_FORMAT_PNETCDF (4) +#define NC_FORMAT_DAP2 (5) +#define NC_FORMAT_DAP4 (6) +#define NC_FORMAT_UNDEFINED (0) +/**@}*/ + +/** Let nc__create() or nc__open() figure out a suitable buffer size. */ +#define NC_SIZEHINT_DEFAULT 0 + +/** In nc__enddef(), align to the buffer size. */ +#define NC_ALIGN_CHUNK ((size_t)(-1)) + +/** Size argument to nc_def_dim() for an unlimited dimension. */ +#define NC_UNLIMITED 0L + +/** Attribute id to put/get a global attribute. */ +#define NC_GLOBAL -1 + +/** +Maximum for classic library. + +In the classic netCDF model there are maximum values for the number of +dimensions in the file (\ref NC_MAX_DIMS), the number of global or per +variable attributes (\ref NC_MAX_ATTRS), the number of variables in +the file (\ref NC_MAX_VARS), and the length of a name (\ref +NC_MAX_NAME). + +These maximums are enforced by the interface, to facilitate writing +applications and utilities. However, nothing is statically allocated +to these sizes internally. + +These maximums are not used for netCDF-4/HDF5 files unless they were +created with the ::NC_CLASSIC_MODEL flag. + +As a rule, NC_MAX_VAR_DIMS <= NC_MAX_DIMS. +*/ +/**@{*/ +#define NC_MAX_DIMS 1024 +#define NC_MAX_ATTRS 8192 +#define NC_MAX_VARS 8192 +#define NC_MAX_NAME 256 +#define NC_MAX_VAR_DIMS 1024 /**< max per variable dimensions */ +/**@}*/ + +/** This is the max size of an SD dataset name in HDF4 (from HDF4 documentation).*/ +#define NC_MAX_HDF4_NAME 64 + +/** In HDF5 files you can set the endianness of variables with + nc_def_var_endian(). This define is used there. */ +/**@{*/ +#define NC_ENDIAN_NATIVE 0 +#define NC_ENDIAN_LITTLE 1 +#define NC_ENDIAN_BIG 2 +/**@}*/ + +/** In HDF5 files you can set storage for each variable to be either + * contiguous or chunked, with nc_def_var_chunking(). This define is + * used there. */ +/**@{*/ +#define NC_CHUNKED 0 +#define NC_CONTIGUOUS 1 +/**@}*/ + +/** In HDF5 files you can set check-summing for each variable. +Currently the only checksum available is Fletcher-32, which can be set +with the function nc_def_var_fletcher32. These defines are used +there. */ +/**@{*/ +#define NC_NOCHECKSUM 0 +#define NC_FLETCHER32 1 +/**@}*/ + +/**@{*/ +/** Control the HDF5 shuffle filter. In HDF5 files you can specify + * that a shuffle filter should be used on each chunk of a variable to + * improve compression for that variable. This per-variable shuffle + * property can be set with the function nc_def_var_deflate(). */ +#define NC_NOSHUFFLE 0 +#define NC_SHUFFLE 1 +/**@}*/ + +/** The netcdf version 3 functions all return integer error status. + * These are the possible values, in addition to certain values from + * the system errno.h. + */ +#define NC_ISSYSERR(err) ((err) > 0) + +#define NC_NOERR 0 /**< No Error */ +#define NC2_ERR (-1) /**< Returned for all errors in the v2 API. */ + +/** Not a netcdf id. + +The specified netCDF ID does not refer to an +open netCDF dataset. */ +#define NC_EBADID (-33) +#define NC_ENFILE (-34) /**< Too many netcdfs open */ +#define NC_EEXIST (-35) /**< netcdf file exists && NC_NOCLOBBER */ +#define NC_EINVAL (-36) /**< Invalid Argument */ +#define NC_EPERM (-37) /**< Write to read only */ + +/** Operation not allowed in data mode. This is returned for netCDF +classic or 64-bit offset files, or for netCDF-4 files, when they were +been created with ::NC_CLASSIC_MODEL flag in nc_create(). */ +#define NC_ENOTINDEFINE (-38) + +/** Operation not allowed in define mode. + +The specified netCDF is in define mode rather than data mode. + +With netCDF-4/HDF5 files, this error will not occur, unless +::NC_CLASSIC_MODEL was used in nc_create(). + */ +#define NC_EINDEFINE (-39) + +/** Index exceeds dimension bound. + +The specified corner indices were out of range for the rank of the +specified variable. For example, a negative index or an index that is +larger than the corresponding dimension length will cause an error. */ +#define NC_EINVALCOORDS (-40) +#define NC_EMAXDIMS (-41) /**< NC_MAX_DIMS exceeded */ +#define NC_ENAMEINUSE (-42) /**< String match to name in use */ +#define NC_ENOTATT (-43) /**< Attribute not found */ +#define NC_EMAXATTS (-44) /**< NC_MAX_ATTRS exceeded */ +#define NC_EBADTYPE (-45) /**< Not a netcdf data type */ +#define NC_EBADDIM (-46) /**< Invalid dimension id or name */ +#define NC_EUNLIMPOS (-47) /**< NC_UNLIMITED in the wrong index */ + +/** NC_MAX_VARS exceeded. Max number of variables exceeded in a +classic or 64-bit offset file, or an netCDF-4 file with +::NC_CLASSIC_MODEL on. */ +#define NC_EMAXVARS (-48) + +/** Variable not found. + +The variable ID is invalid for the specified netCDF dataset. */ +#define NC_ENOTVAR (-49) +#define NC_EGLOBAL (-50) /**< Action prohibited on NC_GLOBAL varid */ +#define NC_ENOTNC (-51) /**< Not a netcdf file */ +#define NC_ESTS (-52) /**< In Fortran, string too short */ +#define NC_EMAXNAME (-53) /**< NC_MAX_NAME exceeded */ +#define NC_EUNLIMIT (-54) /**< NC_UNLIMITED size already in use */ +#define NC_ENORECVARS (-55) /**< nc_rec op when there are no record vars */ +#define NC_ECHAR (-56) /**< Attempt to convert between text & numbers */ + +/** Start+count exceeds dimension bound. + +The specified edge lengths added to the specified corner would have +referenced data out of range for the rank of the specified +variable. For example, an edge length that is larger than the +corresponding dimension length minus the corner index will cause an +error. */ +#define NC_EEDGE (-57) +#define NC_ESTRIDE (-58) /**< Illegal stride */ +#define NC_EBADNAME (-59) /**< Attribute or variable name contains illegal characters */ +/* N.B. following must match value in ncx.h */ + +/** Math result not representable. + +One or more of the values are out of the range of values representable +by the desired type. */ +#define NC_ERANGE (-60) +#define NC_ENOMEM (-61) /**< Memory allocation (malloc) failure */ +#define NC_EVARSIZE (-62) /**< One or more variable sizes violate format constraints */ +#define NC_EDIMSIZE (-63) /**< Invalid dimension size */ +#define NC_ETRUNC (-64) /**< File likely truncated or possibly corrupted */ +#define NC_EAXISTYPE (-65) /**< Unknown axis type. */ + +/* Following errors are added for DAP */ +#define NC_EDAP (-66) /**< Generic DAP error */ +#define NC_ECURL (-67) /**< Generic libcurl error */ +#define NC_EIO (-68) /**< Generic IO error */ +#define NC_ENODATA (-69) /**< Attempt to access variable with no data */ +#define NC_EDAPSVC (-70) /**< DAP server error */ +#define NC_EDAS (-71) /**< Malformed or inaccessible DAS */ +#define NC_EDDS (-72) /**< Malformed or inaccessible DDS */ +#define NC_EDATADDS (-73) /**< Malformed or inaccessible DATADDS */ +#define NC_EDAPURL (-74) /**< Malformed DAP URL */ +#define NC_EDAPCONSTRAINT (-75) /**< Malformed DAP Constraint*/ +#define NC_ETRANSLATION (-76) /**< Untranslatable construct */ +#define NC_EACCESS (-77) /**< Access Failure */ +#define NC_EAUTH (-78) /**< Authorization Failure */ + +/* Misc. additional errors */ +#define NC_ENOTFOUND (-90) /**< No such file */ +#define NC_ECANTREMOVE (-91) /**< Can't remove file */ + +/* The following was added in support of netcdf-4. Make all netcdf-4 + error codes < -100 so that errors can be added to netcdf-3 if + needed. */ +#define NC4_FIRST_ERROR (-100) + +/** Error at HDF5 layer. */ +#define NC_EHDFERR (-101) +#define NC_ECANTREAD (-102) /**< Can't read. */ +#define NC_ECANTWRITE (-103) /**< Can't write. */ +#define NC_ECANTCREATE (-104) /**< Can't create. */ +#define NC_EFILEMETA (-105) /**< Problem with file metadata. */ +#define NC_EDIMMETA (-106) /**< Problem with dimension metadata. */ +#define NC_EATTMETA (-107) /**< Problem with attribute metadata. */ +#define NC_EVARMETA (-108) /**< Problem with variable metadata. */ +#define NC_ENOCOMPOUND (-109) /**< Not a compound type. */ +#define NC_EATTEXISTS (-110) /**< Attribute already exists. */ +#define NC_ENOTNC4 (-111) /**< Attempting netcdf-4 operation on netcdf-3 file. */ + +/** Attempting netcdf-4 operation on strict nc3 netcdf-4 file. */ +#define NC_ESTRICTNC3 (-112) +#define NC_ENOTNC3 (-113) /**< Attempting netcdf-3 operation on netcdf-4 file. */ +#define NC_ENOPAR (-114) /**< Parallel operation on file opened for non-parallel access. */ +#define NC_EPARINIT (-115) /**< Error initializing for parallel access. */ +#define NC_EBADGRPID (-116) /**< Bad group ID. */ +#define NC_EBADTYPID (-117) /**< Bad type ID. */ +#define NC_ETYPDEFINED (-118) /**< Type has already been defined and may not be edited. */ +#define NC_EBADFIELD (-119) /**< Bad field ID. */ +#define NC_EBADCLASS (-120) /**< Bad class. */ +#define NC_EMAPTYPE (-121) /**< Mapped access for atomic types only. */ +#define NC_ELATEFILL (-122) /**< Attempt to define fill value when data already exists. */ +#define NC_ELATEDEF (-123) /**< Attempt to define var properties, like deflate, after enddef. */ +#define NC_EDIMSCALE (-124) /**< Probem with HDF5 dimscales. */ +#define NC_ENOGRP (-125) /**< No group found. */ +#define NC_ESTORAGE (-126) /**< Can't specify both contiguous and chunking. */ +#define NC_EBADCHUNK (-127) /**< Bad chunksize. */ +#define NC_ENOTBUILT (-128) /**< Attempt to use feature that was not turned on when netCDF was built. */ +#define NC_EDISKLESS (-129) /**< Error in using diskless access. */ +#define NC_ECANTEXTEND (-130) /**< Attempt to extend dataset during ind. I/O operation. */ +#define NC_EMPI (-131) /**< MPI operation failed. */ + +#define NC4_LAST_ERROR (-131) + +/* This is used in netCDF-4 files for dimensions without coordinate + * vars. */ +#define DIM_WITHOUT_VARIABLE "This is a netCDF dimension but not a netCDF variable." + +/* This is here at the request of the NCO team to support our + * mistake of having chunksizes be first ints, then size_t. Doh! */ +#define NC_HAVE_NEW_CHUNKING_API 1 + + +/*Errors for all remote access methods(e.g. DAP and CDMREMOTE)*/ +#define NC_EURL (NC_EDAPURL) /* Malformed URL */ +#define NC_ECONSTRAINT (NC_EDAPCONSTRAINT) /* Malformed Constraint*/ + + +/* + * The Interface + */ + +/* Declaration modifiers for DLL support (MSC et al) */ +#if defined(DLL_NETCDF) /* define when library is a DLL */ +# if defined(DLL_EXPORT) /* define when building the library */ +# define MSC_EXTRA __declspec(dllexport) +# else +# define MSC_EXTRA __declspec(dllimport) +# endif +#include +#else +#define MSC_EXTRA +#endif /* defined(DLL_NETCDF) */ + +# define EXTERNL MSC_EXTRA extern + +#if defined(DLL_NETCDF) /* define when library is a DLL */ +EXTERNL int ncerr; +EXTERNL int ncopts; +#endif + +EXTERNL const char * +nc_inq_libvers(void); + +EXTERNL const char * +nc_strerror(int ncerr); + +EXTERNL int +nc__create(const char *path, int cmode, size_t initialsz, + size_t *chunksizehintp, int *ncidp); + +EXTERNL int +nc_create(const char *path, int cmode, int *ncidp); + +EXTERNL int +nc__open(const char *path, int mode, + size_t *chunksizehintp, int *ncidp); + +EXTERNL int +nc_open(const char *path, int mode, int *ncidp); + +/* Learn the path used to open/create the file. */ +EXTERNL int +nc_inq_path(int ncid, size_t *pathlen, char *path); + +/* Given an ncid and group name (NULL gets root group), return + * locid. */ +EXTERNL int +nc_inq_ncid(int ncid, const char *name, int *grp_ncid); + +/* Given a location id, return the number of groups it contains, and + * an array of their locids. */ +EXTERNL int +nc_inq_grps(int ncid, int *numgrps, int *ncids); + +/* Given locid, find name of group. (Root group is named "/".) */ +EXTERNL int +nc_inq_grpname(int ncid, char *name); + +/* Given ncid, find full name and len of full name. (Root group is + * named "/", with length 1.) */ +EXTERNL int +nc_inq_grpname_full(int ncid, size_t *lenp, char *full_name); + +/* Given ncid, find len of full name. */ +EXTERNL int +nc_inq_grpname_len(int ncid, size_t *lenp); + +/* Given an ncid, find the ncid of its parent group. */ +EXTERNL int +nc_inq_grp_parent(int ncid, int *parent_ncid); + +/* Given a name and parent ncid, find group ncid. */ +EXTERNL int +nc_inq_grp_ncid(int ncid, const char *grp_name, int *grp_ncid); + +/* Given a full name and ncid, find group ncid. */ +EXTERNL int +nc_inq_grp_full_ncid(int ncid, const char *full_name, int *grp_ncid); + +/* Get a list of ids for all the variables in a group. */ +EXTERNL int +nc_inq_varids(int ncid, int *nvars, int *varids); + +/* Find all dimids for a location. This finds all dimensions in a + * group, or any of its parents. */ +EXTERNL int +nc_inq_dimids(int ncid, int *ndims, int *dimids, int include_parents); + +/* Find all user-defined types for a location. This finds all + * user-defined types in a group. */ +EXTERNL int +nc_inq_typeids(int ncid, int *ntypes, int *typeids); + +/* Are two types equal? */ +EXTERNL int +nc_inq_type_equal(int ncid1, nc_type typeid1, int ncid2, + nc_type typeid2, int *equal); + +/* Create a group. its ncid is returned in the new_ncid pointer. */ +EXTERNL int +nc_def_grp(int parent_ncid, const char *name, int *new_ncid); + +/* Rename a group */ +EXTERNL int +nc_rename_grp(int grpid, const char *name); + +/* Here are functions for dealing with compound types. */ + +/* Create a compound type. */ +EXTERNL int +nc_def_compound(int ncid, size_t size, const char *name, nc_type *typeidp); + +/* Insert a named field into a compound type. */ +EXTERNL int +nc_insert_compound(int ncid, nc_type xtype, const char *name, + size_t offset, nc_type field_typeid); + +/* Insert a named array into a compound type. */ +EXTERNL int +nc_insert_array_compound(int ncid, nc_type xtype, const char *name, + size_t offset, nc_type field_typeid, + int ndims, const int *dim_sizes); + +/* Get the name and size of a type. */ +EXTERNL int +nc_inq_type(int ncid, nc_type xtype, char *name, size_t *size); + +/* Get the id of a type from the name. */ +EXTERNL int +nc_inq_typeid(int ncid, const char *name, nc_type *typeidp); + +/* Get the name, size, and number of fields in a compound type. */ +EXTERNL int +nc_inq_compound(int ncid, nc_type xtype, char *name, size_t *sizep, + size_t *nfieldsp); + +/* Get the name of a compound type. */ +EXTERNL int +nc_inq_compound_name(int ncid, nc_type xtype, char *name); + +/* Get the size of a compound type. */ +EXTERNL int +nc_inq_compound_size(int ncid, nc_type xtype, size_t *sizep); + +/* Get the number of fields in this compound type. */ +EXTERNL int +nc_inq_compound_nfields(int ncid, nc_type xtype, size_t *nfieldsp); + +/* Given the xtype and the fieldid, get all info about it. */ +EXTERNL int +nc_inq_compound_field(int ncid, nc_type xtype, int fieldid, char *name, + size_t *offsetp, nc_type *field_typeidp, int *ndimsp, + int *dim_sizesp); + +/* Given the typeid and the fieldid, get the name. */ +EXTERNL int +nc_inq_compound_fieldname(int ncid, nc_type xtype, int fieldid, + char *name); + +/* Given the xtype and the name, get the fieldid. */ +EXTERNL int +nc_inq_compound_fieldindex(int ncid, nc_type xtype, const char *name, + int *fieldidp); + +/* Given the xtype and fieldid, get the offset. */ +EXTERNL int +nc_inq_compound_fieldoffset(int ncid, nc_type xtype, int fieldid, + size_t *offsetp); + +/* Given the xtype and the fieldid, get the type of that field. */ +EXTERNL int +nc_inq_compound_fieldtype(int ncid, nc_type xtype, int fieldid, + nc_type *field_typeidp); + +/* Given the xtype and the fieldid, get the number of dimensions for + * that field (scalars are 0). */ +EXTERNL int +nc_inq_compound_fieldndims(int ncid, nc_type xtype, int fieldid, + int *ndimsp); + +/* Given the xtype and the fieldid, get the sizes of dimensions for + * that field. User must have allocated storage for the dim_sizes. */ +EXTERNL int +nc_inq_compound_fielddim_sizes(int ncid, nc_type xtype, int fieldid, + int *dim_sizes); + +/** This is the type of arrays of vlens. */ +typedef struct { + size_t len; /**< Length of VL data (in base type units) */ + void *p; /**< Pointer to VL data */ +} nc_vlen_t; + +/** Calculate an offset for creating a compound type. This calls a + * mysterious C macro which was found carved into one of the blocks of + * the Newgrange passage tomb in County Meath, Ireland. This code has + * been carbon dated to 3200 B.C.E. */ +#define NC_COMPOUND_OFFSET(S,M) (offsetof(S,M)) + +/* Create a variable length type. */ +EXTERNL int +nc_def_vlen(int ncid, const char *name, nc_type base_typeid, nc_type *xtypep); + +/* Find out about a vlen. */ +EXTERNL int +nc_inq_vlen(int ncid, nc_type xtype, char *name, size_t *datum_sizep, + nc_type *base_nc_typep); + +/* When you read VLEN type the library will actually allocate the + * storage space for the data. This storage space must be freed, so + * pass the pointer back to this function, when you're done with the + * data, and it will free the vlen memory. */ +EXTERNL int +nc_free_vlen(nc_vlen_t *vl); + +EXTERNL int +nc_free_vlens(size_t len, nc_vlen_t vlens[]); + +/* Put or get one element in a vlen array. */ +EXTERNL int +nc_put_vlen_element(int ncid, int typeid1, void *vlen_element, + size_t len, const void *data); + +EXTERNL int +nc_get_vlen_element(int ncid, int typeid1, const void *vlen_element, + size_t *len, void *data); + +/* When you read the string type the library will allocate the storage + * space for the data. This storage space must be freed, so pass the + * pointer back to this function, when you're done with the data, and + * it will free the string memory. */ +EXTERNL int +nc_free_string(size_t len, char **data); + +/* Find out about a user defined type. */ +EXTERNL int +nc_inq_user_type(int ncid, nc_type xtype, char *name, size_t *size, + nc_type *base_nc_typep, size_t *nfieldsp, int *classp); + +/* Write an attribute of any type. */ +EXTERNL int +nc_put_att(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const void *op); + +/* Read an attribute of any type. */ +EXTERNL int +nc_get_att(int ncid, int varid, const char *name, void *ip); + +/* Enum type. */ + +/* Create an enum type. Provide a base type and a name. At the moment + * only ints are accepted as base types. */ +EXTERNL int +nc_def_enum(int ncid, nc_type base_typeid, const char *name, + nc_type *typeidp); + +/* Insert a named value into an enum type. The value must fit within + * the size of the enum type, the name size must be <= NC_MAX_NAME. */ +EXTERNL int +nc_insert_enum(int ncid, nc_type xtype, const char *name, + const void *value); + +/* Get information about an enum type: its name, base type and the + * number of members defined. */ +EXTERNL int +nc_inq_enum(int ncid, nc_type xtype, char *name, nc_type *base_nc_typep, + size_t *base_sizep, size_t *num_membersp); + +/* Get information about an enum member: a name and value. Name size + * will be <= NC_MAX_NAME. */ +EXTERNL int +nc_inq_enum_member(int ncid, nc_type xtype, int idx, char *name, + void *value); + + +/* Get enum name from enum value. Name size will be <= NC_MAX_NAME. */ +EXTERNL int +nc_inq_enum_ident(int ncid, nc_type xtype, long long value, char *identifier); + +/* Opaque type. */ + +/* Create an opaque type. Provide a size and a name. */ +EXTERNL int +nc_def_opaque(int ncid, size_t size, const char *name, nc_type *xtypep); + +/* Get information about an opaque type. */ +EXTERNL int +nc_inq_opaque(int ncid, nc_type xtype, char *name, size_t *sizep); + +/* Write entire var of any type. */ +EXTERNL int +nc_put_var(int ncid, int varid, const void *op); + +/* Read entire var of any type. */ +EXTERNL int +nc_get_var(int ncid, int varid, void *ip); + +/* Write one value. */ +EXTERNL int +nc_put_var1(int ncid, int varid, const size_t *indexp, + const void *op); + +/* Read one value. */ +EXTERNL int +nc_get_var1(int ncid, int varid, const size_t *indexp, void *ip); + +/* Write an array of values. */ +EXTERNL int +nc_put_vara(int ncid, int varid, const size_t *startp, + const size_t *countp, const void *op); + +/* Read an array of values. */ +EXTERNL int +nc_get_vara(int ncid, int varid, const size_t *startp, + const size_t *countp, void *ip); + +/* Write slices of an array of values. */ +EXTERNL int +nc_put_vars(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const void *op); + +/* Read slices of an array of values. */ +EXTERNL int +nc_get_vars(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + void *ip); + +/* Write mapped slices of an array of values. */ +EXTERNL int +nc_put_varm(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const void *op); + +/* Read mapped slices of an array of values. */ +EXTERNL int +nc_get_varm(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, void *ip); + +/* Extra netcdf-4 stuff. */ + +/* Set compression settings for a variable. Lower is faster, higher is + * better. Must be called after nc_def_var and before nc_enddef. */ +EXTERNL int +nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, + int deflate_level); + +/* Find out compression settings of a var. */ +EXTERNL int +nc_inq_var_deflate(int ncid, int varid, int *shufflep, + int *deflatep, int *deflate_levelp); + +/* Find out szip settings of a var. */ +EXTERNL int +nc_inq_var_szip(int ncid, int varid, int *options_maskp, int *pixels_per_blockp); + +/* Set fletcher32 checksum for a var. This must be done after nc_def_var + and before nc_enddef. */ +EXTERNL int +nc_def_var_fletcher32(int ncid, int varid, int fletcher32); + +/* Inquire about fletcher32 checksum for a var. */ +EXTERNL int +nc_inq_var_fletcher32(int ncid, int varid, int *fletcher32p); + +/* Define chunking for a variable. This must be done after nc_def_var + and before nc_enddef. */ +EXTERNL int +nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp); + +/* Inq chunking stuff for a var. */ +EXTERNL int +nc_inq_var_chunking(int ncid, int varid, int *storagep, size_t *chunksizesp); + +/* Define fill value behavior for a variable. This must be done after + nc_def_var and before nc_enddef. */ +EXTERNL int +nc_def_var_fill(int ncid, int varid, int no_fill, const void *fill_value); + +/* Inq fill value setting for a var. */ +EXTERNL int +nc_inq_var_fill(int ncid, int varid, int *no_fill, void *fill_valuep); + +/* Define the endianness of a variable. */ +EXTERNL int +nc_def_var_endian(int ncid, int varid, int endian); + +/* Learn about the endianness of a variable. */ +EXTERNL int +nc_inq_var_endian(int ncid, int varid, int *endianp); + +/* Set the fill mode (classic or 64-bit offset files only). */ +EXTERNL int +nc_set_fill(int ncid, int fillmode, int *old_modep); + +/* Set the default nc_create format to NC_FORMAT_CLASSIC, + * NC_FORMAT_64BIT, NC_FORMAT_NETCDF4, NC_FORMAT_NETCDF4_CLASSIC. */ +EXTERNL int +nc_set_default_format(int format, int *old_formatp); + +/* Set the cache size, nelems, and preemption policy. */ +EXTERNL int +nc_set_chunk_cache(size_t size, size_t nelems, float preemption); + +/* Get the cache size, nelems, and preemption policy. */ +EXTERNL int +nc_get_chunk_cache(size_t *sizep, size_t *nelemsp, float *preemptionp); + +/* Set the per-variable cache size, nelems, and preemption policy. */ +EXTERNL int +nc_set_var_chunk_cache(int ncid, int varid, size_t size, size_t nelems, + float preemption); + +/* Set the per-variable cache size, nelems, and preemption policy. */ +EXTERNL int +nc_get_var_chunk_cache(int ncid, int varid, size_t *sizep, size_t *nelemsp, + float *preemptionp); + +EXTERNL int +nc_redef(int ncid); + +/* Is this ever used? */ +EXTERNL int +nc__enddef(int ncid, size_t h_minfree, size_t v_align, + size_t v_minfree, size_t r_align); + +EXTERNL int +nc_enddef(int ncid); + +EXTERNL int +nc_sync(int ncid); + +EXTERNL int +nc_abort(int ncid); + +EXTERNL int +nc_close(int ncid); + +EXTERNL int +nc_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp); + +EXTERNL int +nc_inq_ndims(int ncid, int *ndimsp); + +EXTERNL int +nc_inq_nvars(int ncid, int *nvarsp); + +EXTERNL int +nc_inq_natts(int ncid, int *nattsp); + +EXTERNL int +nc_inq_unlimdim(int ncid, int *unlimdimidp); + +/* The next function is for NetCDF-4 only */ +EXTERNL int +nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp); + +/* Added in 3.6.1 to return format of netCDF file. */ +EXTERNL int +nc_inq_format(int ncid, int *formatp); + +/* Added in 4.3.1 to return additional format info */ +EXTERNL int +nc_inq_format_extended(int ncid, int *formatp, int* modep); + +/* Begin _dim */ + +EXTERNL int +nc_def_dim(int ncid, const char *name, size_t len, int *idp); + +EXTERNL int +nc_inq_dimid(int ncid, const char *name, int *idp); + +EXTERNL int +nc_inq_dim(int ncid, int dimid, char *name, size_t *lenp); + +EXTERNL int +nc_inq_dimname(int ncid, int dimid, char *name); + +EXTERNL int +nc_inq_dimlen(int ncid, int dimid, size_t *lenp); + +EXTERNL int +nc_rename_dim(int ncid, int dimid, const char *name); + +/* End _dim */ +/* Begin _att */ + +EXTERNL int +nc_inq_att(int ncid, int varid, const char *name, + nc_type *xtypep, size_t *lenp); + +EXTERNL int +nc_inq_attid(int ncid, int varid, const char *name, int *idp); + +EXTERNL int +nc_inq_atttype(int ncid, int varid, const char *name, nc_type *xtypep); + +EXTERNL int +nc_inq_attlen(int ncid, int varid, const char *name, size_t *lenp); + +EXTERNL int +nc_inq_attname(int ncid, int varid, int attnum, char *name); + +EXTERNL int +nc_copy_att(int ncid_in, int varid_in, const char *name, int ncid_out, int varid_out); + +EXTERNL int +nc_rename_att(int ncid, int varid, const char *name, const char *newname); + +EXTERNL int +nc_del_att(int ncid, int varid, const char *name); + +/* End _att */ +/* Begin {put,get}_att */ + +EXTERNL int +nc_put_att_text(int ncid, int varid, const char *name, + size_t len, const char *op); + +EXTERNL int +nc_get_att_text(int ncid, int varid, const char *name, char *ip); + +EXTERNL int +nc_put_att_uchar(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const unsigned char *op); + +EXTERNL int +nc_get_att_uchar(int ncid, int varid, const char *name, unsigned char *ip); + +EXTERNL int +nc_put_att_schar(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const signed char *op); + +EXTERNL int +nc_get_att_schar(int ncid, int varid, const char *name, signed char *ip); + +EXTERNL int +nc_put_att_short(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const short *op); + +EXTERNL int +nc_get_att_short(int ncid, int varid, const char *name, short *ip); + +EXTERNL int +nc_put_att_int(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const int *op); + +EXTERNL int +nc_get_att_int(int ncid, int varid, const char *name, int *ip); + +EXTERNL int +nc_put_att_long(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const long *op); + +EXTERNL int +nc_get_att_long(int ncid, int varid, const char *name, long *ip); + +EXTERNL int +nc_put_att_float(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const float *op); + +EXTERNL int +nc_get_att_float(int ncid, int varid, const char *name, float *ip); + +EXTERNL int +nc_put_att_double(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const double *op); + +EXTERNL int +nc_get_att_double(int ncid, int varid, const char *name, double *ip); + +EXTERNL int +nc_put_att_ushort(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const unsigned short *op); + +EXTERNL int +nc_get_att_ushort(int ncid, int varid, const char *name, unsigned short *ip); + +EXTERNL int +nc_put_att_uint(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const unsigned int *op); + +EXTERNL int +nc_get_att_uint(int ncid, int varid, const char *name, unsigned int *ip); + +EXTERNL int +nc_put_att_longlong(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const long long *op); + +EXTERNL int +nc_get_att_longlong(int ncid, int varid, const char *name, long long *ip); + +EXTERNL int +nc_put_att_ulonglong(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const unsigned long long *op); + +EXTERNL int +nc_get_att_ulonglong(int ncid, int varid, const char *name, + unsigned long long *ip); + +EXTERNL int +nc_put_att_string(int ncid, int varid, const char *name, + size_t len, const char **op); + +EXTERNL int +nc_get_att_string(int ncid, int varid, const char *name, char **ip); + +/* End {put,get}_att */ +/* Begin _var */ + +EXTERNL int +nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, + const int *dimidsp, int *varidp); + +EXTERNL int +nc_inq_var(int ncid, int varid, char *name, nc_type *xtypep, + int *ndimsp, int *dimidsp, int *nattsp); + +EXTERNL int +nc_inq_varid(int ncid, const char *name, int *varidp); + +EXTERNL int +nc_inq_varname(int ncid, int varid, char *name); + +EXTERNL int +nc_inq_vartype(int ncid, int varid, nc_type *xtypep); + +EXTERNL int +nc_inq_varndims(int ncid, int varid, int *ndimsp); + +EXTERNL int +nc_inq_vardimid(int ncid, int varid, int *dimidsp); + +EXTERNL int +nc_inq_varnatts(int ncid, int varid, int *nattsp); + +EXTERNL int +nc_rename_var(int ncid, int varid, const char *name); + +EXTERNL int +nc_copy_var(int ncid_in, int varid, int ncid_out); + +#ifndef ncvarcpy +/* support the old name for now */ +#define ncvarcpy(ncid_in, varid, ncid_out) ncvarcopy((ncid_in), (varid), (ncid_out)) +#endif + +/* End _var */ +/* Begin {put,get}_var1 */ + +EXTERNL int +nc_put_var1_text(int ncid, int varid, const size_t *indexp, const char *op); + +EXTERNL int +nc_get_var1_text(int ncid, int varid, const size_t *indexp, char *ip); + +EXTERNL int +nc_put_var1_uchar(int ncid, int varid, const size_t *indexp, + const unsigned char *op); + +EXTERNL int +nc_get_var1_uchar(int ncid, int varid, const size_t *indexp, + unsigned char *ip); + +EXTERNL int +nc_put_var1_schar(int ncid, int varid, const size_t *indexp, + const signed char *op); + +EXTERNL int +nc_get_var1_schar(int ncid, int varid, const size_t *indexp, + signed char *ip); + +EXTERNL int +nc_put_var1_short(int ncid, int varid, const size_t *indexp, + const short *op); + +EXTERNL int +nc_get_var1_short(int ncid, int varid, const size_t *indexp, + short *ip); + +EXTERNL int +nc_put_var1_int(int ncid, int varid, const size_t *indexp, const int *op); + +EXTERNL int +nc_get_var1_int(int ncid, int varid, const size_t *indexp, int *ip); + +EXTERNL int +nc_put_var1_long(int ncid, int varid, const size_t *indexp, const long *op); + +EXTERNL int +nc_get_var1_long(int ncid, int varid, const size_t *indexp, long *ip); + +EXTERNL int +nc_put_var1_float(int ncid, int varid, const size_t *indexp, const float *op); + +EXTERNL int +nc_get_var1_float(int ncid, int varid, const size_t *indexp, float *ip); + +EXTERNL int +nc_put_var1_double(int ncid, int varid, const size_t *indexp, const double *op); + +EXTERNL int +nc_get_var1_double(int ncid, int varid, const size_t *indexp, double *ip); + +EXTERNL int +nc_put_var1_ushort(int ncid, int varid, const size_t *indexp, + const unsigned short *op); + +EXTERNL int +nc_get_var1_ushort(int ncid, int varid, const size_t *indexp, + unsigned short *ip); + +EXTERNL int +nc_put_var1_uint(int ncid, int varid, const size_t *indexp, + const unsigned int *op); + +EXTERNL int +nc_get_var1_uint(int ncid, int varid, const size_t *indexp, + unsigned int *ip); + +EXTERNL int +nc_put_var1_longlong(int ncid, int varid, const size_t *indexp, + const long long *op); + +EXTERNL int +nc_get_var1_longlong(int ncid, int varid, const size_t *indexp, + long long *ip); + +EXTERNL int +nc_put_var1_ulonglong(int ncid, int varid, const size_t *indexp, + const unsigned long long *op); + +EXTERNL int +nc_get_var1_ulonglong(int ncid, int varid, const size_t *indexp, + unsigned long long *ip); + +EXTERNL int +nc_put_var1_string(int ncid, int varid, const size_t *indexp, + const char **op); + +EXTERNL int +nc_get_var1_string(int ncid, int varid, const size_t *indexp, + char **ip); + +/* End {put,get}_var1 */ +/* Begin {put,get}_vara */ + +EXTERNL int +nc_put_vara_text(int ncid, int varid, const size_t *startp, + const size_t *countp, const char *op); + +EXTERNL int +nc_get_vara_text(int ncid, int varid, const size_t *startp, + const size_t *countp, char *ip); + +EXTERNL int +nc_put_vara_uchar(int ncid, int varid, const size_t *startp, + const size_t *countp, const unsigned char *op); + +EXTERNL int +nc_get_vara_uchar(int ncid, int varid, const size_t *startp, + const size_t *countp, unsigned char *ip); + +EXTERNL int +nc_put_vara_schar(int ncid, int varid, const size_t *startp, + const size_t *countp, const signed char *op); + +EXTERNL int +nc_get_vara_schar(int ncid, int varid, const size_t *startp, + const size_t *countp, signed char *ip); + +EXTERNL int +nc_put_vara_short(int ncid, int varid, const size_t *startp, + const size_t *countp, const short *op); + +EXTERNL int +nc_get_vara_short(int ncid, int varid, const size_t *startp, + const size_t *countp, short *ip); + +EXTERNL int +nc_put_vara_int(int ncid, int varid, const size_t *startp, + const size_t *countp, const int *op); + +EXTERNL int +nc_get_vara_int(int ncid, int varid, const size_t *startp, + const size_t *countp, int *ip); + +EXTERNL int +nc_put_vara_long(int ncid, int varid, const size_t *startp, + const size_t *countp, const long *op); + +EXTERNL int +nc_get_vara_long(int ncid, int varid, + const size_t *startp, const size_t *countp, long *ip); + +EXTERNL int +nc_put_vara_float(int ncid, int varid, + const size_t *startp, const size_t *countp, const float *op); + +EXTERNL int +nc_get_vara_float(int ncid, int varid, + const size_t *startp, const size_t *countp, float *ip); + +EXTERNL int +nc_put_vara_double(int ncid, int varid, const size_t *startp, + const size_t *countp, const double *op); + +EXTERNL int +nc_get_vara_double(int ncid, int varid, const size_t *startp, + const size_t *countp, double *ip); + +EXTERNL int +nc_put_vara_ushort(int ncid, int varid, const size_t *startp, + const size_t *countp, const unsigned short *op); + +EXTERNL int +nc_get_vara_ushort(int ncid, int varid, const size_t *startp, + const size_t *countp, unsigned short *ip); + +EXTERNL int +nc_put_vara_uint(int ncid, int varid, const size_t *startp, + const size_t *countp, const unsigned int *op); + +EXTERNL int +nc_get_vara_uint(int ncid, int varid, const size_t *startp, + const size_t *countp, unsigned int *ip); + +EXTERNL int +nc_put_vara_longlong(int ncid, int varid, const size_t *startp, + const size_t *countp, const long long *op); + +EXTERNL int +nc_get_vara_longlong(int ncid, int varid, const size_t *startp, + const size_t *countp, long long *ip); + +EXTERNL int +nc_put_vara_ulonglong(int ncid, int varid, const size_t *startp, + const size_t *countp, const unsigned long long *op); + +EXTERNL int +nc_get_vara_ulonglong(int ncid, int varid, const size_t *startp, + const size_t *countp, unsigned long long *ip); + +EXTERNL int +nc_put_vara_string(int ncid, int varid, const size_t *startp, + const size_t *countp, const char **op); + +EXTERNL int +nc_get_vara_string(int ncid, int varid, const size_t *startp, + const size_t *countp, char **ip); + +/* End {put,get}_vara */ +/* Begin {put,get}_vars */ + +EXTERNL int +nc_put_vars_text(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const char *op); + +EXTERNL int +nc_get_vars_text(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + char *ip); + +EXTERNL int +nc_put_vars_uchar(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const unsigned char *op); + +EXTERNL int +nc_get_vars_uchar(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + unsigned char *ip); + +EXTERNL int +nc_put_vars_schar(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const signed char *op); + +EXTERNL int +nc_get_vars_schar(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + signed char *ip); + +EXTERNL int +nc_put_vars_short(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const short *op); + +EXTERNL int +nc_get_vars_short(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + short *ip); + +EXTERNL int +nc_put_vars_int(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const int *op); + +EXTERNL int +nc_get_vars_int(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + int *ip); + +EXTERNL int +nc_put_vars_long(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const long *op); + +EXTERNL int +nc_get_vars_long(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + long *ip); + +EXTERNL int +nc_put_vars_float(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const float *op); + +EXTERNL int +nc_get_vars_float(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + float *ip); + +EXTERNL int +nc_put_vars_double(int ncid, int varid, + const size_t *startp, const size_t *countp, const ptrdiff_t *stridep, + const double *op); + +EXTERNL int +nc_get_vars_double(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + double *ip); + +EXTERNL int +nc_put_vars_ushort(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const unsigned short *op); + +EXTERNL int +nc_get_vars_ushort(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + unsigned short *ip); + +EXTERNL int +nc_put_vars_uint(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const unsigned int *op); + +EXTERNL int +nc_get_vars_uint(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + unsigned int *ip); + +EXTERNL int +nc_put_vars_longlong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const long long *op); + +EXTERNL int +nc_get_vars_longlong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + long long *ip); + +EXTERNL int +nc_put_vars_ulonglong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const unsigned long long *op); + +EXTERNL int +nc_get_vars_ulonglong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + unsigned long long *ip); + +EXTERNL int +nc_put_vars_string(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const char **op); + +EXTERNL int +nc_get_vars_string(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + char **ip); + +/* End {put,get}_vars */ +/* Begin {put,get}_varm */ + +EXTERNL int +nc_put_varm_text(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const char *op); + +EXTERNL int +nc_get_varm_text(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, char *ip); + +EXTERNL int +nc_put_varm_uchar(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const unsigned char *op); + +EXTERNL int +nc_get_varm_uchar(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, unsigned char *ip); + +EXTERNL int +nc_put_varm_schar(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const signed char *op); + +EXTERNL int +nc_get_varm_schar(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, signed char *ip); + +EXTERNL int +nc_put_varm_short(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const short *op); + +EXTERNL int +nc_get_varm_short(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, short *ip); + +EXTERNL int +nc_put_varm_int(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const int *op); + +EXTERNL int +nc_get_varm_int(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, int *ip); + +EXTERNL int +nc_put_varm_long(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const long *op); + +EXTERNL int +nc_get_varm_long(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, long *ip); + +EXTERNL int +nc_put_varm_float(int ncid, int varid,const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const float *op); + +EXTERNL int +nc_get_varm_float(int ncid, int varid,const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, float *ip); + +EXTERNL int +nc_put_varm_double(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t *imapp, const double *op); + +EXTERNL int +nc_get_varm_double(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, double *ip); + +EXTERNL int +nc_put_varm_ushort(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, const unsigned short *op); + +EXTERNL int +nc_get_varm_ushort(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, unsigned short *ip); + +EXTERNL int +nc_put_varm_uint(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, const unsigned int *op); + +EXTERNL int +nc_get_varm_uint(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, unsigned int *ip); + +EXTERNL int +nc_put_varm_longlong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, const long long *op); + +EXTERNL int +nc_get_varm_longlong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, long long *ip); + +EXTERNL int +nc_put_varm_ulonglong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, const unsigned long long *op); + +EXTERNL int +nc_get_varm_ulonglong(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, unsigned long long *ip); + +EXTERNL int +nc_put_varm_string(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, const char **op); + +EXTERNL int +nc_get_varm_string(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, char **ip); + +/* End {put,get}_varm */ +/* Begin {put,get}_var */ + +EXTERNL int +nc_put_var_text(int ncid, int varid, const char *op); + +EXTERNL int +nc_get_var_text(int ncid, int varid, char *ip); + +EXTERNL int +nc_put_var_uchar(int ncid, int varid, const unsigned char *op); + +EXTERNL int +nc_get_var_uchar(int ncid, int varid, unsigned char *ip); + +EXTERNL int +nc_put_var_schar(int ncid, int varid, const signed char *op); + +EXTERNL int +nc_get_var_schar(int ncid, int varid, signed char *ip); + +EXTERNL int +nc_put_var_short(int ncid, int varid, const short *op); + +EXTERNL int +nc_get_var_short(int ncid, int varid, short *ip); + +EXTERNL int +nc_put_var_int(int ncid, int varid, const int *op); + +EXTERNL int +nc_get_var_int(int ncid, int varid, int *ip); + +EXTERNL int +nc_put_var_long(int ncid, int varid, const long *op); + +EXTERNL int +nc_get_var_long(int ncid, int varid, long *ip); + +EXTERNL int +nc_put_var_float(int ncid, int varid, const float *op); + +EXTERNL int +nc_get_var_float(int ncid, int varid, float *ip); + +EXTERNL int +nc_put_var_double(int ncid, int varid, const double *op); + +EXTERNL int +nc_get_var_double(int ncid, int varid, double *ip); + +EXTERNL int +nc_put_var_ushort(int ncid, int varid, const unsigned short *op); + +EXTERNL int +nc_get_var_ushort(int ncid, int varid, unsigned short *ip); + +EXTERNL int +nc_put_var_uint(int ncid, int varid, const unsigned int *op); + +EXTERNL int +nc_get_var_uint(int ncid, int varid, unsigned int *ip); + +EXTERNL int +nc_put_var_longlong(int ncid, int varid, const long long *op); + +EXTERNL int +nc_get_var_longlong(int ncid, int varid, long long *ip); + +EXTERNL int +nc_put_var_ulonglong(int ncid, int varid, const unsigned long long *op); + +EXTERNL int +nc_get_var_ulonglong(int ncid, int varid, unsigned long long *ip); + +EXTERNL int +nc_put_var_string(int ncid, int varid, const char **op); + +EXTERNL int +nc_get_var_string(int ncid, int varid, char **ip); + +/* Begin Deprecated, same as functions with "_ubyte" replaced by "_uchar" */ +EXTERNL int +nc_put_att_ubyte(int ncid, int varid, const char *name, nc_type xtype, + size_t len, const unsigned char *op); +EXTERNL int +nc_get_att_ubyte(int ncid, int varid, const char *name, + unsigned char *ip); +EXTERNL int +nc_put_var1_ubyte(int ncid, int varid, const size_t *indexp, + const unsigned char *op); +EXTERNL int +nc_get_var1_ubyte(int ncid, int varid, const size_t *indexp, + unsigned char *ip); +EXTERNL int +nc_put_vara_ubyte(int ncid, int varid, const size_t *startp, + const size_t *countp, const unsigned char *op); +EXTERNL int +nc_get_vara_ubyte(int ncid, int varid, const size_t *startp, + const size_t *countp, unsigned char *ip); +EXTERNL int +nc_put_vars_ubyte(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const unsigned char *op); +EXTERNL int +nc_get_vars_ubyte(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + unsigned char *ip); +EXTERNL int +nc_put_varm_ubyte(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, const unsigned char *op); +EXTERNL int +nc_get_varm_ubyte(int ncid, int varid, const size_t *startp, + const size_t *countp, const ptrdiff_t *stridep, + const ptrdiff_t * imapp, unsigned char *ip); +EXTERNL int +nc_put_var_ubyte(int ncid, int varid, const unsigned char *op); +EXTERNL int +nc_get_var_ubyte(int ncid, int varid, unsigned char *ip); +/* End Deprecated */ + +#ifdef LOGGING + +/* Set the log level. 0 shows only errors, 1 only major messages, + * etc., to 5, which shows way too much information. */ +EXTERNL int +nc_set_log_level(int new_level); + +/* Use this to turn off logging by calling + nc_log_level(NC_TURN_OFF_LOGGING) */ +#define NC_TURN_OFF_LOGGING (-1) + +#else /* not LOGGING */ + +#define nc_set_log_level(e) + +#endif /* LOGGING */ + +/* Show the netCDF library's in-memory metadata for a file. */ +EXTERNL int +nc_show_metadata(int ncid); + +/* End {put,get}_var */ + +/* #ifdef _CRAYMPP */ +/* + * Public interfaces to better support + * CRAY multi-processor systems like T3E. + * A tip of the hat to NERSC. + */ +/* + * It turns out we need to declare and define + * these public interfaces on all platforms + * or things get ugly working out the + * FORTRAN interface. On !_CRAYMPP platforms, + * these functions work as advertised, but you + * can only use "processor element" 0. + */ + +EXTERNL int +nc__create_mp(const char *path, int cmode, size_t initialsz, int basepe, + size_t *chunksizehintp, int *ncidp); + +EXTERNL int +nc__open_mp(const char *path, int mode, int basepe, + size_t *chunksizehintp, int *ncidp); + +EXTERNL int +nc_delete(const char *path); + +EXTERNL int +nc_delete_mp(const char *path, int basepe); + +EXTERNL int +nc_set_base_pe(int ncid, int pe); + +EXTERNL int +nc_inq_base_pe(int ncid, int *pe); + +/* #endif _CRAYMPP */ + +/* This v2 function is used in the nc_test program. */ +EXTERNL int +nctypelen(nc_type datatype); + +/* Begin v2.4 backward compatiblity */ +/* + * defining NO_NETCDF_2 to the preprocessor + * turns off backward compatiblity declarations. + */ +#ifndef NO_NETCDF_2 + +/** Backward compatible alias. */ +/**@{*/ +#define FILL_BYTE NC_FILL_BYTE +#define FILL_CHAR NC_FILL_CHAR +#define FILL_SHORT NC_FILL_SHORT +#define FILL_LONG NC_FILL_INT +#define FILL_FLOAT NC_FILL_FLOAT +#define FILL_DOUBLE NC_FILL_DOUBLE + +#define MAX_NC_DIMS NC_MAX_DIMS +#define MAX_NC_ATTRS NC_MAX_ATTRS +#define MAX_NC_VARS NC_MAX_VARS +#define MAX_NC_NAME NC_MAX_NAME +#define MAX_VAR_DIMS NC_MAX_VAR_DIMS +/**@}*/ + + +/* + * Global error status + */ +EXTERNL int ncerr; + +#define NC_ENTOOL NC_EMAXNAME /* Backward compatibility */ +#define NC_EXDR (-32) /* */ +#define NC_SYSERR (-31) + +/* + * Global options variable. + * Used to determine behavior of error handler. + */ +#define NC_FATAL 1 +#define NC_VERBOSE 2 + +EXTERNL int ncopts; /* default is (NC_FATAL | NC_VERBOSE) */ + +EXTERNL void +nc_advise(const char *cdf_routine_name, int err, const char *fmt,...); + +/* + * C data type corresponding to a netCDF NC_LONG argument, + * a signed 32 bit object. + * + * This is the only thing in this file which architecture dependent. + */ +typedef int nclong; + +EXTERNL int +nccreate(const char* path, int cmode); + +EXTERNL int +ncopen(const char* path, int mode); + +EXTERNL int +ncsetfill(int ncid, int fillmode); + +EXTERNL int +ncredef(int ncid); + +EXTERNL int +ncendef(int ncid); + +EXTERNL int +ncsync(int ncid); + +EXTERNL int +ncabort(int ncid); + +EXTERNL int +ncclose(int ncid); + +EXTERNL int +ncinquire(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimp); + +EXTERNL int +ncdimdef(int ncid, const char *name, long len); + +EXTERNL int +ncdimid(int ncid, const char *name); + +EXTERNL int +ncdiminq(int ncid, int dimid, char *name, long *lenp); + +EXTERNL int +ncdimrename(int ncid, int dimid, const char *name); + +EXTERNL int +ncattput(int ncid, int varid, const char *name, nc_type xtype, + int len, const void *op); + +EXTERNL int +ncattinq(int ncid, int varid, const char *name, nc_type *xtypep, int *lenp); + +EXTERNL int +ncattget(int ncid, int varid, const char *name, void *ip); + +EXTERNL int +ncattcopy(int ncid_in, int varid_in, const char *name, int ncid_out, + int varid_out); + +EXTERNL int +ncattname(int ncid, int varid, int attnum, char *name); + +EXTERNL int +ncattrename(int ncid, int varid, const char *name, const char *newname); + +EXTERNL int +ncattdel(int ncid, int varid, const char *name); + +EXTERNL int +ncvardef(int ncid, const char *name, nc_type xtype, + int ndims, const int *dimidsp); + +EXTERNL int +ncvarid(int ncid, const char *name); + +EXTERNL int +ncvarinq(int ncid, int varid, char *name, nc_type *xtypep, + int *ndimsp, int *dimidsp, int *nattsp); + +EXTERNL int +ncvarput1(int ncid, int varid, const long *indexp, const void *op); + +EXTERNL int +ncvarget1(int ncid, int varid, const long *indexp, void *ip); + +EXTERNL int +ncvarput(int ncid, int varid, const long *startp, const long *countp, + const void *op); + +EXTERNL int +ncvarget(int ncid, int varid, const long *startp, const long *countp, + void *ip); + +EXTERNL int +ncvarputs(int ncid, int varid, const long *startp, const long *countp, + const long *stridep, const void *op); + +EXTERNL int +ncvargets(int ncid, int varid, const long *startp, const long *countp, + const long *stridep, void *ip); + +EXTERNL int +ncvarputg(int ncid, int varid, const long *startp, const long *countp, + const long *stridep, const long *imapp, const void *op); + +EXTERNL int +ncvargetg(int ncid, int varid, const long *startp, const long *countp, + const long *stridep, const long *imapp, void *ip); + +EXTERNL int +ncvarrename(int ncid, int varid, const char *name); + +EXTERNL int +ncrecinq(int ncid, int *nrecvarsp, int *recvaridsp, long *recsizesp); + +EXTERNL int +ncrecget(int ncid, long recnum, void **datap); + +EXTERNL int +ncrecput(int ncid, long recnum, void *const *datap); + +/* End v2.4 backward compatiblity */ +#endif /*!NO_NETCDF_2*/ + +#if defined(__cplusplus) +} +#endif + +/* Temporary hack to shut up warnings */ +#ifndef __MINGW32_VERSION +#define END_OF_MAIN() +#endif + +/* Define two hard-coded functionality-related + macros, but this is not going to be + standard practice. */ +#ifndef NC_HAVE_RENAME_GRP +#define NC_HAVE_RENAME_GRP /*!< rename_grp() support. */ +#endif + +#ifndef NC_HAVE_INQ_FORMAT_EXTENDED +#define NC_HAVE_INQ_FORMAT_EXTENDED /*!< inq_format_extended() support. */ +#endif + +#define NC_HAVE_META_H + +#endif /* _NETCDF_ */ diff --git a/netcdf-sys/testdata/simple_xy.nc b/netcdf-sys/testdata/simple_xy.nc new file mode 100644 index 0000000000000000000000000000000000000000..010c0a95f2c4769d2c98aaad71f63d67ebc86e90 GIT binary patch literal 384 zcmXxdXHtSO5CBk6>=nh1fCWJm6v2iK1@Rg(;}Q6QxA!gIxHB)i$tIg#e~?Zku_#TE z9w+ + {{ + if (!$cast) && ($me.attrtype != $nc_type) { + return Err("Types are not equivalent and cast==false".to_string()); + } + let mut err: i32; + let mut attlen : u64 = 0; + let name_copy: ffi::CString = + ffi::CString::new($me.name.clone()).unwrap(); + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_inq_attlen($me.file_id, $me.var_id, name_copy.as_ptr(), + &mut attlen); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + if attlen != 1 { + return Err("Multi-value attributes not yet implemented".to_string()); + } + let mut buf: $rs_type = 0 as $rs_type; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = $nc_fn($me.file_id, $me.var_id, + name_copy.as_ptr(), + &mut buf); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + Ok(buf) + }} +} + +pub struct Attribute { + pub name : String, + pub attrtype : i32, + pub id: i32, + pub var_id: i32, + pub file_id: i32, +} + +impl Attribute { + pub fn get_char(&self, cast: bool) -> Result { + if (!cast) && (self.attrtype != nc_char) { + return Err("Types are not equivalent and cast==false".to_string()); + } + let attr_char_str; + let name_copy: ffi::CString = + ffi::CString::new(self.name.clone()).unwrap(); + let mut attlen : u64 = 0; + unsafe { + let mut err; + { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_inq_attlen(self.file_id, self.var_id, name_copy.as_ptr(), + &mut attlen); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + // careful; netcdf does not write null terminators here + let mut attr_char_buf_vec = vec![0i8; (attlen+1) as usize]; + let attr_char_buf_ptr: *mut i8 = attr_char_buf_vec.as_mut_ptr(); + { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_get_att_text(self.file_id, self.var_id, + name_copy.as_ptr(), + attr_char_buf_ptr); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + let attr_c_str = ffi::CStr::from_ptr(attr_char_buf_ptr); + attr_char_str = string_from_c_str(attr_c_str); + } + Ok(attr_char_str) + } + + pub fn get_byte(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_byte, i8, nc_get_att_schar, cast) + } + + pub fn get_short(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_short, i16, nc_get_att_short, cast) + } + + pub fn get_ushort(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_ushort, u16, nc_get_att_ushort, cast) + } + + pub fn get_int(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_int, i32, nc_get_att_int, cast) + } + + pub fn get_uint(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_uint, u32, nc_get_att_uint, cast) + } + + pub fn get_int64(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_int64, i64, nc_get_att_longlong, cast) + } + + pub fn get_uint64(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_uint64, u64, nc_get_att_ulonglong, cast) + } + + pub fn get_float(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_float, f32, nc_get_att_float, cast) + } + + pub fn get_double(&self, cast: bool) -> Result { + get_attr_as_type!(self, nc_double, f64, nc_get_att_double, cast) + } +} + +pub fn init_attributes(attrs: &mut HashMap, + file_id: i32, + var_id: i32, + natts_in: i32) { // TODO: better interface to indicate these are var attrs + let mut nattrs = 0i32; + if natts_in == -1 { + // these are global attrs; have to determine number of attrs + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let err = nc_inq_natts(file_id, &mut nattrs); + assert_eq!(err, nc_noerr); + } + } else { + nattrs = natts_in; + } + + // read each attr name, type, value + let mut attr_type: nc_type = 0; + for i_attr in 0..nattrs { + let mut name_buf_vec = vec![0i8; (nc_max_name + 1) as usize]; + let name_c_str: &ffi::CStr; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let name_buf_ptr : *mut i8 = name_buf_vec.as_mut_ptr(); + let err = nc_inq_attname(file_id, var_id, i_attr, name_buf_ptr); + assert_eq!(err, nc_noerr); + let err = nc_inq_atttype(file_id, var_id, name_buf_ptr, &mut attr_type); + assert_eq!(err, nc_noerr); + name_c_str = ffi::CStr::from_ptr(name_buf_ptr); + } + let name_str: String = string_from_c_str(name_c_str); + attrs.insert(name_str.clone(), + Attribute{name: name_str.clone(), + attrtype: attr_type, + id: i_attr, + var_id: var_id, + file_id: file_id}); + } +} diff --git a/src/dimension.rs b/src/dimension.rs new file mode 100644 index 0000000..3120b7b --- /dev/null +++ b/src/dimension.rs @@ -0,0 +1,40 @@ +use std::ffi; +use std::collections::HashMap; +use netcdf_sys::*; +use string_from_c_str; + +#[derive(Clone)] +pub struct Dimension { + pub name : String, + pub len: u64, + pub id: i32, +} + +pub fn init_dimensions(dims: &mut HashMap, grp_id: i32) { + // determine number of dims + let mut ndims = 0i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let err = nc_inq_ndims(grp_id, &mut ndims); + assert_eq!(err, nc_noerr); + } + + // read each dim name and length + for i_dim in 0..ndims { + let mut buf_vec = vec![0i8; (nc_max_name + 1) as usize]; + let mut dimlen : u64 = 0u64; + let c_str: &ffi::CStr; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let buf_ptr : *mut i8 = buf_vec.as_mut_ptr(); + let err = nc_inq_dim(grp_id, i_dim, buf_ptr, &mut dimlen); + assert_eq!(err, nc_noerr); + c_str = ffi::CStr::from_ptr(buf_ptr); + } + let str_buf: String = string_from_c_str(c_str); + dims.insert(str_buf.clone(), + Dimension{name: str_buf.clone(), + len: dimlen, + id: i_dim}); + } +} diff --git a/src/file.rs b/src/file.rs new file mode 100644 index 0000000..e32456f --- /dev/null +++ b/src/file.rs @@ -0,0 +1,78 @@ +use std::ffi; +use std::path; +use std::collections::HashMap; +use netcdf_sys::*; +use group::{init_group, Group}; +use NC_ERRORS; + +pub struct File { + pub id: i32, + pub name: String, + pub root: Group, +} + +pub fn open(file: &str) -> Result { + let data_path = path::Path::new(file); + let f = ffi::CString::new(data_path.to_str().unwrap()).unwrap(); + let mut ncid : i32 = -999999i32; + let err : i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_open(f.as_ptr(), nc_nowrite, &mut ncid); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + let mut root = Group { + name: "root".to_string(), + id: ncid, + variables: HashMap::new(), + attributes: HashMap::new(), + dimensions: HashMap::new(), + sub_groups: HashMap::new(), + }; + init_group(&mut root); + Ok(File { + id: ncid, + name: file.to_string(), + root: root, + }) +} + +pub fn create(file: &str) -> Result { + let data_path = path::Path::new(file); + let f = ffi::CString::new(data_path.to_str().unwrap()).unwrap(); + let mut ncid : i32 = -999999i32; + let err : i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_create(f.as_ptr(), nc_write|nc_netcdf4, &mut ncid); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + let root = Group { + name: "root".to_string(), + id: ncid, + variables: HashMap::new(), + attributes: HashMap::new(), + dimensions: HashMap::new(), + sub_groups: HashMap::new(), + }; + Ok(File { + id: ncid, + name: file.to_string(), + root: root, + }) +} + +impl Drop for File { + fn drop(&mut self) { + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let err = nc_close(self.id); + assert_eq!(err, nc_noerr); + } + } +} + diff --git a/src/group.rs b/src/group.rs new file mode 100644 index 0000000..81fd95a --- /dev/null +++ b/src/group.rs @@ -0,0 +1,367 @@ +use std::collections::HashMap; +use std::ffi; +use netcdf_sys::*; +use dimension::{init_dimensions, Dimension}; +use attribute::{init_attributes, Attribute}; +use variable::{init_variables, Variable}; +use string_from_c_str; +use NC_ERRORS; + +pub struct Group { + pub name : String, + pub id : i32, + pub variables : HashMap, + pub attributes : HashMap, + pub dimensions : HashMap, + pub sub_groups : HashMap, +} + +macro_rules! put_var_as_type { + ( $me:ident, $ncid:ident, $varid:ident, $nc_fn:ident ) + => + {{ + let err : i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = $nc_fn($ncid, $varid, $me.as_ptr()); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + Ok(()) + }}; +} + +macro_rules! put_attr_as_type { + ( $me:ident, $attname:ident, $ncid:ident, $nctype: ident, + $varid:ident, $nc_fn:ident ) + => + {{ + let name_c: ffi::CString = ffi::CString::new($attname.clone()).unwrap(); + let err : i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = $nc_fn($ncid, $varid, name_c.as_ptr(), $nctype, 1, $me); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + Ok(()) + }}; +} + +// Write support for all variable types ... excuse the repetition :( +pub trait PutVar { + fn get_nc_type(&self) -> i32; + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> ; + fn len(&self) -> usize; +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_byte } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_schar) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_short } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_short) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_ushort } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_ushort) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_int } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_int) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_uint } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_uint) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_int64 } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_longlong) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_uint64 } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_ulonglong) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_float } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_float) + } +} + +impl PutVar for Vec { + fn get_nc_type(&self) -> i32 { nc_double } + fn len(&self) -> usize { self.len() } + fn put(&self, ncid: i32, varid: i32) -> Result<(), String> { + put_var_as_type!(self, ncid, varid, nc_put_var_double) + } +} + + +// Write support for all attribute types +pub trait PutAttr { + fn get_nc_type(&self) -> i32; + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> ; +} + +impl PutAttr for i8 { + fn get_nc_type(&self) -> i32 { nc_byte } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_byte, varid, nc_put_att_schar) + } +} + +impl PutAttr for i16 { + fn get_nc_type(&self) -> i32 { nc_short } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_short, varid, nc_put_att_short) + } +} + +impl PutAttr for u16 { + fn get_nc_type(&self) -> i32 { nc_ushort } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_ushort, varid, nc_put_att_ushort) + } +} + +impl PutAttr for i32 { + fn get_nc_type(&self) -> i32 { nc_int } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_int, varid, nc_put_att_int) + } +} + +impl PutAttr for u32 { + fn get_nc_type(&self) -> i32 { nc_uint } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_uint, varid, nc_put_att_uint) + } +} + +impl PutAttr for i64 { + fn get_nc_type(&self) -> i32 { nc_int64 } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_int64, varid, nc_put_att_longlong) + } +} + +impl PutAttr for u64 { + fn get_nc_type(&self) -> i32 { nc_uint64 } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_uint64, varid, nc_put_att_ulonglong) + } +} + +impl PutAttr for f32 { + fn get_nc_type(&self) -> i32 { nc_float } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_float, varid, nc_put_att_float) + } +} + +impl PutAttr for f64 { + fn get_nc_type(&self) -> i32 { nc_double } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + put_attr_as_type!(self, name, ncid, nc_double, varid, nc_put_att_double) + } +} + +impl PutAttr for String { + fn get_nc_type(&self) -> i32 { nc_char } + fn put(&self, ncid: i32, varid: i32, name: &str) -> Result<(), String> { + let name_c: ffi::CString = ffi::CString::new(name.clone()).unwrap(); + let attr_c: ffi::CString = ffi::CString::new(self.clone()).unwrap(); + let err : i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_put_att_text( + ncid, varid, name_c.as_ptr(), + attr_c.to_bytes().len() as u64, attr_c.as_ptr()); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + Ok(()) + } +} + +impl Group { + pub fn add_attribute(&mut self, name: &str, val: T) + -> Result<(), String> { + try!(val.put(self.id, nc_global, name)); + self.attributes.insert( + name.to_string().clone(), + Attribute { + name: name.to_string().clone(), + attrtype: val.get_nc_type(), + id: 0, // XXX Should Attribute even keep track of an id? + var_id: nc_global, + file_id: self.id + } + ); + Ok(()) + } + + pub fn add_dimension(&mut self, name: &str, len: u64) + -> Result<(), String> { + let name_c: ffi::CString = ffi::CString::new(name.clone()).unwrap(); + let mut dimid: i32 = 0; + let err : i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_def_dim(self.id, name_c.as_ptr(), len, &mut dimid); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + self.dimensions.insert( + name.to_string().clone(), + Dimension { + name: name.to_string().clone(), + len: len, + id: dimid + } + ); + Ok(()) + } + + // TODO this should probably take &Vec<&str> instead of &Vec + pub fn add_variable( + &mut self, name: &str, dims: &Vec, data: &T) + -> Result<(), String> + { + let name_c: ffi::CString = ffi::CString::new(name.clone()).unwrap(); + let mut dimids: Vec = Vec::with_capacity(dims.len()); + let mut var_len : u64 = 1; + let mut var_dims : Vec = Vec::with_capacity(dims.len()); + let nctype = data.get_nc_type(); + for dim_name in dims { + if !self.dimensions.contains_key(dim_name) { + return Err("Invalid dimension name".to_string()); + } + var_dims.push(self.dimensions.get(dim_name).unwrap().clone()); + } + for dim in &var_dims { + dimids.push(dim.id); + var_len *= dim.len; + } + if data.len() != (var_len as usize) { + return Err("Vec length must match product of all dims".to_string()); + } + let mut varid: i32 = 0; + let err : i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + err = nc_def_var(self.id, name_c.as_ptr(), nctype, + dims.len() as i32, dimids.as_ptr(), &mut varid); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + try!(data.put(self.id, varid)); + self.variables.insert( + name.to_string().clone(), + Variable { + name: name.to_string().clone(), + attributes: HashMap::new(), + dimensions: var_dims, + vartype: nctype, + id: varid, + len: var_len, + file_id: self.id + } + ); + Ok(()) + } +} + +fn init_sub_groups(grp_id: i32, sub_groups: &mut HashMap, + parent_dims: &HashMap) { + let mut ngrps = 0i32; + // Max number of groups in a file is only limited by i32 max (32767)... + // allocating a vec this size is inefficient but there's no obvious way + // to query the number of groups beforehand! + // http://www.unidata.ucar.edu/software/netcdf/docs/group__groups.html#details + let mut grpids : Vec = Vec::with_capacity(nc_max_int as usize); + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + + // number of groups and grp id's + let err = nc_inq_grps(grp_id, &mut ngrps, grpids.as_mut_ptr()); + assert_eq!(err, nc_noerr); + grpids.set_len(ngrps as usize); + } + for i_grp in 0..ngrps { + let mut namelen = 0u64; + let c_str: &ffi::CStr; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + // name length + let err = nc_inq_grpname_len(grpids[i_grp as usize], &mut namelen); + assert_eq!(err, nc_noerr); + // name + let mut buf_vec = vec![0i8; (namelen+1) as usize]; + let buf_ptr : *mut i8 = buf_vec.as_mut_ptr(); + let err = nc_inq_grpname(grpids[i_grp as usize], buf_ptr); + assert_eq!(err, nc_noerr); + c_str = ffi::CStr::from_ptr(buf_ptr); + } + let str_buf: String = string_from_c_str(c_str); + + // Per NetCDF doc, "Dimensions are visible in their groups, and all + // child groups." + let mut new_grp = Group { + name: str_buf.clone(), + id: grpids[i_grp as usize], + variables: HashMap::new(), + attributes: HashMap::new(), + dimensions: parent_dims.clone(), + sub_groups: HashMap::new(), + }; + init_group(&mut new_grp); + sub_groups.insert(str_buf.clone(), new_grp); + } +} + +pub fn init_group(grp: &mut Group) { + init_dimensions(&mut grp.dimensions, grp.id); + + init_attributes(&mut grp.attributes, grp.id, nc_global, -1); + + init_variables(&mut grp.variables, grp.id, &grp.dimensions); + + init_sub_groups(grp.id, &mut grp.sub_groups, &grp.dimensions); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f1f9c22 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,108 @@ +//! Rust bindings for Unidata's [libnetcdf] (http://www.unidata.ucar.edu/software/netcdf/) +//! +//! # Examples +//! +//! Read: +//! +//! ``` +//! # let path_to_simple_xy = netcdf::test_file("simple_xy.nc"); +//! // Open file simple_xy.nc: +//! let file = netcdf::open(&path_to_simple_xy).unwrap(); +//! +//! // Access any variable, attribute, or dimension through simple HashMap's: +//! let var = file.root.variables.get("data").unwrap(); +//! +//! // Read variable as any NC_TYPE, optionally failing if doing so would +//! // force a cast: +//! let data : Vec = var.get_int(false).unwrap(); +//! +//! // All variable data is read into 1-dimensional Vec. +//! for x in 0..(6*12) { +//! assert_eq!(data[x], x as i32); +//! } +//! ``` +//! +//! Write: +//! +//! ``` +//! let f = netcdf::test_file_new("crabs.nc"); // just gets a path inside repo +//! +//! let mut file = netcdf::create(&f).unwrap(); +//! +//! let dim_name = "ncrabs"; +//! file.root.add_dimension(dim_name, 10).unwrap(); +//! +//! let var_name = "crab_coolness_level"; +//! let data : Vec = vec![42; 10]; +//! // Variable type written to file is inferred from Vec type: +//! file.root.add_variable( +//! var_name, +//! &vec![dim_name.to_string()], +//! &data +//! ).unwrap(); +//! ``` + +extern crate netcdf_sys; + +#[macro_use] +extern crate lazy_static; + +use netcdf_sys::{libnetcdf_lock, nc_strerror}; +use std::ffi; +use std::str; +use std::path; +use std::env; +use std::fs; +use std::collections::HashMap; + +pub mod file; +pub mod variable; +pub mod attribute; +pub mod group; +pub mod dimension; + +pub use file::open; +pub use file::create; + +fn string_from_c_str(c_str: &ffi::CStr) -> String { + // see http://stackoverflow.com/questions/24145823/rust-ffi-c-string-handling + // for good rundown + let buf: &[u8] = c_str.to_bytes(); + let str_slice: &str = str::from_utf8(buf).unwrap(); + str_slice.to_owned() +} + + +lazy_static! { + pub static ref NC_ERRORS: HashMap = { + let mut m = HashMap::new(); + // Invalid error codes are ok; nc_strerror will just return + // "Unknown Error" + for i in -256..256 { + let msg_cstr : &ffi::CStr; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let msg : *const i8 = nc_strerror(i); + msg_cstr = &ffi::CStr::from_ptr(msg); + } + m.insert(i, string_from_c_str(msg_cstr)); + } + m + }; +} + +// Helpers for getting file paths +pub fn test_file(f: &str) -> String { + let mnf_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let path = path::Path::new(&mnf_dir).join( + "testdata").join(f); + path.to_str().unwrap().to_string() +} + +pub fn test_file_new(f: &str) -> String { + let mnf_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + let path = path::Path::new(&mnf_dir).join("testout"); + let new_file = path.join(f); + let _err = fs::create_dir(path); + new_file.to_str().unwrap().to_string() +} diff --git a/src/variable.rs b/src/variable.rs new file mode 100644 index 0000000..c69fe5f --- /dev/null +++ b/src/variable.rs @@ -0,0 +1,144 @@ +use std::ffi; +use std::collections::HashMap; +use netcdf_sys::*; +use dimension::Dimension; +use group::PutAttr; +use attribute::{init_attributes, Attribute}; +use string_from_c_str; +use NC_ERRORS; + +macro_rules! get_var_as_type { + ( $me:ident, $nc_type:ident, $vec_type:ty, $nc_fn:ident , $cast:ident ) + => + {{ + if (!$cast) && ($me.vartype != $nc_type) { + return Err("Types are not equivalent and cast==false".to_string()); + } + let mut buf: Vec<$vec_type> = Vec::with_capacity($me.len as usize); + let err: i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + buf.set_len($me.len as usize); + err = $nc_fn($me.file_id, $me.id, buf.as_mut_ptr()); + } + if err != nc_noerr { + return Err(NC_ERRORS.get(&err).unwrap().clone()); + } + Ok(buf) + }}; +} + +pub struct Variable { + pub name : String, + pub attributes : HashMap, + pub dimensions : Vec, + pub vartype : i32, + pub id: i32, + pub len: u64, // total length; the product of all dim lengths + pub file_id: i32, +} + +impl Variable { + pub fn get_char(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_char, u8, nc_get_var_uchar, cast) + } + pub fn get_byte(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_byte, i8, nc_get_var_schar, cast) + } + pub fn get_short(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_short, i16, nc_get_var_short, cast) + } + pub fn get_ushort(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_ushort, u16, nc_get_var_ushort, cast) + } + pub fn get_int(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_int, i32, nc_get_var_int, cast) + } + pub fn get_uint(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_uint, u32, nc_get_var_uint, cast) + } + pub fn get_int64(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_int64, i64, nc_get_var_longlong, cast) + } + pub fn get_uint64(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_uint64, u64, nc_get_var_ulonglong, cast) + } + pub fn get_float(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_float, f32, nc_get_var_float, cast) + } + pub fn get_double(&self, cast: bool) -> Result, String> { + get_var_as_type!(self, nc_double, f64, nc_get_var_double, cast) + } + + pub fn add_attribute(&mut self, name: &str, val: T) + -> Result<(), String> { + try!(val.put(self.file_id, self.id, name)); + self.attributes.insert( + name.to_string().clone(), + Attribute { + name: name.to_string().clone(), + attrtype: val.get_nc_type(), + id: 0, // XXX Should Attribute even keep track of an id? + var_id: self.id, + file_id: self.file_id + } + ); + Ok(()) + } +} + +pub fn init_variables(vars: &mut HashMap, grp_id: i32, + grp_dims: &HashMap) { + // determine number of vars + let mut nvars = 0i32; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let err = nc_inq_nvars(grp_id, &mut nvars); + assert_eq!(err, nc_noerr); + } + // read each dim name and length + for i_var in 0..nvars { + let mut buf_vec = vec![0i8; (nc_max_name + 1) as usize]; + let c_str: &ffi::CStr; + let mut var_type : i32 = 0; + let mut ndims : i32 = 0; + let mut dimids : Vec = Vec::with_capacity(nc_max_dims as usize); + let mut natts : i32 = 0; + unsafe { + let _g = libnetcdf_lock.lock().unwrap(); + let buf_ptr : *mut i8 = buf_vec.as_mut_ptr(); + let err = nc_inq_var(grp_id, i_var, buf_ptr, + &mut var_type, &mut ndims, + dimids.as_mut_ptr(), &mut natts); + dimids.set_len(ndims as usize); + assert_eq!(err, nc_noerr); + c_str = ffi::CStr::from_ptr(buf_ptr); + } + let str_buf: String = string_from_c_str(c_str); + let mut attr_map : HashMap = HashMap::new(); + init_attributes(&mut attr_map, grp_id, i_var, natts); + // var dims should always be a subset of the group dims: + let mut dim_vec : Vec = Vec::new(); + let mut len : u64 = 1; + for dimid in dimids { + // maintaining dim order is crucial here so we can maintain + // rule that "last dim varies fastest" in our 1D return Vec + for (_, grp_dim) in grp_dims { + if dimid == grp_dim.id { + len *= grp_dim.len; + dim_vec.push(grp_dim.clone()); + break + } + } + } + vars.insert(str_buf.clone(), + Variable{name: str_buf.clone(), + attributes: attr_map, + dimensions: dim_vec, + vartype: var_type, + len: len, + id: i_var, + file_id: grp_id}); + } +} + diff --git a/testdata/patmosx_v05r03-preliminary_NOAA-19_asc_d20130630_c20140325.nc b/testdata/patmosx_v05r03-preliminary_NOAA-19_asc_d20130630_c20140325.nc new file mode 100644 index 0000000000000000000000000000000000000000..1c844dcd9de933c1cd3efddb3b7e1d8107b9ae35 GIT binary patch literal 16409 zcmeHN33OZ4nf?>8BtRf+28Tc%kc1GtX!S;b$kMZ=U`a+#b`~g)XX(XOBTGh-?U)=I z_9X`)Rv%YowlLBfsL(Q?E=2%cO%TODhy<}d8 z23+LY-TG!DqEc(UWsY)h7Pvy&X60N!$0rivW>==H~NX1w<`=g^7C5%sKL~z37t~ z{OS^DsBq%Oedzn>6A)vS^hl>;4vp@MUszE+n#3*3v2_0Qd!jwiY!3$ZqYDqluU;Y* z%^7pH&ZTXRU=7)M0BqqkOE zx`5KWK_JsWbL((m5p7`(H49>CNA9X)=|vuLGEoPb!|B|5dW<2O1RC0~{j^5fhv&m* z=96*QLKm&t2fx6NMQFx+k2F&g^Wy{}4b53sCt9gS!7ohPhvuRC_GzOe4?cxxEt=2% z_`&hCXa;`$k-&eVP4`@}jF^MGe11;bv+e}Cia(7K@npY_Zav|ND-z6*oqojr&z9z) z3}XZLm>6l#=pKFpE(ojjK6k)0=D$R_w2SmSZ|S!nRt)Clkyx>y`9kiP*d+c^!)RPY zELGB;N=v5wSTX=6j>mHDK)_gv=rZm={PyF{@3?aeSvufz?sW6{!_&odtRgyqJJ)l^ zLv$c_YM7{YqJy~eJbyVFHk>=F_zU(`?8e#L!LkqGFO||r@A8-BM7-}*$E!kT9(OX_ zf$lksJCAc`A<=y9yvrTP=Hc9F<;8RYA71&m`)X{@3cTEP{{Mo{|14U@ zk_o}VIVElO@Oe~uHcvV4kqaor2PPyG8l9SYeUl{Z&JQ2FjEcPNun?_ijQ5`3EFHDw z+8tZy78bkZLJnz6y=$v<=pPPW@I5-6)jDR0q4~q1H~cOAl&38aorq@hqQ|e1l+ziv zKKc{7i6#FDqHm*ly77$bXeMg_m8cQTCvRSPy`&8O#aVxwbhsZ~-FOFW;^jj_oQ`JO zUvI&Sio>`4qmDbNlaYcjjuTRMslT8aK9?Gd5}KFq{QQ@al6bGR{T}LP9fc!zEE;bo z{gxi%bB&ehX#VL>%@2{6&lDi)(72y@<57BqeHO3~=b0iG{oA9oniq$CNuxRI?4uu} zR%Y_?!UEFR%ZnbTbqZd80<6@QpK5(w3Ou3(heI82w_24@A!$9ai>!ENg9b3@L|{_>@)~v&jBZOIU3r? z9h`LFfP&!88o^YOwBU;Sk39PS*!~Ou|08x&px)r4kTG`ZQ<6YDdGRlwmX@&Rvw!*r zs&u|C_@MC_X$S8({lsS_=KN~$hUX-nY>1!!&l1aTKiqgha)*{4@x=?0zgL@5UZ(eL z3vmi4_1xiqg@Wve!OJ)qO>Wu2uSy=#16%HVUGj>YBeicxe%1>2HE&9;(l0J{?T`dL z@x7KEl6U#>zc;)iiRhXGuX=~hViul2v>MHRuZ?~vN#}cx)&C)>gt7me^|9nm-}b=) zpGhw07gxOc8U3Dps$+y}M%(`NJ71l!J_CjPzpDSm`L7JKq~M1Zv(pRq!u2D{a|Z?s zl0tO1-3fE2GS-u}M2fAXFiHwKCCg7Y+MC?Xs4I6|Vu%!BQGwlVa3;ZHDq~Uln7_b~ z_VSln-X=J)E4kCj9q5%++^O6^`a63!*YKB0XP7FFisUy*UW8=qC1*j3KBbsYvgMLX zP6r$bG9dDZmSF1b&RXtN2I}-Be>s(CfWK7w4w9Gj^COPAiUIhs z(4M=Yx#aMR+9g)qyTdbqb= zQsviX(*qJ`-#F{q0V#@m=$w3pD&xY9+MPMc23_nSFhs>fwHUh!S z8F>s0S)&blF(lPM5dd z>8W@763(VJcT1bcyT&4ND4$CWC(V>HTu5j8mBFNM48X23wq$N_(99MEjknv_5(6fv zutpXOijg;!r9*i$lO9ZGLDSMYWzaOTjIKCfjwz!i0Dxy`xQKa1)5U@EG)mGCoGF40 z0Ld1LMwT&V87`Ai`b=ebC}r@tVoq7Q7Wgwum0UJyDp_-Ms?nIv7%|crqc3B^!0mc= zabOn*c5&b@f&(3)aE1M6l4?a{jkd)iu-0q0PP=78gQr$xAetD?9nz zanTPyk-ZYmcrQHj-Y*=ust_inkQ>e?O<@DR3nRsLDynG>ouPQVA==##ba`AY&IZlx z@p>9mr_1AMXm`7tJWh|Rp)+v2@@KHF2F>Ymw*c1Tc3ZG6kGnzZodVY5Y67g!<+5Pi zm|$gi3Rs`h2iPWGiv{cPc&CPK^0@)q;&ocEUXO2T*cKpCT~1IU*5>mxO}REeIRWeP zHCwPvp601xT|O6JJsz9hX8f3nXMh5|7+}M;cv`21^|`PYt|qV7qM;RwoQh|zCLmH> zEpBVyyiTucYS67nw*~flM5ByL5m(M@v9R6x!>n9GJc(ML|GglZgifJRG zXL4B`ZeG=V^nB=fXi7ZB`sv_-A3g~^Mo}M}2A+Yp?rNVB&otJx_N}{q>i~Czek`U3 zO+A?#G4n>hsl$oQTMjcPJ6T!cxOnQFP4ymU!s%=ie`_4Lc%5XhnN0~jJIPSDr_I-j zKMZB}9Pvonf-QOZ*rLH)VZA=$^yQr%J)1L(dRMD%6q0(1NpM0X>q-3bLht$-vPlOn znFS&~8i+50s+gODS|%F<9k}-u5P?{vJEnybs-6f(R6P{yiE9Fw^CArF#KWQ7WY8m&($|KcqXQ(}f!L~HX%3@kt;@qEYgqEnCOiT3oyTZZg z=^P8&VUuA)N^TGSotHf z$DiULRX6f^V@#lK0l7BiZXY6Tar{8JF=}KG*$ToBwRWpJ9t)_N#u6*`DH4wMBvh?x z_4anQcDVy3G(z1*W6?k?+7S+_(S&7b^oUBsJvua+P{o?Zvlt;EtXXKoo zb08OcSaU{urZe=l*B7hhN&ULVRW-TY&uzcIJhFW{BdF8HiJSe_K*x~nGl z+RrbN$gO^4Xu$+>s~>vGvoVm%#}x-wi*@h&iSbI%{ zq6=29CLx$OvyxR3nz7O<@%3lfDrF5=CB4rWGYe@WYg>;&qp(gNMBWqWLAFq>wwia< zdt8dMrOnma=JCmD-gZ)i+ixl3L>H zIlYMNcCk=QH5Atu>qZn8vc7C=8lA32ceCPTBi06cIc_D@^+Rka546-~v?tQ8#`Rc- zu6C&r7z*thb>`ocE(e&4@*|&rRuZ?x^F`j=#QT~O&mkl9)+->%ntYT z+3|GkXeB#r4AWxAZbQdcvcs+K6gy69{IrrCZuqFvX2;LhEdV>pd+YH}pC6Y#`|`4i zT`8fM7DEm=e>oU(Kxt=zW2ZQB(7r4nb1Ts71WR7Ja}+w#R&iQSR7bdhKqeU0#3f$U z()uIiJQzU^Eum`z)!VUqpX+QMA6LC@U%d35lP1`I_+Heu@yZ{SFM%@Yq?s*P2|!f; z3iTSzSr;~WBh6PfxWB>+bY&!lfH9$Vb#Yi5sAk?*#Q23)G{IN2 zxSK7dY)a>-6emah5i6YL+hSfWls?}JI#9?gqN=Dr+^g#C{?)1$#;tFKZ7C1slrImh z){w{nJqK)p;as82Xu89z)Gkgz3BL7cs_tsn6CpJoLF^nrz}k+xTuz&Hboo2KaT9lTtGu?}NrVSj2;*wh-y80nHa&Jsz>Cum?fawtuGLx?5l=NsvgE4PqCgt1A>o0% zUsYHv#>}>`L|1lKH_DRDBdt-8X3!kyf)dfSljU-Ns%WrQV#mG7)S%9%Qv~HgB1&1C z&OCBR6a9bwc-;G^BhO^#w<>OFbq?}Nm)5K(SgyuzjqQu_b>`S;E}ybO6%S}(D4`G) zEf7{w)S#TGborw}MeB)o_(iw$D~`+#cD+|s5LBbOAQ4tIKvzgsoiY)XKY^rBHw2mS zmOJ5qVk3)1zX^lZjcSXyzpGJ=c7~&>8b>IPG2tkJ?zkFMR{DG4Xhk~{Att6h4mTzd zMI^0MltBDBT~lDSa$O3N9h|4eTv{R=V7$Fy6}hrbrBjU|@fBZP-hmQB^^siC=o`-9 znqBN;)ZT%pAN2yAnOvWdQ33-7(ke;>p&fD*R(S%77L+BOfk^OJMMZXUK+%$EldsWF zws)$oTxrnKfIMEUrK04-X5K91GQ)gVFXk?#3A#&O{=vn9}>Xf~V@1)P%Lss&}qh}&1E!2e${ zoEhW5z=Hyh{jKEy9dZ-8FN3bK`>~_{W>suQ#kn>F0 zeSjwDgu3VyuqC8b_t85)41H3H^xe$ z*H_fFlnE~k4-S?N-~%?e63!+w!zmQ1q_Ze{Nf)g08m^5&ZW`LDetl!0%ikNXUtca; z0dU@2n?d!EfumTb^yN9LnPvZ}XbukHHlaAowJfNH5joZIlADw{VrFtfK+rd)MBzs9 ztjSykB`-OQ;%XXEXM-#yORLb?#W5~VTFJ7SMXgVL#7LzJ01Sj9npS3HGL_dyjC`7< zP%v^^X@N|taCe3W5x)-B3ED+%n)HI}%A^-u7gl<~%MyiR(hIKhl3$=stMnq7K`xnx z8OZ^o4va|Y=3u%|sOaa0Qo6|9TAxAaTB~2y0BKYfrHkXLi?7^ylLhY@p~qou1)r;7 zSYwH0wt8WfL6Z9OhUMP!y?q(E5$CyGU=jO;MAAU dS0L49{)!T+k_;*0*zTOzpb#xy0K!S0{{|zpLCOFC literal 0 HcmV?d00001 diff --git a/testdata/pres_temp_4D.nc b/testdata/pres_temp_4D.nc new file mode 100644 index 0000000000000000000000000000000000000000..aaf7b930cb1aa718c4921c662d4609141cd7d863 GIT binary patch literal 2784 zcmeIyO=uid0KoCrCTY_q)u>f-@URCD9z5jGg9i_rc99-DLJ*N4BBh7~DN;nr5=4TClp<23NDwJSM1qKvBI0kVfg*_Xo`D}T|H;go z$;;%O%Di$Qgm93fw9`?FCZm!ATXMd$l#PpVBNsV7#F0{^oZma4yJ?S$Rp=xfs_S*pg=*%SHKG6xH7>S88$L!RgRp*X#0i`+q-s@7@1vyzag6 zqHH~Oe==5|u{P~HtF@?JZ`6|aPTsSPWc$A{+X@5Ov-N*T@JKW|buHCxMRj#nX8Y>}W zylN(!rb#PghV+mg(nES!59?t)taCc2b2_IZ9qC9%dPI-t5j~>wI<71zpeu zUDQQg)I~k2NA;*4)nj^0kLfX8(j{HeB|Wak^|&6_WnI=~UDg#{(G^|MRbACpUDXqM zLQm)kUDGvP(=~0{Gj(0pbsg(i$2!&x-Ovr)(35&nPwGiMrKj|ip3>8LT2Jd~J)>vz zjGoc6dREWsSv{xc^qij4^Lk#->v_GP7xaQ&(2IIeFX~0Tq?h!PUeZn7Op#`gCL6Ta z37NM7DbftmWP=tvA#)~>BF$jv@9`a`=p)SlgH&m<%qr__aG6c6(PEn$++v5j>~hcd z9B8MLZhAOQFVB(Ud0yZ}`Z&Q$yiA%5r+JkDUgLG%V2~UIN>quNW}YTzILkSfInN3g zSmh#@SYw^Hd53q|;62{w11@ug5BZ2qKIST)aE(v-jO(=6;&Z-Wn=ko_uerfZzTsPL z@g2AMo*jPRNA7T!pZJ+y*yUG#<9F_HpFj9Bgj0b79HfnQIyl5(Iyu5oy6EN@kMTG? zJi(JZ#c`hI8J?xrx0Frzma@(L^=G1G%`qP}^HDP&HNC(7?5{s({Xd>#ne(i0fmJSY Oi8a>QUw#o0uByAL_~^+ zh=_<35s@MyA|fIZf=Ce&k>cd+?Cj)otpyPj4<3G(@aE0kCCBq8#=_>YN47DM6nfQO zzZg5;$?l}pEN{Kx^%Psjq>~-|{+X?P*<5x$-fvcWT_=ug|K4I;PUEwXZ*$Fm?b)>yC*5j))Oz!wzZ>$ocW=7jkT;5t!Z5w+SImov@i(y zrC?r5TGp!8G|`5(w5_QYhURnTwWMXOYE2VuXiM9g>S8cIa4lNWidMC*i8i&R9ZhvH zXMSj2OIp#Y)-}gH$>OD%OgD3XvhclbA&u+=y91F++&p& zjQEIuYBa+<=Qz(5ZnDB-Uht`{L0mu@+z__`#eD&1h9$Bj48}x_FvvtEfnfq8(ZskUnz$Uj zc`@;>@t}!!Z^n~}#`ps~m>9g9fbUg*kAW5vB`Ptg7nrYJRdv_bU%jeMy}s8TZ7mG0 z5BdTDB{lco$qOn_WD9Ee6_mH&O?)2;>Dm(S==SRf`2XPuJSI8XBwus zj!YVLPx863@fp1QGVWsK`54HoE2Ah%i|sZj>*`G@mY`}LRIhs8m{d;vvWdiKW+d5vacnd*b~!!NpS_w%zRq`< z|1=x?tib|2);{2G!`B11&T0CE1(v1f)Mr^1##DlK8OKX$O|^2xbyP(_RotM_{5q2C zUAB zv~)^x?8$px_>kJ_YJ2e9QsQ*>0~w%ZFPoHqsjC{YAo#xUd}q{v23ns zGBKV?42~psJ7W9kE+dm4-~22|h+5bLe^rvle)qcnRg#8A;{WLOmpiT`%Ca`|1NkUS zBsxSAuWyGqYIq{uO{`h@i@3(FXMY+Y5Ll(_tngaCuTsreJ`y|D;<0m{``fCR#-Py6 zMM`H!yicv8HXfU|I3ADV8n@PgMO8eo3S)=mwzfdmdeswYKSjLBY)c|J zjBcT8%!yfrnN@GBHP##Jj>P&ktNk<#v*Oh|k<8}k&4#H8ln^6XXA7~G9gf+u$u1!N z%=>RInbj<#5nf7vl97xg=wdD~PB9n<> zmD%LwEDOL}FK=k6zljPr3}pf5?t@JY3p-aYY`yn!StvD7EG2_R0i%FXz$jo8FbWt2 zi~>dhqkvJsC}0#Y3M^FtIrqysU(WS%j+b-0oYN(LEa&wV7&r)A36vCHA$-Y;$#EWO z2UUu8og1SvXL8weW|^>DdrGYV>hBL!M($51?YId^7_|ZYDuGqNjlgQ41FQko0yhD5 z&XUSo;BN)$3_O+V;BN=k19f(q$~)oj0`3Ox0ZOWk5?b0gY09?O1+@>@1mrw3+W{BU zLEs_aVc-$qQQ$G)ao`DHGq44C5*PtSfiYk!unox1`+-u128|9&i35a@0i%FXU>Oux Y=!2BJQ~%uuDS2@DASM3w8v{>&09u#3cK`qY literal 0 HcmV?d00001 diff --git a/testdata/simple_xy.nc b/testdata/simple_xy.nc new file mode 100644 index 0000000000000000000000000000000000000000..010c0a95f2c4769d2c98aaad71f63d67ebc86e90 GIT binary patch literal 384 zcmXxdXHtSO5CBk6>=nh1fCWJm6v2iK1@Rg(;}Q6QxA!gIxHB)i$tIg#e~?Zku_#TE z9w+ = var.get_int(false).unwrap(); + + assert_eq!(data.len(), 6*12); + for x in 0..(6*12) { + assert_eq!(data[x], x as i32); + } + + // do the same thing but cast to float + let data : Vec = var.get_float(true).unwrap(); + + assert_eq!(data.len(), 6*12); + for x in 0..(6*12) { + assert_eq!(data[x], x as f32); + } +} + +#[test] +#[should_panic(expected = "Types are not equivalent and cast==false")] +fn var_cast_fail() { + let f = test_file("simple_xy.nc"); + + let file = netcdf::open(&f).unwrap(); + let var = file.root.variables.get("data").unwrap(); + + // getting int Variable as float with false argument should fail. + let _data : Vec = var.get_float(false).unwrap(); +} + +#[test] +fn last_dim_varies_fastest() { + let f = test_file("simple_xy.nc"); + + let file = netcdf::open(&f).unwrap(); + assert_eq!(f, file.name); + + let var = file.root.variables.get("data").unwrap(); + let data : Vec = var.get_int(false).unwrap(); + + let nx = var.dimensions[0].len; + let ny = var.dimensions[1].len; + + assert_eq!(nx, 6); + assert_eq!(ny, 12); + assert_eq!(nx*ny, var.len); + + for x in 0..nx { + for y in 0..ny { + let ind = x*nx + y; + assert_eq!(data[ind as usize], ind as i32); + } + } +} + +#[test] +fn open_pres_temp_4d() { + let f = test_file("pres_temp_4D.nc"); + + let file = netcdf::open(&f).unwrap(); + assert_eq!(f, file.name); + + let pres = file.root.variables.get("pressure").unwrap(); + assert_eq!(pres.dimensions[0].name, "time"); + assert_eq!(pres.dimensions[1].name, "level"); + assert_eq!(pres.dimensions[2].name, "latitude"); + assert_eq!(pres.dimensions[3].name, "longitude"); + + // test var attributes + assert_eq!(pres.attributes.get("units").unwrap().get_char(false).unwrap(), + "hPa".to_string()); +} + +#[test] +fn nc4_groups() { + let f = test_file("simple_nc4.nc"); + + let file = netcdf::open(&f).unwrap(); + assert_eq!(f, file.name); + + let grp1 = file.root.sub_groups.get("grp1").unwrap(); + assert_eq!(grp1.name, "grp1".to_string()); + + let var = grp1.variables.get("data").unwrap(); + let data : Vec = var.get_int(true).unwrap(); + for x in 0..(6*12) { + assert_eq!(data[x], x as i32); + } +} + +// Write tests +#[test] +fn create() { + let f = test_file_new("create.nc"); + + let file = netcdf::create(&f).unwrap(); + assert_eq!(f, file.name); +} + +#[test] +fn def_dims_vars_attrs() { + { + let f = test_file_new("def_dims_vars_attrs.nc"); + + let mut file = netcdf::create(&f).unwrap(); + + let dim1_name = "ljkdsjkldfs"; + let dim2_name = "dsfkdfskl"; + file.root.add_dimension(dim1_name, 10).unwrap(); + file.root.add_dimension(dim2_name, 20).unwrap(); + assert_eq!(file.root.dimensions.get(dim1_name).unwrap().len, 10); + assert_eq!(file.root.dimensions.get(dim2_name).unwrap().len, 20); + + let var_name = "varstuff_int"; + let data : Vec = vec![42; (10*20)]; + file.root.add_variable( + var_name, + &vec![dim1_name.to_string(), dim2_name.to_string()], + &data + ).unwrap(); + assert_eq!(file.root.variables.get(var_name).unwrap().len, 20*10); + + let var_name = "varstuff_float"; + let data : Vec = vec![42.2; 10]; + file.root.add_variable( + var_name, + &vec![dim1_name.to_string()], + &data + ).unwrap(); + assert_eq!(file.root.variables.get(var_name).unwrap().len, 10); + + // test global attrs + file.root.add_attribute( + "testattr1", + 3, + ).unwrap(); + file.root.add_attribute( + "testattr2", + "Global string attr".to_string(), + ).unwrap(); + + // test var attrs + file.root.variables.get_mut(var_name).unwrap().add_attribute( + "varattr1", + 5, + ).unwrap(); + file.root.variables.get_mut(var_name).unwrap().add_attribute( + "varattr2", + "Variable string attr".to_string(), + ).unwrap(); + } + + // now, read in the file we created and verify everything + { + let f = test_file_new("def_dims_vars_attrs.nc"); + + let file = netcdf::open(&f).unwrap(); + + // verify dimensions + let dim1_name = "ljkdsjkldfs"; + let dim2_name = "dsfkdfskl"; + let dim1 = file.root.dimensions.get(dim1_name).unwrap(); + let dim2 = file.root.dimensions.get(dim2_name).unwrap(); + assert_eq!(dim1.len, 10); + assert_eq!(dim2.len, 20); + + // verify variable data + let var_name = "varstuff_int"; + let data_test : Vec = vec![42; (10*20)]; + let data_file : Vec = + file.root.variables.get(var_name).unwrap().get_int(false).unwrap(); + assert_eq!(data_test.len(), data_file.len()); + for i in 0..data_test.len() { + assert_eq!(data_test[i], data_file[i]); + } + + let var_name = "varstuff_float"; + let data_test : Vec = vec![42.2; 10]; + let data_file : Vec = + file.root.variables.get(var_name).unwrap().get_float(false).unwrap(); + assert_eq!(data_test.len(), data_file.len()); + for i in 0..data_test.len() { + assert_eq!(data_test[i], data_file[i]); + } + + // verify global attrs + assert_eq!(3, + file.root.attributes.get("testattr1").unwrap().get_int(false).unwrap()); + assert_eq!("Global string attr".to_string(), + file.root.attributes.get("testattr2").unwrap().get_char(false).unwrap()); + + // verify var attrs + assert_eq!(5, + file.root.variables.get(var_name).unwrap() + .attributes.get("varattr1").unwrap().get_int(false).unwrap()); + assert_eq!("Variable string attr", + file.root.variables.get(var_name).unwrap() + .attributes.get("varattr2").unwrap().get_char(false).unwrap()); + + } +} + +#[test] +fn all_var_types() { + // write + { + let f = test_file_new("all_var_types.nc"); + let mut file = netcdf::create(&f).unwrap(); + + let dim_name = "dim1"; + file.root.add_dimension(dim_name, 10).unwrap(); + + // byte + let data : Vec = vec![42 as i8; 10]; + let var_name = "var_byte"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // short + let data : Vec = vec![42 as i16; 10]; + let var_name = "var_short"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // ushort + let data : Vec = vec![42 as u16; 10]; + let var_name = "var_ushort"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // int + let data : Vec = vec![42 as i32; 10]; + let var_name = "var_int"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // uint + let data : Vec = vec![42 as u32; 10]; + let var_name = "var_uint"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // int64 + let data : Vec = vec![42 as i64; 10]; + let var_name = "var_int64"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // uint64 + let data : Vec = vec![42 as u64; 10]; + let var_name = "var_uint64"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // float + let data : Vec = vec![42.2 as f32; 10]; + let var_name = "var_float"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + // double + let data : Vec = vec![42.2 as f64; 10]; + let var_name = "var_double"; + file.root.add_variable( + var_name, + &vec![dim_name.to_string()], + &data + ).unwrap(); + } + + // read + { + let f = test_file_new("all_var_types.nc"); + let file = netcdf::open(&f).unwrap(); + + // byte + let data : Vec = + file.root.variables.get("var_byte").unwrap().get_byte(false).unwrap(); + for i in 0..10 { + assert_eq!(42 as i8, data[i]); + } + // short + let data : Vec = + file.root.variables.get("var_short").unwrap().get_short(false).unwrap(); + for i in 0..10 { + assert_eq!(42 as i16, data[i]); + } + // ushort + let data : Vec = + file.root.variables.get("var_ushort").unwrap().get_ushort(false).unwrap(); + for i in 0..10 { + assert_eq!(42 as u16, data[i]); + } + // int + let data : Vec = + file.root.variables.get("var_int").unwrap().get_int(false).unwrap(); + for i in 0..10 { + assert_eq!(42 as i32, data[i]); + } + // uint + let data : Vec = + file.root.variables.get("var_uint").unwrap().get_uint(false).unwrap(); + for i in 0..10 { + assert_eq!(42 as u32, data[i]); + } + // int64 + let data : Vec = + file.root.variables.get("var_int64").unwrap().get_int64(false).unwrap(); + for i in 0..10 { + assert_eq!(42 as i64, data[i]); + } + // uint64 + let data : Vec = + file.root.variables.get("var_uint64").unwrap().get_uint64(false).unwrap(); + for i in 0..10 { + assert_eq!(42 as u64, data[i]); + } + // float + let data : Vec = + file.root.variables.get("var_float").unwrap().get_float(false).unwrap(); + for i in 0..10 { + assert_eq!(42.2 as f32, data[i]); + } + // double + let data : Vec = + file.root.variables.get("var_double").unwrap().get_double(false).unwrap(); + for i in 0..10 { + assert_eq!(42.2 as f64, data[i]); + } + + } + +} + +#[test] +fn all_attr_types() { + { + let f = test_file_new("all_attr_types.nc"); + let mut file = netcdf::create(&f).unwrap(); + + // byte + file.root.add_attribute( + "attr_byte", + 3 as i8, + ).unwrap(); + // short + file.root.add_attribute( + "attr_short", + 3 as i16, + ).unwrap(); + // ushort + file.root.add_attribute( + "attr_ushort", + 3 as u16, + ).unwrap(); + // int + file.root.add_attribute( + "attr_int", + 3 as i32, + ).unwrap(); + // uint + file.root.add_attribute( + "attr_uint", + 3 as u32, + ).unwrap(); + // int64 + file.root.add_attribute( + "attr_int64", + 3 as i64, + ).unwrap(); + // uint64 + file.root.add_attribute( + "attr_uint64", + 3 as u64, + ).unwrap(); + // float + file.root.add_attribute( + "attr_float", + 3.2 as f32, + ).unwrap(); + // double + file.root.add_attribute( + "attr_double", + 3.2 as f64, + ).unwrap(); + } + + { + let f = test_file_new("all_attr_types.nc"); + let file = netcdf::open(&f).unwrap(); + + // byte + assert_eq!(3 as i8, + file.root.attributes.get("attr_byte").unwrap().get_byte(false).unwrap()); + // short + assert_eq!(3 as i16, + file.root.attributes.get("attr_short").unwrap().get_short(false).unwrap()); + // ushort + assert_eq!(3 as u16, + file.root.attributes.get("attr_ushort").unwrap().get_ushort(false).unwrap()); + // int + assert_eq!(3 as i32, + file.root.attributes.get("attr_int").unwrap().get_int(false).unwrap()); + // uint + assert_eq!(3 as u32, + file.root.attributes.get("attr_uint").unwrap().get_uint(false).unwrap()); + // int64 + assert_eq!(3 as i64, + file.root.attributes.get("attr_int64").unwrap().get_int64(false).unwrap()); + // uint64 + assert_eq!(3 as u64, + file.root.attributes.get("attr_uint64").unwrap().get_uint64(false).unwrap()); + // float + assert_eq!(3.2 as f32, + file.root.attributes.get("attr_float").unwrap().get_float(false).unwrap()); + // double + assert_eq!(3.2 as f64, + file.root.attributes.get("attr_double").unwrap().get_double(false).unwrap()); + + } +}