Commit 7648f3f0 authored by Carl Schaffer's avatar Carl Schaffer
Browse files

adding comments and cleaning code

parent 3922fc05
pro split_ifu,filebase
; Split IFU calibrated data into individual maps
; Arguments:
; filebase: string, basic path of observation
; e.g. /instruments/gris/20180923/level1/23sep18.001
; to process all data from run 001 from September 23rd 2018
;
; Files are split into single map cubes of [x,y,lambda,polarization].
; The files are named as gris-ifu_20180923_090807_l1s_029_030.fits
; The last two fields in the filename signify run number and map number respectively.
; Retrieve versioning information from repository
gitrev = get_git_revision()
; Verify inputs, find matching files fro filebase
folder = file_dirname(file_dirname(filebase))
files = file_search(filebase+"*cc",count=file_count)
......@@ -12,19 +23,27 @@ pro split_ifu,filebase
return
endif
; Process headers of *cc files. We iterate over the header cards and track the most recent value in a dictionary by keyword.
; The contents of this dictionary are copied to a map wise header every time a new map starts(ISERIE keyword is encountered)
; These intermediate headers are stored in the series_headers list
header_dict = Dictionary()
primed = 0
series_headers = List()
primed = 0 ; We want to skip the first ISERIE record, this flag ensures that
foreach file_i, files do begin
print,"Processing "+file_i
fits_read,file_i,data,header,/header_only
foreach field, header do begin
; extract keyword
key = repstr(strtrim(strmid(field,0,8)),"-","_")
; Trigger storage of intermediate header
if key eq "ISERIE" then begin
; print, "New map!"
if primed then begin
; Header storage block
header_i = strarr(n_elements(header_dict))
i=0
foreach field_i, header_dict do begin
......@@ -38,14 +57,15 @@ pro split_ifu,filebase
endif
; Skip invalid "END" and "" keywords
a = where(key eq ["", "END"],invalid_keyword)
if not(invalid_keyword) then begin
header_dict[key]=field
endif
endforeach
endforeach
endforeach; keywords
endforeach; files
; Add header to list of header blocks
; Header storage block, triggered after last file
header_i = strarr(n_elements(header_dict))
i=0
foreach field, header_dict do begin
......@@ -54,6 +74,7 @@ pro split_ifu,filebase
endforeach
series_headers.add,header_i[sort(header_i)]
; reshape all data to [lambda, x, y, pol, maps]
; (add degenerate dimensions of size 1 for maps and
; pol states if only one exists)
......@@ -67,39 +88,47 @@ pro split_ifu,filebase
im= reform(im,im_size[1],im_size[2],im_size[3],im_size[4],1)
endif
im_size = size(im)
im = transpose(im, [1,2,0,3,4])
; Write split files.
n_maps = min([im_size[5],n_elements(series_headers)])
; Note: In theory the numbers of maps in the files and in the headers should be identical.
; I've found readifu to skip the last map in some files which is why I'm taking the minimum
; number of maps here
; Iterate over maps and headers
for i_map=0,n_maps-1 do begin
head_i = series_headers[i_map]
run_string = strmid(file_basename(file_i),8,3)
smap=string(sxpar(head_i,"ISERIE"),format='(I3.3)')
date_string = strtrim(repstr(sxpar(head_i,"DATE-OBS"),'-',''))
; Generate filename
run_string = strmid(file_basename(file_i),8,3); run number
map_string = string(sxpar(head_i,"ISERIE"),format='(I3.3)'); map number
date_string = strtrim(repstr(sxpar(head_i,"DATE-OBS"),'-','')); date
time_string = repstr(strmid(sxpar(head_i,"UTIME"),0,8),':',''); starting time of map
mode_string = "l1s"
if sxpar(head_i,"STATES") ne 4 then mode_string = "l1p"
fn_fields = ["gris-ifu",date_string,time_string,mode_string,run_string,smap]
mode_string = "l1s" ; describes data (l1s: level 1 spectroscopic
if sxpar(head_i,"STATES") eq 4 then mode_string = "l1p"; if multiple states are present switch to 'level1 polarimetric'
; order fields and join with underscores
fn_fields = ["gris-ifu",date_string,time_string,mode_string,run_string,map_string]
filename = strjoin(fn_fields,"_")+".fits"
; Ensure target folder is present
; do this here to ensure it is only created as files are written
outdir=file_search(folder+'/level1_split',count=cntdir)
if(cntdir eq 0) then spawn,'mkdir '+folder+'/level1_split'
outdir=file_search(folder+'/level1_split',count=cntdir)
outpath = outdir+'/'+filename
print,"Writing: "+outpath
; Add provenance information to header
sxaddpar,head_i,'HISTORY','split_ifu run on: '+string(systime(0))
sxaddpar,head_i,'HISTORY','split_ifu git revision: '+gitrev[0]
sxaddpar,head_i,'HISTORY','split_ifu git repo: '+gitrev[2]
sxaddpar,head_i,'HISTORY','split_ifu git commit date: '+gitrev[1]
outpath = outdir+'/'+filename
print,outpath
fits_write, outpath, im[*,*,*,*,i_map], head_i
endfor
; CD,'/dat/sdc/gris_ifu/20190607'
; ; preview_frames,'23sep18.005'
; im = lee('level1/07jun19.000-02cc')
; help,im
; Write split file
print,"Writing: "+outpath
fits_write, outpath, im[*,*,*,*,i_map], head_i
endfor; maps
end
\ No newline at end of file
end; split ifu
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment