This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| data:data_analysis_manual:read_catalog_python [2021/06/30 08:54] – moved eric buchlin | data:data_analysis_manual:read_catalog_python [2024/03/29 13:11] (current) – Page is deprecated eric buchlin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== How to read the UiO FITS files catalog in Python ====== | + | ====== How to read and use the UiO FITS files catalog in Python ====== |
| - | <file python | + | <note warning> |
| + | |||
| + | ===== CSV catalog (new, recommended) ===== | ||
| + | |||
| + | <file python | ||
| from pathlib import Path | from pathlib import Path | ||
| import pandas as pd | import pandas as pd | ||
| Line 7: | Line 11: | ||
| # SPICE data tree path, to be changed to your SPICE data mirror | # SPICE data tree path, to be changed to your SPICE data mirror | ||
| data_path = "/ | data_path = "/ | ||
| + | |||
| + | |||
| + | def date_parser(string): | ||
| + | try: | ||
| + | return pd.Timestamp(string) | ||
| + | except ValueError: | ||
| + | | ||
| + | |||
| def read_uio_cat(): | def read_uio_cat(): | ||
| """ | """ | ||
| - | Read UiO text table SPICE FITS files catalog | + | Read UiO SPICE FITS files CSV catalog |
| - | http:// | + | http:// |
| Return | Return | ||
| Line 17: | Line 29: | ||
| pandas.DataFrame | pandas.DataFrame | ||
| Table | Table | ||
| + | """ | ||
| + | cat_file = Path(data_path) / " | ||
| + | if not cat_file.exists(): | ||
| + | print(f' | ||
| + | sys.exit(1) | ||
| + | date_columns = [' | ||
| + | df = pd.read_csv(cat_file, | ||
| + | return df | ||
| + | </ | ||
| - | Example queries that can be done on the result: | + | The same applies for the catalog included in the data releases (here: release 2.0), which can simply |
| - | * `df[(df.LEVEL == " | + | <file python read_release_cat.py> |
| - | | + | import pandas as pd |
| + | |||
| + | def date_parser(string): | ||
| + | try: | ||
| + | return pd.Timestamp(string) | ||
| + | except ValueError: | ||
| + | | ||
| + | |||
| + | date_columns = ['DATE-BEG',' | ||
| + | cat = pd.read_csv( | ||
| + | ' | ||
| + | | ||
| + | parse_dates=date_columns | ||
| + | ) | ||
| + | # TODO interpret the JSON included in columns `proc_steps` and `windows`. | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Text catalog ===== | ||
| + | |||
| + | <file python read_uio_cat_txt.py> | ||
| + | from pathlib import Path | ||
| + | import pandas as pd | ||
| + | |||
| + | # SPICE data tree path, to be changed to your SPICE data mirror | ||
| + | data_path | ||
| + | |||
| + | |||
| + | def date_parser(string): | ||
| + | try: | ||
| + | return pd.Timestamp(string) | ||
| + | except ValueError: | ||
| + | | ||
| + | |||
| + | |||
| + | def read_uio_cat(): | ||
| + | | ||
| + | Read UiO text table SPICE FITS files catalog | ||
| + | http:// | ||
| + | |||
| + | Return | ||
| + | ------ | ||
| + | pandas.DataFrame | ||
| + | Table | ||
| """ | """ | ||
| cat_file = Path(data_path) / " | cat_file = Path(data_path) / " | ||
| + | if not cat_file.exists(): | ||
| + | print(f' | ||
| + | sys.exit(1) | ||
| columns = list(pd.read_csv(cat_file, | columns = list(pd.read_csv(cat_file, | ||
| date_columns = [' | date_columns = [' | ||
| - | df = pd.read_table(cat_file, | + | df = pd.read_table(cat_file, |
| - | parse_dates=date_columns, | + | parse_dates=date_columns, |
| - | | + | |
| - | df.STUDYTYP | + | |
| return df | return df | ||
| </ | </ | ||
| - | '' | ||
| + | ===== Using the catalog ===== | ||
| + | |||
| + | Then we can read the catalog and filter it: | ||
| + | |||
| + | <file python filter_cat.py> | ||
| + | cat = read_uio_cat() | ||
| + | filtered_cat = cat[(cat[' | ||
| + | </ | ||
| + | |||
| + | `cat` then contains the full catalogue (as a `pandas` dataframe) and `filtered_cat` contains a catalogue in which rows have been filtered (in this particular case) by observation date and file level. | ||
| + | |||
| + | The catalogue or filtered catalogue can be exported, e.g. to a CSV table by `cat.to_csv()`. | ||