#!/usr/bin/env python from os import system import sys, string ifile=open('sst.dat','r') pfile=open('sst.ppm','w') mxv=-100. mnv=100. h=0 #read data file once to find min and max value: while 1: aline=ifile.readline() if not aline: break if aline[0]=="#": continue #splits aline at white space, then eval turn strings to number. #map appies eval to each member of the list: vals=map(eval,string.split(aline)) h=h+1 for v in (vals): if (v>9999): continue mxv=max(mxv,v) mnv=min(mnv,v) w=len(vals) ifile.close() print "min value=",mnv," max value=",mxv,' width=',w,' height=',h sc=255./(mxv-mnv) #print header: pfile.write("P3\n") #use P3 for ascii, P6 for binary pfile.write("%d %d\n" % (w, h)) pfile.write("255\n") #read data file again to assign colors: ifile=open('sst.dat','r') while 1: aline=ifile.readline() if not aline: break if aline[0]=="#": continue vals=map(eval,string.split(aline)) for v in (vals): if (v>9999): (r,g,b)=(0.,0.,0.) else: #STUDENTS: fix the following three lines to assign colors to the v: #(hint: you may want to use the value of sc to help you) g=255. r=0. b=0. pfile.write("%d %d %d\n" % (r,g,b)) #use with P3 # pfile.write("%c%c%c" % (red,green,blue))#use with P6 ifile.close pfile.close()