CMSC 106 Project #6 Fall 2001

Due date: Wednesday, November 28, 2001


1. Purpose


This project will give you practice processing strings using pointers and C's character and string processing library functions. This will allow you to gain a fuller understanding of the relationship in C between arrays and pointers. In order to make sure you are using the string functions already defined for you in <string.h>, you may not use any loops that process an array character by character. You will be writing a program that processes weather data encoded in the standard international meteorological reporting format. Your program will read a series of reports, extract the necessary data values, and print an organized summary of the data for a selected group of locations.


2. Project description


As you read this section, you may also want to refer to the ``Sample output'' section below. Your program will first read a list of station names and identifiers. These will be the locations for which we are interested in obtaining data. Then it will read a series of encoded data reports. For each report from a recognized location, it will decode the report to obtain the necessary data values. After all the data reports have been read and decoded, the program will print a summary report organized alphabetically by location.


2.1 Program input

The input data file will consist of 2 sections: the station list and the list of data reports. Each line of the station list will contain a station identifier, the name of the station, and the state in which it is located. The lines will be in the form: CCCC Station Name,SS where

Each line in the station list will be no longer than 80 characters. The station identifier will be separated from the station name by at least one blank. The station name will be no longer than 72 characters; it may or may not contain imbedded blanks. The station name will be separated from the state abbreviation by a comma; the comma may be followed by 0 or more blanks. The program will read and process station identifier lines until it reads a station identifier ``####''. There may or may not be additional information on the last station identifier line. There will be no more than 40 stations in the station list. Following the station list, the program will read the data reports. The reports will be encoded in the standard format known as METAR. (The U.S. version of this format is completely described in Federal Meteorological Handbook Chapter 12 but we will be implementing a subset of the complete code.) Each report will be on a single line no longer than 120 characters. A report consists of a series of data groups separated from each other by a single blank in the format:

CCCC DDhhmmZ dddff[f]Ggg[g]KT nnnVxxx vvvvvSM wwww NNNhhh TT/TD Apppp RMK other

The only data groups which are required to be in a report are the station identifier and the time of the observation; all other groups may be omitted. Certain groups may appear more than once in a particular report. Data groups other than the station identifier and the time may appear in any order, but remarks must appear after all other groups. The various fields are defined as follows:

  1. Station identifier (CCCC)
    The station identifier consists of 4 characters which correspond to the station codes which may appear in the station list.
  2. Observation time (DDhhmmZ)
    The date and time of the observation are given in a data group ending with an upper-case 'Z'. The date (DD) consists of 2 digits indicating the day of the month. Note that the month itself is not included; it is assumed to be the current month (i.e., currently 11 for November). The time consists of 4 digits: the hour (hh) followed by the minute (mm).
  3. Wind (dddff[f]Ggg[g]KT)
    The wind group ends with "KT" indicating that wind speeds are in knots. The direction (ddd) is given to the nearest 10 degrees (from 0 to 350) using 3 digits. If the direction is less than 100 degrees, then a leading 0 is used. If the wind direction is variable, and the wind speed is less than 6 knots, then "VRB" may be used in place of the direction ddd. The wind speed (ff[f]) is given in knots using 2 or 3 digits. Values less than 10 are given with a leading 0. Values of 100 or more are given with 3 digits. Wind gusts (gg[g]), if any, are indicated by a 'G' immediately following the wind speed. Gusts are given in knots using 2 or 3 digits. For example, a wind from due west (270 degrees) at 20 knots with gusts to 35 knots would be coded "27020G35KT"
  4. Variable wind (nnnVxxx)
    If the wind direction is variable, and the wind speed is greater than 6 knots, then the range of directions is given in a variable wind group which contains the 2 directions (nnn and xxx) separated by 'V'. (For example, if the wind is mainly from 210 degrees at 10 knots, but variable from 180 to 240, it would be coded "21010KT 180V240".)
  5. Visibility (vvvvvSM)
    The visibility group ends with "SM" to indicate that the units are statute miles. The METAR code allows for fractional values, but the program may assume that all visibility values are in whole miles.
  6. Present weather (wwww) A report may contain up to 3 present weather groups. Current precipitation or sky obscuration is indicated by a 2-character symbol: The current weather symbol may be preceded by a minus sign ('-') to indicate "light" or a plus sign ('+') to indicate "heavy". In addition, the current weather symbol may also be preceded by a 2-character descriptor: For example, ``heavy rain shower(s)'' would be coded as "+SHRA".
  7. Sky condition (NNNhhh)
    Sky condition (cloud cover) is coded in a separate group for each cloud layer up to the first overcast layer. Each group consists of an amount (NNN) and a height (hhh). Height is given in multiples of hundreds of feet. Amount is indicated by a 3-character abbreviation. In order from minimum to maximum coverage, these are:
  8. Temperature/Dew Point (TT/TD)
    Air temperature (TT) and dew-point temperature (TD) are each coded using 2 digits rounded to the nearest degree Celsius. The two temperature values are separated by a slash ('/'). Temperatures below 0 are preceded by an upper-case 'M'. For example, a temperature of 4C with a dew point of -2C would be coded as "04/M02". If the temperature is not available, the entire temperature/dew point group is omitted. If the dew point is not available, the temperature is coded followed by a slash and no entry is made for dew point. For example, a temperature of 1.5C and a missing dew point would be coded as "02/".
  9. Air pressure ( Apppp)
    The air pressure, or altimeter setting, begins with an upper-case 'A'. The value is given in hundredths of an inch of mercury, omitting the decimal point. For example, "A3023" indicates 30.23 inches.
  10. Remarks ( RMK other)
    The symbol "RMK" indicates that the following 1 or more groups (other) are remarks. Remarks may contain additional details beyond those included in the regular groups. The program will ignore all remarks.
The program will read and decode data reports until it reaches the end of the input file. There will be no more than 50 reports in the input file.

2.2 Processing and output


The program will first read the station list, storing the identifiers, station names, and state abbreviations in arrays of strings. If a station identifier has already been stored, then the program must print out a message and skip the processing of that line in the station list. The message must be in the form:



Duplicate station CCCC



where CCCC is the station identifier. After the program has read and processed all the station identifiers, it will read, decode, and store the data reports. As each report is read, the program must check the station identifier against the identifiers in the station list. If the identifier does not appear in the list, the program must print a message and ignore the rest of that data report. The message must be in the form:

Unknown station CCCC

where CCCC is the identifier in the report. For each report from a known station, the program must decode each data group and store the values. After all of the data reports have been read and decoded, the program must print out a formatted summary of the data. The summary must be preceded by a single separator line containing 10 equal signs ``='' followed on the next line by the word ``SUMMARY''. The summary must be printed in alphabetical order by state abbreviation. The group of stations for a particular state must be preceded by a header message, as shown in the sample output. Within each state, the stations must be printed in alphabetical order by station name. If there are 2 or more reports from the same station, the program must print them in the same order that they were read from the input. The data reports from each station must be separated by a blank line as shown in the sample output, and all data values must be neatly aligned in columns and labeled as shown. The data values must be listed in the order shown. If a particular data value was not reported, then the symbol ``NA'' should be printed, except:

If the wind is reported, but the speed is 0, then the word ``Calm'' should be printed. If sky conditions are reported for more than 1 layer, then the conditions for the layer with the maximum cloud coverage should be printed.

2.3 Processing requirements

  1. THE MAIN RULE - YOU MAY NOT USE ANY CHARACTER BY CHARACTER TRAVERSALS OF ANY CHARACTER ARRAY !!! If you are using the name of a character array with an index i.e. arr[i] and you have a loop that uses i++ - you are traversing the array character by character. If you are using pointers to a character array char *ptr and you have a loop that uses ptr++, you are traversing the array character by character.

  2. You must use at least 6 string.h fuctions from the following list: strlen, strchr, strcat, strcpy, strncat, strncpy, strcmp, strncmp, strcspn, strpbrk, strrchr, strspn, strstr, or strtok.


3. Project requirements


All your C programs in this course should be written in ANSI C, which means they must compile and run correctly with cc -std1 -trapuv on the OIT UNIX Class Cluster. You will lose credit if your program generates any warning messages when it is compiled. Prototypes must appear for all functions defined, prototypes must be listed at the top of the program file, and at most one return statement may be used in any function, including main.

Even if you already know what they are, you may not use any C language features other than those introduced in Chapters 1 through 13 of your textbook, plus those in Section 23.4 and any function in Section 23.5 whose name begins with ``str'', and the language features presented in lecture while these chapters were covered. In addition neither the goto nor the continue statement may be used, and the break statement may not be used in any loop. Your program may not use the exit() library function at all. Lastly, no global variables may be used. Using any of these disallowed C features will result in losing credit.

Your program must make use of C's string library functions anywhere possible. You will lose substantial credit if you write code duplicating their effects, rather than just calling them (e.g., if you write loops to copy or compare strings, search for something in a string, find the length of a string, etc., you will lose credit). The character library functions (textbook section 23.4) may also be used, but you will not be penalized if you duplicate their effects rather than using them. You will lose substantial credit if you use any traversals of an array character by character.

In additon to using the string library functions, your program must be written using user-defined functions where approprriate. Your program must define and call at least six functions you have written; writing more than six such functions would be extremely good practice and the best way to avoid errors in developing your program. If your program doesn't contain, and call, at least six such functions, it will be graded as if it does not work on the primary input- even if its output is correct. Below are ideas for some possible functions; feel free to use others. You are permitted to write as many functions as you want, and if you write any of the functions suggested below they need not perform the exact tasks as described.


Your program must have a comment near the top which contains your name, login ID, student ID, your section number, your TA's name, and an original description of the action and operation of the program. Do not put your alias in this comment! In addition, you must have a comment before each function, explaining its action and operation. Your program should be written using good programming style and formatting, as discussed in class and throughout your textbook. For this project, style is considered to consist of:



4. Developing your program


You may want to skip this section at first, read the rest of the project, and come back to study it carefully when you are about to begin writing your program.

Mistakes with string library functions or pointers are very likely to result in a fatal program execution error (core dump). A statement may look completely correct, but just because some string or pointer contains a certain value the program will fail. This makes it extremely important to use debug printf statements to narrow down where in your program the error occurs, before you can even begin to figure out what's wrong and how to fix it.


4.1 Possible development steps

Be sure to test each function as it is implemented, before integrating it with the rest of the program! It is frequently very easy to test a function which has character array parameters, because you can often call it by passing any string literals you like into the parameters. As an example, say you write a function named split, which is supposed to find the position where a string is to be split in half (perhaps it has another integer parameter as well). You can call your function several times, as split("This is a character string", 15) or split("Try another character string", 12), and print the result which your function produces each time. It's easy to see by hand what result your function should produce, so test it with a number of strings and be certain it gives the right answer every time. You can write a little test program file to call your function, or just add these test calls right at the beginning of main. Once you are positive your function works, then you can call it as part of your project and be confident that your program will be likely to work fine.


4.2 Finding compilation errors

Here are several common compilation errors having to do with strings produced by the cc compiler on our class machines and what they mean:


X is being converted to "pointer to char"

(where X is a statement using one of the string library functions)

You probably forgot the #include <string.h> at the top of your program file.

"X" is not an lvalue, but occurs in a context that requires one.

You are trying to assign something to the name of an array. You can assign something (a pointer value, or NULL) to a pointer variable, but not to the name of an array. Maybe you meant the variable on the left of an assignment to be declared as a pointer instead of an array.


4.3 Program debugging


  1. Don't forget that, for technical reasons, all debug printf statements must end with a newline character, or their results may not show up on the screen if the program has a fatal execution error (core dump).

  2. As mentioned above, core dumps are a common result from problems with strings and pointers. If a program with several functions has a core dump, and you don't know where the problem is, you can add a debug printf statement at the beginning of each function and at the end of each function, before it returns. These debug print statements can just say something like ``Starting function X'' and ``Leaving function X''. Run your program again, and if the output shows that some function was entered but never completed, that function, or one of the functions it calls, may be where your problem lies. Remember the terminating \n when inserting debug printf statements.

  3. If you are having trouble getting a string to print in its entirety, try printing the first character or characters from that string using either pointer or subscript notation. This could tell you if the string is at least starting in the correct place. The problem could involve a missing or misplaced null character ('\0').

  4. Draw lots of pictures to trace exactly where things are in memory, and be sure never to attempt to dereference or follow a pointer which is not pointing at something valid!

  5. If after you have tried these techniques, and tested each of your functions, you still can't figure out why your program doesn't work, bring a printout to our office hours, and we can help you learn how to track the problem down.


4.4 Helpful hints



5. Academic integrity statement


Any evidence of unauthorized use of computer accounts or cooperation on projects will be submitted to the Student Honor Council, which could result in an XF for the course, suspension, or expulsion from the University. Projects are to be written INDIVIDUALLY. For academic honesty purposes, projects are to be considered comparable to a take-home exam. Any cooperation or exchange of ideas which would be prohibited on an exam is also prohibited on a project assignment, and WILL BE REPORTED to the Honor Council.


VIOLATIONS OF ACADEMIC HONESTY INCLUDE:


  1. failing to do all or any of the work on a project by yourself, other than assistance from the instructional staff.

  2. using any ideas or any part of another student's project, or copying any other individual's work in any way.

  3. giving any parts or ideas from your project, including test data, to another student.

  4. having programs on an open account or on a PC that other students can access.

  5. transferring any part of a project to or from another student or individual by any means, electronic or otherwise.


IT IS THE RESPONSIBILITY, UNDER THE UNIVERSITY HONOR POLICY, OF ANY STUDENT WHO LEARNS OF AN INCIDENT OF ACADEMIC DISHONESTY TO REPORT IT TO THEIR INSTRUCTOR.


6. Submitting your project


Your project must be electronically submitted by the date above, before 11:00 pm, to avoid losing credit as described in the syllabus. No projects more than two days late will be accepted for credit without prior permission or a valid medical excuse, as described on your syllabus. Only the project which you electronically submit, according to the procedures provided, can be graded; it is your responsibility to test your program and verify that it works properly before submitting. Lost passwords or other system problems do not constitute valid justifications for late projects, so do not put off working on your program or wait to submit it at the last minute!

Turn in your assignment using the ``submit'' program as before, except using ``6'' for the project number. You are to submit only the .c file containing your source code, not the executable version of your program! If your program is in a file named ``decode.c'', submit would be run as submit 6 decode.c.

Before you submit your project, you must exactly follow the specific submission checklist in the ``Testing projects before submitting'' handout separately posted by your instructor!



7. Sample output


Assuming the name of the executable version of the program is ``decode.x'', here is a sample execution for one input data set. The input file is named ``p6.in1'', whose contents are shown as displayed by the UNIX ``cat''command. Following the data file's contents, the results of running the program with input redirected from that file are shown. The p6.in1 file will be available in your class posting account.

Be sure to test your program against a variety of inputs, so you are sure it works in all circumstances!

% cat p6.in1
KDCA Washington National, DC
KBWI Baltimore-Washington International, MD
KADW Andrews Air Force Base, MD
PADK Adak, AK
PANC Anchorage International, AK
PAFA Fairbanks International, AK
KMSP Minneapolis-St. Paul, MN  
KDLH Duluth International, MN
KPIH Pocatello, ID
####
KMSP 061453Z 24009KT 6SM HZ FEW250 14/11 A3000 RMK AO2 SLP160 T01390111 53010  
KPIH 061456Z 00000KT 10SM -RA BKN070 OVC090 05/04 A3004 RMK AO2 RAE08B50 SLP165 P0000 60000 T00500039 55007   
KADW 061255Z 32009KT 7SM FEW040 08/02 A3020 RMK WND DATA ESTMD SLP231 8/100 9/100
KDCA 061351Z 34011KT 10SM CLR 07/02 A3023 RMK AO2 SLP235 T00671022
%

% decode.x < p6.in1
==========
Summary

Conditions for DC:
Location:          Washington National
Date/Time:         11/6, 1351
Wind:              from 340 degrees at 11 KT
Visibility:        10 miles(s)
Sky conditions:    Clear
Temperature:       7 C
Dew Point:         2 C
Pressure:          30.23 in.

Conditions for ID:
Location:          Pocatello
Date/Time:         11/6, 1456
Wind:              Calm
Visibility:        10 miles(s)
Sky conditions:    Overcast at 9000 feet
Weather:           Light Rain
Temperature:       5 C
Dew Point:         4 C
Pressure:          30.04 in.

Conditions for MD:
Location:          Andrews Air Force Base
Date/Time:         11/6, 1255
Wind:              from 320 degrees at 9 KT
Visibility:        7 miles(s)
Sky conditions:    Mostly Clear at 4000 feet
Temperature:       8 C
Dew Point:         2 C
Pressure:          30.20 in.

Conditions for MN:
Location:          Minneapolis-St. Paul
Date/Time:         11/6, 1453
Wind:              from 240 degrees at 9 KT
Visibility:        6 miles(s)
Sky conditions:    Mostly Clear at 25000 feet
Weather:           Haze
Temperature:       14 C
Dew Point:         11 C
Pressure:          30.00 in.



For this project, the primary input consists of the contents of the input file given above. Note that this primary input does not exercise several conditions discussed above which your program should work for in order to earn credit for the secondary inputs. You may obtain addtional sample data (updated hourly 24 hours a day) at: United States Weather (Select a state, then select "Current weather conditions.")



Steve Scolnik
2001-11-15

Web Accessibility