Tutorial 03 -Code Logic: Functions, Conditionals and Loops

import csv
def setup():
size(480,120)
with open(“4.5_week.csv”) as e:
reader = csv.reader(e)
header = reader.next() # skip the header row.
print header

global magnitudes # means make this “ magnitudes” as global var
magnitudes = []
# make a group

for row in reader: # var = row
magnitude = float(row[4])
magnitude+=1
magnitudes.append(magnitude)
print magnitudes

def draw():
background(0)
scalar = 10
x = 0
for magnitude in magnitudes:
leftEdge = x — (magnitude*scalar)/2
rightEdge = x + (magnitude*scalar)/2
if leftEdge < mouseX and mouseX<rightEdge:
fill(155,221,255) # columbia’s color
rectMode(CENTER)
rect(x,height/2,magnitude*scalar*1.5,magnitude*scalar*1.5,8)
else:
fill(255,255,255)
ellipse(x,height/2,magnitude*scalar,magnitude*scalar)
x+=5
saveFrame(‘output.png’)
Using While Loop to map the earthquake locations
import csv
def setup():
size(480,480)
with open(“4.5_week.csv”) as e:
reader = csv.reader(e)
header = reader.next() # skip the header row.
print header

global magnitudes # means make this “ magnitudes” as global var
global lats
global lons
magnitudes = []
lats = []
lons = []
# make a group

for row in reader: # var = row
magnitude = float(row[4])
magnitudes.append(magnitude)
# print magnitudes
lat = float(row[1])

lats.append(lat)
# print lats

lon = float(row[2])

lons.append(lon)
# print lats
def draw():
background(0)
scalar = 0.8
x = 0
i = 0
t = 3
while i < len(magnitudes):
lon = lons[i]
lat = lats[i]

magnitude = magnitudes[i]
u = lon+width/2
v = (-lat)+height/2 # (-lat)


# set range
xleftEdge = u-(magnitude*scalar)/4
xrightEdge = u+(magnitude*scalar)/4
yleftEdge = v-(magnitude*scalar)/4
yrightEdge = v+(magnitude*scalar)/4
if (xleftEdge < mouseX and mouseX< xrightEdge) and (yleftEdge< mouseY and mouseY <yrightEdge):
fill(155,221,255) # columbia’s color
rectMode(CENTER)
rect(u,v,magnitude*scalar*t,magnitude*scalar*t,8)
textSize(12)
text(“The magnitude of this earthquake is located at “+ str(lat)+”, “+str(lon), 30,height*3/4)
else:
fill(255,255,255)
ellipse(u,v,magnitude*scalar,magnitude*scalar)
i+=1