#json #json-parser #astronomy #ivoa #stc

stc-s

Rust implementation of a STC-S serializer/deserializer with JSON support

1 unstable release

0.1.0 Dec 12, 2023

#978 in Encoding

Download history 1384/week @ 2024-05-31 28/week @ 2024-06-07 34/week @ 2024-06-14 174/week @ 2024-06-21 158/week @ 2024-06-28 189/week @ 2024-07-05 158/week @ 2024-07-12 31/week @ 2024-07-19 111/week @ 2024-07-26 4/week @ 2024-08-02 5/week @ 2024-08-23 75/week @ 2024-08-30 273/week @ 2024-09-06 9/week @ 2024-09-13

362 downloads per month
Used in moc

Apache-2.0 OR MIT

230KB
7.5K SLoC

stc-s or STCSLibRust

Library to read/write IVOA STC-S strings in Rust, and to convert them from/to JSON. The parser has been implemented following:

Other existing implementations:

Motivations

This library is to be used in MOC lib Rust to support the creation of MOCs -- S-MOCs but also ST-MOCs, F-MOCs, ... -- from (simple) STC−S descriptions. E.g., this feature has been requested in MOCPy, see issue #111.

It may also be used in:

Status

This library is in an early stage of development. We are (reasonably) open to changes and we appreciate feedbacks. Please, provide us with more STC-S examples!

STC-S String from API example

Stc::new().set_space(
  PositionInterval::from_frame(Frame::ICRS)
    .set_refpos(SpaceTimeRefPos::Geocenter)
    .set_lo_hi_limits(vec![170.0, -20.0, 190.0, 10.0])
    .set_resolution(vec![0.0001])
).to_string();

gives the string:

"PositionInterval ICRS GEOCENTER 170 -20 190 10 Resolution 0.0001"

You can parse the string back to an object using (unwrap must be avoided, see tests in lib.rs for a complete example):

let ascii = "PositionInterval ICRS GEOCENTER 170 -20 190 10 Resolution 0.0001";
let stc = Stc::parse::<VerboseError<&str>>(ascii).unwrap().1;

STC-S to JSON conversion examples

The following examples are extracted from the internal tests.

Example 1

Circle ICRS TOPOCENTER 147.6 69.9 0.4

is converted into:

{
  "space": {
    "Circle": {
      "frame": "ICRS",
      "refpos": "TOPOCENTER",
      "pos": [
        147.6,
        69.9
      ],
      "radius": 0.4
    }
  }
}

Example 2

Time TDB BARYCENTER MJD 50814.0 
Position ICRS BARYCENTER 147.3 69.3

is converted into:

{
  "time": {
    "Time": {
      "timescale": "TDB",
      "refpos": "BARYCENTER",
      "time": {
        "MJD": "50814.0"
      }
    }
  },
  "space": {
    "frame": "ICRS",
    "refpos": "BARYCENTER",
    "pos": [
      147.3,
      69.3
    ]
  }
}

Example 3

TimeInterval TT GEOCENTER 1996-01-01T00:00:00 1996-01-01T00:30:00
 Time MJD 50814.0 Error 1.2 Resolution 0.8 PixSize 1024.0
Circle ICRS GEOCENTER 179.0 -11.5 0.5 Position 179.0 -11.5
 Error 0.000889 Resolution 0.001778 Size 0.000333 0.000278
 PixSize 0.000083 0.000083
Spectral BARYCENTER 1420.4 unit MHz Resolution 10.0
RedshiftInterval BARYCENTER VELOCITY OPTICAL 200.0 2300.0
 Redshift 300.0 Resolution 0.7 PixSize 0.3

is converted into:

{
  "time": {
    "TimeInterval": {
      "timescale": "TT",
      "refpos": "GEOCENTER",
      "start": [
        {
          "Iso": "1996-01-01T00:00:00Z"
        }
      ],
      "stop": [
        {
          "Iso": "1996-01-01T00:30:00Z"
        }
      ],
      "time": {
        "MJD": "50814.0"
      },
      "error": 1.2,
      "resolution": 0.8,
      "pixsize": 1024.0
    }
  },
  "space": {
    "Circle": {
      "frame": "ICRS",
      "refpos": "GEOCENTER",
      "pos": [
        179.0,
        -11.5
      ],
      "radius": 0.5,
      "position": [
        179.0,
        -11.5
      ],
      "error": [
        0.000889
      ],
      "resolution": [
        0.001778
      ],
      "size": [
        0.000333,
        0.000278
      ],
      "pixsize": [
        0.000083,
        0.000083
      ]
    }
  },
  "spectral": {
    "Value": {
      "refpos": "BARYCENTER",
      "value": 1420.4,
      "unit": "MHz",
      "resolution": 10.0
    }
  },
  "redshift": {
    "RedshiftInterval": {
      "refpos": "BARYCENTER",
      "type": "VELOCITY",
      "dopplerdef": "OPTICAL",
      "lolimit": [
        200.0
      ],
      "hilimit": [
        2300.0
      ],
      "redshift": 300.0,
      "resolution": 0.7,
      "pixsize": 0.3
    }
  }
}

Example 4

Union ICRS TOPOCENTER
(Circle 180 10 20
Circle 190 20 20
Intersection
 (Circle 120 -10 20
  Difference
   (Circle 130 -10 20
    Circle 125 -10 2
   )
  Not
   (Circle 118 -8 3)
 )
)

is converted into:

{
  "space": {
    "Union": {
      "frame": "ICRS",
      "refpos": "TOPOCENTER",
      "elems": [
        {
          "Circle": {
            "pos": [
              180.0,
              10.0
            ],
            "radius": 20.0
          }
        },
        {
          "Circle": {
            "pos": [
              190.0,
              20.0
            ],
            "radius": 20.0
          }
        },
        {
          "Intersection": {
            "elems": [
              {
                "Circle": {
                  "pos": [
                    120.0,
                    -10.0
                  ],
                  "radius": 20.0
                }
              },
              {
                "Difference": {
                  "left": {
                    "Circle": {
                      "pos": [
                        130.0,
                        -10.0
                      ],
                      "radius": 20.0
                    }
                  },
                  "right": {
                    "Circle": {
                      "pos": [
                        125.0,
                        -10.0
                      ],
                      "radius": 2.0
                    }
                  }
                }
              },
              {
                "Not": {
                  "Circle": {
                    "pos": [
                      118.0,
                      -8.0
                    ],
                    "radius": 3.0
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

To-do list

  • Parse and write STC-S.
  • Support JSON serialization and deserialization.
  • Support parsing of STC-S as defined in TAP.
  • Make a CLI.
  • Make a JS/Wasm library.
  • Add everywhere builders, getters and setters like in the FillFrameRefposFlavor structure to make a clean API.
  • Create a visitor.
    • implement an empty visitor
    • implement an echo visitor
    • implement a stcs2moc visitor (available in MOCLibRust
  • Implement fold to avoid too wide lines.
  • Support for STC XML serialization/deserialization?

License

Like most projects in Rust, this project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.4–2.4MB
~50K SLoC