week 2 lecture: a bit about bytes, basics of computing and...

25
Week 2 Lecture: A Bit About Bytes, Basics of Computing and Programming Introduction to Programming for GIS & Remote Sensing GEO6938-4172 GEO4938-4166

Upload: others

Post on 20-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Week 2 Lecture:A Bit About Bytes, Basics of Computing and Programming

Introduction to Programming for GIS & Remote Sensing

GEO6938-4172GEO4938-4166

Page 2: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Recap From Lab Making clouds move…

– Impressive?– Useless?

ENVI> player1 = [[0.0, 1.0, 0.0],[1.0, 1.0, 1.0],[0.0, 1.0, 0.0]]ENVI> player2 = [[1.0, 0.0, 0.0],[1.0, 1.0, 1.0],[0.0, 0.0, 1.0]]

ENVI> output = kernel_wars(player1, player2)

Player 1 has 19068 squaresPlayer 2 has 20910 squaresPlayer 2 Wins!

ENVI> player1 = [[0.0, 0.0, 1.0],[1.0, 1.0, 1.0],[0.0, 1.0, 0.0]]

ENVI> output = kernel_wars(player1, player2)

Player 1 has 20339 squaresPlayer 2 has 19661 squaresPlayer 1 Wins!

Page 3: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

The Landscape Extension

Page 4: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

The Landscape Extension

Page 5: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

“Reading” the Code

Whitespace/Indenting

Enclosures

Other symbols, assignment, etc.

Comments

Page 6: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

“Programs” Are Instructions…

Scripts and programs are written in a language …

Like we are learning to read them…

… the computer must read them as well!

MAGIC?

IDL

Page 7: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

It’s all a matter of translation…

To understand code, we translate it into English…– Or “pseudocode”– Very “high” level

Computers translate it into machine language…– Very “low” level

Page 8: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

So like a good book…

We start at the beginning:– What is a program?

• A sequence of instructions to be interpreted• Content of program is called source code• Computer carries out instructions using CPU

and memory– “Programming” is the writing, testing and

maintaining of source code, written in someprogramming language

Page 9: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

It’s all a matter of translation…

To understand code, you and I translate it into English…– Or “pseudocode”– Very “high” level

Computers translate it into machine language…– Very “low” level

Some slides adapted from lectures by Stefan Leyk and Jeremy Smithhttp://www.colorado.edu/geography/class_homepages/geog_4303_f07/

Page 10: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Machine Language

Binary code– Ones and zeroes, “bits”– CPU-specific– Written on punch cards

Difficult to understand (lowest of “low”):00000 10011110 LOAD 1111000001 10110100 STOR 1010000010 10011110 LOAD 1111000011 11010100 ADD 1010000100 10111111 STOR 1111100101 00000000 HALT

Page 11: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Assembly Language

Moving on up:– Assembly “written” by hexadecimal commands– Interpreted at hardware level– Still hardware-specific (i.e. non-portable)

Still difficult to understand, but can be coded by hand and offers direct manipulation of CPU and memory ( “low”):

46 INC ESI ; increment value in ESI3AC2 CMP AL,DL ; compare register values74 0A JE SHORT 7C90ECA9 ; if equal, jump to a spot in memory84C0 TEST AL,AL ; compare AL register to zero75 F5 JNZ SHORT 7C90EC98 ; if zero, jump to memory location

Page 12: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Higher Level Languages

“Higher” basically means farther from the hardware…– Instructions (programming language) must be translated to

“lower” level language like assembly– Instructions abstract away CPU and memory operations– Instructions may be compiled to assembly (like C,

FORTRAN, Pascal)– Alternatively, instructions may be compiled to byte code,

and interpreted (like Java, Visual Basic, .NET)• Essentially an intermediate step created to aid portability

Page 13: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Scripting vs. Programming

<sigh><computer.science>

Scripters are not programmers! </computer.science><me>

Who cares? It’s all about getting the computer to do what you want it to do!

</me></sigh>

Page 14: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

High Level Languages and Their Differences

Interpreted language– No compilation necessary– Source code is read by an intermediate program, translated to assembly

• A virtual machine does the translation, execution– Everything happens at run-time (Perl, Python, VBScript)

Compiled language– Source code is translated to an executable before run-time– Executable may make full translation to CPU/memory instructions (C++, Fortran,

VB)– However, the executable may require further translation

• Source code translated to byte code at compilation (portable, run in VM, like IDL, Java, and VB)

Translated language– Source code translated directly to assembly by compiler (C)

Page 15: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

High Level Languages and Their Differences, cont’d.

Yet, some “interpreted” languages have byte code compilers:– Python, Perl and IDL can be compiled to a more efficient

intermediate form than the raw source code

And some languages must be compiled, yet still are fully run-time implemented (Visual Basic for Applications)…

Page 16: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

So, ummm, scripting or programming?

We’re doing it all!

Python (ArcGIS)– “Multi-paradigm” language

• Object-oriented or procedural– Extension language (may be

compiled into other proggies)– Interpreted– Example of an interactive

language • Use of a shell allows immediate

access to environment

http://www.freenetpages.co.uk/hp/alan.gauld/

Off-side rule language (indents have meaning):

Page 17: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

IDL is Similar to Python, but… IDL is not interpreted, it is

compiled

IDL (ENVI, standalone VM)– “Multi-paradigm” language

• Object-oriented or procedural– Another example of an

interactive language

– Procedures and functions are compiled, byte code

– IDL is a keyword language • (vs. off-side rule vs. curly brace

language, etc.)

Page 18: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

VBA is compiled, but interpreted?– Compiled only to check syntax– “Multi-paradigm” language

• Object-oriented or procedural

– Very common extension language (may be compiled into other proggies)

– “Kind-of” an interactive language– VBA is also a keyword language

• (vs. off-side rule vs. curly brace language, etc.)

Visual Basic for Applications

Page 19: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Structured Programming

According to Edsger Dijkstra:– All programs can be structured in four possible

ways:• Sequences of instructions• Branches• Loops• Modules

– These are the basis for programming logic

Page 20: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Sequences of Instructions

Strict sequential flow:

Step One Two Three Ta-Da!

Page 21: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Branches

Conditional constructs are used to determine program flow– Test is a True/False statement

Step One TestCondition

Path One

Path Two

Page 22: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Loops

Continuously repeated tests until a condition is met, after which flow continues

RepeatedSteps

TestCondition

Page 23: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Modules

Identical sequences that need to be performed multiple times are grouped in a module or sub-routine– Can be executed as many times as necessary by

main program

Module/Sub-Routine

Page 24: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Beyond Structure, What Else?

Data to be operated on– Variables

• Types, data structures (e.g. arrays, lists, hashes)

Operations– Actions done on data (i.e. verbs, adding, multiply,

compare, >, <, =) Interaction

– GUI, dialog, Input/Output (file access, formatting, etc.)

Page 25: Week 2 Lecture: A Bit About Bytes, Basics of Computing and …users.clas.ufl.edu/forrest/prog/lectures/2008-01-15... · 2008. 1. 15. · Moving on up: – Assembly “written” by

Examples of Programming Structure

Batch-driven Programs Interactive Programs

http://www.freenetpages.co.uk/hp/alan.gauld/