Wednesday, May 21, 2008: practice # 1


Now, briefly, let's review some images displaying commands:

; open a 700x600 graphics window
  WINDOW, XSIZE=700, YSIZE=600

; load color table 5
  loadct, 5

; a 2-d Gaussian
  z = SHIFT(DIST(40), 20, 20) & z=EXP(-(z/10)^2)

; 2-d image, byte and plotting area scaled
  TVSCL, CONGRID(z, !D.X_Size, !D.Y_Size)

; wire mesh
  SURFACE, z  ; wire mesh 

; contour, 10 level, linear, in a square area
  sqplset, /SET
  mai = MAX(z, MIN=mii)
  CONTOUR, z, LEVELS=FINDGEN(10)*(mai-mii)/9. + mii

; colored levels, log levels
  levs = mii*10^((INDGEN(10)+1)*ALOG10(mai/mii)/9.)
  c = loadct19()
  CONTOUR, z, LEVELS=levs, C_COLORS=c

; ... filled
  CONTOUR, z, LEVELS=levs, C_COLORS=c, /FILL

; shade surface (def. rotated 30 deg X, 30 deg Z)
  SHADE_SURF, z

; shade surface 60 deg X rotated 
  SHADE_SURF, z, AX=60

; shade surface 60 deg Z rotated 
  SHADE_SURF, z, AZ=60
   
; mixed
  SHOW3, z

; mixed defining contour levels
  SHOW3, z, E_CONTOUR={LEVELS: FINDGEN(10)*(mai-mii)/9. + mii}


Writing / reading ASCII files:

Useful tips:

; A shifted Gaussian
  z = SHIFT(DIST(40), 0, 20) & z = EXP(-(z/10)^2)
  SURFACE, z

; Only the left part
  SURFACE, z[0:19,*]

; save into a new array
  z1 = z[0:19,*]

; plot the right part (in green)
  c = loadct19()
  SURFACE, z[20:*,*], COLOR=c[3]

; open a file (unit 1) for formatted writing
  OPENW, 1, 'hgauss1.out'

; print formatted data
  PRINTF, 1, z1

; close the file
  CLOSE, 1

; use the ASCII file viewer routine XDISPLAYFILE
  XDISPLAYFILE, 'hgauss1.out'

; note that the first row is z1[*,0]
  PRINT, z1[*,0]

; let's read the file into a float array
  zf = FLTARR(20,40)

; this time the unit ID is managed automatically
  OPENR, lun, 'hgauss1.out', /GET_LUN
  
; read from the unit "lun"
  READF, lun, zf

; free the unit ID
  FREE_LUN, lun

; overplot in red omitting the X axis
  SURFACE, zf, COLOR=c[2], XSTY=4, /NOERA


Writing / reading a GIF image:
; dump curent plot window into a GIF file
  WRITE_GIF, 'idl_dump.gif', TVRD(0,0, !D.X_VSIZE, !D.Y_VSIZE)

; check it's there
  $ ls -l idl_dump.gif

; view it. It should be B/W if you have a TRUE color display (see below)
  $ display idl_dump.gif &

; let's load and display a GIF file
  READ_GIF, 'ross_logo1.gif', ima, r,g,b
  HELP, ima, r,g,b

; load from the R G B variables the display color translation tables
  TVLCT, r,g,b

; now can display the image
  TV, ima

; to fit the image into the display area
  TV, CONGRID(ima, !D.X_Size, !D.Y_Size)

; check the more general routines QUERY_DICOM e READ_DICOM:
  DOC_LIBRARY, 'query_dicom'
  DOC_LIBRARY, 'read_dicom' 

; let's modify a bit the image and save it.
; If less than 256 colors, then it's better to load a color table...
  LOADCT, 0

; then replace colors with those of the image
  TVLCT, r,g,b

; at this point I need to manage a special COMMON block to make the changes
; effective
  COMMON colors,r_orig,g_orig,b_orig,r_curr,g_curr,b_curr 
  r_orig = r & r_curr = r 
  g_orig = g & g_curr = g 
  b_orig = b & b_curr = b 

; now can edit the colors with "xpalette"
  xpalette

; I suggest to modify the background (white).

; make the desired changes and redisplay
  TV, ima

; if we have a TRUE color display, can read the 3 channels separately
;  r =  TVRD(0, 0, 661, 317, 1)
;  g =  TVRD(0, 0, 661, 317, 2)
;  b =  TVRD(0, 0, 661, 317, 3)

; let's save into a file the selected screen pixels
  WRITE_GIF, 'idl_dump2.gif', ima, r_orig,g_orig,b_orig

; have a look to the result
  $ display idl_dump2.gif &

Can also have a look to:
  READ_TIFF / WRITE_TIFF
and the intrinsic commands:
  READ_JPEG, WRITE_JPEG, READ_PNG, WRITE_PNG

Read / write FITS files:

There are several procedures to accomplish this aim. Let's use fits_read:

  fits_read, '../Data/crab_dss2_r_6x6.fits', dat6, hdr6
  HELP, dat6, hdr6

; Let's show the image in a new window
  WINDOW, 1, XS=500, YS=400
  TV, CONGRID(BYTSCL(dat6), !D.X_Size, !D.Y_Size) 

; can print the header
  print, hdr6

; or
  xdispfile, TEXT=hdr6

; can also use my personal routine "fitsreadx" to get a structure
  err = fitsreadx('../Data/crab_dss2_r_4x4.fits',d) 
  HELP, d, /STRU

; let's write part of the image into a new FITS file:
  d6 = dat6[0:99,0:99]
  MWRFITS, d6, 'outdata6.fits'

; let's check
  $ ls -l outdata6.fits

; the output header is minimal. Let's re-write it
  fits_read, 'outdata6.fits', d, h 
  print, h

; the CREATE keyword is used to overwrite the file!
  MWRFITS, d6, 'outdata6.fits', hdr6, /CREA

  fits_read, 'outdata6.fits', d, h 
  HELP, h

  PRINT, h[0:7]

  ERASE
  TV, BYTSCL(d)

; as an alternative, to write the file could use "fits_write"
  fits_write, 'outdata6.fits', d6, hdr6

; Re-read
  fits_read, 'outdata6.fits', dd, hh
  PRINT, hh[0:7]

; or even "writefits" (have a look to the help with doc_library)

Now let's have a look and run the programs in the Demos directory...

...

Go to the course Main page L. Nicastro, INAF-IASF, 2009-01-22