pro sav_sda, outfil, index, data, qs, dir=dir, $ append=append, skipmap=skipmap, use_index_size=use_index_size, $ progverno=progverno, progname=progname, fhead_input=fhead_input, $ compress=compress, qdebug=qdebug, type_comp=type_comp ; ;+ ;NAME: ; sav_sda ;PURPOSE: ; To save an SXT index and data array to a file ;CALLING SEQUENCE: ; sav_sda, outfil, index, data ; sav_sda, 'sfm_15nov_flare', index, data, /append ;INPUT: ; outfil - The name of the file to create or append the ; the data. NOTE: IDL "findfile" has a problem ; with filename that use the ~ to specify the ; directory. This means that the /append option ; will not work with file name using ~. ; index - An array of index structures ; data - A data array. NX x NY x N where the number of ; elements in index must be "N". "N" can be 1. ;OPTIONAL INPUT: ; qs - The quasi-static structure ;OPTIONAL KEYWORD INPUT: ; dir - The output directory (option not to have to ; specify it in the filename) ; append - If set, append the dataset to an existing file ; If the file does not exist, it will create a new ; file. ; skipmap - If set, do not generate the roadmap for the file. ; This would be used when writing out a large number ; of datasets (typically PFI) and using the /APPEND ; option. The final call writing out the datasets ; will have to not have this switch set in order to ; have the roadmap created and the file be readable. ; use_index_size - If set, look at the size of the image as ; specified in the index and write out only that portion. ; This is generally needed when saving FFI images and ; there are 4x4 images embedded in a 512x512xN data ; array. The default is to save it how it is passed in ; since it might have been processed and it's size ; changed on purpose. ; progname - The name of the program that is calling SAV_SDA ; progverno - The version number of the program that is calling SAV_SDA ; fhead_input - The file header of the input files to the program ; that is calling SAV_SDA so that the REFORMAT version ; number is saved in the output file header ; compress - If set, then compress the data portion of the data. ; If it is a scalar, then it is used for all images. ; If it is an array, then it applies to each of the images. ; ; type_comp - Determines compression type used. 1 = Unix Compress, ; 2 = gzip, Default = 1. ;HISTORY: ; Written Sep-91 by M.Morrison ; 11-Nov-91 MDM - Changed to allow to save only index if wanted. ; Need to pass data as undefined or a one element ; scalar. ; Also added the option to skip generating the map ; so that the writes are faster. The last time it is ; called, the map should be written. - BECAREFUL ; 12-Nov-91 MDM - Added "use_index_size" option so that if a matrix ; is passed and it had images that were padded because ; of a large image, the size of the image saved is the ; "original" image size. The default is to save it as ; passed since it might have had it's size changed ; in another routine (change 4x4 to 2x2) ; 5-May-92 MDM - Added document header ;V 1.00 24-Aug-92 MDM - Added "progverno", "progname" and "fhead_input" ; keyword inputs ;V 1.01 24-Aug-92 MDM - Added /shape_sav to WRTSXTMAP call -- ; copy the shape_sav values to the roadmap ; "shape_cmd" field (instead of the shape_cmd values) ; This is needed because the roadmap field "shape_cmd" ; is used to figure out the output array size. ;V 1.02 12-Oct-92 MDM - Modification to allow non-standard image shapes ; to be saved. Previous restriction was that nx*ny ; be a multiple of 16 (the file record size) ;V 1.03 6-Dec-93 MDM - Modification to WRTSXTMAP to use the old roadmap instead ; of re-generating the whole thing. ;V 1.04 7-Dec-93 MDM - Modified not to read the roadmap when /append and ; /skipmap is set. ; 26-Jan-94 AKT - Modified to add /block keyword to openu call. mk_sfd ; crashed because couldn't position within last 512 bytes. ; 9-Mar-94 MDM - Modified the header information ;V 1.10 7-Mar-95 MDM - Added /COMPRESS option ; - Added /QDEBUG ; 12-Dec 97 PGS - Added type_comp keyword, wrote compression type to ; bits 6 & 7 of index0.gen.data_word_type. ; bit6=1 => Unix compress = default, bit7=1 => gzip compress. ; 25-Jan-98 SLF - define COMPRESS if not already done ; 5-May-99 SLF - add /NOLOWER to open_da_file call ; (permit upper case file/path names) ; 2-Jul-10 LWA - Commented out if (i eq 0) then print, 'Setting compressed data flag' ;- progName1 = 'SAV_SDA.PRO' progVerNo1 = 1.10*1000 progName2 = progName1 progVerNo2 = progVerNo1 ; if (keyword_set(progverno)) then progverno1 = progverno if (keyword_set(progname)) then progname1 = progname ; ref_struct, ref rsiz = ref.SXT_Fil_Rec_Siz ; break_file, outfil, dsk_log, dir0, filnam, ext outfil0 = filnam + ext if (not keyword_set(dir)) then dir = dir0 fileid = strmid(filnam, 3, 6) + ext prefix = strupcase(strmid(filnam, 0, 3)) ; get_lun, lun ; ff = findfile(dir+outfil0) qexist = 0 if (ff(0) ne '') then qexist = 1 if (qexist and keyword_set(append)) then begin rd_pointer, dir+outfil0, fpoint rd_fheader, dir+outfil0, fhead if (not keyword_set(skipmap)) then rd_roadmap, dir+outfil0, roadmap_in bytout = fpoint.map_section ;start writting at end of datasection (overwrite roadmap - regenerate) openu, lun, dir+outfil0, /block ; /block added by AKT 1/26/94 end else begin open_da_file, lun, dir, '', outfil0, rsiz, bytout, fpoint, qs, /nolower gen_struct, file_header=fhead ;define "fhead" because if don't call "wrtSXTMap" ;then it is undefined for "wrt1p_fh" if (keyword_set(fhead_input)) then begin fhead.refverno = fhead_input.refverno fhead.ref2verno = fhead_input.ref2verno end end ; n = n_elements(index) ncomp=n_elements(compress) if ncomp eq 0 then compress=0 ; default = no compress if n ne ncomp then compress=replicate(compress(0),n) ; scalar or n<>ncomp if n_elements(type_comp) eq 0 then type_comp=0 for i=0,n-1 do begin index0 = index(i) if (n_elements(data) gt 1) then begin ;have true data to save data0 = data(*,*,i) ; if (keyword_set(use_index_size)) then begin ien = index0.sxt.shape_sav(0) -1 jen = index0.sxt.shape_sav(1) -1 data0 = data0(0:ien, 0:jen) end ; siz = size(data0) nx = siz(1) ny = siz(2) typ = siz( siz(0)+1 ) ; qcomp = (compress(i)); tcomp = BYTE(type_comp*compress(i)) if (qcomp) then if (i le n_elements(compress)-1) then qcomp = compress(i) if (qcomp) then begin data2 = data_compress(data0, qdebug=qdebug, type_comp=type_comp) if (get_nbytes(data2) lt get_nbytes(data0)) then begin data0 = temporary(data2) end else begin qcomp = 0 ;don't compress if less efficient print, 'SAV_SDA: Compression set, but no savings so not done' end end ; ndatabyte = get_nbytes(data0) if ((ndatabyte mod rsiz) ne 0) then ndatabyte = ndatabyte + (rsiz - (ndatabyte mod rsiz)) ;added 12-Oct-92 end else begin nx = 0 ny = 0 typ = 0 ndatabyte = 0 end ; index0.sxt.shape_sav(0) = nx index0.sxt.shape_sav(1) = ny index0.gen.nIndexByte = get_nbytes(index0) index0.gen.nDataByte = ndatabyte index0.gen.data_word_type = typ ;TODO - what about compression flag? if (typ eq 1) then begin index0.gen.data_word_type = typ + 16 ;flag that it is compressed data (we think) ;if (i eq 0) then print, 'Setting compressed data flag' ;LWA 2-Jul-2010 end ; index0.gen.data_word_type = index0.gen.data_word_type + tcomp*32 + qcomp*128;PGS write setqandt = BYTE( tcomp*32 + qcomp*128);pgs index0.gen.data_word_type = (index0.gen.data_word_type OR setqandt);pgs if ((mask(index0.sxt.pfi_ffi,0,2) eq 0) and (ny gt 64)) then begin if (i eq 0) then print, 'Assembed OR being saved - setting "pfi_ffi" flag' index0.sxt.pfi_ffi = index0.sxt.pfi_ffi + 2 end rdwrt, 'W', lun, bytout, rsiz, Index0, 1 ;if (ndatabyte gt 0) then rdwrt, 'W', lun, bytout, rsiz, data0, 1 if (ndatabyte gt 0) then rdwrt, 'W', lun, bytout, rsiz, data0, 1, /quiet end ; file_type = 'SPR' if (btest0(index0.sxt.pfi_ffi,0)) then file_type = 'SFR' ; fpoint.map_section = bytout if (not keyword_set(skipmap)) then begin wrtSXTMap, lun, fpoint, bytout, fhead, rsiz, /shape_sav, roadmap_in=roadmap_in end wrt1p_fh, lun, fpoint, fhead, rsiz, progName1, progVerNo1, fileid, file_type, 'SXT', progname2=progname2, progverno2=progverno2 ; free_lun, lun end