Twas the Lecture Before Holiday Break Readings Present

Download Report

Transcript Twas the Lecture Before Holiday Break Readings Present

Twas the Lecture
Before Holiday Break
Readings
Present
Makefiles
Image Array Manipulation and I/O
1 Programs
Simple Widget
Rolando V. Raqueño
Wednesday, May 25, 2016
Holiday Reading
• The Cuckoo’s Egg (Gift Quiz Material)
References for doing homework and final
project
• Gumley Chapters
– 2, 3.1-3.2,4.1-4.4 (programming)
– 7.1-7.5 (displaying images)
– 9.1-9.3 (GUI programming)
• UNIX Power Tools Chapters
– 48-51
Rolando V. Raqueño
2
Wednesday, May 25, 2016
netpbm.tgz
netpbm.tgz
This is the package that contains the
entire PBMPLUS (also known as
netpbm) and is distributed in a format
known as
Compressed TAR File
or
gzipped TAR file
Rolando V. Raqueño
3
Wednesday, May 25, 2016
present.tgz
• To illustrate how to process these types of
files, there is a file called simpler file
(present.tgz) which can be accessed via
anonymous ftp from
% ftp ftp.cis.rit.edu
Rolando V. Raqueño
4
Wednesday, May 25, 2016
Anonymous ftp session login
% ftp ftp.cis.rit.edu
Name (ftp.cis.rit.edu:rvrpci): anonymous
331 Guest login ok, send your complete email address as password.
Password:
Rolando V. Raqueño
5
Wednesday, May 25, 2016
Most important ftp command
ftp> ?
Rolando V. Raqueño
6
Wednesday, May 25, 2016
ftp session change directory
ftp> cd people/rolo/simg726
250 "/people/rolo/simg726" is new cwd.
ftp> ls
200 PORT command successful.
150 Opening ASCII mode data connection for
/bin/ls.
Makefile_example.tgz
fortran_stats.tgz
present.tgz
226 Listing completed.
54 bytes received in 0.015 seconds (3.50
Kbytes/s)
7
Rolando V. Raqueño
Wednesday, May 25, 2016
ftp file transfer type
ftp> ascii
200 Type okay.
ftp> binary
200 Type okay.
Rolando V. Raqueño
8
Wednesday, May 25, 2016
ftp getting a single file
ftp> get present.tgz
200 PORT command successful.
150 Opening ASCII mode data connection
for present.tgz (1211 bytes).
226 Transfer completed.
local: present.tgz remote: present.tgz
1219 bytes received in 0.015 seconds
(80.45 Kbytes/s)
Rolando V. Raqueño
9
Wednesday, May 25, 2016
Getting multiple files
ftp> prompt
Interactive mode off.
ftp> hash
Hash mark printing on (8192 bytes/hash
mark).
ftp> mget *.tgz
200 PORT command successful.
150 Opening ASCII mode data connection
for Makefile_example.tgz (31544 bytes).
##
Rolando V. Raqueño
10
Wednesday, May 25, 2016
Getting out of ftp
ftp> quit
221 Goodbye.
Rolando V. Raqueño
11
Wednesday, May 25, 2016
Most common ftp mistake
• Sending a binary file under ASCII mode
• Result
– Characters not conforming to the ASCII
character set are removed
• ALWAYS
– Check the size of the source file and the
transferred file
Rolando V. Raqueño
12
Wednesday, May 25, 2016
Uncompressing and UnTAR
• To uncompress the file
% gunzip present.tgz
• To extract the contents of the tar file
% tar xvf present.tar
• Challenge - look at the file xmas.c and
figure out what it does
% cd present
% more xmas.c
Rolando V. Raqueño
13
Wednesday, May 25, 2016
Compiling the Package
• To compile or build the package, a Makefile
is typically present which is an automatic
mechanism to compile all the files and place
them in the proper locations
% make
• To run the program
% xmas
Rolando V. Raqueño
14
Wednesday, May 25, 2016
What is a Makefile
• A makefile is a description of how to
build “things” in UNIX
• Automates the creation of “stuff”
• It is different from a script or batch file
– Describes the relationship of components
to the resulting object
– Keeps track of intermediate results and
their age
Rolando V. Raqueño
15
Wednesday, May 25, 2016
Other Makefile examples
• Makefile_example.tgz
• Fortran_stats.tgz
Rolando V. Raqueño
16
Wednesday, May 25, 2016
Makefile_example
• Take a raw file -> convert to pgm image
• Take pgm image -> rotate image
• Take rotated image -> label image
• Makefiles work on file dependencies
Rolando V. Raqueño
17
Wednesday, May 25, 2016
[Mm]akefile
% more Makefile
MyCat_labeled.pgm : MyCat_rotated.pgm label.dat
ppmlabel -x 0 -y 200 -file label.dat
MyCat_rotated.pgm > MyCat_labeled.pgm
MyCat_rotated.pgm : MyCat.pgm
pnmrotate 45 MyCat.pgm > MyCat_rotated.pgm
MyCat.pgm : MyCat.raw
rawtopgm 256 256 MyCat.raw > MyCat.pgm
clean:
rm MyCat_labeled.pgm MyCat_rotated.pgm MyCat.pgm
Rolando V. Raqueño
18
Wednesday, May 25, 2016
If a file is out of date…
• If you edit a file and update it, the
makefile will know
• Example
– Edit label.dat to some other new text
– Type make again
– Look at the image
Rolando V. Raqueño
19
Wednesday, May 25, 2016
Fortran_stats example
• Compile a fortran program that
computes the mean of 1,2,3,4,5
• To compile the program
% make
• To run the program
% stats
Rolando V. Raqueño
20
Wednesday, May 25, 2016
Fortran_stats
% more Makefile
stats : main.o sum.o mean.o
g77 -o stats main.o sum.o mean.o
main.o : main.f
g77 -c main.f
sum.o : sum.f
g77 -c sum.f
mean.o : mean.f
g77 -c mean.f
clean:
rm *.o
Rolando V. Raqueño
21
Wednesday, May 25, 2016
Fortran_stats
% more Makefile
FORTRAN = g77
stats : main.o sum.o mean.o
$(FORTRAN) -o stats main.o sum.o
mean.o
main.o : main.f
$(FORTRAN) -c main.f
sum.o : sum.f
$(FORTRAN) -c sum.f
mean.o : mean.f
$(FORTRAN) -c mean.f
clean:
22
rm *.o
Rolando V. Raqueño
Wednesday, May 25, 2016
Typical unreadable makefile
% more makefile
FORTRAN = g77
OBJS = main.o mean.o sum.o
.f.o:
$(FORTRAN) -c $*.f
stats: $(OBJS)
$(FORTRAN) -o stats $(OBJS)
clean:
rm *.o
Rolando V. Raqueño
23
Wednesday, May 25, 2016
Array Manipulation and I/O
Rolando V. Raqueño
24
Wednesday, May 25, 2016
Array Manipulation in IDL
• Let’s create an array representing a
multiband image
IDL> image=bindgen(2,3,4)
IDL> print,image
0
1
2
3
4
5
Rolando V. Raqueño
25
Wednesday, May 25, 2016
Array Manipulation in IDL
• image continued
6
8
10
7
9
11
12
14
16
13
15
17
Rolando V. Raqueño
26
Wednesday, May 25, 2016
Array Manipulation in IDL
• image continued
18
20
22
Rolando V. Raqueño
19
21
23
27
Wednesday, May 25, 2016
Array Manipulation in IDL
• Extract the first and last band of the image
IDL> print,image(*,*,0)
0
1
2
3
4
5
IDL> print,image(*,*,3)
18 19
20 21
22 23
Rolando V. Raqueño
28
Wednesday, May 25, 2016
Array Manipulation in IDL
• Extract the first and last band of the image
simultaneously
IDL> print,image(*,*, [0,3] )
0
1
2
3
4
5
18
20
22
Rolando V. Raqueño
19
21
23
29
Wednesday, May 25, 2016
Array Manipulation in IDL
• Extracting the “color” or “spectral vector”
of the first pixel
IDL> print,image(0,0,*)
0
6
12
18
Rolando V. Raqueño
30
Wednesday, May 25, 2016
Array Manipulation in IDL
• Assign band 1 to band 4
IDL> band1=image(*,*,0)
IDL> image(*,*,3) = band1
• Shortcut
IDL> image(*,*,3)=image(*,*,0)
Rolando V. Raqueño
31
Wednesday, May 25, 2016
Array Manipulation in IDL
• Subsectioning parts of an image
IDL> print,image(0:1,0:1,*)
0
1
2
3
6
8
7
9
12
14
13
15
0
2
1
3
Rolando V. Raqueño
32
Wednesday, May 25, 2016
Array Manipulation
• where function to find indices of elements that
meet certain criteria
IDL> print, where( image gt 5 and image lt 12)
• Use of other arrays to index other arrays
IDL> indices=where(image gt 5 and image lt 12)
IDL> image(indices)=-1
Rolando V. Raqueño
33
Wednesday, May 25, 2016
Reading & Writing Data Files
in IDL
Rolando V. Raqueño
34
Wednesday, May 25, 2016
Reading & Writing Data Files
• IDL can read many types of file formats
• Files can be categorized into two
general types
– Formatted ( Readable Text & Numbers,
ASCII)
– Unformatted( Images)
Rolando V. Raqueño
35
Wednesday, May 25, 2016
Reading & Writing Data Files
• The three IDL commands we can use
for opening files are as follows
openr (open a file for reading)
openw (open a file for writing)
openu (open a file for reading/writing)
Rolando V. Raqueño
36
Wednesday, May 25, 2016
sample_data_1.dat
0
2
4
6
8
10
1
3
5
7
9
11
Rolando V. Raqueño
37
Wednesday, May 25, 2016
Syntax for Opening Data Files
• Example command for opening a file for
input
IDL> openr, 1, ‘sample_data_1.dat’
– The argument 1 is referred to as the logical
unit number (also known as a file handle)
‘sample_data_1.dat’ is the input file name
Rolando V. Raqueño
38
Wednesday, May 25, 2016
Reading the Data
• Need to allocate the array to read in the
data
IDL> a=fltarr( 12)
OR
IDL> a=fltarr( 2, 6)
• Read in the data using the readf
statement
IDL> readf, 1, a
Rolando V. Raqueño
39
Wednesday, May 25, 2016
Closing the Data File
• To close the data file you can give the
following command
IDL> close, 1
• A common mistake is to try to open a file(s)
that is already open causing the error
% OPENR: File unit is already open: 1.
% Execution halted at $MAIN$
(OPENR)
• Solution
IDL> close, /all
Rolando V. Raqueño
40
Wednesday, May 25, 2016
Putting it all together
IDL>
IDL>
IDL>
IDL>
IDL>
openr, 1, ‘sample_data_1.dat’
a = fltarr( 2,12)
readf, 1, a
close, 1
print, a
Rolando V. Raqueño
41
Wednesday, May 25, 2016
Another Way of Reading in a File
IDL> openr, lun, ‘sample_data_1.dat’, $
/get_lun
IDL> a = fltarr( 2, 6 )
IDL> readf, lun, a
IDL> free_lun, lun
IDL> print, a
Rolando V. Raqueño
42
Wednesday, May 25, 2016
Yet Another Way
IDL>
IDL>
IDL>
IDL>
IDL>
IDL>
file_name = ‘sample_data_1.dat’
a = fltarr( 2, 6 )
openr, lun, file_name, /get_lun
readf, lun, a
free_lun, lun
print, a
Rolando V. Raqueño
43
Wednesday, May 25, 2016
To Output a Formatted File
IDL> b = bindgen( 3, 2)*10+42
IDL> openw, lun, ‘test_output.dat’, $
/get_lun
IDL> printf, lun, b
IDL> free_lun, lun
Rolando V. Raqueño
44
Wednesday, May 25, 2016
test_output.dat
42
72
52
82
Rolando V. Raqueño
62
92
45
Wednesday, May 25, 2016
To Output an Unformatted File
IDL> b = bindgen( 3, 2)*10+42
IDL> openw, lun, ‘test_output.dat’,$
/get_lun
IDL> writeu, lun, b
IDL> free_lun, lun
Rolando V. Raqueño
46
Wednesday, May 25, 2016
test_output.dat
*4>HR\
• Why? Because ...
* - has an ASCII value of 42 decimal
4
>
H
R
\
Rolando V. Raqueño
- has an ASCII value of 52 decimal
- has an ASCII value of 62 decimal
- has an ASCII value of 72 decimal
- has an ASCII value of 82 decimal
- has an ASCII value of 92 decimal
47
Wednesday, May 25, 2016
test_P2_image.pgm
P2
3
2
255
42
52
62
72
82
92
Rolando V. Raqueño
48
Wednesday, May 25, 2016
test_P5_image.pgm
P5
3 2
255
*4>HR\
Rolando V. Raqueño
49
Wednesday, May 25, 2016
Testing Out Your or Somebody
Else’s Image I/O Routines
• Read in a small test image
IDL> a=Read_P2_Image(“test_input.pgm”)
• Write out the small test image
IDL> Write_P2_Image,“test_output.pgm”,a
• Read in the small test image you just wrote
IDL> b=Read_P2_Image(“test_output.pgm”)
• Statistics (sum, mean, etc.) must be equal
• Finally, sum_squares( a - b ) must be 0.0
Rolando V. Raqueño
50
Wednesday, May 25, 2016
Multiband Image Pixel
Ordering
• Band Sequential
array( column, row, band)
array( sample, line, band)
• Band Interleaved by Line
array( column, band, row )
array( sample, band, line)
• Band Interleaved by Pixel
array( band, column, row )
array( band, sample, line )
Rolando V. Raqueño
51
Wednesday, May 25, 2016
Multiband Image Pixel
Ordering
• Let us take the following file
• test_data.dat
0 1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21
22 23
• And look at the different interleaving
representations as a multiband image.
Rolando V. Raqueño
52
Wednesday, May 25, 2016
Band Sequential Example
• If we read in the test_data.dat using the
array defined as a=bindgen(2,3,4),
we would get
Band 0 Band 1 Band 2 Band 3
0 1
2 3
4 5
Rolando V. Raqueño
6 7
8 9
10 11
12 13
14 15
16 17
53
18 19
20 21
22 23
Wednesday, May 25, 2016
Band Interleaved by Line
• If we read in test_data.dat using the array
defined as a=bindgen(2,4,3), we would
get
Band 0
Band 1
Band 2
Band 3
0 1
8 9
16 17
Rolando V. Raqueño
2 3
10 11
18 19
4 5
12 13
20 21
54
6 7
14 15
22 23
Wednesday, May 25, 2016
Band Interleaved by Pixel
• If we read in test_data.dat using the
array defined as a=bindgen(4,2,3),
we would get
Band 0 Band 1 Band 2 Band 3
0 4
8 12
16 20
Rolando V. Raqueño
1 5
9 13
17 21
2 6
10 14
18 22
55
3 7
11 15
19 23
Wednesday, May 25, 2016
Reform/Transpose Conversion
• To convert previous examples from BSQ
to BIL, we can use the following command
• Assume a BSQ image
a=bindgen(2,3,4)
IDL> b = reform( a, 2, 4, 3 )
IDL> c = transpose( b, [0, 2, 1] )
• The image c is now a BIL image
Rolando V. Raqueño
56
Wednesday, May 25, 2016
Reform/Transpose Conversion
• To convert previous examples from BSQ to
BIP, we can use the following command
• Assume a BSQ image
a=bindgen(2,3,4)
IDL> b = reform( a, 4, 2, 3 )
IDL> c = transpose( b, [1, 2, 0] )
• The image c is now a BIP image
Rolando V. Raqueño
57
Wednesday, May 25, 2016
Widget Programming
Rolando V. Raqueño
58
Wednesday, May 25, 2016
Widget
• What is a widget?
– It is a graphical user interface component
• Basic IDL Widget Toolkit
– Widget_Base, Widget_Button
– Widget_Draw, Widget_DropList
– Widget_Label, Widget_List
– Widget_Slider, Widget_Text
Rolando V. Raqueño
59
Wednesday, May 25, 2016
Widget_Base
• This widget type is used to hold other widget
types. You can think of this as your blank canvas.
It is also known as the “parent window” in other
terms.
• To invoke a widget base, you would give the
following commands
IDL> base = Widget_Base()
IDL> Widget_Control, base, /realize
Rolando V. Raqueño
60
Wednesday, May 25, 2016
What the IDL Commands Mean
• base = Widget_Base()
– In this function call, base will contain a unique
identifier (ID) to this specific instance of a blank
window. The window is still described in
memory, but not displayed
• Widget_Control, base, /realize
– This tells IDL to display the entity with the ID
value in the variable base
Rolando V. Raqueño
61
Wednesday, May 25, 2016
Result of the Widget_Base
• You should see a blank window similar
to the one below
Rolando V. Raqueño
62
Wednesday, May 25, 2016
Other Widget Types
• To invoke a Widget_Button
IDL> button1 = Widget_Button( base )
IDL> Widget_Control, base, /realize
Rolando V. Raqueño
63
Wednesday, May 25, 2016
To Change the Title of a
Button Widget
IDL>Widget_Control,button1,Set_Value=“OK”
Rolando V. Raqueño
64
Wednesday, May 25, 2016
Destroying a Widget
IDL> Widget_Control,button1,/destroy
Rolando V. Raqueño
65
Wednesday, May 25, 2016
To Destroy the Base Widget
IDL> Widget_Control,base,/destroy
– If there are any widgets attached to the base,
those children widgets will also be destroyed.
Rolando V. Raqueño
66
Wednesday, May 25, 2016
Other Widget Examples
IDL>
IDL>
IDL>
IDL>
base=widget_base(column=1)
button=widget_button(base)
label=widget_label(base)
widget_control,base,/realize
Rolando V. Raqueño
67
Wednesday, May 25, 2016
Other Widget Examples
IDL>
IDL>
IDL>
IDL>
base1=widget_base(column=1)
list1=widget_list(base1)
slider1=widget_slider(base1)
widget_control, base1, /realize
Rolando V. Raqueño
68
Wednesday, May 25, 2016
Adding to the Previous
Example
IDL> base2=widget_base(base1,row=2)
IDL> button1=widget_button(base2,
value="First Button")
IDL> button2=widget_button(base2,
value="Second Button")
IDL> widget_control,base1,/realize
Rolando V. Raqueño
69
Wednesday, May 25, 2016
Modifying Previous Example
IDL> widget_control,slider1,set_value=100
IDL> widget_control,base1,/realize
IDL> widget_control,button2,set_value=
"Last Button"
Rolando V. Raqueño
70
Wednesday, May 25, 2016
Getting Information from a
Widget
IDL>widget_control,button1,get_value=button_value
IDL>print,button_value
First Button
Rolando V. Raqueño
71
Wednesday, May 25, 2016
Have a Safe and Happy
Holiday
Remember, friends don’t let
friends program drunk
Rolando V. Raqueño
72
Wednesday, May 25, 2016