use core::fmt;
use core::str::FromStr;
use crate::astro::time::civil::{civil_from_julian_day_number, day_of_year_int, days_in_month};
use crate::astro::time::gnss::{week_epoch_julian_day_number, week_from_calendar};
use crate::astro::time::model::TimeScale;
use crate::astro::time::scales::julian_day_number;
use crate::terrain;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum AnalysisCenter {
Igs,
CodRap,
CodPrd1,
CodPrd2,
Esa,
Cod,
Gfz,
IgsUlt,
CodUlt,
EsaUlt,
GfzUlt,
}
impl AnalysisCenter {
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::Igs => "igs",
Self::CodRap => "cod_rap",
Self::CodPrd1 => "cod_prd1",
Self::CodPrd2 => "cod_prd2",
Self::Esa => "esa",
Self::Cod => "cod",
Self::Gfz => "gfz",
Self::IgsUlt => "igs_ult",
Self::CodUlt => "cod_ult",
Self::EsaUlt => "esa_ult",
Self::GfzUlt => "gfz_ult",
}
}
#[must_use]
pub fn from_code(code: &str) -> Option<Self> {
match code {
"igs" => Some(Self::Igs),
"cod_rap" => Some(Self::CodRap),
"cod_prd1" => Some(Self::CodPrd1),
"cod_prd2" => Some(Self::CodPrd2),
"esa" => Some(Self::Esa),
"cod" => Some(Self::Cod),
"gfz" => Some(Self::Gfz),
"igs_ult" => Some(Self::IgsUlt),
"cod_ult" => Some(Self::CodUlt),
"esa_ult" => Some(Self::EsaUlt),
"gfz_ult" => Some(Self::GfzUlt),
_ => None,
}
}
}
impl fmt::Display for AnalysisCenter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code())
}
}
impl FromStr for AnalysisCenter {
type Err = DataCatalogError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_code(s).ok_or_else(|| DataCatalogError::UnknownCenter(s.to_string()))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ProductType {
Sp3,
Clk,
Nav,
Ionex,
}
impl ProductType {
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::Sp3 => "sp3",
Self::Clk => "clk",
Self::Nav => "nav",
Self::Ionex => "ionex",
}
}
#[must_use]
pub fn from_code(code: &str) -> Option<Self> {
match code {
"sp3" => Some(Self::Sp3),
"clk" => Some(Self::Clk),
"nav" => Some(Self::Nav),
"ionex" => Some(Self::Ionex),
_ => None,
}
}
}
impl fmt::Display for ProductType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code())
}
}
impl FromStr for ProductType {
type Err = DataCatalogError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_code(s).ok_or_else(|| DataCatalogError::UnknownProductType(s.to_string()))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArchiveProtocol {
Http,
Https,
}
impl ArchiveProtocol {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Http => "http",
Self::Https => "https",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArchiveCompression {
Gzip,
None,
}
impl ArchiveCompression {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Gzip => "gzip",
Self::None => "none",
}
}
const fn suffix(self) -> &'static str {
match self {
Self::Gzip => ".gz",
Self::None => "",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArchiveLayout {
GfzRapidWeek,
GfzUltraWeek,
GpsWeek,
BkgProductsWeek,
BkgBrdcYearDoy,
BkgObsYearDoy,
AiubCodeMgexYear,
AiubCodeYear,
AiubCodeRoot,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProductFilenameKind {
Sampled,
Nav,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProductTypeConvention {
pub product_type: ProductType,
pub content_code: &'static str,
pub extension: &'static str,
pub kind: ProductFilenameKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CenterProductConvention {
pub product_type: ProductType,
pub token: &'static str,
pub layout: ArchiveLayout,
pub span: &'static str,
pub default_sample: &'static str,
pub compression: ArchiveCompression,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CenterCatalogEntry {
pub center: AnalysisCenter,
pub code: &'static str,
pub protocol: ArchiveProtocol,
pub host: &'static str,
pub root_url: &'static str,
pub products: &'static [CenterProductConvention],
pub issues: &'static [&'static str],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TerrainSourceEntry {
pub protocol: ArchiveProtocol,
pub host: &'static str,
pub compression: ArchiveCompression,
pub root_url: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NoOpenMirrorProduct {
pub center: &'static str,
pub product_type: &'static str,
}
const PRODUCT_TYPE_CONVENTIONS: [ProductTypeConvention; 4] = [
ProductTypeConvention {
product_type: ProductType::Sp3,
content_code: "ORB",
extension: "SP3",
kind: ProductFilenameKind::Sampled,
},
ProductTypeConvention {
product_type: ProductType::Clk,
content_code: "CLK",
extension: "CLK",
kind: ProductFilenameKind::Sampled,
},
ProductTypeConvention {
product_type: ProductType::Nav,
content_code: "MN",
extension: "rnx",
kind: ProductFilenameKind::Nav,
},
ProductTypeConvention {
product_type: ProductType::Ionex,
content_code: "GIM",
extension: "INX",
kind: ProductFilenameKind::Sampled,
},
];
const COD_RAP_PRODUCTS: [CenterProductConvention; 1] = [CenterProductConvention {
product_type: ProductType::Ionex,
token: "COD0OPSRAP",
layout: ArchiveLayout::AiubCodeRoot,
span: "01D",
default_sample: "01H",
compression: ArchiveCompression::Gzip,
}];
const COD_PRD_PRODUCTS: [CenterProductConvention; 1] = [CenterProductConvention {
product_type: ProductType::Ionex,
token: "COD0OPSPRD",
layout: ArchiveLayout::AiubCodeRoot,
span: "01D",
default_sample: "01H",
compression: ArchiveCompression::Gzip,
}];
const ESA_PRODUCTS: [CenterProductConvention; 3] = [
CenterProductConvention {
product_type: ProductType::Sp3,
token: "ESA0MGNFIN",
layout: ArchiveLayout::GpsWeek,
span: "01D",
default_sample: "05M",
compression: ArchiveCompression::Gzip,
},
CenterProductConvention {
product_type: ProductType::Clk,
token: "ESA0MGNFIN",
layout: ArchiveLayout::GpsWeek,
span: "01D",
default_sample: "30S",
compression: ArchiveCompression::Gzip,
},
CenterProductConvention {
product_type: ProductType::Ionex,
token: "ESA0OPSFIN",
layout: ArchiveLayout::GpsWeek,
span: "01D",
default_sample: "02H",
compression: ArchiveCompression::Gzip,
},
];
const COD_PRODUCTS: [CenterProductConvention; 3] = [
CenterProductConvention {
product_type: ProductType::Sp3,
token: "COD0MGXFIN",
layout: ArchiveLayout::AiubCodeMgexYear,
span: "01D",
default_sample: "05M",
compression: ArchiveCompression::Gzip,
},
CenterProductConvention {
product_type: ProductType::Clk,
token: "COD0MGXFIN",
layout: ArchiveLayout::AiubCodeMgexYear,
span: "01D",
default_sample: "30S",
compression: ArchiveCompression::Gzip,
},
CenterProductConvention {
product_type: ProductType::Ionex,
token: "COD0OPSFIN",
layout: ArchiveLayout::AiubCodeYear,
span: "01D",
default_sample: "01H",
compression: ArchiveCompression::Gzip,
},
];
const GFZ_PRODUCTS: [CenterProductConvention; 2] = [
CenterProductConvention {
product_type: ProductType::Sp3,
token: "GFZ0OPSRAP",
layout: ArchiveLayout::GfzRapidWeek,
span: "01D",
default_sample: "15M",
compression: ArchiveCompression::Gzip,
},
CenterProductConvention {
product_type: ProductType::Clk,
token: "GFZ0OPSRAP",
layout: ArchiveLayout::GfzRapidWeek,
span: "01D",
default_sample: "30S",
compression: ArchiveCompression::Gzip,
},
];
const IGS_PRODUCTS: [CenterProductConvention; 1] = [CenterProductConvention {
product_type: ProductType::Nav,
token: "BRDC00WRD",
layout: ArchiveLayout::BkgBrdcYearDoy,
span: "01D",
default_sample: "01D",
compression: ArchiveCompression::Gzip,
}];
const IGS_ULT_PRODUCTS: [CenterProductConvention; 1] = [CenterProductConvention {
product_type: ProductType::Sp3,
token: "IGS0OPSULT",
layout: ArchiveLayout::BkgProductsWeek,
span: "02D",
default_sample: "15M",
compression: ArchiveCompression::Gzip,
}];
const COD_ULT_PRODUCTS: [CenterProductConvention; 1] = [CenterProductConvention {
product_type: ProductType::Sp3,
token: "COD0OPSULT",
layout: ArchiveLayout::AiubCodeRoot,
span: "01D",
default_sample: "05M",
compression: ArchiveCompression::None,
}];
const ESA_ULT_PRODUCTS: [CenterProductConvention; 1] = [CenterProductConvention {
product_type: ProductType::Sp3,
token: "ESA0OPSULT",
layout: ArchiveLayout::GpsWeek,
span: "02D",
default_sample: "15M",
compression: ArchiveCompression::Gzip,
}];
const GFZ_ULT_PRODUCTS: [CenterProductConvention; 1] = [CenterProductConvention {
product_type: ProductType::Sp3,
token: "GFZ0OPSULT",
layout: ArchiveLayout::GfzUltraWeek,
span: "02D",
default_sample: "05M",
compression: ArchiveCompression::Gzip,
}];
const OPSULT_ISSUES: [&str; 4] = ["0000", "0600", "1200", "1800"];
const COD_ULT_ISSUES: [&str; 1] = ["0000"];
const CENTER_ORDER: [AnalysisCenter; 11] = [
AnalysisCenter::CodRap,
AnalysisCenter::CodPrd1,
AnalysisCenter::CodPrd2,
AnalysisCenter::Igs,
AnalysisCenter::Esa,
AnalysisCenter::Cod,
AnalysisCenter::Gfz,
AnalysisCenter::IgsUlt,
AnalysisCenter::CodUlt,
AnalysisCenter::EsaUlt,
AnalysisCenter::GfzUlt,
];
const CATALOG: [CenterCatalogEntry; 11] = [
CenterCatalogEntry {
center: AnalysisCenter::CodRap,
code: "cod_rap",
protocol: ArchiveProtocol::Http,
host: "ftp.aiub.unibe.ch",
root_url: "http://ftp.aiub.unibe.ch",
products: &COD_RAP_PRODUCTS,
issues: &[],
},
CenterCatalogEntry {
center: AnalysisCenter::CodPrd1,
code: "cod_prd1",
protocol: ArchiveProtocol::Http,
host: "ftp.aiub.unibe.ch",
root_url: "http://ftp.aiub.unibe.ch",
products: &COD_PRD_PRODUCTS,
issues: &[],
},
CenterCatalogEntry {
center: AnalysisCenter::CodPrd2,
code: "cod_prd2",
protocol: ArchiveProtocol::Http,
host: "ftp.aiub.unibe.ch",
root_url: "http://ftp.aiub.unibe.ch",
products: &COD_PRD_PRODUCTS,
issues: &[],
},
CenterCatalogEntry {
center: AnalysisCenter::Igs,
code: "igs",
protocol: ArchiveProtocol::Https,
host: "igs.bkg.bund.de",
root_url: "https://igs.bkg.bund.de/root_ftp/IGS",
products: &IGS_PRODUCTS,
issues: &[],
},
CenterCatalogEntry {
center: AnalysisCenter::Esa,
code: "esa",
protocol: ArchiveProtocol::Https,
host: "navigation-office.esa.int",
root_url: "https://navigation-office.esa.int/products/gnss-products",
products: &ESA_PRODUCTS,
issues: &[],
},
CenterCatalogEntry {
center: AnalysisCenter::Cod,
code: "cod",
protocol: ArchiveProtocol::Http,
host: "ftp.aiub.unibe.ch",
root_url: "http://ftp.aiub.unibe.ch",
products: &COD_PRODUCTS,
issues: &[],
},
CenterCatalogEntry {
center: AnalysisCenter::Gfz,
code: "gfz",
protocol: ArchiveProtocol::Https,
host: "isdc-data.gfz.de",
root_url: "https://isdc-data.gfz.de/gnss/products",
products: &GFZ_PRODUCTS,
issues: &[],
},
CenterCatalogEntry {
center: AnalysisCenter::IgsUlt,
code: "igs_ult",
protocol: ArchiveProtocol::Https,
host: "igs.bkg.bund.de",
root_url: "https://igs.bkg.bund.de/root_ftp/IGS",
products: &IGS_ULT_PRODUCTS,
issues: &OPSULT_ISSUES,
},
CenterCatalogEntry {
center: AnalysisCenter::CodUlt,
code: "cod_ult",
protocol: ArchiveProtocol::Http,
host: "ftp.aiub.unibe.ch",
root_url: "http://ftp.aiub.unibe.ch",
products: &COD_ULT_PRODUCTS,
issues: &COD_ULT_ISSUES,
},
CenterCatalogEntry {
center: AnalysisCenter::EsaUlt,
code: "esa_ult",
protocol: ArchiveProtocol::Https,
host: "navigation-office.esa.int",
root_url: "https://navigation-office.esa.int/products/gnss-products",
products: &ESA_ULT_PRODUCTS,
issues: &OPSULT_ISSUES,
},
CenterCatalogEntry {
center: AnalysisCenter::GfzUlt,
code: "gfz_ult",
protocol: ArchiveProtocol::Https,
host: "isdc-data.gfz.de",
root_url: "https://isdc-data.gfz.de/gnss/products",
products: &GFZ_ULT_PRODUCTS,
issues: &OPSULT_ISSUES,
},
];
const SKADI_SOURCE: TerrainSourceEntry = TerrainSourceEntry {
protocol: ArchiveProtocol::Https,
host: "s3.amazonaws.com",
compression: ArchiveCompression::Gzip,
root_url: "https://s3.amazonaws.com/elevation-tiles-prod",
};
const ALLOWED_HOSTS: [&str; 5] = [
"ftp.aiub.unibe.ch",
"navigation-office.esa.int",
"isdc-data.gfz.de",
"igs.bkg.bund.de",
"s3.amazonaws.com",
];
const NO_OPEN_MIRRORS: [NoOpenMirrorProduct; 7] = [
NoOpenMirrorProduct {
center: "grg",
product_type: "sp3",
},
NoOpenMirrorProduct {
center: "grg",
product_type: "clk",
},
NoOpenMirrorProduct {
center: "wum",
product_type: "sp3",
},
NoOpenMirrorProduct {
center: "wum",
product_type: "clk",
},
NoOpenMirrorProduct {
center: "grg_ult",
product_type: "sp3",
},
NoOpenMirrorProduct {
center: "grg_ult",
product_type: "clk",
},
NoOpenMirrorProduct {
center: "igs",
product_type: "ionex",
},
];
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DataCatalogError {
UnknownCenter(String),
UnknownProductType(String),
UnsupportedProduct {
center: AnalysisCenter,
product_type: ProductType,
},
NoOpenMirror {
center: String,
product_type: String,
},
InvalidDate {
year: i32,
month: u8,
day: u8,
},
DateOutOfRange,
DateBeforeGpsEpoch(ProductDate),
InvalidGpsDayOfWeek(u8),
InvalidSample(String),
InvalidIssue(String),
MissingIssue {
center: AnalysisCenter,
},
UnexpectedIssue {
center: AnalysisCenter,
},
UnsupportedIssue {
center: AnalysisCenter,
issue: String,
},
InvalidDateTime {
hour: u8,
minute: u8,
second: u8,
},
NoUltraIssue,
NoAvailableUltraIssue,
InvalidStation(String),
InvalidCoordinate {
lat_deg_bits: u64,
lon_deg_bits: u64,
},
InvalidTileIndex {
lat_index: i32,
lon_index: i32,
},
InvalidTileId(String),
}
impl fmt::Display for DataCatalogError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownCenter(center) => write!(f, "unknown analysis center {center:?}"),
Self::UnknownProductType(product_type) => {
write!(f, "unknown product type {product_type:?}")
}
Self::UnsupportedProduct {
center,
product_type,
} => write!(f, "{center} does not serve {product_type}"),
Self::NoOpenMirror {
center,
product_type,
} => write!(f, "{center}/{product_type} has no open mirror"),
Self::InvalidDate { year, month, day } => {
write!(f, "invalid product date {year:04}-{month:02}-{day:02}")
}
Self::DateOutOfRange => write!(f, "product date is out of range"),
Self::DateBeforeGpsEpoch(date) => {
write!(f, "product date {date} is before the GPS week epoch")
}
Self::InvalidGpsDayOfWeek(day) => {
write!(f, "invalid GPS day-of-week {day}")
}
Self::InvalidSample(sample) => write!(f, "invalid sample code {sample:?}"),
Self::InvalidIssue(issue) => write!(f, "invalid issue time {issue:?}"),
Self::MissingIssue { center } => write!(f, "{center} requires an issue time"),
Self::UnexpectedIssue { center } => write!(f, "{center} does not take an issue time"),
Self::UnsupportedIssue { center, issue } => {
write!(f, "{center} does not publish issue {issue:?}")
}
Self::InvalidDateTime {
hour,
minute,
second,
} => write!(f, "invalid product time {hour:02}:{minute:02}:{second:02}"),
Self::NoUltraIssue => write!(f, "no ultra-rapid issue at or before target"),
Self::NoAvailableUltraIssue => {
write!(f, "no available ultra-rapid issue at or before target")
}
Self::InvalidStation(station) => write!(f, "invalid station code {station:?}"),
Self::InvalidCoordinate {
lat_deg_bits,
lon_deg_bits,
} => write!(
f,
"invalid terrain coordinate lat={} lon={}",
f64::from_bits(*lat_deg_bits),
f64::from_bits(*lon_deg_bits)
),
Self::InvalidTileIndex {
lat_index,
lon_index,
} => write!(
f,
"invalid terrain tile index lat={lat_index} lon={lon_index}"
),
Self::InvalidTileId(id) => write!(f, "invalid skadi tile id {id:?}"),
}
}
}
impl std::error::Error for DataCatalogError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HgtConversionError {
BadLength {
expected: usize,
got: usize,
},
InvalidTileIndex {
lat_index: i32,
lon_index: i32,
},
}
impl fmt::Display for HgtConversionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadLength { expected, got } => {
write!(
f,
"invalid SRTM1 HGT length: expected {expected}, got {got}"
)
}
Self::InvalidTileIndex {
lat_index,
lon_index,
} => write!(
f,
"invalid terrain tile index lat={lat_index} lon={lon_index}"
),
}
}
}
impl std::error::Error for HgtConversionError {}
const MIN_TERRAIN_LAT_INDEX: i32 = -90;
const MAX_TERRAIN_LAT_INDEX: i32 = 89;
const MIN_TERRAIN_LON_INDEX: i32 = -180;
const MAX_TERRAIN_LON_INDEX: i32 = 179;
const MIN_TERRAIN_LAT_DEG: f64 = -90.0;
const MAX_TERRAIN_LAT_DEG: f64 = 90.0;
const MIN_TERRAIN_LON_DEG: f64 = -180.0;
const MAX_TERRAIN_LON_DEG: f64 = 180.0;
const SRTM1_POSTINGS_PER_AXIS: usize = 3601;
const SRTM1_HGT_LEN: usize = SRTM1_POSTINGS_PER_AXIS * SRTM1_POSTINGS_PER_AXIS * 2;
const DTED_SRTM1_DATA_BLOCK_LEN: usize = 12 + 2 * SRTM1_POSTINGS_PER_AXIS;
const DTED_SRTM1_LEN: usize =
terrain::DATA_OFFSET + SRTM1_POSTINGS_PER_AXIS * DTED_SRTM1_DATA_BLOCK_LEN;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ProductDate {
pub year: i32,
pub month: u8,
pub day: u8,
}
impl ProductDate {
pub fn new(year: i32, month: u8, day: u8) -> Result<Self, DataCatalogError> {
let days = days_in_month(i64::from(year), i64::from(month));
if !(1..=9999).contains(&year) || days == 0 || day == 0 || i64::from(day) > days {
return Err(DataCatalogError::InvalidDate { year, month, day });
}
Ok(Self { year, month, day })
}
pub fn from_gps_week_day(week: u32, day_of_week: u8) -> Result<Self, DataCatalogError> {
if day_of_week > 6 {
return Err(DataCatalogError::InvalidGpsDayOfWeek(day_of_week));
}
let epoch_jdn =
week_epoch_julian_day_number(TimeScale::Gpst).expect("GPST has a week-numbering epoch");
let offset_days = i64::from(week)
.checked_mul(7)
.and_then(|days| days.checked_add(i64::from(day_of_week)))
.ok_or(DataCatalogError::DateOutOfRange)?;
product_date_from_jdn(
epoch_jdn
.checked_add(offset_days)
.ok_or(DataCatalogError::DateOutOfRange)?,
)
}
pub fn gps_week(self) -> Result<u32, DataCatalogError> {
week_from_calendar(
TimeScale::Gpst,
i64::from(self.year),
i64::from(self.month),
i64::from(self.day),
)
.ok_or(DataCatalogError::DateBeforeGpsEpoch(self))
}
#[must_use]
pub fn day_of_year(self) -> u16 {
day_of_year_int(self.year, i32::from(self.month), i32::from(self.day)) as u16
}
fn add_days(self, days: i64) -> Result<Self, DataCatalogError> {
product_date_from_jdn(
self.julian_day_number()
.checked_add(days)
.ok_or(DataCatalogError::DateOutOfRange)?,
)
}
fn julian_day_number(self) -> i64 {
julian_day_number(self.year, i32::from(self.month), i32::from(self.day))
}
}
impl fmt::Display for ProductDate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ProductDateTime {
pub date: ProductDate,
pub hour: u8,
pub minute: u8,
pub second: u8,
}
impl ProductDateTime {
pub fn new(
date: ProductDate,
hour: u8,
minute: u8,
second: u8,
) -> Result<Self, DataCatalogError> {
if hour > 23 || minute > 59 || second > 59 {
return Err(DataCatalogError::InvalidDateTime {
hour,
minute,
second,
});
}
Ok(Self {
date,
hour,
minute,
second,
})
}
fn ordering_minutes(self) -> i64 {
self.date.julian_day_number() * 1_440 + i64::from(self.hour) * 60 + i64::from(self.minute)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct UltraIssue {
pub date: ProductDate,
pub issue: String,
}
impl UltraIssue {
pub fn new(date: ProductDate, issue: &str) -> Result<Self, DataCatalogError> {
validate_issue(issue)?;
Ok(Self {
date,
issue: issue.to_string(),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProductSpec {
pub center: AnalysisCenter,
pub product_type: ProductType,
pub date: ProductDate,
pub sample: String,
pub issue: Option<String>,
}
impl ProductSpec {
pub fn new(
center: AnalysisCenter,
product_type: ProductType,
date: ProductDate,
sample: &str,
issue: Option<&str>,
) -> Result<Self, DataCatalogError> {
validate_product(center, product_type, sample, issue)?;
Ok(Self {
center,
product_type,
date,
sample: sample.to_string(),
issue: issue.map(ToOwned::to_owned),
})
}
pub fn gps_week(&self) -> Result<u32, DataCatalogError> {
self.date.gps_week()
}
#[must_use]
pub fn day_of_year(&self) -> u16 {
self.date.day_of_year()
}
pub fn canonical_filename(&self) -> Result<String, DataCatalogError> {
let convention = validate_product(
self.center,
self.product_type,
&self.sample,
self.issue.as_deref(),
)?;
let descriptor = product_type_convention(self.product_type);
Ok(match descriptor.kind {
ProductFilenameKind::Sampled => format!(
"{}_{}_{}_{}_{}.{}",
convention.token,
date_block(self.date, self.issue.as_deref()),
convention.span,
self.sample,
descriptor.content_code,
descriptor.extension
),
ProductFilenameKind::Nav => format!(
"{}_R_{}_{}_{}.{}",
convention.token,
date_block(self.date, None),
convention.span,
descriptor.content_code,
descriptor.extension
),
})
}
pub fn archive_url(&self) -> Result<String, DataCatalogError> {
let convention = validate_product(
self.center,
self.product_type,
&self.sample,
self.issue.as_deref(),
)?;
let entry = center_catalog(self.center).expect("catalog entry exists for enum variant");
let filename = self.canonical_filename()?;
Ok(format!(
"{}/{}/{}{}",
entry.root_url,
dir_path(convention.layout, self.date)?,
filename,
convention.compression.suffix()
))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StationObservationSpec {
pub station: String,
pub date: ProductDate,
pub sample: String,
}
impl StationObservationSpec {
pub fn new(station: &str, date: ProductDate, sample: &str) -> Result<Self, DataCatalogError> {
validate_station(station)?;
validate_sample(sample)?;
Ok(Self {
station: station.to_string(),
date,
sample: sample.to_string(),
})
}
pub fn canonical_filename(&self) -> Result<String, DataCatalogError> {
station_obs_filename(&self.station, self.date, &self.sample)
}
pub fn archive_url(&self) -> Result<String, DataCatalogError> {
station_obs_url(&self.station, self.date, &self.sample)
}
}
#[must_use]
pub const fn catalog() -> &'static [CenterCatalogEntry] {
&CATALOG
}
#[must_use]
pub const fn centers() -> &'static [AnalysisCenter] {
&CENTER_ORDER
}
#[must_use]
pub const fn product_types() -> &'static [ProductTypeConvention] {
&PRODUCT_TYPE_CONVENTIONS
}
#[must_use]
pub const fn allowed_hosts() -> &'static [&'static str] {
&ALLOWED_HOSTS
}
#[must_use]
pub const fn skadi_source_entry() -> TerrainSourceEntry {
SKADI_SOURCE
}
pub fn skadi_tile_id(lat_index: i32, lon_index: i32) -> Result<String, DataCatalogError> {
validate_terrain_tile_index(lat_index, lon_index)?;
let lat_hemi = if lat_index >= 0 { 'N' } else { 'S' };
let lon_hemi = if lon_index >= 0 { 'E' } else { 'W' };
Ok(format!(
"{lat_hemi}{:02}{lon_hemi}{:03}",
lat_index.abs(),
lon_index.abs()
))
}
pub fn skadi_band(lat_index: i32) -> Result<String, DataCatalogError> {
validate_terrain_lat_index(lat_index)?;
let lat_hemi = if lat_index >= 0 { 'N' } else { 'S' };
Ok(format!("{lat_hemi}{:02}", lat_index.abs()))
}
pub fn skadi_archive_url(lat_index: i32, lon_index: i32) -> Result<String, DataCatalogError> {
let band = skadi_band(lat_index)?;
let tile_id = skadi_tile_id(lat_index, lon_index)?;
Ok(format!(
"{}/skadi/{}/{}.hgt{}",
SKADI_SOURCE.root_url,
band,
tile_id,
SKADI_SOURCE.compression.suffix()
))
}
pub fn dted_tile_filename(lat_index: i32, lon_index: i32) -> Result<String, DataCatalogError> {
validate_terrain_tile_index(lat_index, lon_index)?;
Ok(format!(
"{}_{}{}",
terrain::format_lat(lat_index),
terrain::format_lon(lon_index),
terrain::DTED_SUFFIX
))
}
pub fn dted_block_dir(lat_index: i32, lon_index: i32) -> Result<String, DataCatalogError> {
validate_terrain_tile_index(lat_index, lon_index)?;
Ok(terrain::terrain_block_dir(lat_index, lon_index))
}
pub fn dted_cache_relpath(lat_index: i32, lon_index: i32) -> Result<String, DataCatalogError> {
Ok(format!(
"{}/{}",
dted_block_dir(lat_index, lon_index)?,
dted_tile_filename(lat_index, lon_index)?
))
}
pub fn parse_skadi_tile_id(id: &str) -> Result<(i32, i32), DataCatalogError> {
let bytes = id.as_bytes();
if bytes.len() != 7
|| !matches!(bytes[0], b'N' | b'S')
|| !matches!(bytes[3], b'E' | b'W')
|| !bytes[1..3].iter().all(u8::is_ascii_digit)
|| !bytes[4..7].iter().all(u8::is_ascii_digit)
{
return Err(DataCatalogError::InvalidTileId(id.to_string()));
}
let lat_abs = id[1..3]
.parse::<i32>()
.map_err(|_| DataCatalogError::InvalidTileId(id.to_string()))?;
let lon_abs = id[4..7]
.parse::<i32>()
.map_err(|_| DataCatalogError::InvalidTileId(id.to_string()))?;
if (bytes[0] == b'S' && lat_abs == 0) || (bytes[3] == b'W' && lon_abs == 0) {
return Err(DataCatalogError::InvalidTileId(id.to_string()));
}
let lat_index = if bytes[0] == b'N' { lat_abs } else { -lat_abs };
let lon_index = if bytes[3] == b'E' { lon_abs } else { -lon_abs };
validate_terrain_tile_index(lat_index, lon_index)?;
Ok((lat_index, lon_index))
}
pub fn terrain_tile_index(lat_deg: f64, lon_deg: f64) -> Result<(i32, i32), DataCatalogError> {
if !lat_deg.is_finite()
|| !lon_deg.is_finite()
|| !(MIN_TERRAIN_LAT_DEG..=MAX_TERRAIN_LAT_DEG).contains(&lat_deg)
|| !(MIN_TERRAIN_LON_DEG..=MAX_TERRAIN_LON_DEG).contains(&lon_deg)
{
return Err(DataCatalogError::InvalidCoordinate {
lat_deg_bits: lat_deg.to_bits(),
lon_deg_bits: lon_deg.to_bits(),
});
}
let (mut lat_index, mut lon_index) = terrain::terrain_grid(lon_deg, lat_deg);
if lat_index == MAX_TERRAIN_LAT_DEG as i32 {
lat_index = MAX_TERRAIN_LAT_INDEX;
}
if lon_index == MAX_TERRAIN_LON_DEG as i32 {
lon_index = MAX_TERRAIN_LON_INDEX;
}
validate_terrain_tile_index(lat_index, lon_index)?;
Ok((lat_index, lon_index))
}
pub fn hgt_to_dted(
lat_index: i32,
lon_index: i32,
hgt: &[u8],
) -> Result<Vec<u8>, HgtConversionError> {
validate_hgt_tile_index(lat_index, lon_index)?;
if hgt.len() != SRTM1_HGT_LEN {
return Err(HgtConversionError::BadLength {
expected: SRTM1_HGT_LEN,
got: hgt.len(),
});
}
let mut out = vec![b' '; DTED_SRTM1_LEN];
out[0..4].copy_from_slice(b"UHL1");
out[4..12].copy_from_slice(dted_coord_field(lon_index, true).as_bytes());
out[12..20].copy_from_slice(dted_coord_field(lat_index, false).as_bytes());
out[47..51].copy_from_slice(b"3601");
out[51..55].copy_from_slice(b"3601");
for lon_posting in 0..SRTM1_POSTINGS_PER_AXIS {
let block_start = terrain::DATA_OFFSET + lon_posting * DTED_SRTM1_DATA_BLOCK_LEN;
let checksum_start = block_start + DTED_SRTM1_DATA_BLOCK_LEN - 4;
out[block_start] = terrain::DATA_SENTINEL;
let count = (lon_posting as u32).to_be_bytes();
out[block_start + 1..block_start + 4].copy_from_slice(&count[1..4]);
out[block_start + 4..block_start + 6].copy_from_slice(&(lon_posting as u16).to_be_bytes());
out[block_start + 6..block_start + 8].copy_from_slice(&0u16.to_be_bytes());
for lat_posting in 0..SRTM1_POSTINGS_PER_AXIS {
let hgt_row = SRTM1_POSTINGS_PER_AXIS - 1 - lat_posting;
let hgt_sample_start = 2 * (hgt_row * SRTM1_POSTINGS_PER_AXIS + lon_posting);
let sample = i16::from_be_bytes([hgt[hgt_sample_start], hgt[hgt_sample_start + 1]]);
let encoded = encode_dted_signed_magnitude(sample).to_be_bytes();
let dted_sample_start = block_start + 8 + 2 * lat_posting;
out[dted_sample_start..dted_sample_start + 2].copy_from_slice(&encoded);
}
let checksum = out[block_start..checksum_start]
.iter()
.fold(0i32, |acc, byte| acc + i32::from(*byte));
out[checksum_start..checksum_start + 4].copy_from_slice(&checksum.to_be_bytes());
}
debug_assert_eq!(out.len(), 25_981_042);
Ok(out)
}
#[must_use]
pub const fn no_open_mirrors() -> &'static [NoOpenMirrorProduct] {
&NO_OPEN_MIRRORS
}
pub fn open_mirror(
center: AnalysisCenter,
product_type: ProductType,
) -> Result<(), DataCatalogError> {
open_mirror_code(center.code(), product_type.code())
}
pub fn open_mirror_code(center: &str, product_type: &str) -> Result<(), DataCatalogError> {
if NO_OPEN_MIRRORS
.iter()
.any(|entry| entry.center == center && entry.product_type == product_type)
{
Err(DataCatalogError::NoOpenMirror {
center: center.to_string(),
product_type: product_type.to_string(),
})
} else {
Ok(())
}
}
#[must_use]
pub fn center_catalog(center: AnalysisCenter) -> Option<&'static CenterCatalogEntry> {
CATALOG.iter().find(|entry| entry.center == center)
}
pub fn product_convention(
center: AnalysisCenter,
product_type: ProductType,
) -> Result<&'static CenterProductConvention, DataCatalogError> {
open_mirror(center, product_type)?;
let entry = center_catalog(center).expect("catalog entry exists for enum variant");
entry
.products
.iter()
.find(|product| product.product_type == product_type)
.ok_or(DataCatalogError::UnsupportedProduct {
center,
product_type,
})
}
pub fn default_sample(
center: AnalysisCenter,
product_type: ProductType,
) -> Result<&'static str, DataCatalogError> {
Ok(product_convention(center, product_type)?.default_sample)
}
pub fn gps_week(date: ProductDate) -> Result<u32, DataCatalogError> {
date.gps_week()
}
#[must_use]
pub fn day_of_year(date: ProductDate) -> u16 {
date.day_of_year()
}
pub fn product(
center: AnalysisCenter,
product_type: ProductType,
date: ProductDate,
sample: Option<&str>,
issue: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
let sample = match sample {
Some(sample) => sample,
None => default_sample(center, product_type)?,
};
ProductSpec::new(center, product_type, date, sample, issue)
}
pub fn canonical_filename(
center: AnalysisCenter,
product_type: ProductType,
date: ProductDate,
sample: Option<&str>,
issue: Option<&str>,
) -> Result<String, DataCatalogError> {
product(center, product_type, date, sample, issue)?.canonical_filename()
}
pub fn archive_url(
center: AnalysisCenter,
product_type: ProductType,
date: ProductDate,
sample: Option<&str>,
issue: Option<&str>,
) -> Result<String, DataCatalogError> {
product(center, product_type, date, sample, issue)?.archive_url()
}
pub fn mgex_clk(
center: AnalysisCenter,
date: ProductDate,
sample: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
product(center, ProductType::Clk, date, sample, None)
}
pub fn mgex_nav(
center: AnalysisCenter,
date: ProductDate,
sample: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
product(center, ProductType::Nav, date, sample, None)
}
pub fn mgex_ionex(
center: AnalysisCenter,
date: ProductDate,
sample: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
product(center, ProductType::Ionex, date, sample, None)
}
pub fn rapid_ionex(
date: ProductDate,
sample: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
product(
AnalysisCenter::CodRap,
ProductType::Ionex,
date,
sample,
None,
)
}
#[must_use]
pub const fn predicted_day_offset(center: AnalysisCenter) -> i64 {
match center {
AnalysisCenter::CodPrd2 => 1,
_ => 0,
}
}
pub fn predicted_ionex(
center: AnalysisCenter,
date: ProductDate,
sample: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
match center {
AnalysisCenter::CodPrd1 | AnalysisCenter::CodPrd2 => {
let target = date.add_days(predicted_day_offset(center))?;
product(center, ProductType::Ionex, target, sample, None)
}
other => Err(DataCatalogError::UnsupportedProduct {
center: other,
product_type: ProductType::Ionex,
}),
}
}
pub fn mgex_sp3(
center: AnalysisCenter,
date: ProductDate,
sample: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
product(center, ProductType::Sp3, date, sample, None)
}
pub fn ops_ultra_sp3(
center: AnalysisCenter,
date: ProductDate,
sample: Option<&str>,
issue: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
let issue = issue.unwrap_or("0000");
product(center, ProductType::Sp3, date, sample, Some(issue))
}
pub fn ops_ultra_clk(
center: AnalysisCenter,
date: ProductDate,
sample: Option<&str>,
issue: Option<&str>,
) -> Result<ProductSpec, DataCatalogError> {
let issue = issue.unwrap_or("0000");
product(center, ProductType::Clk, date, sample, Some(issue))
}
pub fn latest_ops_ultra_sp3(
center: AnalysisCenter,
target: ProductDateTime,
sample: Option<&str>,
available_issues: Option<&[UltraIssue]>,
) -> Result<ProductSpec, DataCatalogError> {
let selected = latest_ultra_issue(center, target, available_issues)?;
ops_ultra_sp3(center, selected.date, sample, Some(&selected.issue))
}
pub fn ultra_issue_candidates(
center: AnalysisCenter,
target: ProductDateTime,
) -> Result<Vec<UltraIssue>, DataCatalogError> {
let entry = center_catalog(center).expect("catalog entry exists for enum variant");
let _ = product_convention(center, ProductType::Sp3)?;
if entry.issues.is_empty() {
return Err(DataCatalogError::UnsupportedProduct {
center,
product_type: ProductType::Sp3,
});
}
let mut candidates = Vec::new();
for date in [target.date, target.date.add_days(-1)?] {
for issue in entry.issues.iter().rev() {
if issue_ordering_minutes(date, issue)? <= target.ordering_minutes() {
candidates.push(UltraIssue::new(date, issue)?);
}
}
}
Ok(candidates)
}
pub fn latest_ultra_issue(
center: AnalysisCenter,
target: ProductDateTime,
available_issues: Option<&[UltraIssue]>,
) -> Result<UltraIssue, DataCatalogError> {
let candidates = ultra_issue_candidates(center, target)?;
if candidates.is_empty() {
return Err(DataCatalogError::NoUltraIssue);
}
if let Some(available) = available_issues {
candidates
.into_iter()
.find(|candidate| {
available
.iter()
.any(|issue| issue.date == candidate.date && issue.issue == candidate.issue)
})
.ok_or(DataCatalogError::NoAvailableUltraIssue)
} else {
Ok(candidates[0].clone())
}
}
pub fn gim_date_candidates(
center: AnalysisCenter,
target: ProductDate,
lookback: u32,
) -> Result<Vec<ProductDate>, DataCatalogError> {
let _ = product_convention(center, ProductType::Ionex)?;
let base = target.add_days(predicted_day_offset(center))?;
let mut out = Vec::with_capacity(usize::try_from(lookback).unwrap_or(usize::MAX));
for back in 0..=lookback {
out.push(base.add_days(-i64::from(back))?);
}
Ok(out)
}
pub fn station_obs(
station: &str,
date: ProductDate,
sample: Option<&str>,
) -> Result<StationObservationSpec, DataCatalogError> {
StationObservationSpec::new(station, date, sample.unwrap_or("30S"))
}
pub fn station_obs_filename(
station: &str,
date: ProductDate,
sample: &str,
) -> Result<String, DataCatalogError> {
validate_station(station)?;
validate_sample(sample)?;
Ok(format!(
"{}_R_{}_01D_{}_MO.crx",
station,
date_block(date, None),
sample
))
}
pub fn station_obs_url(
station: &str,
date: ProductDate,
sample: &str,
) -> Result<String, DataCatalogError> {
let filename = station_obs_filename(station, date, sample)?;
Ok(format!(
"https://igs.bkg.bund.de/root_ftp/IGS/{}/{}.gz",
dir_path(ArchiveLayout::BkgObsYearDoy, date)?,
filename
))
}
#[must_use]
pub const fn station_obs_protocol() -> ArchiveProtocol {
ArchiveProtocol::Https
}
fn validate_terrain_lat_index(lat_index: i32) -> Result<(), DataCatalogError> {
if (MIN_TERRAIN_LAT_INDEX..=MAX_TERRAIN_LAT_INDEX).contains(&lat_index) {
Ok(())
} else {
Err(DataCatalogError::InvalidTileIndex {
lat_index,
lon_index: 0,
})
}
}
fn validate_terrain_tile_index(lat_index: i32, lon_index: i32) -> Result<(), DataCatalogError> {
if (MIN_TERRAIN_LAT_INDEX..=MAX_TERRAIN_LAT_INDEX).contains(&lat_index)
&& (MIN_TERRAIN_LON_INDEX..=MAX_TERRAIN_LON_INDEX).contains(&lon_index)
{
Ok(())
} else {
Err(DataCatalogError::InvalidTileIndex {
lat_index,
lon_index,
})
}
}
fn validate_hgt_tile_index(lat_index: i32, lon_index: i32) -> Result<(), HgtConversionError> {
if (MIN_TERRAIN_LAT_INDEX..=MAX_TERRAIN_LAT_INDEX).contains(&lat_index)
&& (MIN_TERRAIN_LON_INDEX..=MAX_TERRAIN_LON_INDEX).contains(&lon_index)
{
Ok(())
} else {
Err(HgtConversionError::InvalidTileIndex {
lat_index,
lon_index,
})
}
}
fn dted_coord_field(index: i32, is_longitude: bool) -> String {
let hemi = match (is_longitude, index >= 0) {
(true, true) => 'E',
(true, false) => 'W',
(false, true) => 'N',
(false, false) => 'S',
};
format!("{:03}0000{hemi}", index.abs())
}
fn encode_dted_signed_magnitude(sample: i16) -> u16 {
if sample == i16::MIN {
0
} else if sample >= 0 {
sample as u16
} else {
0x8000 | (-i32::from(sample) as u16)
}
}
fn product_type_convention(product_type: ProductType) -> &'static ProductTypeConvention {
PRODUCT_TYPE_CONVENTIONS
.iter()
.find(|descriptor| descriptor.product_type == product_type)
.expect("product descriptor exists for enum variant")
}
fn validate_product(
center: AnalysisCenter,
product_type: ProductType,
sample: &str,
issue: Option<&str>,
) -> Result<&'static CenterProductConvention, DataCatalogError> {
let convention = product_convention(center, product_type)?;
validate_sample(sample)?;
validate_issue_for_center(center, issue)?;
Ok(convention)
}
fn validate_issue_for_center(
center: AnalysisCenter,
issue: Option<&str>,
) -> Result<(), DataCatalogError> {
let entry = center_catalog(center).expect("catalog entry exists for enum variant");
match (entry.issues.is_empty(), issue) {
(true, None) => Ok(()),
(true, Some(_)) => Err(DataCatalogError::UnexpectedIssue { center }),
(false, None) => Err(DataCatalogError::MissingIssue { center }),
(false, Some(issue)) => {
validate_issue(issue)?;
if entry.issues.contains(&issue) {
Ok(())
} else {
Err(DataCatalogError::UnsupportedIssue {
center,
issue: issue.to_string(),
})
}
}
}
}
fn validate_sample(sample: &str) -> Result<(), DataCatalogError> {
let bytes = sample.as_bytes();
let valid = bytes.len() == 3
&& bytes[0].is_ascii_digit()
&& bytes[1].is_ascii_digit()
&& bytes[2].is_ascii_uppercase();
if valid {
Ok(())
} else {
Err(DataCatalogError::InvalidSample(sample.to_string()))
}
}
fn validate_issue(issue: &str) -> Result<(), DataCatalogError> {
let bytes = issue.as_bytes();
let valid_digits = bytes.len() == 4 && bytes.iter().all(u8::is_ascii_digit);
if !valid_digits {
return Err(DataCatalogError::InvalidIssue(issue.to_string()));
}
let hour = issue[0..2]
.parse::<u8>()
.map_err(|_| DataCatalogError::InvalidIssue(issue.to_string()))?;
let minute = issue[2..4]
.parse::<u8>()
.map_err(|_| DataCatalogError::InvalidIssue(issue.to_string()))?;
if hour <= 23 && minute <= 59 {
Ok(())
} else {
Err(DataCatalogError::InvalidIssue(issue.to_string()))
}
}
fn validate_station(station: &str) -> Result<(), DataCatalogError> {
let bytes = station.as_bytes();
let valid = bytes.len() == 9
&& bytes
.iter()
.all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit());
if valid {
Ok(())
} else {
Err(DataCatalogError::InvalidStation(station.to_string()))
}
}
fn issue_minutes(issue: &str) -> Result<u16, DataCatalogError> {
validate_issue(issue)?;
let hour = issue[0..2]
.parse::<u16>()
.map_err(|_| DataCatalogError::InvalidIssue(issue.to_string()))?;
let minute = issue[2..4]
.parse::<u16>()
.map_err(|_| DataCatalogError::InvalidIssue(issue.to_string()))?;
Ok(hour * 60 + minute)
}
fn issue_ordering_minutes(date: ProductDate, issue: &str) -> Result<i64, DataCatalogError> {
Ok(date.julian_day_number() * 1_440 + i64::from(issue_minutes(issue)?))
}
fn date_block(date: ProductDate, issue: Option<&str>) -> String {
format!(
"{}{:03}{}",
date.year,
date.day_of_year(),
issue.unwrap_or("0000")
)
}
fn dir_path(layout: ArchiveLayout, date: ProductDate) -> Result<String, DataCatalogError> {
Ok(match layout {
ArchiveLayout::GfzRapidWeek => format!("rapid/w{}", date.gps_week()?),
ArchiveLayout::GfzUltraWeek => format!("ultra/w{}", date.gps_week()?),
ArchiveLayout::GpsWeek => date.gps_week()?.to_string(),
ArchiveLayout::BkgProductsWeek => format!("products/{}", date.gps_week()?),
ArchiveLayout::BkgBrdcYearDoy => {
format!("BRDC/{}/{:03}", date.year, date.day_of_year())
}
ArchiveLayout::BkgObsYearDoy => format!("obs/{}/{:03}", date.year, date.day_of_year()),
ArchiveLayout::AiubCodeMgexYear => format!("CODE_MGEX/CODE/{}", date.year),
ArchiveLayout::AiubCodeYear => format!("CODE/{}", date.year),
ArchiveLayout::AiubCodeRoot => "CODE".to_string(),
})
}
fn product_date_from_jdn(jdn: i64) -> Result<ProductDate, DataCatalogError> {
let (year, month, day) = civil_from_julian_day_number(jdn);
let year = i32::try_from(year).map_err(|_| DataCatalogError::DateOutOfRange)?;
let month = u8::try_from(month).map_err(|_| DataCatalogError::DateOutOfRange)?;
let day = u8::try_from(day).map_err(|_| DataCatalogError::DateOutOfRange)?;
ProductDate::new(year, month, day).map_err(|_| DataCatalogError::DateOutOfRange)
}