[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ldm pqcat



Roland et al,

I created a perl filter "hds2hds" to modify the headers and trailers from
NOAAport HDS as specified in the previous e-mail. There is a section in
the script where extra modifications can be inserted as the NOAAport
products change their format. "hds2hds" needs to be made executable and 
the location of perl should be checked for correctness in the first
line of the script. "hds2hds" script uses a c program "crc_alden" to
create the checksums.  To compile  "crc_alden" :

- put crc_alden.c in the pqing directory of the distribution so it can
find the crc_tab.h file and compile.

% cc -o crc_alden crc_alden.c 

Both hds2hds and crc_alden.c are attached to this message.


To run the pqcat with the filter, notice the pqcat -a flag is omitted:

pqcat -o 0 -i 15 -f HRS -s /home/ldm/etc/hds.sw | hds2hds /dev/term/i1c

Robb...

On Thu, 2 Sep 1999, Robb Kambic wrote:

> Roland,
> 
> In reference to the pqcat problem:
> 
> After much investigation, it was discovered that the extra header and
> trailer characters were part of the NOAAport HDS products. The extra
> characters are within the WMO header specification. So instead of kuldging
> the ldm with special code to remove these characters, UPC plans on
> creating a filter that will take the output from pqcat and eliminate these
> characters. This is a much cleaner solution because the NOAAport feed will
> probably change again, different set of headers or trailing characters.
> Also, the filter will be a small perl script that can maintained by alden
> as the need arises.
> 
> Robb...
> 
> ===============================================================================
> Robb Kambic                              Unidata Program Center
> Software Engineer III                    Univ. Corp for Atmospheric Research
> address@hidden                   WWW: http://www.unidata.ucar.edu/
> ===============================================================================
> 
> 
> 

===============================================================================
Robb Kambic                                Unidata Program Center
Software Engineer III                      Univ. Corp for Atmospheric Research
address@hidden             WWW: http://www.unidata.ucar.edu/
===============================================================================
#!/usr/local/bin/perl
#
#  Program to modify headers and trailers of NOAAport HDS to look like the 
#  old FOS format.  It also appends the NWS checksum.
#
#  usage: 
#  % pqcat -o 0 -i 15 -f HRS -s ./hds.sw | hds2hds outputFile        
#
#  written by Robb Kambic for alden   9/10/99
#
# process command line switches, no valid switches at this time
while ($_ = $ARGV[0], /^-/) {
         shift;
       last if /^--$/;
             /^(-v)/ && $verbose++;
}
# process input parameters
if( $#ARGV == 0 ) {
        open( STDOUT, ">$ARGV[ 0 ]" ) || die "could not open $ARGV[ 0 ] $!\n" ;
}
select( STDOUT ) ; $| = 1 ;

# set interrupt handler
$SIG{ 'INT' }  = 'atexit' ;
$SIG{ 'KILL' }  = 'atexit' ;
$SIG{ 'TERM' }  = 'atexit' ;
$SIG{ 'QUIT' }  = 'atexit' ;

# Now begin parsing stream and breaking on \r\r\n\cC
$/ = "\r\r\n\cC" ;
# main loop   set select processing here from STDIN
START:
while( 1 ) {
        open( STDIN, '-' ) ;
        vec($rin,fileno(STDIN),1) = 1;
        $timeout = 1800 ; # 30 minutes
        $nfound = select( $rout = $rin, undef, undef, $timeout );
        # timed out
        if( ! $nfound ) {
                print STDERR "Shut down, time out 30 minutes\n" ;
                atexit() ;
        }
        atexit( "eof" ) if( eof( STDIN ) ) ;

        $_ = <STDIN> ; # get product
        #
        # Check and modify NOAAport headers and trailers
        #
        s#^(\cA\r\r\n\d\d\d)\s#\1# ; 
        s#7777\cC\0\r\r\n\cC$#7777\r\r\n\r\r\n\cC# ;
        #
        print $_ ; # write product
        # write product to tmp file hdsprod
        open( OUT, ">hdsprod" ) || die "cannot write hdsprod: $!" ;
        print OUT $_ ;
        close( OUT ) ;
        $prodlen =  length( $_ ) ;
        #print STDERR "hds2hds prod length =$prodlen\n\n" ;
        #
        # Create and write checksum
        $checksum = `crc_alden $prodlen < hdsprod` ;
        print $checksum ;
        #
        atexit( "eof" ) if( eof( STDIN ) ) ;
} # end while( 1 )
atexit( "eof" );
exit( 0 ) ; #should never get here

# execute at exit
sub atexit
{
local( $sig ) = @_ ;

if( $sig eq "eof" ) {
        print STDERR "eof on STDIN -- shutting down\n" ;
} elsif( defined( $sig )) {
        print STDERR "Caught SIG$sig -- shutting down\n" ;
}
close( STDOUT ) ;
close( STDERR ) ;
exit( 0 ) ;

}
/*
 *   Copyright 1993, University Corporation for Atmospheric Research
 *   See ../COPYRIGHT file for copying and redistribution conditions.
 *
 * crc_alden
 * 
 *  Used to create alden (NWS) type check sum, extracted from pqcat code.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "crc_tab.h"

static unsigned short
add_crc(unsigned short crc, unsigned char ch)
{
        return crc_table[(crc & 0x00ff) ^ ch] ^ (crc >> 8);
}

/*
 */
/*ARGSUSED*/
int 
main(int ac, char *av[])
{

                const char *progname = av[0];
                const int size = (int) atoi(av[1]);
                unsigned short crc = 0;
                void *datap = (void *) malloc(size +1);

                if(read(STDIN_FILENO, datap, size) != size)
                {
                        int errnum = errno;
                        fprintf(stderr, "%s\n", "crc read failed") ;
                        return errnum;
                }
/*
                fprintf(stderr, "%s\n", datap);
                fprintf(stderr, "crc_alden product size =%d\n", size );
*/
                
                {
                const unsigned char *cp = (unsigned char *)datap;
                const unsigned char *end = &cp[size];
                for(; cp < end; cp++)
                        crc = add_crc(crc, *cp);
                }
                {
                unsigned char crc_bytes[2];
                crc_bytes[0] = crc & 0x00ff; /* low */
                crc_bytes[1] = crc >> 8; /* high */
                if(write(STDOUT_FILENO, crc_bytes, 2) != 2)
                {
                        int errnum = errno;
                        fprintf(stderr, "%s\n", "crc write failed") ;
                        return errnum;
                }
                fflush( stdout );
                }
        return 0;
}