function IMAGEPDS, filename,label, NOSCALE = noscale, SILENT = silent ;+ ; NAME: ; IMAGEPDS ; PURPOSE: ; Read a PDS image file into an IDL data variable. ; ; CALLING SEQUENCE: ; Result=IMAGEPDS (Filename,Label[,/NOSCALE,/SILENT] ) ; ; INPUTS: ; FILENAME = Scalar string containing the name of the PDS data file ; to be read. ; Label = String array containing the "header" from the PDS file. ; ; OUTPUTS: ; Result = PDS image data array constructed from designated record. ; ; OPTIONAL INPUT KEYWORDS: ; NOSCALE - If present and non-zero, then the ouput data will not be ; scaled using the optional SCALING_FACTOR and OFFSETkeywords ; in the PDS header. Default is to scale. ; ; SILENT - Normally, IMAGEPDS will display the size of the array at ; the terminal. The SILENT keyword will suppress this ; ; EXAMPLE: ; Read a PDS file TEST.PDS into an IDL image array, im. Do not scale ; the data with BSCALE and BZERO. ; ; IDL> im = IMAGEPDS( 'TEST.PDS', lbl, /NOSCALE) ; ; WARNING ; IMAGEPDS is intended to be used only on MSB architectures ('big- ; endian'), if the file being read was written on an MSB architecture; ; it has no conversion from MSB to other architectures, yet. ; ; PROCEDURES USED: ; Functions: PDSPAR, STR2NUM ; ; MODIFICATION HISTORY: ; Adapted by John D. Koch from READFITS by Wayne Landsman,December,1994 ;- On_error,2 ;Return to user ; Check for filename input if N_params() LT 2 then begin print,'Syntax - result = IMAGEPDS(filename,label[,/NOSCALE,/SILENT])' return, -1 endif silent = keyword_set( SILENT ) fname = filename ; Read object to determine type of data in file object = pdspar(label,'OBJECT',COUNT=objects,INDEX=obj_ind) if !ERR EQ -1 then message, $ 'ERROR - '+fname+' missing required OBJECT keyword' pointer = pdspar(label,'IMAGE') if !ERR EQ -1 then message, $ 'ERROR - No pointers to image data found in '+fname ; Read dimensions of data array Xvar = pdspar( label,'line_samples',COUNT=xcount,INDEX=x_ind) Yvar = pdspar( label,'lines',COUNT=ycount,INDEX=y_ind) if xcount(0) NE ycount(0) then message, $ 'ERROR - '+fname+': LINE_SAMPLES and LINES count discrepancy.' bitpix = pdspar( label, 'SAMPLE_BITS',COUNT=pixes,INDEX=pix_ind) if pixes(0) NE xcount(0) then message, $ 'ERROR - '+fname+': LINE_SAMPLES and SAMPLE_BITS count discrepancy.' images = xcount(0) if images GT 1 then begin data = CREATE_STRUCT('images',images) if not (SILENT) then message,'Return type will be a structure with '$ +strtrim(string(images+1),2)+' elements',/INFORM endif ; Read in the image data iter = 0 for i=0,objects(0)-1 do begin obj_now = obj_ind(i) if i LT objects(0)-1 then obj_nxt = obj_ind(i+1) else begin lblsz = size(label) obj_nxt = lblsz(1) endelse xp = where(x_ind GT obj_now AND x_ind LT obj_nxt(0)) yp = where(y_ind GT obj_now AND y_ind LT obj_nxt(0)) bp = where(pix_ind GT obj_now AND pix_ind LT obj_nxt(0)) if xp(0) GT -1 AND yp(0) GT -1 AND bp(0) GT -1 then begin X = long(Xvar(xp(0))) Y = long(Yvar(yp(0))) if not (SILENT) then begin ;Print size of array being read if X GT 0 then if Y GT 0 then begin text = string(X)+' by'+string(Y) message,'Now reading ' + text + ' array',/INFORM endif else message,fname+" has X or Y = 0, no data array read",/CON endif ; Read pointer to find location of the image data point = pointer(iter(0)) skip=0 temp = str2num(point,TYPE=t) if t GT 6 then begin l = strlen(point) p = strpos(point,'"') p2 = strpos(point,'"',p+1) if p LT 0 then begin p = strpos(point,"'") p2 = strpos(point,"'",p+1) endif c = strpos(point,',') if (c GT -1) then skip = str2num(strtrim(strmid(point,c+1,L-c-1),2))-1 if (p GT -1) then point=strmid(point,p+1,p2-p-1) d = strpos(fname,'/') tail = d dir = '' while d GT -1 do begin ; extract the path to the directory, tail = d + 1 d = strpos(fname,'/',tail) endwhile dir = strmid(fname,0,tail) fname = dir + strlowcase(point) ; add the new filename onto the path openr, unit, fname, ERROR = err, /GET_LUN, /BLOCK dir = strmid(fname,0,tail) fname = dir + strupcase(point) openr, unit, fname, ERROR = err, /GET_LUN, /BLOCK if err LT 0 then fname = dir + strlowcase(point) openr, unit, fname, ERROR = err, /GET_LUN, /BLOCK if err LT 0 then fname = dir + (point) openr, unit, fname, ERROR = err, /GET_LUN, /BLOCK if err LT 0 then message,'Error opening file ' + ' ' + fname endif else begin skip = temp -1 openr, unit, fname, ERROR = err, /GET_LUN, /BLOCK if err LT 0 then message,'Error opening file ' + ' ' + fname endelse status = fstat(unit) nbytesleft = status.size - skip bits = str2num(bitpix(bp(0))) bits = bits(0) ; Read image data CASE bits OF 8:begin IDL_type = 1 file = assoc(unit,bytarr(X,Y,/NOZERO),skip) end 16:begin IDL_type = 2 file = assoc(unit,intarr(X,Y,/NOZERO),skip) end 32:begin IDL_type = 3 file = assoc(unit,lonarr(X,Y,/NOZERO),skip) end -32:begin IDL_type = 4 file = assoc(unit,fltarr(X,Y,/NOZERO),skip) end -64:begin IDL_type = 5 file = assoc(unit,dblarr(X,Y,/NOZERO),skip) end else: message,'ERROR - Illegal value of BITPIX (= ' + $ strtrim(string(bitpix),2) + ') in '+fname ENDCASE element = file(0) free_lun, unit ; If necessary, Convert to host byte ordering filearch = pdspar(label,'sample_type') if !ERR EQ -1 then message, 'ERROR - '+fname+' missing required keyword' systarch = !VERSION.arch spot=strpos(filearch(0),'_') ; Search for and get parameter values fore=strmid(filearch(0),0,spot(0)) ; Convert value from file arch to host (MSB) arch CASE fore(0) OF 'UNSIGNED': 'MSB': 'VAX': element=conv_vax_unix(element) 'LSB': element=conv_vax_unix(element) 'IEEE': ieee_to_host,element '': ieee_to_host,element else: begin message,'No recognized file architecture found in '$ +fname+' , IEEE file architecture assumed.',/INF ieee_to_host,element end ENDCASE ; Scale data unless /NOSCALE is set if not keyword_set( NOSCALE ) then begin bscale = float(pdspar(label, 'scaling_factor',INDEX=scl_ind)) bzero = float(pdspar(label, 'offset',INDEX=zer_ind)) ; Find which images have scaling and offset factors defined sp = where(scl_ind GT obj_now AND scl_ind LT obj_nxt(0)) zp = where(zer_ind GT obj_now AND zer_ind LT obj_nxt(0)) if sp(0) GT -1 AND zp(0) GT -1 then begin scl = bscale(sp(0)) zero = bzero(zp(0)) if ( scl NE 1. ) then element = temporary(element)*scl if (zero NE 0) then element = temporary(element)+ zero endif endif ; Add element to the data structure or rename as 'data' if only 1 image if images GT 1 then begin image = 'image'+strtrim(string(i),2) data = CREATE_STRUCT(data,image,element) endif else data = element iter = iter + 1 endif endfor if iter(0) NE images(0) then message,$ 'ERROR - '+fname+': Number of images expected does not equal number found.' if images GT 1 then if not (SILENT) then help, /STRUCTURE, data ; Return array return, data end