Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: simple reading example #10

Open
mfouesneau opened this issue May 1, 2024 · 4 comments
Open

Documentation: simple reading example #10

mfouesneau opened this issue May 1, 2024 · 4 comments

Comments

@mfouesneau
Copy link

Hi,

Would it be possible to have a simple example of reading a votable from either a file or string? How do I get the data, parameters, etc?

My limited API understanding here

let mut vot = VOTableWrapper::<InMemTableDataRows>::from_ivoa_xml_str(&response)
        .unwrap()
        .votable
        .get_first_table()
        .unwrap();

But it seems that all values have disappeared or are hiding somewhere. Exporting the content to json or yaml does not show the table data either.

@fxpineau
Copy link
Member

fxpineau commented May 7, 2024

Hi @mfouesneau :),
I just see your message (no/missed notification?).
Have you started to code in Rust?
I have not yet put efforts in the doc since I am not aware of people coding directly in Rust in the community...

You can test vot-cli and look at its source code starting with input.rs and output.rs.

Here a minimal example (not tested/may contain typos) extracted from the vot-cli source code:

let input_file = File::open(input_path).map_err(VOTableError::Io)?;
let reader = BufReader::new(input_file);
let vot = VOTableWrapper::<InMemTableDataRows>::from_ivoa_xml_reader(reader)?;

let output_file = File::create(output_path).map_err(VOTableError::Io)?;
let writer = BufWriter::new(output_file);
let pretty = true;
vot.to_json_writer(writer, pretty)?;

@mfouesneau
Copy link
Author

Hi @fxpineau, yes I'm looking into it slowly. My main issue is that the documentation and examples/unittests do not tell me where is what.

I am trying to read a votable, params and data, but I have no idea which structure these are in your class...

When I output the json/yaml content, I do not see the data. you can try for instance the svo filter service votables.

@fxpineau
Copy link
Member

fxpineau commented May 7, 2024

@mfouesneau,

Ok, I added a quick and dirty "demo" test here
I had to put a few fields public (see commit fec3c2081077e40b8df5d1f1235d27acbfddef63)

Here the example code (yes, I could have done things differently):

    // http://svo2.cab.inta-csic.es/theory/fps/fps.php?ID=2MASS/2MASS.H
    let path = "resources/SVO_FILTER_2MASS_H.xml";
    let vot = VOTableWrapper::<InMemTableDataRows>::from_ivoa_xml_file(path)
      .unwrap()
      .unwrap();
    if let Some(table) = vot.get_first_table() {
      // Print params
      table
        .elems
        .iter()
        .filter_map(|e| match e {
          TableElem::Param(param) => Some(param),
          _ => None,
        })
        .for_each(|param| {
          println!(
            "PARAM. name: {}; datatype: {}, value: {} ",
            param.field.name, param.field.datatype, param.value
          )
        });
      // Print field
      table
        .elems
        .iter()
        .filter_map(|e| match e {
          TableElem::Field(field) => Some(field),
          _ => None,
        })
        .for_each(|field| println!("FIELD. name: {}; datatype: {}", field.name, field.datatype));
      // Print rows
      println!("ROWS: ");
      if let Some(data) = &table.data {
        match &data.data {
          DataElem::TableData(tabledata) => {
            for row in &tabledata.content.rows {
              for field in row {
                print!("{} , ", field.to_string());
              }
              println!();
            }
          }
          _ => todo!(),
        }
      }
    }

and the result:

PARAM. name: FilterProfileService; datatype: char, value: ivo://svo/fps 
PARAM. name: filterID; datatype: char, value: 2MASS/2MASS.H 
PARAM. name: WavelengthUnit; datatype: char, value: Angstrom 
PARAM. name: WavelengthUCD; datatype: char, value: em.wl 
PARAM. name: Description; datatype: char, value: 2MASS H 
PARAM. name: PhotSystem; datatype: char, value: 2MASS 
PARAM. name: DetectorType; datatype: char, value: 0 
PARAM. name: Band; datatype: char, value: H 
PARAM. name: Facility; datatype: char, value: 2MASS 
PARAM. name: ProfileReference; datatype: char, value: http://www.ipac.caltech.edu/2mass/releases/allsky/doc/sec6_4a.html#rsr 
PARAM. name: CalibrationReference; datatype: char, value: http://adsabs.harvard.edu/abs/2003AJ....126.1090C 
PARAM. name: Description; datatype: char, value: 2MASS H 
PARAM. name: components; datatype: char, value: Filter + Instrument + Atmosphere 
PARAM. name: WavelengthRef; datatype: double, value: 16620 
PARAM. name: WavelengthMean; datatype: double, value: 16620 
PARAM. name: WavelengthEff; datatype: double, value: 16620 
PARAM. name: WavelengthMin; datatype: double, value: 14787.378640179 
PARAM. name: WavelengthMax; datatype: double, value: 18231.020407164 
PARAM. name: WidthEff; datatype: double, value: 2509.4034987068 
PARAM. name: WavelengthCen; datatype: double, value: 16487.192828097 
PARAM. name: WavelengthPivot; datatype: double, value: 16457.503740034 
PARAM. name: WavelengthPeak; datatype: double, value: 16710 
PARAM. name: WavelengthPhot; datatype: double, value: 16422.955724896 
PARAM. name: FWHM; datatype: double, value: 2609.6475383665 
PARAM. name: Fsun; datatype: double, value: 22.596337946016 
PARAM. name: PhotCalID; datatype: char, value: 2MASS/2MASS.H/Vega 
PARAM. name: MagSys; datatype: char, value: Vega 
PARAM. name: ZeroPoint; datatype: double, value: 1024 
PARAM. name: ZeroPointUnit; datatype: char, value: Jy 
PARAM. name: ZeroPointType; datatype: char, value: Pogson 
FIELD. name: Wavelength; datatype: double
FIELD. name: Transmission; datatype: double
ROWS: 
12890 , 0 , 
13150 , 0 , 
13410 , 0 , 
13680 , 0 , 
13970 , 0 , 
14180 , 0 , 
14400 , 0.0005 , 
14620 , 0.0027999999 , 
14780 , 0.0081000002 , 
14860 , 0.0286999997 , 
14930 , 0.0870999992 , 
15040 , 0.2013999969 , 
15150 , 0.4381999969 , 
15280 , 0.6863999963 , 
15390 , 0.8180999756 , 
15460 , 0.8820999861 , 
15510 , 0.9118000269 , 
15560 , 0.9269000292 , 
15650 , 0.9293000102 , 
15720 , 0.872699976 , 
15770 , 0.8565999866 , 
15830 , 0.8826000094 , 
15920 , 0.9180999994 , 
15970 , 0.926699996 , 
16020 , 0.9075999856 , 
16130 , 0.925999999 , 
16190 , 0.9204999804 , 
16280 , 0.9241999984 , 
16330 , 0.9235000014 , 
16420 , 0.9417999983 , 
16480 , 0.9491000175 , 
16570 , 0.980700016 , 
16590 , 0.9937000275 , 
16710 , 1 , 
16840 , 0.956099987 , 
17010 , 0.9240999818 , 
17150 , 0.9821000099 , 
17270 , 0.991599977 , 
17390 , 0.9886999726 , 
17460 , 0.9792000055 , 
17510 , 0.9682000279 , 
17530 , 0.9369999766 , 
17560 , 0.9190000296 , 
17640 , 0.8422999978 , 
17750 , 0.6671000123 , 
17850 , 0.2694000006 , 
17900 , 0.4515999854 , 
17960 , 0.1730999947 , 
18030 , 0.1076999977 , 
18100 , 0.0706999972 , 
18130 , 0.0051000002 , 
18180 , 0.0199999996 , 
18280 , 0.0004 , 
18350 , 0 , 
18500 , 0.0001 , 
18710 , 0 , 
18930 , 0 , 
19140 , 0 , 

@fxpineau
Copy link
Member

(Depending on your use case, the best option may be to implement the VOTableVisitor trait, especially methods visit_field_start and visit_tabledata)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants