Posts mit dem Label grib werden angezeigt. Alle Posts anzeigen
Posts mit dem Label grib werden angezeigt. Alle Posts anzeigen

Donnerstag, 19. Februar 2015

WRF+MET tutorial review

So, I've spent the past 10 days in Boulder, CO, attending the WRF tutorial and the MET tutorial. WRF is the Weather Research and Forecasting model and MET is the Model Evaluation Tool, both (to some degree) developed and maintained at the National Center for Atmospheric Research (NCAR) @ Boulder, where the tutorials were held (it is somehow part of another acronym, UCAR, the University Cooperation for Atmospheric Research. I don't get the overall connection of all those acronyms - NOAA would be another one, or MMM, or NESL, or NCEP, and there are certainly more). First a general remark about the whole trip: it was certainly worth it!

Important notice: This report describes my very personal and thus totally subjective impression of the tutorials. It is by no means intended to offend anybody or anything. Just my personal view. Feel free to agree or disagree in the comments.

Part 1: the WRF (pronounce: worf, as in the Klingon original) tutorial

The instructors

There were a number of instructors for all the different aspects of running the WRF system, basically the WRF core developers and some of the support staff. In short, have a look at the authors' list of the WRF manual, these are the people who taught us.
In the following, I will try to explain both, the content of the tutorial (like the basic steps for running WRF) as well as my impression of the tutorial itself.

The build process

Ok, so WRF is an old piece of software. But why on earth hang on to some custom, home-made, weird build system including a very counter-intuitive directory structure? Why not use available build tools, like, for the more traditional software developers, autotools (automake/autoconf) or, the more modern way, cmake? There are certainly reasons for using the custom system, but they are beyond me. That being said, the build usually works more or less out-of-the-box, provided there is a reasonable number of dependencies installed (basically make, C & Fortran compilers, m4, NetCDF, mpich). After successful compilation of WRF and then WPS, there are a number of executables (easily detected by the very disturbing .exe suffix) in the different directories. Most notably, there are ungrib.exe, geogrid.exe and metgrid.exe as part of WPS, and real.exe (or ideal.exe) and wrf.exe as part of WRF itself. The explanations of the actual WRF build within the tutorial were short, but contained what was needed to build WRF (which was part of the practical sessions).

Data preprocessing

In order to use WRF for a real climate/weather simulation, it is necessary to feed in some data. That means in particular that an initial state of the whole system is needed, and on top of that some sort of boundary condition that applies to the spatial borders at as many time steps as possible. The only exception to the need of boundary conditions would be a WRF run using the whole earth as a spatial domain, in this case periodic boundary conditions kick in. However, running WRF as a global model is generally discouraged and using a completely different system is advised (MPAS). So from now on I assume that boundary conditions are given. Moreover, there is (more or less) static data required, such as topography, land use, land/sea mask, and so forth. The WRF preprocessing system WPS basically does the following:
  1. Prepare (pronounce: unpack) the data for initial and boundary conditions that come from an external source (such as GFS) and that are given as GRIB (or GRIB2) files. This is what the executable ungrib.exe does. More precisely, it converts the data from GRIB files into an intermediate file format, which is again something completely custom-made for WRF.
  2. Prepare the static data such as land-sea mask. This is what the executable geogrid.exe does.
  3. Combine the two and reproject the data blob to the projection that WRF is going to use (either Lambert conformal, Polar stereographic, Mercator or regular Lat-Lon). This is what metgrid.exe does. Note that only horizontal interpolation is performed at this stage, the vertical interpolation is part of the next step. The resulting files consist of the complete initial conditions and the boundary conditions for successive timesteps.
The output of WPS are files in NetCDF format that contain the initial and boundary conditions for the spatial and temporal domain and that can then be processed by WRF. Important configuration options for WPS include the valid times of the data source, the projection and the extent of the spatial domain. To actually run ungrib.exe, the given GRIB files need to follow a very specific naming convention, which is another weird inconvenience, but not a show-stopper (just like most of the system's little and large weirdnesses). Also, it is not entirely clear which GRIB files, and in particular which projections, are supported (there are GRIB files which contain Gaussian grids, or triangular grids). Moreover, different GRIB files require different tables that define which data is to be extracted from the files are required. Those tables are again in a very specific format, but luckily there are pre-defined tables for the most common GRIB files. The explanations in the tutorial may have been confusing for people that have never dealt with GRIB files, but for me they were fine.
The static data ships as a separate tarball with WPS and it is actually possible to enhance it by custom data (for example higher resolution data in specific areas). However, and again, the file format for this static data is something totally non-standard and GIS people will probably throw their hands up in horror. There is a number of standard formats for raster data out there and again, it is beyond me why none of them is used. But anyway, running the three preprocessing steps usually poses no big trouble and is also quite fast, as it basically consists of data conversion and (horizontal) interpolation.
A short note on the configuration options: All the options for WPS (the same is true for WRF also) are given in a file called namelist.wps, which resembles a Fortran-style configuration consisting of named options in named dictionaries. That is per se not a bad choice, however, there is one thing that strikes me as rather odd: within the actual WRF source code, all options are accessed from a central dictionary, no matter in which dictionary they were in within the namelist file. That means that despite the different dictionaries, no option name may occur twice, even if given in different dictionaries. But I may be wrong with that conclusion (and that would be a problem for people actually extending WRF by adding new options, not the standard user).
The tutorial gave a very good introduction to the WPS and how to prepare the data such that WRF can be run successfully. The exercises in the practise sessions were prepared thoroughly, with input data ready to be fed into WPS. Additionally, there was one exercise without given data or goal, but rather a do-it-your-own-way, which I really enjoyed. Feeding in the standard data (that would be GFS) is really not that hard and already gets you started.

Running WRF

Running the main WRF system is a two-step process. First, the input data as given by WPS needs to be interpolated vertically (remember, WPS only interpolates horizontally). This is a strict requirement since the WRF model uses its own notion of how vertical layers are expressed, which is quite different from standard pressure levels or height-above-ground/mean-sea-level. This is done by the program real.exe - as the name hints this is for "real" data, in contrast to idealized cases, where WPS is not required and all initial and boundary conditions are created programmatically, but this is a different story I will not go into here. So, the real.exe program creates NetCDF files called wrfinput and wrfbdy (short for boundary) containing all the necessary input data to do the real work - run wrf.exe. All the steps taken so far are not very computationally intensive, as they mainly consisted of data unpacking and interpolation. But running WRF itself tends to be more demanding, hence it should be run on more than one core. Luckily, WRF supports MPI out of the box, so no additional obstacle here, simply run wrf.exe on as many cores/machines as are available. As it turns out, MPI does not play well with NAT, so I could not find a way to run WRF in VMs/containers distributed over different physical machines (I tried this several months ago). Unless I am missing some non-obvious network configuration, it seems highly complicated or plainly impossible to do so. So the advice is: run WRF on dedicated machines, and all is well. The tutorial covered a great deal of information on what wrf.exe actually does and what the numerous configuration options do. In short, (partial) differential equations are solved by a Runge-Kutta-method, i.e. solving the time derivative by RK and resolving the spatial derivatives by finite differences. There is one noteworthy detail, which is that the RK-step is separated to account for two different time-scales (i.e. the system that has to be solved is something like a slow-fast system, although the time scales are not that far apart, maybe by a factor of 100-1000). I might have misunderstood some of the details, but the basic mechanism should be correct. The actual atmospheric model is described by a physics module which is highly customizable and takes various different options. In particular, the model for the planetary boundary layer and the cloud model are crucial, and a lot more physical models for the atmosphere that I already forgot. The tutorial covered most of the options and the physics schemes behind them, but of course there was not enough time to go into all the details. I really missed some sort of "cheat-sheet" for WRF, a short summary of all the options and a one-liner what is important at which grid sizes. Another aspect that was missing from the tutorial (although it is not strictly the scope of a general WRF tutorial) was tips and tricks for actually running WRF in a (semi-)productive way, i.e. hardware recommendations or what tools to use for the job management task.

Postprocessing

In short, postprocessing WRF data is a desaster. The WRF output consists of NetCDF files, but there are 2 problems: The data is on a "staggered" grid, meaning that most data is at located at the center of the grid cells, but winds are located at the edges (also, there is specific data located at the vertical edge instead of the center, but this is usually not really important for further processing). And the data is on WRF specific vertical levels. So there are two things that need to be done: unstagger the grid and interpolate to pressure/height levels. Unfortunately, there is not one usable way to do this in a clean and sane manner. Instead, there are a number of postprocessing tools, none of which works straight out-of-the-box. The tool that I've finally managed to run in the practice session is called UPP (universal post processor), it generates an obscene amount of support files and requires an insane configuration, but eventually it can generate standard GRIB(1) files. Which is all I need. Somehow also the NCAR script language NCL (which is also capable of doing graphics) shoud be able to do this, but I couldn't get that far. At least I did manage to use cdo (yes, I am very much in favour for using standard and specialized software for each job) for unstaggering the wind data, but the vertical interpolation is still an open problem, but it might be possible with cdo, which would take away much of the pain of WRF postprocessing. Again, the reasons behind the decision not to support one single (and simple) post-processing tool to convert the WRF output to some usable data format (or even better, provide a working cdo configuration) and let the user decide what software to use from that point onwards, but to pseudo-support a number of more or less unusable and cryptic software is beyond me.

Part 2: The MET tutorial

Well, most participants actually attended because they were in Boulder for the WRF tutorial and decided to stay for two additional days. For me it was quite the opposite. Currently I am in the process of building a large model evaluation system and I was curious what exactly the NCAR Model Evaluation Tool does (and what model evaluation is all about). The reason is simple: I don't have much (any) experience in model evaluation and maybe I will include MET as an option in my own system. But after attending the tutorial I have to admit that this is not very likely, it simply does not meet my demands for performance, features and extendability.

Preprocessing the data

Depending on the source data there needs to be some preprocessing. For point observational data, the source may be one of the more or less obscure PrepBUFR, MADIS or a simple ASCII format and is transformed into a NetCDF file. Gridded data does not need special pre-processing, it can be processed directly as GRIB1 or GRIB2 or CF-compliant NetCDF. Of course, if verifying gridded data versus gridded data, both data sets need to be in the same projection (the developers are thinking about including reprojection into MET in the next version, which is, in my opinion, a big mistake and completely unnecessary, since there are standard tools that can handle the reprojection beforehand). Moreover, there is some custom tool to define a "polygon" (list of lon/lat pairs, not a polygon in the GIS sense) that can be used as a mask to the gridded data. Again, why there is a custom tool for defining a custom "polygon" definition instead of using, say, a well-known text (WKT) definition of standard GIS objects is beyond me.

Statistics

The main output of the MET system is statistics of the data, i.e. first and second order statistics, skill scores and confidence intervals. The tutorial covered a good portion of the background for this, but of course it was no replacement for a statistics 101 class. MET also provides tools for combining the statistics of multiple MET runs, which allows for processing of large data sets in chunks.

Advanced grid evaluation

The most interesting topic for me was the grid-versus-grid evaluation, which can be used to compare two different model runs or to verify model data using gridded observational data (e.g. radar or satellite). It appears that this type of comparison is a rather new topic, and unfortunately the most interesting methods present in MET where hardly explained (wavelet-based methods, optical flow-based methods). Another approach was explained in a bit more detail, the object based evaluation (called MODE in MET-speak). But also this explanation was not really satisfying, as it really is only well-known methods from image processing, which are inconsistently renamed. Actually, this was the one occasion where I lost my good mood during the tutorial, because re-implementing stuff and inventing a whole new nomenclature for well-known methods (segmentation, classification, tracking) is simply evil, scientifically speaking. At least I had the opportunity to speak to one of the experts for spatial verification, and he could point me to a list of resources to read through, which already helps a lot.

Conclusion

As I've already mentioned, the trip was certainly worth it. The WRF tutorial was really good, most lectures were clear and actually entertaining, the practise sessions were helpful and in the end I felt like I was able to understand and run a WRF system. Moreover, now I really want to run WRF myself, which is probably the biggest compliment for the instructors.
On the other hand, there was the MET tutorial, which was interesting in its own respect. But I have to admit that I was expecting a bit more, but at least the tutorial gave me some insights on model evaluation basics, and now I know that I am following the right track with my own evaluation system.
Probably I forgot to write about a lot of things, but I would like to mention one more aspect of the tutorial: the participants. For the WRF tutorial there were roughly 50 people, maybe half (or two-thirds) from academia and the rest from industry, and from all over the world. The people I met there were all very nice, and I think we all had a really good time in Boulder!

Montag, 24. November 2014

Regridding spatial data

This post is about one of the most difficult tasks in processing spatial data, in particular gridded data: regridding. Regridding is all about "warping" a given data blob into a different grid. First of all, it is tremendously helpful to really know what grid the original data is in. Here are a few examples:
Regular lon/lat grid:
Given the corner coordinate as a longitude/latitude pair and the per-data-pixel offset in degrees, this is one of the easiest format for gridded spatial data. In terms of projections, this can be expressed as a standard epsg:4326 projection (disregarding subtile differences in the meaning of longitude and latitude depending on the used "datum", i.e. the used model of the earth), meaning that all longitude/latitude coordinate pairs are simply "flattened" to the plane. Dealing with this kind of data is usually pleasant, gdal and other spatial libraries fully support this, no big deal. Except, the notion of longitude/latitude in commonly used data files in the data format "grib" (which is often used in numerical weather models) is slightly (usually neglectible) different from the standard WGS84 datum.
Rotated lon/lat grid:
The regular lon/lat grid has (at least) one major shortcoming: Considering the whole globe, it is far too detailed near the poles and too coarse at the equator. The reason is simply that the number of grid points per latitude, starting from the north pole going south at constant latitude steps, is constant. So if the region of interest is a small area it might be a considerable improvement to the accuracy if the pole is "virtually" rotated to that area, such that the highest accuracy is obtained for this ROI. Although this rotation does not pose a huge problem computationally, not all libraries can properly deal with data files that contain data in this form (again, "grib" supports this). Also, it is usually a bit more tricky to formulate a simple projection definition for this kind of data.
Gaussian grid:
The Gaussian grid basically uses non-evenly spaced latitude values in combination with evenly spaced longitude values (per one latitude the longitude values are evently spaced, but the number of longitude values may be different at different latitudes). Again, this grid type is not terribly complicated, but it is also not commonly supported (no easy projection definition). Luckily, the errors introduced if treating a Gaussian grid like a regular grid are usually neglectible (and mostly close to the poles anyway).
Triangular/hexagonal grid:
This is probably the most complicated (apart from completely irregular grids) grid. There is no projection definition, no support in most libraries, and dealing with this kind of grid usually boils down to handle it just like a complete irregular grid, meaning the need to extract the coordinates of the cells (usually: the center).

So, in order to use the data, some kind of transformation is necessary. That can be either the knowledge of the coordinates (in lon/lat) for each point, or the warping of the whole data blob into some well-known grid. For simple grids, it is as easy as using the standard gdal tool gdalwarp. For more complicated grids however, this is not enough. One solution that seems to work for transforming many kinds of grids back and forth is "cdo", which is also able to interpolate data from one grid to another. It appears that "cdo" is not very widely known, but if grid transformations is an issue, it is definitely worth a try!

Update:
Although cdo is indeed capable of dealing with/regridding triangular or unstructured grids, unfortunately it has some issues when the input is a grib file (issues = segmentation faults). So the "easy" solution is to convert the grib file to a different file format (this, for some reason, is possible with cdo), such as NetCDF. Again, there is a "but": gribs with triangular meshes cannot be translated to NetCDF directly. Instead, the grib data needs to be transferred to a different format, along with a manual definition of the grid. The result can then be converted to NetCDF and finally warped to a different grid.

Update #2:
Luckily, cdo is under active development and the bug using grib files has been fixed (will be in version 1.6.7)!

Donnerstag, 9. Oktober 2014

PostGIS raster and out-db storage

The following is a story about data management, terminology, GIS and frustration.
The story starts with weather data, which is what I deal with most of my working hours. And dealing with weather and climate data is far from trivial. Not only are there large amounts of data (and boy, there are!) which one has to handle, it is usually good to understand how that data was produced and for which location in space and time it applies. The former is not strictly necessary, the latter is essential, which leads me to GIS. GIS means geo information system and is an acronym used for pretty much anything that in one way or the other relates to maps and/or positions on earth (or, for that matter, positions on or maps of any sphere-like shaped body). The fact that the earth is not an exact sphere and the myriads of different ways to express that fact makes dealing with GIS hard. Also, all the different ways of flattening the surface in order to create a 2d representation (i.e. a map) are overwhelming. That is why there is software to make life a little easier, software, that manages to convert from different types of maps back and forth, and to generally do stuff with the spatial data.
Luckily, there is a number of free and open source software packages for GIS, be it desktop applications such as QGIS or GRASS, libraries such as proj.4 or GDAL, or things as complex as an extension to an SQL database to allow spatial data in the context of the relational database (i.e. PostGIS). And, as the title of this post suggests, PostGIS is what I am writing about. The first thing to note is that, generally speaking, PostGIS is a spatial enabled database, i.e. a database that allows to store and manipulate spatial data. Typically, in GIS two main types of spatial data are considered: shapes (or: vector-based, as in computer graphics) data and rasters (or: bitmap-based, as in computer graphics). PostGIS can deal with both types, and it does handle it pretty damn good. For example, you can tell PostGIS to get raster data out of a specific raster at a given geographic position (say, you have a raster with temperature data and you would like to know the temperature at a given longitude/latitude). The good news is that dealing with GIS data is pretty much standardized, there is even a standard for handling GIS data with SQL (and PostGIS follows this standard). And, there are open standards for file formats, which takes away much of the pain that dealing with different competing (or proprietary) standards generates in other areas.
Now let's talk a bit about the actual data. One of the commonly used weather models is called GFS (global forecast system), it is a global model for a wide variety of parameters (temperature, pressure, wind speed, just to name a few), usually more than 300 parameters are computed. The file format in which this data is distributed is called grib, a name that probably should give a hint towards gridded binary data. Despite the fact that this data format is awfully complicated and requires a lot of additional information (so-called tables) about the dataset, which may or may not be available, it is widely used in meteorology. For the GFS, it is indeed not a bad choice, as it supports JPEG2000 compression, which really makes a difference in file size. So, to state a few numbers, one computed time-step of the highest accuracy GFS (i.e. 0.5 degrees horizontal and vertical resolution) with all of its hundreds of parameters needs about 50MB. Note that this is only one time step, i.e. the complete forecast for 8 days in 3-hour steps takes 64 times that amount. Since the GFS is updated four times a day, the complete dataset of one day amounts to about 10GB. This is quite a large amount.
Back to the data handling with PostGIS. PostGIS supports importing raster data from files, so in theory it is indeed possible to simply stick the complete grib file into the database and go on with the really important work. However, in this case, the data compression of the grib files is lost, which increases the required memory by a factor of 5 - 10 (!). Not to mention that the import takes an awkward amount of time. Luckily, there is a simple solution to that problem: PostGIS is able to import raster data without the actual data, by simply referencing the original file instead of importing it. Splendid! However, there is one small drawback in the whole raster-data-into-PostGIS loop: the data format in which the actual data is stored inside PostGIS is not documented, despite the fact that it is called Well-Known-Binary (WKB) format. After some googling the reader may object that there is a well documented standard for that file format. Indeed there is, but it only refers to shape data, not to raster data! I think that the WKB for rasters was created by the PostGIS/raster creators. But since it is pretty much the opposite of well-known, we'll probably never know.
Let's get back to the data for a moment. The grib files that hold the GFS data usually contain the complete parameter set for one time step, meaning it is not really one single raster, but merely a collection of rasters. One raster per parameter, or, to express that in the terminology of GDAL, which is the library that PostGIS uses to handle raster data, one raster band within the raster file per parameter. So, the data storage solution seems obvious: store the raster as references (called out-db raster) in PostGIS! This allows to keep only one copy of the original files, it allows to do all the nice things that PostGIS is designed to do transparently with the data which is actually stored in the grib files, so PostGIS handles the data access and everything is beautiful. Right? Wrong. Taking a very close look at what PostGIS actually stores for out-db rasters (thanks open source!), it appears that the WKB format only allows one byte to store the band number of the data file that is referenced. In other words, PostGIS actually stores some metadata of the raster and the pairs (referenced band number, referenced file name), but the referenced band number is only allowed numbers between 0 and 255 (GDAL starts counting raster bands from one, so that translates to band numbers between 1 and 256). So for band numbers 1-256 that works fine. But larger band numbers cannot be stored in one byte, and even worse, there is an overflow, i.e. it is not only impossible to access band 257 of the original grib file, instead band number 1 is accessed.
Why is that bad? Because there instead of a meaningful error message (or, even better, the correct band), PostGIS happily returns the wrong values when requesting a band that cannot be accessed. If I had not looked into the source code, I would not have spotted this shortcoming, so at some point the data would have been all wrong. And that is bad. To be fair, now that I know what to look for, I found the corresponding issue in the PostGIS raster bug tracker and there is a fix promised for version 2.2, but that is not available yet. And the really annoying thing is: there are only a very inconvenient work-arounds for this bug, namely to split the grib file into smaller grib files containing less than 256 bands or, of course, to import the data itself into the database and live with the 10-fold increase in required disk-memory.