[GRASSLIST:2331] NEXRAD radar stage III precipitation estimates into GRASS 5.x
Thomas Adams
Thomas.Adams at noaa.gov
Thu Jan 22 13:20:15 EST 2004
Sorry for the delay getting this out. Attached are 4 files:
(1) xmrg2ascii.c - C source code to convert an 'XMRG' file into an ASCII
file of point values
(2) sumxmrg.pl - a Perl script that will sum the hourly XMRG data into a
user determined number of HOURS - that is, the point values will be
summed across files
(3) xmrg0328200019z - an example XMRG (binary) file created on a BIG
ENDIAN (non-Intel) computer, so you may need to do some byte swapping
(modification of the code) on an Intel based Linux machine
(4) xmrg0328200019z.asc.gz - ASCII output from (3)
A few notes:
- to compile xmrg2ascii.c: gcc -o xmrg2ascii xmrg2ascii.c -lm (need
the Math library)
- the data in the XMRG files are stored in hundreths of mm and the
default is to convert to inches, so change the following line in the
source as needed:
fprintf(stdout,"%9.4f %7.4f %.2f\n",-ll.x,ll.y,(double)zval / 2540.0);
- you'll need to either modify this C source or write a script to write
the output to the GRASS site file format, which is pretty
straightforward to do
For more information regarding 'XMRG' file format and the means by which
the data is created go to http://www.weather.gov/oh/hrl/pps/pps.htm
Regards,
Tom
--
Thomas E Adams
National Weather Service
Ohio River Forecast Center
1901 South State Route 134
Wilmington, OH 45177
EMAIL: thomas.adams at noaa.gov
VOICE: 937-383-0528
FAX: 937-383-0033
-------------- next part --------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct
{
double x;
double y;
} HRAP;
HRAP HrapToLatLong(double,double);
/* This utility program was written by Mark J. Fenbers at OHRFC in the late
Winter of 1997. It is one that reads the XMRG format (as it is conventionally
called), first utilized by Stage 3 software from OH, and determines the
lat/lon coordinates for each grid point and writes these out along with the
grid point's value. In other words, it is an XMRG to ASCII reformatter for
use by other programs. Output grid values are converted from 100ths of mm to
hundredths of inches. */
int main(int argc,char **argv)
{
FILE *fpi;
int fourbytes,hrapi,hrapj,numi,numj,i,j;
char userid[11],dttm[21],process[9],leftover[120];
unsigned short int zval;
HRAP ll;
/* If no command-line arguments are supplied, use "standard input,"
otherwise, attempt to open file given as command-line arg. Output is sent
to "standard output." */
if(argc < 2)
fpi = stdin;
else
if((fpi = fopen(argv[1],"rb")) == NULL)
perror(argv[1]),exit(-1);
/* The XMRG file format is a FORTRAN "free-format." FORTRAN (using free
format) encapsulates each data record with a four-byte integer giving the
number of bytes in the data record. These integers before and after each
data record can be ignored in C programs (after being read in).
"fourbytes" is a dummy variable to read in this wrapper from the record. */
/* Read in starting HRAP coordinate and number of rows/cols */
fread(&fourbytes,1,4,fpi);
fread(&hrapi,1,4,fpi);
fread(&hrapj,1,4,fpi);
fread(&numi,1,4,fpi);
fread(&numj,1,4,fpi);
if(fourbytes - 16 > 0)
fread(leftover,1,fourbytes - 16, fpi);
fread(&fourbytes,1,4,fpi);
fprintf(stderr,"hrap-i,j = %d,%d nx,ny = %d,%d\n",hrapi,hrapj,numi,numj);
/* Read in creation information (user, date/time, generation process) */
fread(&fourbytes,1,4,fpi);
fread(userid,1,10,fpi);
fread(dttm,1,20,fpi);
fread(process,1,8,fpi);
if(fourbytes - 42 > 0)
fread(leftover,1,fourbytes - 42, fpi);
fread(&fourbytes,1,4,fpi);
fprintf(stderr,"userid = \"%s\"\ndate/time = \"%s\"\nprocess = \"%s\"\n",userid,dttm,process);
/* Loop to read/process/write each grid point */
for(j = 0; j < numj; j++)
{
fread(&fourbytes,1,4,fpi);
for(i = 0; i < numi; i++)
{
fread(&zval,1,2,fpi);
if(zval & 0x8000)
continue;
ll = HrapToLatLong((double)(hrapi + i),(double)(hrapj + j));
fprintf(stdout,"%9.4f %7.4f %.2f\n",-ll.x,ll.y,(double)zval / 2540.0);
/* fprintf(stdout,"%9.4f %7.4f %x\n",-ll.x,ll.y,zval); */
}
fread(&fourbytes,1,4,fpi);
}
fclose(fpi);
return(0);
}
/**************************************************************************/
/* HrapToLatLong: converts from HRAP coordinates to latitude-longitude */
/***************************************************************************
Function type:
HRAP structure
Called by function:
main
Functions called:
none
Global variables:
HrapToLatLong - function
LatLongToHrap - function
Local variables:
tlat - float; standard latitude in radians
earthr - float; earth's radius (km)
xmesh - float; mesh length at 60 deg North
raddeg - float; conversion from radians to degrees
stlon - float; standard longitude
ll - HRAP structure; latitude/longitude
******************************************** BEGIN HrapToLatLong ***********/
HRAP HrapToLatLong(double hrap_col, double hrap_row)
{
double x, y, rr, gi, ang;
double tlat, earthr, xmesh, raddeg, stlon;
HRAP ll;
earthr = 6371.2;
stlon = 105.0;
raddeg = 57.29577951;
xmesh = 4.7625;
tlat = 60.0 / raddeg;
x = hrap_col - 401.0;
y = hrap_row - 1601.0;
rr = x * x + y * y;
gi = ((earthr * (1.0 + sin(tlat))) / xmesh);
gi *= gi;
ll.y = asin((gi - rr) / (gi + rr)) * raddeg;
ang = atan2(y,x) * raddeg;
if(ang < 0.0)
ang += 360.0;
ll.x = 270.0 + stlon - ang;
if(ll.x < 0.0)
ll.x += 360.0;
else if(ll.x > 360.0)
ll.x -= 360.0;
return ll;
}
/********************************************* END HrapToLatLong ***********/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sumxmrg.722DEFANGED-pl
Type: application/octet-stream
Size: 4460 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-user/attachments/20040122/f45e6b89/sumxmrg.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xmrg0328200019z
Type: application/octet-stream
Size: 93628 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-user/attachments/20040122/f45e6b89/xmrg0328200019z.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xmrg0328200019z.asc.gz
Type: application/x-gzip
Size: 311154 bytes
Desc: not available
Url : http://lists.osgeo.org/pipermail/grass-user/attachments/20040122/f45e6b89/xmrg0328200019z.asc.gz
More information about the grass-user
mailing list