Skip to content

Commit

Permalink
Add metadata from filename. (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
schlafly authored Feb 20, 2024
1 parent 725b7cb commit c39e136
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
2 changes: 1 addition & 1 deletion romanisim/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def simulate(metadata, objlist,
image_mod.meta, objlist, rng=rng, usecrds=usecrds, darkrate=darkrate,
webbpsf=webbpsf, flat=flat)
if level == 0:
im = dict(data=counts.array)
im = dict(data=counts.array, meta=dict(image_mod.meta.items()))
else:
l1, l1dq = romanisim.l1.make_l1(
counts, ma_table_number, read_noise=read_noise, rng=rng, gain=gain,
Expand Down
71 changes: 66 additions & 5 deletions romanisim/ris_make_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
"""

from copy import deepcopy
import os
import re
import asdf
from astropy import table
from astropy import time
from astropy import coordinates
import galsim
from galsim import roman
import roman_datamodels
from roman_datamodels import stnode
from romanisim import catalog, image, wcs
from romanisim import parameters

Expand Down Expand Up @@ -38,7 +41,8 @@ def merge_nested_dicts(dict1, dict2):
dict1[key] = value


def set_metadata(meta=None, date=None, bandpass='F087', sca=7, ma_table_number=1, truncate=None):
def set_metadata(meta=None, date=None, bandpass='F087', sca=7,
ma_table_number=1, truncate=None):
"""
Set / Update metadata parameters
Expand Down Expand Up @@ -148,6 +152,61 @@ def create_catalog(metadata=None, catalog_name=None, bandpasses=['F087'],
return cat


def parse_filename(filename):
"""
Try program / pass / visit / ... information out of the filename.
Parameters
----------
filename : str
filename to parse
Returns
-------
dictionary of metadata, or None if filename is non-standard
"""

# format is:
# r + PPPPPCCAAASSSOOOVVV_ggsaa_eeee_DET_suffix.asdf
# PPPPP = program
# CC = execution plan number
# AAA = pass number
# SSS = segment number
# OOO = observation number
# VVV = visit number
# gg = group identifier
# s = sequence identifier
# aa = activity identifier
# eeee = exposure number
# rPPPPPCCAAASSSOOOVVV_ggsaa_eeee
# 0123456789012345678901234567890
if len(filename) < 31:
return None

regex = (r'r(\d{5})(\d{2})(\d{3})(\d{3})(\d{3})(\d{3})'
'_(\d{2})(\d{1})([a-zA-Z0-9]{2})_(\d{4})')
pattern = re.compile(regex)
filename = filename[:31]
match = pattern.match(filename)
if match is None:
return None
out = dict(obs_id=filename.replace('_', '')[1:],
visit_id=filename[1:20],
program=match.group(1), # this one is a string
execution_plan=int(match.group(2)),
# pass = int(match.group(3))
segment=int(match.group(4)),
observation=int(match.group(5)),
visit=int(match.group(6)),
visit_file_group=int(match.group(7)),
visit_file_sequence=int(match.group(8)),
visit_file_activity=match.group(9), # this one is a string
exposure=int(match.group(10)))
out['pass'] = int(match.group(3))
# not done above because pass is a reserved python keyword
return out


def simulate_image_file(args, metadata, cat, rng=None, persist=None):
"""
Simulate an image and write it to a file.
Expand All @@ -164,10 +223,6 @@ def simulate_image_file(args, metadata, cat, rng=None, persist=None):
Uniform distribution based off of a random seed
persist : romanisim.persistence.Persistence
Persistence object
Returns
-------
"""

# Simulate image
Expand All @@ -182,6 +237,12 @@ def simulate_image_file(args, metadata, cat, rng=None, persist=None):
romanisimdict['filename'] = str(romanisimdict['filename'])
romanisimdict.update(**extras)

basename = os.path.basename(args.filename)
obsdata = parse_filename(basename)
if obsdata is not None:
im['meta']['observation'].update(**obsdata)
im['meta']['filename'] = stnode.Filename(basename)

# Write file
af = asdf.AsdfFile()
af.tree = {'roman': im, 'romanisim': romanisimdict}
Expand Down
9 changes: 9 additions & 0 deletions romanisim/tests/test_ris_make_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ def test_simulate_image_file(tmp_path):
im = asdf.open(args.filename)
assert im['roman']['data'].shape == (100, 100)
# we made an image


def test_parse_filename():
assert ris_make_utils.parse_filename('blah') is None
obs = ris_make_utils.parse_filename(
'r9999901001001001001_01101_0001_uncal.asdf')
assert obs is not None
assert obs['program'] == '99999'
assert obs['pass'] == 1

0 comments on commit c39e136

Please sign in to comment.