Saturday, January 23, 2016

Weather Maps to eCharts—Making the Header File

In a recent note I outlined how to make a BSB compatible echart from a graphic image of a weather map using a utility called imgkap.  The key to that process is the header file that tells imgkap how to process the image into a .kap file that reads in an echart program.

Our goal is to provide a set of these header files covering each type of weather map so the process can be quick and painless.  On the other hand, if you do want to get a little pain into the process you can see below how these header files are being made. There might even be other reasons, as something like this is required to georeference any image you  might want to use for live navigation. My main goal for writing this up is to document the process if we need it later. Some of the steps took a while to figure out, and would be easy to forget.

Several details of this process discussed here are illustrated in the video below. Later we will add the most important article and video, namely what to do with these georeferenced weather maps once you have them.

We will use a Surface Analysis map for this example.  Here are the steps (you might skip to the bottom for a moment to see the header file we are creating).

Step 1.  Download the image. The maps we are working with now are the new (as of last 2105) color format from OPC. Some are in the png format, others are gifs. The gifs must be converted to pngs, which can be done with most graphics programs, including the stock Paint program in Windows or Preview on a Mac. Just load the gif and save or export as png.

(Later we address the black and white tiff files of the same maps. These have a few special issues to address, including rectangular pixels! )

Step 2.  Read image dimensions (w x h), i.e. 1332 x 1600
In the header file, this is then RA=1332,1600

Step 3. Figure DX, DY = meters per pixel (m/px):
One approach is choose a latitude near the center of the chart, and measure the height of a rectangle some degrees on either side. For example,  if 40N is in the middle, measure the pixels between 30N and 50N, i.e. 587 px. We are going to call the Lat where this is valid PP=40.

Then figure m/px from: 587px = 20º = 1200 nmi x 1852m/nmi = 2222400 m, so there are 3786.0 m/px.   So DX=3786.0,DY=3786.0.

Aside: We have the option for different DX and DY values in the header. All NOAA Mercator charts have square pixels and these are the same for wx maps in png format. (Not sure yet what we will need to do here for the tifs).

Step 4.  We next figure a chart scale, which is needed for the header. The concept of chart scale is transparent for RNC, which are copies of printed charts that do indeed have a scale, i.e. 1” on a 1:80,000 printed paper chart is 80,000 inches on the land. This is not so clear a concept for these weather map files that do not have a standard print size. But we do have to input a dpi for the image called DU, so once we choose that along with the DX,DY values we are committed to a scale.

The png or gif files when downloaded are likely to be at 72 dpi when downloaded on a Mac or 96 dpi when downloaded to a PC. So the 587 px we measured for 1200 nmi, would (using a Mac) correspond to 587/72 = 8.15 inches, which is = 1200 nmi x 1852m/nmi x 100cm/m x 1 inch/2.54cm  = 87496063 inches, so the scale is 1: 87496063 / 8.15 = 10735713 = SC in the header.

You can do the above for whatever dpi the image comes with. It will not matter  so long as we are consistent as outlined above. The dpi value is not a property of the image. It is only related to printing, which we are not doing. What we are doing is fudging a format that was intended to represent a paper chart that had known and fixed dimensions. The announcement a few years ago that NOAA was expanding all of their RNC from 250 dpi to 400 dpi was just a way to say the file dimensions (w x h) are going to be bigger.

Step 5.  Now we have to figure the Lat, Lon of the corners, which involves extrapolation of the scales to project them out to the edges of the paper. (We could crop the images to the scales shown, but that would involve an extra graphics step on each conversion. Doing it this way, we  have no graphics work at all once the header is in hand describing the full image.)

In this surface analysis image the Lon meridians line up nicely with the edges of the images at -175.0 to the west and -115.00 to the east. Note West Lon and South Lat are negative in this system, which is common.  The top of the chart is 9 px above 65 N, and the width of 64N to 65N is 52 px, so the top edge Lat is 65 + 9/52 = 0.173, so top Lat = 65.173.

The bottom edge of the image is 37 px below Lat 16N, and the width of 16N to 17N is 23px, so the bottom of the image is at a lat of 37/23 = 1.609º below 16N, which is 14.391N.

Measuring the pixel width of the bottom margin (37 px) with the selection tool in Mac Preview (left) and Windows Paint (right). Paint does not show the bottom most line of pixels, so it adjusted up one. Step 1 in both cases is be sure the view is set to "Actual Pixels" or "Normal View." This measurement is more precise with a more sophisticated program such as Photoshop or the open source Gimp, with which you can zoom in to close view and still read the actual pixel count.
Step 6.  Now we can specify both the georeference points and the chart corners, which we have simplified here by making them the same 4 points.  The reference points (called REF/1, REF/2,…) can go in any order and be any number larger than 3, but the 4 corners (PLY/1, PLY/2,…) must go in order, starting at the SW corner and proceeding clockwise.  (Actual NOAA RNC include a reference point for all major lights on the chart!)

Step 7.  Now we can construct the header for this chart, using the standard minimum information as determined above. We will call it EPacSurfaceAnalysis. Pixel locations are referenced to the top left, with X increasing to the right and Y increasing toward the bottom.

! East Pacific surface analysis from OPC.
REF/1, 1,1600, 14.391,-175.0
REF/2, 1,1,65.173,-175.0
REF/3, 1332,1,65.173,-115.0
REF/4, 1332,1600,14.391,-115.0

If we name the image EPacSurfaceAnalysis.png, then we can copy this text and paste into a txt file and save it as EPacSurfaceAnalysis_header.kap, and now we are ready to make our map.

This is clearly a bit tedious, but we do not have to do this again. Once this header is made and tested we can use it as is for all East Pacific Surface Analysis maps in the future. The earlier note presented one that works with the EPac 24h surface forecast.  Then getting a map into the echart program will be just download the file to the right place, run imgkap, and open the map.  In fact, you can just copy and paste this text into a cmd line to get your map:  imgkap EPacSurfaceAnalysis.png EPacSurfaceAnalysis_header.kap EPacSurfaceAnalysis.kap


To learn more about the kap header file see this short description:

To get the primary information about the BSB format go to this link at the International Hydrographic Organization.

No comments: