ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 7 Input/Output

Similar documents
write (unit=*,fmt=*) i =, i! will print: i = 3

FORTRAN 90: Formatted Input/Output. Meteorology 227 Fall 2018

Intro. Scheme Basics. scm> 5 5. scm>

MATLAB Introduction to MATLAB Programming

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Unit 4. Input/Output Functions

Some More I O definitions

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CS 115 Lecture 4. More Python; testing software. Neil Moore

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Data Types and Variables in C language

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Should you know scanf and printf?

Lesson 2 Characteristics of Good Code Writing (* acknowledgements to Dr. G. Spinelli, New Mexico Tech, for a substantial portion of this lesson)

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Topic 6: A Quick Intro To C

Chapter 2 THE STRUCTURE OF C LANGUAGE

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

C Language, Token, Keywords, Constant, variable

Formatted Input/Output

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

LESSON 4. The DATA TYPE char

Computational Astrophysics AS 3013 Lecture 6:

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Display Input and Output (I/O)

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Programming for Engineers Introduction to C

Chapter 2, Part I Introduction to C Programming

Chapter 2 Basic Elements of C++

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Have examined process Creating program Have developed program Written in C Source code

Reserved Words and Identifiers

Python Working with files. May 4, 2017

Functions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University

Part II Composition of Functions

Full file at

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Chapter 1 Operations With Numbers

[ the academy_of_code] Senior Beginners

T H E I N T E R A C T I V E S H E L L

AMSR IDL (read_amsr_day_v5.pro): .pro. : PRO example (means this script starts here and it s name is example.pro)

ENVI Tutorial: Introduction to User Functions

Customizing DAZ Studio

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Lecture-6 Lubna Ahmed

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Decisions, Decisions. Testing, testing C H A P T E R 7

COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Chapter 11 Introduction to Programming in C

GrADS for Beginners. Laura Mariotti

An Introduction to MATLAB

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

SIMPLE INPUT and OUTPUT:

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Introduction to C An overview of the programming language C, syntax, data types and input/output

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Civil Engineering Computation

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

IDL Primer - Week 1 John Rausch

Lesson 13 - Vectors Dynamic Data Storage

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CSE 142 Su 04 Computer Programming 1 - Java. Objects

A computer program is a set of instructions that causes a computer to perform some kind of action. It isn t the physical parts of a computer like the

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 10: File Input / Output

Getting Started With Linux and Fortran Part 2

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

These notes are designed to provide an introductory-level knowledge appropriate to understanding the basics of digital data formats.

Software Design & Programming I

Week 3 Lecture 2. Types Constants and Variables

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Fundamental of Programming (C)

PHY4321 Summary Notes

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Advanced C Programming Topics

COP4020 Programming Assignment 1 - Spring 2011

Fundamentals of C Programming

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Chapter 11 Introduction to Programming in C

Creating a new form with check boxes, drop-down list boxes, and text box fill-ins. Customizing each of the three form fields.

Chapter 11 Introduction to Programming in C

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Compiler Design. Lexical Analysis

Transcription:

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 7 Input/Output Reading: Bowman, Chapters 10-12 READING FROM THE TERMINAL The READ procedure is used to read from the terminal. IDL normally uses dynamic variable typing, where the type of variable is determined by what is assigned to it. However, when using the READ statement, dynamic variable typing is not used. When using the READ procedure, IDL assumes that variables are floating-point unless they are first declared otherwise! o To read a floating-point number into a variable, you don t need to first declare it. You can just do read, a o To read a string into a variable, you would first have to declare it as a string variable by assigning a string value to it. This is easily accomplished by assigning a blank character to the variable before reading, as shown: a = read, a o To read an integer into a variable, you would first have to give it an integer value so that IDL knows it is an integer. So, you would have to do a = 0L or a = long(0) read, a You can read an entire array with the READ procedure a = fltarr(5) read, a You cannot read into individual array indexes or partial arrays with the READ procedure a = fltarr(5) read, a[0], a[3] => This won t work! IDL allows you to print a prompt message to the screen when requesting data input from the terminal. To do this, use PROMPT the keyword read, a, prompt = Enter the value for input:

READING MULTIPLE VALUES PER READ STATEMENT Often we want to read in multiple data values on a single line. As long as the data being read is all numerical (either integer, float, or double-precision) you can just do read, a, b, c The input data can be separated by spaces, commas, tabs, or new-line characters. If some of the data values are strings, this presents a challenge. If IDL reads a string variable, it will read everything until the end of the line into the string. Example: a = ; a is a string variable b = 0.0 ; b is a floating-point variable read, a, b If the input line were: Temperature 45.6 Then a read-error would result because Temperature 45.6 would be read into a, and there would be nothing left to read into b. To avoid problems with reading mixed string and numerical data, or multiple strings, with a read statement, there are two options: o Option #1: Use a formatted read statement (discussed in a later section). This will only work if the data is in regular, column format. o Option #2: Read the entire line into a single string variable, and then use the STRSPLT() function to split the data into separate variables. An example of using Option #2 is as follows: o Imagine that the input data is of the form: KLNS 45.0 32.0 NORTHEAST 5 where first is the station ID (string), temperature and dew point (float), wind direction (string) and wind speed (integer). o To read this into the appropriate variables you could do station_id = ; declares this as a string temperature = 0.0 ; declares this as a float dew_point = 0.0 ; declares this as a float direction = ; declares this as a string 2

speed = 0 ; declares this as an integer a = ; declares this as a string read, a ; a is a string variable b = STRSPLT(a,, /extract) ; splits a on white space ; and creates array b station_id = STRCOMPRESS(b[0], /remove_all ) temperature = b[1] dewpoint = b[2] direction = STRCOMPRESS(b[3], /remove_all) speed = b[4] o Note that we used the STRCOMPRESS() function with the /remove_all keyword on the string variables to remove and unwanted leading and trailing white spaces they may contain. o If the data were separated by commas instead of white spaces, then the only change to the above procedure would be to use b = STRSPLT(a,,, /extract) ; splits a on white space WRITING TO THE TERMINAL The PRINT procedure is used to print text to the terminal. READING AND WRITING TO AND FROM FILES Reading and writing to files is accomplished using the READF and PRINTF procedures. To read or write to a file, the file must first be opened. This is accomplished using the OPENR, OPENW, or OPENU procedures. o OPENR opens a file for reading only. o OPENW opens a file for writing only. o OPENU opens a file for reading or writing. When opening a file have to specify a logical unit number (or LUN) and assign it to a file name. You then write to or read from the logical unit number. Rather than picking your own logical unit numbers, IDL will do that for you if you use the /GET_LUN keyword in the OPENR, OPENW, or OPENU procedures. 3

So, to open up a file named output.txt for writing, you would use openw, unit, output.txt, /get_lun o Note that unit is a variable name, and we could use any valid IDL variable name here. If I were opening multiple files I would use unit_1, unit_2, etc. To then write the contents of a variable called speed to the file you would use the PRINTF procedure as follows: printf, unit, speed To open up a file named input.txt for reading you would use openr, unit2, input.txt, /get_lun To read the contents of a variable called speed from the file you would use the READF procedure, readf, unit2, speed When you are all through reading and writing to a file, you should close it using the FREE_LUN procedure. For our example above we would use free_lun, unit free_lun, unit2 You can also close all open units at once using the CLOSE procedure with the /ALL keyword, CLOSE, /ALL The SKIP_LUN procedure (used with the /lines keyword) allows you to skip over a designated number of lines in a file. o This is useful for skipping header lines in an input file FORMATTED INPUT AND OUTPUT The FORMAT keyword can be used with the PRINT or PRINTF procedures to write formatted output to the terminal or a file, and with the READF procedure to read formatted input from a file. The format specification used in the FORMAT keyword is very similar to that used in Fortran. Usage of a typical format statement is shown below a = 23.567 4

b = 567 c = 0.000045632 d = Hello print, a, b, c, d, format = (f7.2, i5, e12.2, a8) would result in the following being printed to the screen: 23.56 567 4.56e-05 Hello 7 5 12 8 where the horizontal line and numbers illustrate the field widths associated with each piece of data. The table below shows the most commonly used specifications: Data Type Format Explanation Specification String aw Outputs character data with a field width of W characters. Integer iw Outputs integer data with a field width of W characters (including minus sign). Floating-point fw.d Outputs floating-point data with a field width of W (including sign and decimal mark) and D decimal places. Scientific notation ew.d Outputs floating-point data in scientific notation, with a total field width of W (including all signs and exponent character) and D decimal places. Blank Space Nx Outputs N blank spaces. You can easily repeat a format specifier or groups of format specifiers by prefacing them with an integer. o Example: print, a, format = (5f8.2) would print 5 floating-point numbers each having 8 columns and 2 decimal points. print, a, format = (5(2x, f8.2)) would print 5 floating-point numbers each having 8 columns and 2 decimal points, with an additional 2 spaces separating each. 5

READING AND WRITING BINARY FILES Binary files (called unformatted files in the Fortran world) can be read and written using the READU and WRITEU procedures. Using the /F77_UNFORMATTED keyword will read binary files that were created from a Fortran program, and will write binary files that can be read with a Fortran program. The file pointer (the position that will be read or written) can be repositioned in a binary file through use of the POINT_LUN procedure. Different operating systems write binary data in a different manner. o Windows and many Linux systems are little-endian while Macs are big-endian. If you try to read binary files written on a little-endian machine with a big-endian machine, your data will be garbled. To account for this, you can use the /SWAP_IF_LITTLE_ENDIAN and /SWAP_IF_LITTLE_ENDIAN keywords. This is not an issue with ASCII files, which are universal. AVOIDING THE END-OF-FILE ERROR When reading a file you may not know ahead of time how many lines it contains. If you try to read past the end of the file you will get an error. There are two ways to try to avoid this: o You can find out ahead of time how many records are in a file using the FILE_LINES() function, where the argument is a file name (not a lun). o You can test to see if you are at the end of file using the EOF() function. An example of using the EOF() function to avoid the end-of-file error, see below: while not eof(unit) do begin readf, unit, variables to be read endwhile o As long as the end-of-file is not reached, eof(unit)will be 0 and the read statement will be executed. When the end-of-file is reached, eof(unit)will be 1 and the read statement won t be executed (thus avoiding the error). 6

The above method works if there is no empty line at the end of your data. If there is, you will still get an error. I ve found a way to avoid this using the ON_IOERROR procedure. The ON_IOERROR procedure lets you tell the program which statement to go to if there is an I/O error. As an example, while not eof(unit) do begin on_ioerror, end_file readf, unit, variables to be read endwhile end_file: optional statement to execute continuation of program If there is an I/O error, such as an end-of-file error, the program won t crash, but will just move to the endfile statement and carry-on from there. o Caution: It s possible that the I/O error was due to something other than reaching the end of the file, in which case using this method may cause you some confusion. READING DATA INTO AN ARRAY WITHOUT KNOWING IN ADVANCE HOW MANY DATA LINES ARE IN THE FILE The method below shows how to read data into an array without knowing in advance how many data lines the file contains, or without worrying whether or not there are blank lines at the end of the file. Assume that we have an input file of the form Temperature Dew Point 45.6 34.5 34.0 32.0 45.3 40.9.... To read this into two arrays temperature and dew point without knowing in advance how many elements we need, we could do a = fltarr(2) ; array to hold values read from file 7

i = 0 ; counter while not(eof) do begin on_ioerror, unit, endfile ; directs to endfile ; statement if io error readf, unit, a ; reads temp and dew point into a if i eq 0 then begin ; creates arrays if i = 0 temperature = [a[0]] dewpoint = [a[1]] endif else begin ; adds to arrays if i > 0 temperature = [temperature, a[0]] dewpoint = [dewpoint, a[1]] endelse i += 1 ; advance counter endwhile endfile: READING OTHER FILE TYPES IDL has many built in procedures for reading other binary file types such as NetCDF, GIF, JPEG, HDF, etc. See the online help if you ever need to know how to read these files (we will cover NetCDF files later). PICKING FILES INTERACTIVELY IDL contains a handy function called DIALOG_PICKFILE(). This function allows you to choose a file name interactively using a Graphical User Interface (GUI). Example: file_name = DIALOG_PICKFILE() gives you a GUI interface that allows you to pick a file. If you then select exercise4.pro such as in the diagram below, and hit Open 8

Then it will assign the complete path and file name to the variable file_name. The example above is from a Windows operating system, but it works similarly in the Linux environment. Note that DIALOG_PICKFILE() is an example of a function that doesn t accept any arguments. But, you still need to call it with empty parentheses. 9