Presentation is loading. Please wait.

Presentation is loading. Please wait.

MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50. Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the.

Similar presentations


Presentation on theme: "MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50. Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the."— Presentation transcript:

1 MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50

2 Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the PRINT command to print output to the screen (today). We use the WRITE command to print output to the printer or a data file (next topic). 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 2

3 Pretty output PRINT nn, GRAV nn FORMAT (stuff) “nn” is called a label number, and must be an integer. The FORMAT line tells the compiler which format to use for output  and you get to define the format 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 3

4 Pretty output To print an integer number: descriptor looks like “Im” 1. “I” says “you will print an integer” (no decimal) 2. “m” is also an integer that tells the compiler how many spaces to allocate to printing the number. 3. The quantity being printed is printed right-justified in the “m” spaces allocated. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 4

5 Pretty output Example…writing “IMAX = 7”: PRINT 20, IMAX 20 FORMAT (1X, I2) “I2” says – write the integer as: -7 “I4” says – write the integer as: ---7invisible! “I8” says – write the integer as: -------7 “right-justified” means that the quantity “7” is to the far right of the space. 1X ??? Soon !!! 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 5

6 Pretty output To print a real number: descriptor looks like “Fw.d” 1. “F” says “you will print a real #”  decimal AND stuff after the decimal 2. “w” is an integer that tells how many total spaces to allocate to printing the number. 3. “d” is an integer that tells how many digits are printed to the right of the decimal. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 6

7 Pretty output Again…the quantity being printed is printed right-justified in the “m” spaces allocated. Rule: in Fw.d, set w  d+3 Example…writing “ GRAV = 9.81 ”: PRINT 40, GRAV 40 FORMAT (1X, F8.2) “F8.2” says: Assign 8 total spaces to print GRAV Of these 8 spaces, 2 are after the decimal Producing the result: Left edge of screen/paper  |----9.81 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 7

8 Pretty output PRINT 40, GRAV 40 FORMAT (1X, F12.4) “F12.4” says: assign 12 total spaces to print GRAV Of these 12 spaces, 4 are after the decimal Producing the result: Left edge of screen/paper  |------9.8100 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 8

9 Pretty output Printing multiple numbers… PRINT 50, GRAV, RAD! XRAD=16.822781 50 FORMAT (1X, 2F12.4) Produces the result: Left  |------9.8100-----16.8227 XRAD=16.822781 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 9

10 Pretty output PRINT 50, GRAV, RAD! XRAD=16.822781 50 FORMAT (1X, 2F20.1) Produces the result: Left  |-----------------9.8----------------16.8 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 10

11 Pretty output To print a very large or small real number – we need “scientific notation”: descriptor looks like “Ew.d” 1. “E” says “scientific notation” 2. w  d+7 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 11

12 Pretty output Example… RATE = 7.292 x 10 -5 ! Earth’s rotation rate (2  /“day”)(s -1 ) ERAD = 6.371 x 10 6 ! Earth’s radius (meters) PRINT 12, ERAD, RATE 12 FORMAT (1X, E12.4, E14.6) Produces the result: Left  |--0.7292E-04--0.637100E+07 This bit always consumes 4 spaces – remember to account for it!! 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 12

13 Pretty output Note… The result is printed with a “zero” in front. So GRAV = 9.81 would be printed as: 0.981E+01 Instead, you could use the “ES” option… PRINT 13, ERAD, RATE 13 FORMAT (1X, ES16.4, ES16.6) Gives: Left  |------7.2920E-05----6.371000E+06 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 13

14 Pretty output Mixing it all up is allowed! example… PRINT 22, IMAX, JMAX, KMAX, TEMP, PRESS 22 FORMAT (1X, I4, I4, I4, F10.2, F10.2) Or more compactly… PRINT 22, IMAX, JMAX, KMAX, TEMP, PRESS 22 FORMAT (1X, 3(I4), 2(F10.2)) Print three integers,Print two real numbers, each “I4”each “F10.2” 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 14

15 Pretty output Spreading things out… We can use the “X” descriptor to enter spaces between output. example… PRINT 22, IMAX, TEMP, PRESS 22 FORMAT (1X, I4, 5X, F10.2, 5X, F10.2) Means: Skip a space (but…see next slide) Print IMAX using “I4” format Skip 5 spaces to the right (“5X”) Print TEMP using “F10.2” format Skip 5 more spaces to the right (“5X”) Print PRESS using “F10.2” format → |---20---------287.40--------1003.50 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 15

16 Pretty output That “1X” @ front of FORMAT statement…history In the old days we used cards. You would hand your cards to an “operator” (a person) behind a counter, and they would “run” the program for you.  Cards read in via a scanner – program “sent off” to the computer.  Runs were called “jobs”.  You would then sit and wait for output. Maybe for DAYS.  Really!  This is/was called “batch mode” of computing (look it up!). The results would come back ON PAPER.  NOT on a screen!!! So … all output was to paper. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 16

17 Pretty output That “1X” @ front… “We” needed to be able to tell the printer things like: At a minimum, start a new line (after printing the last “job”)! Maybe even skip a few lines! Maybe – jump to the top of a new page!!! We used “carriage control” to do this. The stuff @ start of a FORMAT statement is “carriage control”. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 17

18 Pretty output The “carriage control” stuff IS a relic – BUT on some compilers you may get messed up by ignoring this. Some compilers will try to interpret the FIRST thing in a FORMAT statement as “carriage control”. Thus, many people (including me) put that “1X” @ start of every FORMAT statement to MAKE SURE the output comes out OK. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 18

19 Pretty output Example… One some compilers PRINT 22, IMAX, GRAV! IMAX=4220, GRAV = 9.81 22 FORMAT (I4, 5X, F10.2) could give:|220-----------9.81 The character “4” @ start of IMAX is cut off. Instead PRINT 23, IMAX, GRAV! IMAX=4220, GRAV = 9.81 23 FORMAT (1X, I4, 5X, F10.2) would give:|4220-----------9.81 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 19

20 WRITING TO AND READING FROM FILES MET 50

21 Files To read from a file, we still use the READ statement. To write output to a file, we use the WRITE statement. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 21

22 Files WRITE (mm, nn) (things to write) 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 22 Tells us the FORMAT of what we write Tells us where we write the output to

23 Files READ (mm, nn) (things to write) 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 23 Tells us the FORMAT of what we read Tells us where we read the input from

24 Files Example… WRITE (25, 16) X1, X2, X3 16 FORMAT (1X, 3F10.2) Also… WRITE (mm, nn) X1, X2, X3 “mm” and “nn” must be integer WRITE (*, nn) directs output to screen 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 24

25 Files Guidance for values of “mm” … In the old days, WRITE (6, nn) - directed output to the printer READ (5, nn) - read input from the screen Caution: the values ‘5’ and ‘6’ might be hard-wired into old code. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 25

26 Files Files… A file is like a book. It needs to be opened, closed, flipped thru etc. We have commands for this! 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 26

27 Files Opening a file… OPEN (UNIT=mm, FILE=“name of file”, STATUS=“OLD or NEW or replace”, ACTION=“READ or WRITE or READWRITE”, POSITION=“REWIND or APPEND or ASIS”, IOSTAT=integer number) 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 27

28 Files Example… OPEN (UNIT=12, don’t need to write “UNIT” FILE=“temperature_data_1977.dat”, name STATUS=“OLD”, file already exists ACTION=“READWRITE”, usually omit POSITION=“ASIS”,often omit IOSTAT=integer number) see later… 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 28

29 Files Simplified example… OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 29

30 Files Code example…running this… REAL :: DATE, TEMP add code here to generate some data to write! OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“NEW”, IOSTAT=IOS) WRITE (12,22) DATE, TEMP 22 FORMAT (1X, 2F12.4) After you compile and run this code, you would find that the file “temperature_data_1977.dat” has appeared in your directory! 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 30

31 Files And reading from a file? REAL :: DATE, TEMP OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) READ (12,22) DATE, TEMP 22 FORMAT (1X, 2F12.4) IF (IOS > 0) THEN PRINT*,’Bad data or missing data with IOS=‘, IOS ENDIF add some code to do something with this data!!! 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 31

32 Files Two three more things… (1) REWIND command Sometimes we may need to : OPEN a file READ the data READ it all again from the start 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 32

33 Files Code could include these lines… OPEN (12, file=‘name.dat’, status=‘OLD’) READ (12,100) X,Y,Z 12 FORMAT (bla bla bla) do some calculations REWIND (12) READ (12,100) X,Y,Z do some more calculations CLOSE (12) 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 33

34 Files (2) Forms in which data is stored Either:text data Or:binary data 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 34

35 Files text data Looks just like regular characters on the screen IS readable! Read: http://en.wikipedia.org/wiki/Data_filehttp://en.wikipedia.org/wiki/Data_file Example…online (/121) 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 35

36 Files binary data Looks unintelligible Is NOT! Example…online (/121) 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 36

37 Files (3) Reading & writing binary data This is NOT formatted! Space-saving solution for massive datasets. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 37

38 Files Code would include lines like this… OPEN (12, file=‘name.dat’, status=‘OLD’, form=‘unformatted’) READ (12) X,Y,Z note no FORMAT statement! and WRITE (12) X,Y,Zditto! 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 38

39 Files Large datasets often arrive in binary form. A new paradigm in atmospheric science is “netCDF” 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 39


Download ppt "MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50. Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the."

Similar presentations


Ads by Google