Commit 185b0df3 authored by Carl Schaffer's avatar Carl Schaffer
Browse files

Merge branch 'ifu_splitting' into 'master'

Ifu splitting

See merge request !7
parents fdaf5d8a b8160b5b
Pipeline #2771 passed with stage
......@@ -110,7 +110,7 @@ pro fits_write,file_or_fcb,data,header_in,extname=extname,extver=extver, $
; Removed obsolete !ERR system variable W. Landsman Feb 2004
; Check that byte array supplied with table extension W. Landsman Mar 2004
; Make number of bytes 64bit to avoid possible overflow W.L Apr 2006
; Asuume FITS_OPEN has opened the file with /SWAP_IF_LITTLE_ENDIAN
; Assume FITS_OPEN has opened the file with /SWAP_IF_LITTLE_ENDIAN
; W. Landsman September 2006
; Removes BZERO and BSCALE for floating point output, D. Lindler, Sep 2008
;-
......@@ -131,7 +131,7 @@ pro fits_write,file_or_fcb,data,header_in,extname=extname,extver=extver, $
message = ''
s = size(file_or_fcb) & fcbtype = s[s[0]+1]
fcbsize = n_elements(file_or_fcb)
if (fcbsize ne 1) or ((fcbtype ne 7) and (fcbtype ne 8)) then begin
if (fcbsize ne 1) || ((fcbtype ne 7) && (fcbtype ne 8)) then begin
message = 'Invalid Filename or FCB supplied'
goto,error_exit
end
......@@ -186,7 +186,7 @@ pro fits_write,file_or_fcb,data,header_in,extname=extname,extver=extver, $
if naxis gt 0 then axis = s[1:naxis]
idltype = s[naxis+1]
if (idltype gt 5) and (idltype NE 12) and (idltype NE 13) then begin
if (idltype gt 5) && (idltype NE 12) && (idltype NE 13) then begin
message='Data array is an invalid type'
goto,error_exit
endif
......@@ -233,7 +233,7 @@ pro fits_write,file_or_fcb,data,header_in,extname=extname,extver=extver, $
hpos2 = where(keywords eq 'BEGIN EX') & hpos2 = hpos2[0] ;begin ext.
hpos3 = where(keywords eq 'END ') & hpos3 = hpos3[0] ;end of header
if (hpos1 gt 0) and (hpos2 lt hpos1) then begin
if (hpos1 gt 0) && (hpos2 lt hpos1) then begin
message,'Invalid header BEGIN EXTENSION HEADER ... out of place'
goto,error_exit
endif
......@@ -248,12 +248,12 @@ pro fits_write,file_or_fcb,data,header_in,extname=extname,extver=extver, $
; was written
;
if (fcb.nextend eq -1) then begin ;no pdu written yet?
if (hpos2 gt 0) or (Axtension ne '') or (Aextname ne '') or $
(Aextver ne 0) or (Aextlevel ne 0) then begin
if (hpos2 gt 0) || (Axtension ne '') || (Aextname ne '') || $
(Aextver ne 0) || (Aextlevel ne 0) then begin
;
; write null image PDU
;
if (hpos1 gt 0) and (hpos2 gt (hpos1+1)) then $
if (hpos1 gt 0) && (hpos2 gt (hpos1+1)) then $
hmain = [header[hpos1+1:hpos2-1],'END ']
fits_write,fcb,0,hmain,/no_abort,message=message
if message NE '' then goto,error_exit
......@@ -284,7 +284,7 @@ pro fits_write,file_or_fcb,data,header_in,extname=extname,extver=extver, $
end else begin ;PCOUNT, GCOUNT are mandatory for extensions
sxaddpar,h,'PCOUNT',0
sxaddpar,h,'GCOUNT',1
if (Axtension eq 'BINTABLE') or $
if (Axtension eq 'BINTABLE') || $
(Axtension eq 'TABLE ') then begin
tfields = sxpar(header,'TFIELDS') > 0
sxaddpar,h,'TFIELDS',tfields
......@@ -298,7 +298,7 @@ pro fits_write,file_or_fcb,data,header_in,extname=extname,extver=extver, $
if idltype EQ 13 then $
sxaddpar,header,'BZERO',2147483648,'Data is unsigned long'
if idltype GE 12 then sxdelpar,header,'BSCALE'
if (idltype EQ 4) or (idltype EQ 5) then $
if (idltype EQ 4) || (idltype EQ 5) then $
sxdelpar,header,['BSCALE','BZERO']
;
; delete special keywords from user supplied header
......@@ -340,7 +340,7 @@ write_header:
;
; convert to IEEE
;
unsigned = (idltype EQ 12) or (idltype EQ 13)
unsigned = (idltype EQ 12) || (idltype EQ 13)
if idltype EQ 12 then newdata = fix(data - 32768)
if idltype EQ 13 then newdata = long(data - 2147483648)
;
......@@ -377,3 +377,4 @@ error_exit:
message,' ERROR: '+message,/CON
retall
end
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -103,8 +103,8 @@ pro gris_split,filebase
hdr=strjoin(hdr)
hdr=string(reform(byte(hdr),80,strlen(hdr)/80))
if STRTRIM(SXPAR(hdr,"IMGSYS")) eq "IFU" then begin
print, "Splitting not implemented for IFU data yet."
print, "Skipping "+filebase
print, "Splitting IFU data."
split_ifu,filebase
return
endif
......
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)
if file_count eq 0 then begin
print, "No IFU maps found for "+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()
series_headers = List()
primed = 0 ; We want to skip the first ISERIE record, this flag ensures that
history_cards = List()
first_file = 1
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)),"-","_")
if key eq "HISTORY" then begin
if first_file then history_cards.add,field
endif
; 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
header_i[i] = field_i
i=i+1
endforeach
series_headers.add,header_i[sort(header_i)]
endif
primed = 1
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; keywords
first_file=0
endforeach; files
; Header storage block, triggered after last file
header_i = strarr(n_elements(header_dict))
i=0
foreach field, header_dict do begin
header_i[i] = field
i=i+1
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)
im = readifu(filebase)
im_size = size(im)
if sxpar(series_headers[0],"STATES") ne 4 then begin
im = reform(im,im_size[1],im_size[2],im_size[3],1)
endif
im_size = size(im)
if im_size[0] eq 4 then begin
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]
; 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" ; 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"
sxaddpar,head_i,'FILENAME',filename, "Name of file"
; 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
; Add provenance information to header
sxdelpar,head_i,'HISTORY'
foreach hist_field, history_cards do begin
val = sxpar([hist_field], "HISTORY")
sxaddpar,head_i, "HISTORY", val
endforeach
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]
; Write split file
print,"Writing: "+outpath
fits_write, outpath, im[*,*,*,*,i_map], head_i
endfor; maps
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