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.

Slides:



Advertisements
Similar presentations
Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
Advertisements

P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
FORTRAN PROGRAMMING BASICS (2) MET 50. Programming Basics A few extra things from last week… 1. For a program written in Fortran 90, use the extension.
2-1 Chapter 2.  Coding Requirements of IDENTIFICATION DIVISION  Sections of ENVIRONMENT DIVISION  Assigning Files to Devices in ENVIRONMENT DIVISION.
Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
WRITING TO AND READING FROM FILES MET 50. Files To read from a file, we still use the READ statement. To write output to a file, we use the WRITE statement.
MAKING YOUR OUTPUT LOOK NICE! MET 50. The “IF” statement Suppose we have the statements: REAL, parameter :: GRAV = 9.81 PRINT*, ‘Value of GRAV is:’,GRAV.
Input and output. Input streams n there are two ways of handling I/o n list-directed –done using default settings –PRINT*, num –READ*, num n formatted.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Formatted Output What we know: write(*,*) x print *, x General Form: write (unit, format, options) list unit = * : write to the monitor format = *: no.
A BIT MORE ON… REPETITIVE EXECUTION MET 50. The “IF” statement In Meteorology, it is common to use the letters I,J,K to denote counters. “I” is the counter.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
Introduction to C Programming
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
CHARACTER DATA MET 50. Character data We have extensively used REAL and INTEGER data types. There is also: CHARACTER data COMPLEX data LOGICAL data 12/1/2011.
FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications.
Basic Elements of C++ Chapter 2.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
Panorama High School E.G.P./ Training to Put Students’ Grades on the Website Wednesday, September 29,
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Homework Reading Programming Assignments
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Introduction to Python
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 9, 2015)
Foundations of Computer Science Computing …it is all about Data Representation, Storage, Processing, and Communication of Data 10/4/20151CS 112 – Foundations.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Selecting, Formatting, and Printing a finished Report…….
Scientific Notation Read through this lesson on scientific notation and then complete the handout. Keep this power point presentation on your laptop and.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Robert Crawford, MBA West Middle School.  Explain how the binary system is used by computers.  Describe how software is written and translated  Summarize.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
INPUT / OUTPUT STATEMENTS
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
1 Lesson Four. 2 The Spelling Checker Searching for Text Menu shortcuts Printing a File Erasing a File.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Verification & Validation. Batch processing In a batch processing system, documents such as sales orders are collected into batches of typically 50 documents.
O PERATING S YSTEM. What is an Operating System? An operating system is an event driven program which acts as an interface between a user of a computer,
Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
BIG NUMBERS and SMALL NUMBERS (Scientific Notation)
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
JDS5 Training Guide. On Start Up you will see this screen click the OK button Click OK.
© William James Calhoun, 2001 Warm up… Simplify The number of three-letter “words” that can be formed with the English alphabet is the number.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
Chapter 2 More on Math More on Input
Variables, Expressions, and IO
Number and String Operations
Programming in JavaScript
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Programming in JavaScript
Homework Reading Programming Assignments Finish K&R Chapter 1
Primitive Types and Expressions
Running a Java Program using Blue Jay.
Presentation transcript:

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 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

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

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

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: “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

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

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  | /6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 7

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  | /6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 8

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

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

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

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

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  | E E+06 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 13

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

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 → | /6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 15

Pretty output That 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

Pretty output That 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 start of a FORMAT statement is “carriage control”. 10/6/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 17

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 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

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

WRITING TO AND READING FROM FILES MET 50

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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