#!/usr/local/bin/perl 
#########################################################################
#                               ISTP SOFTWARE
#                      Property of the U.S. Government
#                            NASA/GSFC/Code 560
#########################################################################
#
# UNIT NAME:  lzp_rename
#
# PURPOSE:  Determines the proper filename, according to ISTP LZP
#           conventions, given a raw data block file as input.
#           The proper filename is constructed as a function of
#           both raw data characteristics, as well as the original
#           raw data block filename.
#
# INVOCATION METHOD:  lzp_rename [flags] [[filename] ... ]
#
# ARGUMENT LIST:
# NAME          TYPE                USE  DESCRIPTION
# ------------  ------------------  ---  -------------------------------
# flags         string               I   -d = debug without modifying
#                                        -h = display help screen
#                                        -v = enable verbose mode
#
# filename      string               I   list of raw data block files
#                                        to be processed, may contain
#                                        wildcards.
#
# EXTERNAL FILE/RECORD REFERENCES:
# /usr/local/bin/perl shell script interpreter
#
# EXTERNAL VARIABLES:
# none
#
# EXTERNAL UNIT/MACRO/PROCEDURE/FUNCTION REFERENCES:
# none
#
# ABNORMAL TERMINATION CONDITIONS, ERROR MESSAGES:
# Quietly skips files which don't contain enough raw data
# to be processed in a meaningful way.
#
# ASSUMPTIONS, CONSTRAINTS, RESTRICTIONS:
# stdout is only for output file names
# stderr is only for messages
#
# CHANGE HISTORY:
# AUTHOR           CHANGE-ID  RELEASE     DATE      DESCRIPTION OF CHANGE
# ---------------  ---------  ----------  --------  ---------------------
# K. Hogie         ISTP       R1.0        06/11/97  Original code
# R. Wiechert      ISTP       R1.0        08/19/97  Conform to existing PDF
#                                                    naming conventions.
# K. Hogie         ISTP       R1.0        08/21/97  Handle "blk_replay" extension
# R. Wiechert      ISTP       R1.0        08/22/97  Use octal station Id's
#                                                    in filenames.
# D. Fink          ISTP       R1.0        09/01/97  Added Geotail.
# R. Wiechert      ISTP       R1.0        09/12/97  Added to station table
# D. Fink          ISTP       R1.0        10/11/97  Added KEEPNAME logic.
# K. Hogie         ISTP       R1.0b       11/18/97  More Dest codes, cleanup for automation
# R. Wiechert      ISTP       R1.0D       01/27/98  Added diagnostic messages and
#                                                    changed GEOTAIL PDF A to Z (to ignore)
# R. Wiechert      ISTP       R1.3        06/30/98  Standard option parsing
#
# NOTES:
# none
#
#########################################################################

require "getopts.pl";

        #--------------------------------------------------
        #   Parse input parameters
        #--------------------------------------------------

if ( ! &Getopts( 'dnhv' ) ) { $opt_h=1; } # sets opt_* for each option

if ( $opt_d ) { $DEBUG++; }
if ( $opt_n ) { $NUMERIC++; }
if ( $opt_h ) { $HELP++; }
if ( $opt_v ) { $VERBOSE++; }

$NUMFILES = 0 ;
while ( $ARGV[0] ) {
	$FILENAME[ $NUMFILES ] = $ARGV[0] ;
	shift;
	$NUMFILES++ ;
}

if ( $HELP || ($NUMFILES == 0) ) {
	name_usage() ;
	exit() ;
}

$CURRFILE = 0 ;

#########################################################################
#       Table of SRC/DST codes and various related text strings
#
#		From Nascom 542-002   
#
#		Digital Data Source/Destination
#			and Format Codes Handbook
#				for the
#		Nascom Message Switching System
#
#		Revision 5  of Oct. 1996
#
#######################################################################

##################################################################
#											
#	Source/Dest	code table format			
#										
#	  Num #	* Org. Ant3	AntDesc	Geo3 GeoDesc
#
#		(fields separated by at least a , (tab,whitespace around is OK))
#											
#		Num		= Octal	Src/Dest code value	
#		#		= Valid	destination	code flag	
#		*		= Valid	source code	flag			
#		Center	= Controlling Center (JPL, WPS,	MILA, etc.)	
#		Ant3	= 3 or 4 character antenna/control center code	
#		Geo3	= 3	or 4 character geographic code for antenna/control center
#		AntDesc	= Long description of antenna/POCC			
#		GeoDesc	= Long description of geographic location	
#															
##################################################################
#
# #  D  S  Center Ant Geo  AntDesc  GeoDesc
#
$SRCT = << "E_O_T" ;
0001, #, *, MILA, D79,  MIL, DSS-79, MILA DSS-79
0002, #, *, MILA, S70,  MIL, SPC-70 (Signal Processing Center - MILA), MIL71 
0003, #, *, MILA, D72,  MIL, DSS-72 (MILA), MIL71
0006, #, *, ????,  LM,  CA, ???, Lockheed-Martin Palo Alto; California (GOES I-M), Palo Alto
0007, #, -, JSC,  SPIF, JSC, JSC/SPIF (Payload Commands), JSC/SPIF
0010, #, *, GSFC, AGO,  AGO, AGO, Santiago; Chile (AGO)
0011, #, *, JPL,  C22,  JPL, CTT22 Compatibility Test Trailer (DSN), DSN CTT22
0012, #, *, GSFC, REN,  GSFC, Renaissance Lab, GSFC bldg. 23 E120
0013, #, *, JPL,  APL,  JHU,  JPL-Applied Physics Lab, John Hopkins Univ.
0014, #, *, WFF,  WPS,  WPS,  Wallops - PSATS/Command, Wallops
0015, #, -, JPL,  DSN,  DSN,   S/C OBC Dump Data, DSN Emergency Support
0020, #, *, GSFC, FDF,  GSFC, FDF (Trk and Vector Data), FDF
0023, -, -, JPL,  AMMOS,JPL, AMMOS Simulation Subsystem, JPL - AMMOS
0026, #, *, GSFC, SPIF, GSFC, SPIF (Shuttle-1 GSFC Payload Data/Real-time), GSFC-SPIF
0027, #, *, GSFC, SOC,  SOC,  Simulation Operations Control, GSFC-SOC
0030, #, *, GSFC, NTTF, NTTF, NTTF, Greenbelt (NTTF)
0034, #, *, GSFC, STOCC, GSFC, STOCC (Space Telescope - Playback), GSFC-STOCC
0035, #, *, JPL,  DSN , DSN,  DSN Emergency Support - Real-time Data
0036, -, -, GSFC, NIM,  GSFC,  NIMOCC (Future Nimbus 7) NIM-I, GSFC-NIM??
0040, #, *, GSFC, MSOC, GSFC,  MSOCC IMP Real-time, GSFC-MSOCC
0041, #, *, JPL,  LASH, JPL,  STRV-1A & 1B, JPL - Lasham; England (STRV-1A and 1B)
0043, #, *, ???,  LM,   CA,   GOES Processed TLM, to MSOCC/FDF/GSS Bldg. 6/ Lock-Mart Palo Alto
0046, #, *, GSFC, MCE,  MCE, ISTP TPOCC (WIND & POLAR) (Monitor and Command Echo), GSFC-ISTP TPOCC
0047, #, *, JPL,  ESOC, DE,   ICV Data, JPL - ESOCC; Darmstadt, Germany
0051, #, *, GSFC, TIP,  GSFC, MSOCC/TIPIT (ERBS Real-time), GSFC-TIPIT
0052, #, *, GSFC, FAST, GSFC, Small Explorer (FAST), TPOCC
0053, #, *, GSFC, HST,  HST,  STOCC (Space Telescope - R/T), GSFC HST
0054, #, *, GSFC, EUVE, EUVE, Explorer Platform (EUVE) - RTC, GSFC EUVE
0055, -, -, JPL,  D49,  AUS,  DSS-49, Parks, Australia (DSS-49)
0064, #, *, GSFC, SOC,  SOC,  Simulation Operations Control Center (SOCC), GSFC bldg 25
0070, #, -, GSFC, IRT,  IRT,  ICE/IMP Real-time, MSOCC/TIPIT
0074, #, -, GSFC, SMEX, SMEX, SMEX/SAMPEX/TPOCC/IPD, TPOCC/IPD
0075, #, *, GSFC, WPB,  WPB,  ISTP WIND Playback, GGS TPOCC/IPD
0076, #, *, GSFC, RTC,  RTC,  Real-time Control - RTC, MSOCC
0103, #, *, GSFC, NOAA, WFF,  NOAA Tracking Station, Wallops/GOES
0112, #, *, JPL,  ISAS, ISAS, SSOC, ISAS Japan
0115, #, *, GSFC, SAMP, SAMP, SMEX SAMPEX, TPOCC/IPD
0117, #, *, GSFC, FDF,  FDF,  TDRSS Tracking, GSFC FDF
0120, #, *, GSFC, PWI,  PWI,  POLAR IPD PWI Data, GSFC-ISTP
0121, #, *, JPL,  GSOC, JPL,  GSOCC Channel-3, (via JPL)
0123, #, *, JPL,  S04,  DTC,  SPC-04 9M, JPL DTF 21
0125, #, *, JPL,  D05,  DTF,  DSS-05 (34-M STD), JPL DTF 21
0127, #, *, JPL,  S10,  GDS,  SPC-10, (Signal Processing Center - Goldstone)
0133, #, *, JPL,  D06,  DTF,  DSS-06 (70-M), JPL DTF 21
0135, #, *, GSFC, SOH,  SOH,  ISTP SOHO,  TPOCC & IPD
0136, -, -, JPL,  D59,  DE,   DSS-59 15-M Ant #2, Weilheim; Germany 
0137, #, *, JPL,  D07   DTF,  DSS-07 (34-M HEF), JPL DTF 21
0140, #, *, GSFC, WFF,  WFF,  Telemetry/Tracking Data WPSA 9M, Wallops Flight Facility 
0146, #, *, JPL,  D08,  DTF,  DSS 08 (26-M), DTF JPL 21 
0147, -, -, JPL,  D58,  DE,   DSS-58 15-M #1, Germany
0154, #, *, JPL,  TAC1, TOK,  TACC-1, NASDA; Tokyo
0155, #, *, JPL,  TAC2, TOK,  TACC-2, NASDA; Tokyo
0161, -, *, GSFC, D18,  BDA,  DSS-18 BDA, (Source-Only TrackingHigh-Speed - TDPS) DSS-18
0167, #, *, JPL,  NOC, JPL,  NOCC-JPL, (JPL-Network Control)
0170, #, *, JPL,  MSOC, JPL, JPL MSOCC (Playback), JPL Internal MSOCC 
0174, #, -, CNES, FDF, FDF, FDF/CNES (Tracking), FDF/CNES
0177, -, -, JPL,  D03, VLBI,  DSS-03 (OVLBI), DTF-21
0201, #, *, JPL,  D12, GDS,  DSS-12 34M, (Goldstone)
0202, #, *, JPL,  D14, GDS,  DSS-14 70M, (Goldstone)
0203, #, *, JPL,  D15, GDS,  DSS-15 34M, (Goldstone)
0204, #, *, JPL,  D16, GDS,  DSS-16 26M, (Goldstone)
0207, #, -, JSC,  SPIF, JSC, (Payload Commands), JSC/SPIF 
0210, #, -, JPL,  NAV, DSN, (Tracking), DSN NAV & FDF
0216, #, *, JPL,  AGC, AF,  AGC Monitor Blocks, DSN/Air Force 
0220, #, *, JPL,  S40, CAN, SPC-40 (Signal Processing Center, Tidbinbilla)
0221, #, *, JPL,  RAD, RAD, Radarsat, via JPL
0222, #, *, JPL,  D42, CAN, DSS-42 34M, (Tidbinbilla)
0223, #, *, JPL,  D43, CAN, DSS-43 70M, (Tidbinbilla)
0224, #, *, JPL,  D45, CAN, DSS-45 34M, (Tidbinbilla) Tracking Data
0225, #, *, JPL,  D46, CAN, DSS-46 26M, (Tidbinbilla)
0226, #, *, GSFC  SPIF, SPIF, SPIF (Shuttle-2 GSFC Payload Data/Playback), SPIF
0227, -, -, JPL,  D68,  DE,   DSS-68 30-M Ant. #3, Weilheim; Germany 
0230, #, *, GSFC, MPT,  MSOC,  MSOCC (MPT), MSOCC (MPT)
0232, #, *, JPL,  D34,  CAN,  DSS-34,  (Tidbinbilla)
0234, #, *, GSFC, PPB,  PPB,  ISTP POLAR/Playback, TPOCC & IPD
0240, #, *, GSFC, MSOC, MSOC, MSOCC GRO/RTC, MSOCC GRO/RTC
0241, #, *, JPL,  GSOC,  DE, GSOCC Channel 2, GSOCC(Munich; Germany) 
0245, #, -, JSC,  FDF,  FDF, JSC FDF, JSC FDF
0246, #, *, JPL,  GSOC, DE,  GSOCC (Spacelab D-2; Germany 19.2 kbs) SLB-1, GSOCC SLB-1
0247, #, *, JPL,  GSOC, DE,  GSOCC Channel 1, GSOCC (Munich; Germany)
0256, -, -, JPL,  ARC, ARC,  Ames Research Center, Ames Research Center
0257, #, *, JPL,  NAV, NAV,  DSN Navigation, DNS Navigation
0260, #, *, JPL,  TOTS, TOTS, Transportable Orbital Tracking Stations, JPL ???
0267, -, -, JPL,  INT, JPL, Internal use, JPL Internal
0270, #, *, GSFC, SWCE, SWCE, SMEX (SWAS) Cmd/Cmd Echo, TPOCC & MSOCC ICE Cmd/Return
0275, #, *, JPL,  D09,  JPL, DSS-09 (34M BWG), Test Facility
0277, #, *, GSFC, MPT,  GSFC, MPT, MSOCC
0300, #, *, GSFC, SWRT, SWRT, SMEX (SWAS) All Data Rates Telemetry; Monitor Blocks,  TPOCC/PACOR II 
0301, #, *, GSFC, N10, NOAA, NOAA 9/10 Telemetry, IPD
0302, #, *, GSFC, WRT, WRT,  WIND Real-time Tlm, ISTP Wind GGS TPOCC/IPD/SPOF 
0303, #, *, GSFC, W11, WOTS,  11-Meter System, Wallops Orbital Tracking Station (WOTS)
0310, #, *, JPL,  AMM, JPL, JPL-AMMOS, JPL-AMMOS
0311, #, *, JPL,  D24, GDS, DSS-24 (34M BWG) Subnet 1, Goldstone
0312, #, *, JPL,  D25, GDS, DSS-25 (34M BWG) Subnet 2, Goldstone
0313, #, *, JPL,  D26, GDS, DSS-26 (34M BWG) Subnet 3, Goldstone (Int.) & MCC Gnd Sup Equip. No. 2 (Int.)
0316, -, -, JPL,  D53, MAD,  DSS-53,  Madrid (OVLBI)
0320, -, -, JPL,  CH2, TOPX, Channel 2, TOPEX POCC
0322, #, *, JPL,  CH4, DE,  Channel 4, GSOCC (Munich; Germany)
0324, #, *, GSFC, CMD, GSFC, Small Explorer (SAMPEX), TPOCC DSN Cmd/Tlm
0326, #, *, GSFC, FDF, GSFC, (Test Only), FDF
0327, #, *, JPL, S60, MAD,  SPC-60 (Signal Processing Center, Madrid)
0331, #, *, GSFC, FDF, GSFC, (Test Circuit), FDF
0333, #, *, JPL, D17, GDS, DSS-17 9M, (Goldstone)
0335, #, *, GSFC,MSOC, GSFC, MSOCC/IPD, DSN Emergency Support Playback
0336, #, *, ???, MSOC, ???, MSOCC (Real-time Control), ERBS
0337, *, -, JPL, MPT7, JPL, MSOCC MPT-7, JPL AMMOS (Internal)
0340, #, -, GSFC SWAS, GSFC, Small Explorer (SWAS),  PACOR II
0341, #, *, GSFC, GPB, GSFC, Geotail DSN Playback, ISTP Geotail IPD 
0343, #, *, GSFC, SDPF, GSFC, SDPF, GSFC
0345, -, *, JPL, D62, MAD,  DSS-62 Source Code Only, (Madrid) 
0346, #, *, JPL, D27, GDS,  DSS-27 (34-M BWG-HSB??), Goldstone 
0347, #, *, JPL, D28, GDS,  DSS-28 (34-M BWG), Goldstone 
0350, #, *, JPL,  POC, JPL, (Command), TOPEX POCC
0353, #, *, GSFC, MPT, GSFC, Mission Planning Terminal, GSFC MSOCC
0362, #, *, GSFC, PRT, PRT, GGS POCC POLAR R/T Telemetry IPD Port, ISTP TPOCC/IPD 
0363, #, *, JPL, D61, MAD, DSS-61 34M, (Madrid)
0364, #, *, JPL, D63, MAD, DSS-63 70M, (Madrid)
0365, #, *, JPL, D54, MAD, DSS-54 34M BWG,  (Madrid )
0366, #, *, JPL, D66, MAD, DSS-66 26M, (Madrid)
0375, #, *, JPL, D65, MAD, DSS-65 34M, (Madrid)
E_O_T

for ( $I=0; $I<256 ; $I++ ) {	# initialize arrays to octal value string or Undefined
	$SRC_ANT3[ $I ] = $SRC_GEO3[ $I ] =  sprintf( "0%3.3o", $I) ;
	$SRC_ANTDESC[ $I ] = $SRC_GEODESC[ $I ] = "Undefined" ;
}

@SR = split( /\n/, $SRCT ) ;
$I = 0 ;
while( $SR[ $I ] ) {            # process each line of table information
        @LS=split( /\s*,\s*/, $SR[$I]); # split on commas and any surrounding whitespace
        $INDX                   = oct( $LS[0] ) ;     # get numeric station code
        if( $LS[1] eq "#" ) { $SRC_DSTVALID[ $INDX ] = 1 }
        if( $LS[2] eq "*" ) { $SRC_SRCVALID[ $INDX ] = 1 }
        $SRC_CENTER[ $INDX ]    = $LS[ 3 ] ;    # get controlling center name
        $SRC_IRTS[ $INDX ]      = $LS[4] ;   # 3 char name for building IRTS tables
        $SRC_HASH{ $LS[4] }     = $INDX  ;   # build array to associate name with number
        $SRC_ANT3[ $INDX ]      = $LS[4] ;   # 3 char name
        $SRC_GEO3[ $INDX ]      = $LS[5] ;   # descriptive site name
        $SRC_ANTDESC[ $INDX ]   = $LS[6] ;   # 5 char antenna name
        $SRC_GEODESC[ $INDX ]   = $LS[7] ;   # long antenna name
        $I++ ;
}

$SCID[ 0010 ] = "WIND    - Normal" ;
$SCID[ 0011 ] = "WIND    - Simulation" ;
$SCID[ 0015 ] = "POLAR   - Normal" ;
$SCID[ 0016 ] = "POLAR   - Simulation" ;
$SCID[ 0001 ] = "GEOTAIL - Normal" ;
$SCID[ 0025 ] = "SOHO" ;

$PDFT = << "E_O_T" ;
0343, B, RT, WIND,   5.565 Kbps - Science, Maneuver, Contingency (R/T)
0344, A, RT, WIND,   1.113 Kbps - Engineering (R/T)
0345, C, RT, WIND,  11.130 Kbps - 2X-Science, Maneuver, Contingency (R/T)
0342, E, PB, WIND,  64.000 Kbps - Science, Maneuver, Contingency (P/B)
0320, D, PB, WIND,  32.000 Kbps - 1/2-Science, Maneuver, Contingency (P/B)
0321, F, PB, WIND, 128.000 Kbps - 2X-Science, Maneuver, Contingency (P/B)
0322, B, RT, POLAR,  55.650 Kbps - Science, Maneuver, Contingency (R/T)
0323, A, RT, POLAR,   1.113 Kbps - Engineering (R/T)
0325, G, RT, POLAR,  256.000 Kbps - PWI Data (Real-time)
0326, D, PB, POLAR,  512.000 Kbps - Science, Maneuver, Contingency (P/B)
0330, C, PB, POLAR,  256.000 Kbps - 1/2-Science, Maneuver, Contingency (P/B)
0310, Z, RT, GEOTAIL,  16.384 Kbps - Record and Contingency (R/T)
0311, B, RT, GEOTAIL,  65.536 Kbps - Record and Contingency (R/T)
0307, C, PB, GEOTAIL,  65.536 Kbps - Record and Contingency (P/B)
0313, D, PB, GEOTAIL,  131.072 Kbps - Record and Contingency (R/T)
0312, Z, RT, SOHO, SOHO Real-time TLM (VC0 and VC1)
0324, Z, PB, SOHO, SOHO TR Dump TLM (VC4)
0327, Z, PB, SOHO, SOHO MDI TLM (VC2 and VC3)
E_O_T


for ( $I=0; $I<256 ; $I++ ) {	# initialize PDF to X and RTPB to XX and SAT to octal
	$PDF[ $I ] = $PDF_DESC[ $I ] =  "X" ;
	$PDF_RTPB[ $I ] = "XX" ;
	$PDF_SAT[ $I ] = sprintf( "0%3.3o", $I) ;
}

@PR = split( /\n/, $PDFT ) ;
$I = 0 ;
while( $PR[ $I ] ) {		# process each line of table information
    @LS=split( /\s*,\s*/, $PR[$I]); # split on commas and any surrounding whitespace
	$INDX 	  = oct( $LS[0] ) ;	# get numeric PDF code
	$PDF[ $INDX ]      = $LS[1] ;	# 1 char name
	$PDF_RTPB[ $INDX ] = $LS[2] ;	# Real-time or Playback (RT or PB)
	$PDF_SAT[ $INDX ]  = $LS[3] ; 	# Satellite name 
	$PDF_DESC[ $INDX ] = $LS[4] ;	# long description of PDF data
	$I++ ;
}

#########################################################################
#		 This is the major program loop to go through all files requested
#
#######################################################################

FILELOOP:


@FNIN = split( /\// , $FILENAME[ $CURRFILE ] ) ;        # split file name on /

$I = @FNIN -1  ;                 # get num of pieces, 
$FILEONLY = $FNIN[ $I ] ;               # grab field off end of file name
$FNIN[ $I ] = '' ;                      # zap last field
$DIRONLY  = join( '/', @FNIN ) ;        # put whats left back together

$RECNUM = 0 ;
$REC = "" ;
$PRTMSG = "" ;

$FLG = $DOY = $MSOD = $USOMS = 0 ;

$FN = '<'.$FILENAME[ $CURRFILE ] ;
$FNOUT = '>'.$FILENAME[ $CURRFILE ].'.ed' ;	#create edit output file name in case needed
$XX = open( IN, $FN ) ;
if( $XX <= 0 )
{
	# fatal error opening data file
	printf( STDERR "ERROR: lzp_rename: open(%s)\n", $FILENAME[ $CURRFILE ] ) ;
	exit( );
}

$SAT = $RTPB = $PDF = $SRC = $DEST = $T1DOY = $T1MSOD = $T1USOMS = "" ;

$FILE_FMT = $REC_FMT = $BLK_TYPE = $FRM_TYPE = $DATA_TYPE = "" ;

$SYNCPAT = 0x0 ;

$F_HDR_LEN = $R_HDR_LEN = $R_DAT_LEN = $R_RS_LEN = $R_ANN_LEN = $R_TRL_LEN = 0 ;

$RB = sysread( IN, $REC, 720) ;
if( $RB != 720 )
{
	printf( STDERR "ERROR: lzp_rename: short file %s\n", $FILENAME[ $CURRFILE ] ) ;
	goto NEXTFILE ;
} 
$IDRFHDR = substr( $REC, 0, 96 ) ;	# grab first 96 bytes in case IDR format

if( Pattern_Loc( $REC, 0x627627, 0 ) ) {	# found Nascom block sync

	$SYNCPAT = 0x627627 ;
	
	$FILE_FMT = "GBRS" ;	
	$REC_FMT = "BLOCK" ;
	$BLK_TYPE = "DGIB" ;
	$DATA_TYPE = "TLM" ;	# set for basic telemetry for now
	$F_HDR_LEN = 0 ;
	$R_HDR_LEN = 0 ;
	$R_DAT_LEN = 600 ;
	
	if( Pattern_Loc( $REC, 0x627627, 600 ) ) { 	# back to back, no annotation
		$R_RS_LEN  = 0 ;
		$R_ANN_LEN = 0 ;
		$R_TRL_LEN = 0 ;
	}
	
	if( Pattern_Loc( $REC, 0x627627, 608 ) ) { 	# back to back, annotation
		$R_RS_LEN  = 0 ;
		$R_ANN_LEN = 8 ;
		$R_TRL_LEN = 0 ;
	}
}


if( Pattern_Loc( $REC, 0x627627, 116 ) ) { 	# IDR format

	$SYNCPAT = 0x627627 ;
	
	$FILE_FMT = "IDR" ;	
	$REC_FMT = "BLOCK" ;
	$BLK_TYPE = "DGIB" ;
	$DATA_TYPE = "TLM" ;	# set for basic telemetry for now
	$F_HDR_LEN = 96 ;
	$R_HDR_LEN = 20 ;
	$R_DAT_LEN = 600 ;
	$R_RS_LEN  = 0 ;
	$R_ANN_LEN = 0 ;
	$R_TRL_LEN = 4 ;
}

#
# now figure out Satellite and RTPB
#

#########################################################################
#
#       Format of NASCOM block header (DGIB)
#
#      +--------+--------+ 
#    1 | Nascom 4800 BB  | 0 , 1	Nascom Sync ( first 16 bits)
#      +--------+--------+ 
#    2 | Sync   | Source | 2 , 3	Sync ( 8 )  ,  Source code
#      +--------+---+----+
#    3 | Dest   |Seq| BFC| 4 , 5	Destination ,  Seq(3)  BFC(5)
#      +--------+---+----+
#    4 | SCID   |  DSID  | 6 , 7	Spacecraft ID, Data Stream ID
#      +--------+--------+
#    5 | Type   | Blk Cnt| 8 , 9	Data Type   ,  Block count
#      +--------+--------+
#    6 |xyz  Blk Length  |10 ,11	Flgs(3)   Block len. bits (13)
#      +--------+--------+
#    7 |ab DOY(9) MSOD(5)|12 ,13	Flags(2)  DOY(9) First milliseconds of day (5)
#      +--------+--------+
#    8 |   MSOD ( 16 )   |14 ,15	  more milliseconds of day (16)
#      +--------+--------+
#    9 |MSOD(6) USOMS(10)|16 ,17	last MSOD(6)  Microseconds of millisecond (10)
#      +--------+--------+
#      |        |        |
#
#######################################################################


if( $REC_FMT eq "BLOCK" ) {
	$REC_START = $F_HDR_LEN + $R_HDR_LEN ;

	$B = substr( $REC, $REC_START, $R_DAT_LEN ) ;

	( $XSRC, $XDST, $XSCID, $XDSID, $XTYPE ) = 
		unpack( "x3 C1 C1 x C1 C1 C1 x3", $B ) ;

	$RTPB = $PDF_RTPB[ $XDSID ] ;
	$PDF  = $PDF[ $XDSID ] ;

	if( $NUMERIC ) {
		$SRC  = sprintf( "00%3.3o", $XSRC ) ;
		}
	else {
		$SRC  = $SRC_ANT3[ $XSRC ] ;
	}
	$DEST = $SRC_ANT3[ $XDST ] ;

	
	if( $BLK_TYPE eq "DGIB" ) {
		@T1 = unpack( "x12 C6", $B ) ;
		$T1DOY = (( $T1[0] & 0x3f) << 3 ) | (( $T1[1] >> 5 ) & 0x7 )  ;
		$T1MSOD = (($T1[1] & 0x1f ) << 22) | ( $T1[2] << 14) | ( $T1[3] << 6 ) | (( $T1[4] >> 2 ) & 0x3f) ;
		$T1USOMS = ($T1[4] & 0x03 ) << 8 | $T1[5]  ;
	}

	if( $BLK_TYPE eq "DDPS" ) {
		$T1DOY = 0 ;
		$T1MSOD = 0 ;
		$T1USOMS = 0 ;
	}

	if( not $SAT ) {	# try block header
		$SAT = $SCID[ $XSCID ] ;
	}

#
# Start setting the variable that will be used to build the new file name
#
	if( $FILE_FMT eq "IDR" ) { $FNFMT = "I" ; }
	if( $FILE_FMT eq "GBRS" ) { $FNFMT = "G" ; }
	@FNSAT = split( / /, $SAT ) ;
	$FNSAT = $FNSAT[0] ;
	$FNPDF = $PDF ;
	$FNSRC = $SRC ;

	@FNIN = split( /\./ , $FILENAME[ $CURRFILE ] ) ;	# split file name on .

	$I = @FNIN  ;		# get num of pieces, scan file name pieces from end to front
	while( $I-- ) {
		$FNRAND = $FNIN[ $I ] ;	# grab next field off end of file name
		if( $FNRAND eq "blk" ) { next }	# if "blk" skip it and get next piece
		if( $FNRAND eq "ed" ) { next }
		if( $FNRAND eq "dat" ) { next }
		if( $FNRAND eq "blks" ) { next }
		if( $FNRAND eq "blk_replay" ) { next }
		last ;		# if we get here, FNRAND should be OK to use
	}
#
# Check the current file name to see if it is already in the specified format.
# If current file name is OK then keep the current name; do not rename.  The
# current file name is considered OK if the following conditions are met:
#
#    -  Expected fields present and delimited by underscores ("_")
#    -  Year field within the range 1992 to 2020
#    -  Satellite name within the set (WIND, POLAR, GEOTAIL)
#
# Within these constraints, the user may force PDF, data type, year, and day.
# We first split on "/" in case a path is specified rather than just a file name.
#
        $KEEPNAME = "FALSE";
	$CURRSAT = "NONE" ;
	$CURRYR = "NONE" ;
	$SLASH = "/";
	@FNINU2 = split( /$SLASH/ , $FILENAME[ $CURRFILE ] ) ;	# split file name on "/"
	$IU2 = @FNINU2 ;
	$CURRFILENAME = $FNINU2[ $IU2 - 1 ];
	@FNINU = split( /_/ , $CURRFILENAME ) ;	# split file name on _
	$IU = @FNINU  ;		# get num of pieces, scan file name pieces from end to front
	while( $IU-- ) {
		$FNU = $FNINU[ $IU ] ;	# grab next field off end of file name
		if( $FNU ge "1992" and $FNU le "2020") { $CURRYR = $FNU; }
		if( $FNU eq $FNSAT ) { $CURRSAT = $FNU;}
		if( $FNU eq "/".$FNSAT ) { $CURRSAT = $FNU;}
		next;
	}
        if ( $CURRSAT ne "NONE" and $CURRYR ne "NONE" ) { $KEEPNAME = "TRUE" ; }
#
# assuming the file name is to be changed, set and pad random field.
#
	@GMT    = gmtime( time ) ;
	$FNPAD  = sprintf( "%2.2d%2.2d%2.2d", $GMT[2], $GMT[1], $GMT[0] ) ;
	$FNRAND = $FNPAD . $FNRAND ;	# add on date to make at least 5 chars
	$FNRAND = substr( $FNRAND, -5 ) ;	# get last 5 chars of string
	$FNEXT = '.blk' ;
}

#
# Get input file status information
#
#	0=dev, 1=ino, 2=mode, 3=nlink, 4=uid, 5=gid, 6=rdev, 7=size,
#	8=atime, 9=mtime, 10=ctime, 11=blksize, 12=blocks
#
@STAT = stat( IN ) ;
#
#	0=sec, 1=min, 2=hour, 3=mday, 4=mon, 5=year, 6=wday, 7=yday, 8=isdst
#
@FNTIME = gmtime( $STAT[10] ) ;	# get GMT creation time for YEAR

$FNDOY = $T1DOY ;
$FNYEAR = $FNTIME[5] ;
if( $FNYEAR > 70 ) { $FNYEAR = $FNYEAR + 1900 }
	else { $FNYEAR = $FNYEAR + 2000 }

if( not $FILE_FMT ) {		# if file type still not known, give up
	$FILE_FMT = "UNK" ;
	printf( STDERR "ERROR: lzp_rename: unknown file %s\n", $FILENAME[ $CURRFILE ] );
}

if( $FILE_FMT eq "UNK" ) { goto NEXTFILE } ;

#########################################################################
#	Print information on file format identified
########################################################################

if( $KEEPNAME eq "FALSE" ) { # if OK to rename

		# whatever happens, make new file name and return to caller

	$NEWFN = sprintf( "%s_%1.1s%1.1s_%4.4d_%3.3d_%s_%5.5s%4.4s" , 
		$FNSAT, $FNPDF, $FNFMT, $FNYEAR , $FNDOY, $FNSRC, $FNRAND, $FNEXT ) ;

	$MOVFN = $DIRONLY . $NEWFN ;

	printf( STDOUT "%s\n" , $NEWFN ) ;

		# if not in debug mode, do actual file move

	if( not $DEBUG ) {

		# if not DEBUG mode then do rename, else skip actual rename
		$RSTAT = rename( $FILENAME[ $CURRFILE ], $MOVFN ) ; 

		if( $RSTAT != 1 ) { $REN_ERROR = $! ; }
		else{ $REN_ERROR = 0 }
	}

}

# if not OK to rename file, just return old filename
else { printf(STDOUT "%s\n" , $CURRFILENAME ) ; }


#########################################################################
#	When done processing one file
#	
#	Then advance to next file in list or exit if no more files
#
#######################################################################

NEXTFILE:

$CURRFILE++ ;

if( $CURRFILE < $NUMFILES ) { goto FILELOOP }

				#######################
exit( $REN_ERROR) ;		# end of main program #
				#######################
##################################################################################
##################################################################################
##################################################################################

sub name_usage{ 
	printf ( STDERR "\nUSAGE:     lzp_rename [-hvdn] filelist\n" ) ;
	printf ( STDERR "\nPURPOSE:   Assigns standard filenames based on file contents\n" ) ;
	printf ( STDERR "\nOPTIONS:   -h     display help screen\n" ) ;
	printf ( STDERR   "           -v     enable verbose mode\n" ) ;
	printf ( STDERR   "           -d     debug without modifying\n" ) ;
	printf ( STDERR   "           -n     assign octal source codes\n" ) ;
	printf ( STDERR "\nSYNOPSIS:  The specified files are scanned and renamed based\n" ) ;
	printf ( STDERR   "           on the results of the scan; i.e. based on content.\n" ) ;
	printf ( STDERR   "           A list of renamed files is returned via STDOUT.\n" ) ;
	printf ( STDERR   "           Diagnostic messages appear on STDERR.\n" ) ;
	printf ( STDERR "\nNOTES:     The following example shows how to rename the file\n" ) ;
	printf ( STDERR   "           JPLIDR1997344.86775, assigning octal source codes:\n" ) ;
	printf ( STDERR   "           lzp_rename -v -n JPLIDR1997344.86775\n" ) ;
	printf ( STDERR "\n" ) ;
	exit( );
}

#
#######################################################################
#
#	Pattern_Loc( $Record, $Pattern, $Loc )
#
#	Returns 0 = not found, 1 = found
#########################################################################

sub Pattern_Loc{ 

local( $REC, $PAT, $LOC ) = @_ ;

$CHK_VAL = unpack( "x$LOC L1", $REC ) ;	# extract 4-byte string at offset

if( $PAT <= 0xffffff ) { $CHK_VAL = ( $CHK_VAL >> 8 ) & 0xffffff ; }	# shift for 3-byte string

if( $CHK_VAL == $PAT ) { return 1 }
	else { return 0 }
	
}
