#!/usr/bin/perl

#uncompress and incoming NIDS stream to from NOAAPORT and write out NIDS file
#
# By Dale Morris
# Oklahoma Climate Survey
# 1/24/2001
#
# incoming NIDS files are compressed using zlib compression.  This script is based
# on an LDM decoder by Pete Pokrandt (poker@meteor.wisc.edu).
#
# It uses the Perl interface to Zlib.
#
#
# Output files are named according to the radar and Product in the AWIPS header +
# the volume scan times in the NIDS product.
#


#set up preliminaries

use Compress::Zlib;
$input = '';
binmode STDIN;
binmode STDOUT;

#set up product-name translation table (hash) for AWIPS headers.

$Product{"N0R"}="BREF1";
$Product{"N1R"}="BREF2";
$Product{"N2R"}="BREF3";
$Product{"N3R"}="BREF4";
$Product{"N0Z"}="BREF248";
$Product{"N0V"}="BVEL1";
$Product{"N1V"}="BVEL2";
$Product{"NCR"}="CREF";
$Product{"NVL"}="VIL";
$Product{"N0S"}="SRVEL1";
$Product{"N1S"}="SRVEL2";
$Product{"N2S"}="SRVEL3";
$Product{"RCM"}="RCM";
$Product{"NTP"}="RAINS";
$Product{"DPA"}="DPA";
$Product{"N1P"}="RAIN1";
$Product{"N6P"}="RAIN6";
$Product{"NUP"}="RAIN24";
$Product{"DHR"}="HYBRID";
$Product{"NVW"}="VAD";
$Product{"NET"}="ECHO";
$Product{"FTM"}="TEXT";

#read in the NIDS file

read (STDIN,$input,1000000);

#strip off (uncompressed) WMO header

$WMO = substr($input,0,35);

#bailout if product is DPA or RCM

$temp= substr($WMO,26,3);


if ($temp eq "DPA")
   {exit;}

if ($temp eq "RCM")
   {exit}


$tempin = substr($input,35);



#uncompress first block of data

	$input = $tempin;

	$x = inflateInit() or die "Cannot create inflation stream\n";
	($output,$status)=$x->inflate(\$input);

#strip off and save (compressed) WMO & AWIPS headers

	$header = substr($output,0,54);
	$stripped = substr($output,54);

#fetch product description block so we can correctly name the file

       $ProdDesc=substr($stripped,18,88);
       ($junk,$lat,$lon,$ht,$code,$mode,$vcp,$seq,$scannum,$VSDate,$VSTime,$ProdDate,$ProdTime) = unpack("nNNnnnnnnnNnN",$ProdDesc);

       $mtime = ($VSDate -1)*86400 + $VSTime;

# get time for the filename
       
       ($m_sec,$m_min,$m_hour,$m_day,$m_mon,$m_year, $m_dow,$m_doy,$junk) = gmtime($mtime);

       $m_mon++;


#Make sure values have leading "0"

       $m_sec = prepend($m_sec);
       $m_min = prepend($m_min);
       $m_hour = prepend($m_hour);
       $m_day = prepend($m_day);
       $m_mon = prepend($m_mon);
       $m_year = prepend($m_year);
       $m_year = $m_year + 1900;

       $filename=$m_year.$m_mon.$m_day.$m_hour.$m_min;


# get the product and radar from the AWIPS header;

       $AWIPS=substr($header,45,6);

       $AWIPSProd=substr($AWIPS,0,3);
       $radar=substr($AWIPS,3,3);

       $myProd=$Product{$AWIPSProd};

# construct filename in format YYYYMMDDHHNNNN_RRR.nids where NNNN is the nexrad product and RRR is the radar.


       $filename = $filename . $myProd."_".$radar.".nids";
       

       open(OUTFILE,">$filename");

	
	print OUTFILE $stripped;

	if ($status < 0 or $status > 2)
	   { die ("not a zlib compressed file");
	   }
	   

# uncompress remaining blocks of the file

   while (length($input) > 0)
   {


       $x = inflateInit() or die "Cannot create an inflation stream\n";

       ($output,$status)=$x->inflate(\$input);
       print OUTFILE $output;

       if ($status < 0 or $status > 2){
       close OUTFILE;
       exit;

      }
   }



sub prepend {

if ($_[0] < 10) {
    $string = "0" . $_[0];
     }
else  
    {
    $string = $_[0];
    }
return $string;
  }


