eo-identifiers 0.1.1

Parsers for naming conventions of earth observation products and datasets
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//! Sentinel 1
//!
//! # Example
//!
//! ```rust
//! use eo_identifiers::identifiers::sentinel1::{Product, Dataset};
//! use std::str::FromStr;
//!
//! assert!(
//!     Product::from_str("S1A_EW_GRDM_1SDH_20151221T165227_20151221T165332_009143_00D275_8694")
//!     .is_ok()
//! );
//! assert!(
//!     Dataset::from_str("s1a-iw-grd-vh-20221029t171425-20221029t171450-045660-0575ce-002")
//!     .is_ok()
//! );
//! ```
//!
use crate::common_parsers::{parse_esa_timestamp, take_n_digits_in_range};
use crate::{impl_from_str, Mission};
use chrono::NaiveDateTime;
use nom::branch::alt;
use nom::bytes::complete::{tag, tag_no_case, take_while_m_n};
use nom::character::complete::char;
use nom::combinator::map;
use nom::IResult;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MissionId {
    S1A,
    S1B,
}

impl From<MissionId> for Mission {
    fn from(_: MissionId) -> Self {
        Mission::Sentinel1
    }
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Mode {
    IW,
    EW,
    WV,
    S1,
    S2,
    S3,
    S4,
    S5,
    S6,
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ProductType {
    RAW,
    SLC,
    GRD,
    OCN,
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ResolutionClass {
    Full,
    High,
    Medium,
    NotApplicable,
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ProcessingLevel {
    Level0,
    Level1,
    Level2,
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ProductClass {
    Standard,
    Annotation,
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum ProductPolarisation {
    HH,
    VV,
    HHHV,
    VVVH,
}

/// Sentinel 1 Product
///
/// Based on the [official S1 naming convention](https://sentinel.esa.int/web/sentinel/user-guides/sentinel-1-sar/naming-conventions).
#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Product {
    /// Mission id
    ///
    /// # Official description
    ///
    /// > The Mission Identifier (MMM) denotes the satellite and will be either
    /// > S1A for the SENTINEL-1A instrument or S1B for the SENTINEL-1B instrument.
    pub mission_id: MissionId,

    /// Mode
    ///
    /// # Official description
    ///
    /// > The Mode/Beam (BB) identifies the S1-S6 beams for SM products and IW, EW and WV for
    /// > products from the respective modes.
    pub mode: Mode,

    /// Product Type
    ///
    /// # Official description
    ///
    /// > Product Type (TTT) can be RAW, SLC, GRD or OCN.
    pub product_type: ProductType,

    /// Resolution class
    ///
    /// # Official description
    ///
    /// > Resolution Class (R) can be F (Full resolution), H (High resolution),
    /// > M (Medium resolution), or _ (underscore: not applicable to the current product type). Resolution Class is used for GRD only.
    pub resolution_class: ResolutionClass,

    /// Processing level
    ///
    /// # Official description
    ///
    /// > The Processing Level (L) can be 0, 1 or 2.
    pub processing_level: ProcessingLevel,

    /// Product class
    ///
    /// # Official description
    ///
    /// > The Product Class can be Standard (S) or Annotation (A). Annotation products are
    /// > only used internally by the PDGS and are not distributed.
    pub product_class: ProductClass,

    /// Polarisation
    ///
    /// # Official description
    ///
    /// > Polarisation (PP) can be one of:
    /// > * SH (single HH polarisation)
    /// > * SV (single VV polarisation)
    /// > * DH (dual HH+HV polarisation)
    /// > * DV (dual VV+VH polarisation)
    pub polarisation: ProductPolarisation,

    /// start datetime
    pub start_datetime: NaiveDateTime,

    /// stop datetime
    pub stop_datetime: NaiveDateTime,

    /// Orbit number
    ///
    /// # Official description
    ///
    /// > The absolute orbit number at product start time (OOOOOO) will be
    /// > in the range 000001-999999.
    pub orbit_number: u32,

    /// Data take identifier
    ///
    /// # Official description
    ///
    /// > The mission data-take identifier (DDDDDD) will be in the range 000001-FFFFFF.
    pub data_take_identifier: String,

    /// product unique identifier
    ///
    /// # Official description
    ///
    /// > The product unique identifier (CCCC) is a hexadecimal string generated by
    /// > computing CRC-16 on the manifest file using CRC-CCITT.
    pub product_unique_identifier: String,
    // folder extension is skipped
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Hash, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SwathIdentifier {
    S1,
    S2,
    S3,
    S4,
    S5,
    S6,
    IW,
    IW1,
    IW2,
    IW3,
    EW,
    EW1,
    EW2,
    EW3,
    EW4,
    EW5,
    WV,
    WV1,
    WV2,
}

impl SwathIdentifier {
    pub fn is_s(&self) -> bool {
        matches!(
            self,
            Self::S1 | Self::S2 | Self::S3 | Self::S4 | Self::S5 | Self::S6
        )
    }

    pub fn is_iw(&self) -> bool {
        matches!(self, Self::IW1 | Self::IW2 | Self::IW3 | Self::IW)
    }

    pub fn is_ew(&self) -> bool {
        matches!(self, Self::EW1 | Self::EW2 | Self::EW3 | Self::EW)
    }

    pub fn is_wv(&self) -> bool {
        matches!(self, Self::WV1 | Self::WV2 | Self::WV)
    }
}

#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum DatasetPolarisation {
    HH,
    VV,
    HV,
    VH,
}

/// Sentinel 1 Dataset
///
/// Based on the [official S1 naming convention](https://sentinel.esa.int/web/sentinel/user-guides/sentinel-1-sar/naming-conventions).
#[derive(PartialOrd, PartialEq, Eq, Debug, Clone, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Dataset {
    /// Mission id
    ///
    /// # Official description
    ///
    /// > The Mission Identifier (MMM) denotes the satellite and will be either
    /// > S1A for the SENTINEL-1A instrument or S1B for the SENTINEL-1B instrument.
    pub mission_id: MissionId,

    /// swath identifier
    ///
    /// # Official description
    ///
    /// > The Swath Identifier (sss) identifies the s1-s6 beams for SM mode,
    /// > iw1-iw3 for IW mode, ew1-ew5 for EW more and wv1-wv2 for WV mode.
    pub swath_identifier: SwathIdentifier,

    /// Product Type
    ///
    /// # Official description
    ///
    /// > Product Type (TTT) can be RAW, SLC, GRD or OCN.
    pub product_type: ProductType,

    /// Polarisation
    ///
    /// # Official description
    ///
    /// > Polarisation (PP) can be one of:
    /// > * hh (single HH polarisation)
    /// > * vv (single VV polarisation)
    /// > * hv (single HV polarisation)
    /// > * vh (single VH polarisation).
    pub polarisation: DatasetPolarisation,

    /// sensing start datetime
    pub start_datetime: NaiveDateTime,

    /// sensing top datetime
    pub stop_datetime: NaiveDateTime,

    /// Orbit number
    ///
    /// # Official description
    ///
    /// > The absolute orbit number at product start time (OOOOOO) will be
    /// > in the range 000001-999999.
    pub orbit_number: u32,

    /// Data take identifier
    ///
    /// # Official description
    ///
    /// > The mission data-take identifier (DDDDDD) will be in the range 000001-FFFFFF.
    pub data_take_identifier: String,

    /// image number
    ///
    /// # Official description
    ///
    /// > The image number (nnn) identifies each individual image. WV
    /// > vignettes each have their own image number as do each swath and polarization image for SM, IW and EW.
    pub image_number: u32,
}

fn is_not_product_sep(c: core::primitive::char) -> bool {
    c != '_'
}

fn consume_product_sep(s: &str) -> IResult<&str, core::primitive::char> {
    char('_')(s)
}

fn consume_dataset_sep(s: &str) -> IResult<&str, core::primitive::char> {
    char('-')(s)
}

fn parse_mission_id(s: &str) -> IResult<&str, MissionId> {
    alt((
        map(tag_no_case("s1a"), |_| MissionId::S1A),
        map(tag_no_case("s1b"), |_| MissionId::S1B),
    ))(s)
}

fn parse_mode(s: &str) -> IResult<&str, Mode> {
    alt((
        map(tag_no_case("iw"), |_| Mode::IW),
        map(tag_no_case("ew"), |_| Mode::EW),
        map(tag_no_case("wv"), |_| Mode::WV),
        map(tag_no_case("s1"), |_| Mode::S1),
        map(tag_no_case("s2"), |_| Mode::S2),
        map(tag_no_case("s3"), |_| Mode::S3),
        map(tag_no_case("s4"), |_| Mode::S4),
        map(tag_no_case("s5"), |_| Mode::S5),
        map(tag_no_case("s6"), |_| Mode::S6),
    ))(s)
}

fn parse_product_type(s: &str) -> IResult<&str, ProductType> {
    alt((
        map(tag_no_case("grd"), |_| ProductType::GRD),
        map(tag_no_case("ocn"), |_| ProductType::OCN),
        map(tag_no_case("raw"), |_| ProductType::RAW),
        map(tag_no_case("slc"), |_| ProductType::SLC),
    ))(s)
}

fn parse_resolution(s: &str) -> IResult<&str, ResolutionClass> {
    alt((
        map(tag_no_case("f"), |_| ResolutionClass::Full),
        map(tag_no_case("h"), |_| ResolutionClass::High),
        map(tag_no_case("m"), |_| ResolutionClass::Medium),
        map(tag_no_case("_"), |_| ResolutionClass::NotApplicable),
    ))(s)
}

fn parse_processing_level(s: &str) -> IResult<&str, ProcessingLevel> {
    alt((
        map(tag("0"), |_| ProcessingLevel::Level0),
        map(tag("1"), |_| ProcessingLevel::Level1),
        map(tag("2"), |_| ProcessingLevel::Level2),
    ))(s)
}

fn parse_product_class(s: &str) -> IResult<&str, ProductClass> {
    alt((
        map(tag_no_case("s"), |_| ProductClass::Standard),
        map(tag_no_case("a"), |_| ProductClass::Annotation),
    ))(s)
}

fn parse_product_polarisation(s: &str) -> IResult<&str, ProductPolarisation> {
    alt((
        map(tag_no_case("sh"), |_| ProductPolarisation::HH),
        map(tag_no_case("sv"), |_| ProductPolarisation::VV),
        map(tag_no_case("dh"), |_| ProductPolarisation::HHHV),
        map(tag_no_case("dv"), |_| ProductPolarisation::VVVH),
    ))(s)
}

/// nom parser function
pub fn parse_product(s: &str) -> IResult<&str, Product> {
    let (s, mission_id) = parse_mission_id(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, mode) = parse_mode(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, product_type) = parse_product_type(s)?;
    let (s, resolution_class) = parse_resolution(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, processing_level) = parse_processing_level(s)?;
    let (s, product_class) = parse_product_class(s)?;
    let (s, polarisation) = parse_product_polarisation(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, start_datetime) = parse_esa_timestamp(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, stop_datetime) = parse_esa_timestamp(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, orbit_number) = take_n_digits_in_range(6, 1..=999999)(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, data_take_identifier) = take_while_m_n(6, 6, is_not_product_sep)(s)?;
    let (s, _) = consume_product_sep(s)?;
    let (s, product_unique_identifier) = take_while_m_n(4, 4, is_not_product_sep)(s)?;

    Ok((
        s,
        Product {
            mission_id,
            mode,
            product_type,
            resolution_class,
            processing_level,
            product_class,
            polarisation,
            start_datetime,
            stop_datetime,
            orbit_number,
            data_take_identifier: data_take_identifier.to_uppercase(),
            product_unique_identifier: product_unique_identifier.to_uppercase(),
        },
    ))
}

fn parse_dataset_polarisation(s: &str) -> IResult<&str, DatasetPolarisation> {
    alt((
        map(tag_no_case("hh"), |_| DatasetPolarisation::HH),
        map(tag_no_case("vv"), |_| DatasetPolarisation::VV),
        map(tag_no_case("hv"), |_| DatasetPolarisation::HV),
        map(tag_no_case("vh"), |_| DatasetPolarisation::VH),
    ))(s)
}

fn parse_swath_identifier(s: &str) -> IResult<&str, SwathIdentifier> {
    alt((
        map(tag_no_case("s1"), |_| SwathIdentifier::S1),
        map(tag_no_case("s2"), |_| SwathIdentifier::S2),
        map(tag_no_case("s3"), |_| SwathIdentifier::S3),
        map(tag_no_case("s4"), |_| SwathIdentifier::S4),
        map(tag_no_case("s5"), |_| SwathIdentifier::S5),
        map(tag_no_case("s6"), |_| SwathIdentifier::S6),
        map(tag_no_case("iw1"), |_| SwathIdentifier::IW1),
        map(tag_no_case("iw2"), |_| SwathIdentifier::IW2),
        map(tag_no_case("iw3"), |_| SwathIdentifier::IW3),
        map(tag_no_case("iw"), |_| SwathIdentifier::IW),
        map(tag_no_case("ew1"), |_| SwathIdentifier::EW1),
        map(tag_no_case("ew2"), |_| SwathIdentifier::EW2),
        map(tag_no_case("ew3"), |_| SwathIdentifier::EW3),
        map(tag_no_case("ew4"), |_| SwathIdentifier::EW4),
        map(tag_no_case("ew5"), |_| SwathIdentifier::EW5),
        map(tag_no_case("ew"), |_| SwathIdentifier::EW),
        map(tag_no_case("wv1"), |_| SwathIdentifier::WV1),
        map(tag_no_case("wv2"), |_| SwathIdentifier::WV2),
        map(tag_no_case("wv"), |_| SwathIdentifier::WV),
    ))(s)
}

/// nom parser function
pub fn parse_dataset(s: &str) -> IResult<&str, Dataset> {
    let (s, mission_id) = parse_mission_id(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, swath_identifier) = parse_swath_identifier(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, product_type) = parse_product_type(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, polarisation) = parse_dataset_polarisation(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, start_datetime) = parse_esa_timestamp(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, stop_datetime) = parse_esa_timestamp(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, orbit_number) = take_n_digits_in_range(6, 1..=999999)(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, data_take_identifier) = take_while_m_n(6, 6, is_not_product_sep)(s)?;
    let (s, _) = consume_dataset_sep(s)?;
    let (s, image_number) = take_n_digits_in_range(3, 0..=999)(s)?;

    Ok((
        s,
        Dataset {
            mission_id,
            swath_identifier,
            product_type,
            polarisation,
            start_datetime,
            stop_datetime,
            orbit_number,
            data_take_identifier: data_take_identifier.to_uppercase(),
            image_number,
        },
    ))
}

impl_from_str!(parse_dataset, Dataset);
impl_from_str!(parse_product, Product);

#[cfg(test)]
mod tests {
    use crate::identifiers::sentinel1::{
        parse_dataset, parse_product, DatasetPolarisation, MissionId, Mode, ProcessingLevel,
        ProductClass, ProductPolarisation, ProductType, ResolutionClass, SwathIdentifier,
    };
    use crate::identifiers::tests::apply_to_samples_from_txt;

    #[test]
    fn parse_s1_product() {
        let (_, product) =
            parse_product("S1A_IW_GRDH_1SDV_20200207T051836_20200207T051901_031142_039466_A237")
                .unwrap();
        assert_eq!(product.mission_id, MissionId::S1A);
        assert_eq!(product.mode, Mode::IW);
        assert_eq!(product.product_type, ProductType::GRD);
        assert_eq!(product.resolution_class, ResolutionClass::High);
        assert_eq!(product.processing_level, ProcessingLevel::Level1);
        assert_eq!(product.product_class, ProductClass::Standard);
        assert_eq!(product.polarisation, ProductPolarisation::VVVH);
        // timestamps skipped
        assert_eq!(product.orbit_number, 31142);
        assert_eq!(product.data_take_identifier.as_str(), "039466");
        assert_eq!(product.product_unique_identifier.as_str(), "A237");
    }

    #[test]
    fn parse_s1_dataset() {
        let (_, ds) =
            parse_dataset("s1a-iw-grd-vh-20221029t171425-20221029t171450-045660-0575ce-002.tiff")
                .unwrap();
        assert_eq!(ds.mission_id, MissionId::S1A);
        assert_eq!(ds.swath_identifier, SwathIdentifier::IW);
        assert_eq!(ds.product_type, ProductType::GRD);
        assert_eq!(ds.polarisation, DatasetPolarisation::VH);
        // timestamps skipped
        assert_eq!(ds.orbit_number, 45660);
        assert_eq!(ds.data_take_identifier.as_str(), "0575CE");
    }

    #[test]
    fn parse_s1_dataset_no_fileextension() {
        let (_, _ds) =
            parse_dataset("s1a-iw-grd-vh-20221029t171425-20221029t171450-045660-0575ce-002")
                .unwrap();
    }

    #[test]
    fn apply_to_product_testdata() {
        apply_to_samples_from_txt("sentinel1_products.txt", |s| {
            parse_product(s).unwrap();
        })
    }
}