1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#![allow(clippy::similar_names)]
use super::attribute::{AttrValue, Attribute};
use super::dimension::{self, Dimension};
use super::error;
use super::group::{Group, GroupMut};
use super::variable::{Numeric, Variable, VariableMut};
use netcdf_sys::*;
use std::ffi::CString;
use std::marker::PhantomData;
use std::path;
#[derive(Debug)]
pub(crate) struct RawFile {
ncid: nc_type,
}
impl Drop for RawFile {
fn drop(&mut self) {
unsafe {
let _err = error::checked(super::with_lock(|| nc_close(self.ncid)));
}
}
}
impl RawFile {
pub(crate) fn open(path: &path::Path) -> error::Result<File> {
let f = CString::new(path.to_str().unwrap()).unwrap();
let mut ncid: nc_type = 0;
unsafe {
error::checked(super::with_lock(|| {
nc_open(f.as_ptr(), NC_NOWRITE, &mut ncid)
}))?;
}
Ok(File(Self { ncid }))
}
#[allow(clippy::doc_markdown)]
pub(crate) fn append(path: &path::Path) -> error::Result<MutableFile> {
let f = CString::new(path.to_str().unwrap()).unwrap();
let mut ncid: nc_type = -1;
unsafe {
error::checked(super::with_lock(|| {
nc_open(f.as_ptr(), NC_WRITE, &mut ncid)
}))?;
}
Ok(MutableFile(File(Self { ncid })))
}
#[allow(clippy::doc_markdown)]
pub(crate) fn create(path: &path::Path) -> error::Result<MutableFile> {
let f = CString::new(path.to_str().unwrap()).unwrap();
let mut ncid: nc_type = -1;
unsafe {
error::checked(super::with_lock(|| {
nc_create(f.as_ptr(), NC_NETCDF4 | NC_CLOBBER, &mut ncid)
}))?;
}
Ok(MutableFile(File(Self { ncid })))
}
#[cfg(feature = "memory")]
pub(crate) fn open_from_memory<'buffer>(
name: Option<&str>,
mem: &'buffer [u8],
) -> error::Result<MemFile<'buffer>> {
let cstr = std::ffi::CString::new(name.unwrap_or("/")).unwrap();
let mut ncid = 0;
unsafe {
error::checked(super::with_lock(|| {
nc_open_mem(
cstr.as_ptr(),
NC_NOWRITE,
mem.len(),
mem.as_ptr() as *const u8 as *mut _,
&mut ncid,
)
}))?;
}
Ok(MemFile(File(Self { ncid }), PhantomData))
}
}
#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct File(RawFile);
impl File {
pub fn path(&self) -> error::Result<String> {
let name = {
let mut pathlen = 0;
unsafe {
error::checked(super::with_lock(|| {
nc_inq_path(self.0.ncid, &mut pathlen, std::ptr::null_mut())
}))?;
}
let mut name = vec![0_u8; pathlen as _];
unsafe {
error::checked(super::with_lock(|| {
nc_inq_path(
self.0.ncid,
std::ptr::null_mut(),
name.as_mut_ptr() as *mut _,
)
}))?;
}
name
};
Ok(String::from_utf8(name)?)
}
pub fn root(&self) -> Option<Group> {
let mut format = 0;
unsafe { error::checked(super::with_lock(|| nc_inq_format(self.ncid(), &mut format))) }
.unwrap();
match format {
NC_FORMAT_NETCDF4 | NC_FORMAT_NETCDF4_CLASSIC => Some(Group {
ncid: self.ncid(),
_file: PhantomData,
}),
_ => None,
}
}
fn ncid(&self) -> nc_type {
self.0.ncid
}
pub fn variable<'f>(&'f self, name: &str) -> Option<Variable<'f>> {
Variable::find_from_name(self.ncid(), name).unwrap()
}
pub fn variables(&self) -> impl Iterator<Item = Variable> {
super::variable::variables_at_ncid(self.ncid())
.unwrap()
.map(Result::unwrap)
}
pub fn attribute<'f>(&'f self, name: &str) -> Option<Attribute<'f>> {
Attribute::find_from_name(self.ncid(), None, name).unwrap()
}
pub fn attributes(&self) -> impl Iterator<Item = Attribute> {
crate::attribute::AttributeIterator::new(self.0.ncid, None)
.unwrap()
.map(Result::unwrap)
}
pub fn dimension<'f>(&self, name: &str) -> Option<Dimension<'f>> {
super::dimension::dimension_from_name(self.ncid(), name).unwrap()
}
pub fn dimensions(&self) -> impl Iterator<Item = Dimension> {
super::dimension::dimensions_from_location(self.ncid())
.unwrap()
.map(Result::unwrap)
}
pub fn group<'f>(&'f self, name: &str) -> error::Result<Option<Group<'f>>> {
super::group::group_from_name(self.ncid(), name)
}
pub fn groups<'f>(&'f self) -> error::Result<impl Iterator<Item = Group<'f>>> {
super::group::groups_at_ncid(self.ncid())
}
}
#[derive(Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct MutableFile(File);
impl std::ops::Deref for MutableFile {
type Target = File;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl MutableFile {
pub fn root_mut(&mut self) -> Option<GroupMut> {
self.root().map(|root| GroupMut(root, PhantomData))
}
pub fn variable_mut<'f>(&'f mut self, name: &str) -> Option<VariableMut<'f>> {
self.variable(name).map(|var| VariableMut(var, PhantomData))
}
pub fn variables_mut(&mut self) -> impl Iterator<Item = VariableMut> {
self.variables().map(|var| VariableMut(var, PhantomData))
}
pub fn group_mut<'f>(&'f mut self, name: &str) -> error::Result<Option<GroupMut<'f>>> {
self.group(name)
.map(|g| g.map(|g| GroupMut(g, PhantomData)))
}
pub fn groups_mut<'f>(&'f mut self) -> error::Result<impl Iterator<Item = GroupMut<'f>>> {
self.groups().map(|g| g.map(|g| GroupMut(g, PhantomData)))
}
pub fn add_attribute<'a, T>(&'a mut self, name: &str, val: T) -> error::Result<Attribute<'a>>
where
T: Into<AttrValue>,
{
Attribute::put(self.ncid(), NC_GLOBAL, name, val.into())
}
pub fn add_dimension<'f>(&'f mut self, name: &str, len: usize) -> error::Result<Dimension<'f>> {
super::dimension::add_dimension_at(self.ncid(), name, len)
}
pub fn add_unlimited_dimension(&mut self, name: &str) -> error::Result<Dimension> {
self.add_dimension(name, 0)
}
pub fn add_group<'f>(&'f mut self, name: &str) -> error::Result<GroupMut<'f>> {
GroupMut::add_group_at(self.ncid(), name)
}
pub fn add_variable<'f, T>(
&'f mut self,
name: &str,
dims: &[&str],
) -> error::Result<VariableMut<'f>>
where
T: Numeric,
{
VariableMut::add_from_str(self.ncid(), T::NCTYPE, name, dims)
}
pub fn add_string_variable<'f>(
&'f mut self,
name: &str,
dims: &[&str],
) -> error::Result<VariableMut<'f>> {
VariableMut::add_from_str(self.ncid(), NC_STRING, name, dims)
}
pub fn add_variable_from_identifiers<'f, T>(
&'f mut self,
name: &str,
dims: &[dimension::Identifier],
) -> error::Result<VariableMut<'f>>
where
T: Numeric,
{
super::variable::add_variable_from_identifiers(self.ncid(), name, dims, T::NCTYPE)
}
}
#[cfg(feature = "memory")]
#[allow(clippy::module_name_repetitions)]
pub struct MemFile<'buffer>(File, std::marker::PhantomData<&'buffer [u8]>);
#[cfg(feature = "memory")]
impl<'a> std::ops::Deref for MemFile<'a> {
type Target = File;
fn deref(&self) -> &Self::Target {
&self.0
}
}