Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Use this in combination with what you have already done to produce a geopoints variable consisting only of the points within 100km of your chosen location. Plot the result to confirm it.

Saving geopoints data

Geopoints variables can be saved to disk using the write() command:

Code Block
languagepy
 write('my_computed_data.gpt', points)

Converting between geopoints and GRIB

...

Have a look at the supplied Lat Long Matrix file with the edit action. This is a simple text format for storing regularly-spaced geographical matrix data, which Metview can directly import. As soon as you do anything with this file (e.g. visualise or examine), Metview internally converts it into GRIB format (leaving the original file untouched). In this way, we can import such data into Metview and have access to all its GRIB/fieldset functionality.

Reading/Writing General ASCII Data to/from Disk

XXXX Supply a text file!

ASCII files that are not in Geopoints, ASCII Table or Lat/Long Matrix format can be read using the read() function. It will return a list of strings - one string will contain the contents of one line of the file. Look at the supplied text file and see that it contains a list of codes for meteorological parameters:

Code Block
Parameters:
Z/T/U/V/RH

Create a new Macro and type the following code to read and parse this data:

Code Block
languagepy
lines = read('params.txt')
print(lines) # lines is a list of strings

params = lines[2] # take the second line; params is a string
param_list = parse(params, '/') # split the string into a list of strings
print(param_list)

There are many more string functions available.

 

Now do the reverse: write this list of parameters into another text file. The new file should look exactly like the original. Here are some hints:

  • the write() function always takes a filename as its first argument, and it can take a string as its second argument
  • it always overwrites an existing file of the same name, so there exists another function, append() which will add your string to a new line on an existing file
  • so you will need to call write() once with the first line of text, and append() once with the list of parameters
  • the list of parameters will need to be flattened out into a string with '/' as the separator - this will need to be done in a loop with a string variable initialised to '', and each element added with the & operator

Extra Work

Optimisations to file writing

The last example could be made more optimal, which could be important if dealing with large amounts of data:

  • in fact, it could be done with a single write() function with the help of the built-in global variable newline
  • if writing many many lines, there is another syntax which avoids multiple file open and close operations:
    • Code Block
      fh = file('output.txt')  # open a file handle
      for i = 1 to 100 do
          write(fh, 'Line ' & i & newline)
      end for
      fh = 0 # close the file handle