import codecs, os  

fn = 'schrott.dat'

print "In a first step the file is readed and afterwards some logical checks are done"
print "if that file can't be readed you get immediatly a traceback and you have to check yourself what could be wrong"
print "if you like to have some human assistance please ask ReimarBauer for help" 
print "---------------------------------------------------------------------------"

file = open(fn, 'r')
lines = file.readlines()
file.close()
txt = lines[0].split()
NO_HEADER = long(txt[0])
FFI = long(txt[1])
PI_NAME = lines[1].strip()
PI_ORGANISATION = lines[2].strip()
EXPERIMENT_NAME = lines[3].strip()
CAMPAIGN = lines[4].strip()          
txt = lines[5].split()         
AVN = long(txt[0])
TNV = long(txt[1])
txt = lines[6].split() 
DEPENDENT_TIME = "%s %s %s" %(txt[0],txt[1],txt[2]) 
FILE_MODIFICATION_TIME = "%s %s %s" %(txt[3],txt[4],txt[5]) 
TIME_INCREMENT = lines[7].strip() 
TIME_BASE = lines[8].strip() 
NUMBER_OF_VARS = long(lines[9])

if NUMBER_OF_VARS == 1: 
    SCALE_FACTORS = [float(lines[10])]
    MISSING_VALUES = [float(lines[11])]
    SHORT_NAMES = [lines[12]]
else:    
    txt =  lines[10].split()
    SCALE_FACTORS= []
    for t in txt:
        SCALE_FACTORS.append(float(t))
        
    txt =  lines[11].split()   
    MISSING_VALUES =[]
    for t in txt:
        MISSING_VALUES.append(float(t))
        
    SHORT_NAMES = lines[12:12+NUMBER_OF_VARS]
    
NUMBER_OF_COMMENTS = long(lines[12+NUMBER_OF_VARS])
COMMENTS = lines[11+NUMBER_OF_VARS+2:11+NUMBER_OF_VARS+2+NUMBER_OF_COMMENTS]
NUMBER_SPECIAL_COMMENTS = long(lines[11+NUMBER_OF_VARS+2+NUMBER_OF_COMMENTS])
SPECIAL_COMMENTS = lines[11+NUMBER_OF_VARS+2+NUMBER_OF_COMMENTS+1:11+NUMBER_OF_VARS+2+NUMBER_OF_COMMENTS+1+NUMBER_SPECIAL_COMMENTS]
FIRST_DATA_LINE = lines[11+NUMBER_OF_VARS+2+NUMBER_OF_COMMENTS+1+NUMBER_SPECIAL_COMMENTS]

print "FIRST CHECK"
print "----------------------------------------------------"
print "%s %s" % (NO_HEADER, FFI)
print PI_NAME
print PI_ORGANISATION
print EXPERIMENT_NAME
print CAMPAIGN
print "%s %s" % (AVN, TNV)
print "%s %s" % (DEPENDENT_TIME, FILE_MODIFICATION_TIME)
print TIME_INCREMENT
print TIME_BASE
print NUMBER_OF_VARS
print SCALE_FACTORS
print MISSING_VALUES
print SHORT_NAMES
print NUMBER_OF_COMMENTS
print COMMENTS
print NUMBER_SPECIAL_COMMENTS
print SPECIAL_COMMENTS
print FIRST_DATA_LINE

error = False
print "LOGICAL CHECK"
print "----------------------------------------------------"
if FFI != 1001:
    print "%s is wrong used. it should be 1001 " %(str(FFI)) 
    error = True
   
if lines[NO_HEADER] != FIRST_DATA_LINE:
    print "The following line should show the first data line of your file"
    print lines[NO_HEADER]
    print "%s may be is not the right number for header lines" %(str(NO_HEADER))
    error = True
if TIME_BASE != 'Elapsed UT seconds from 0 hours on day line 7':
    print "please use: Elapsed UT seconds from 0 hours on day line 7"
    error = True
record =  lines[NO_HEADER].split()
if len(record)-1 != NUMBER_OF_VARS:
    print "number of columns does not dependent on number of vars of %s" % (str(NUMBER_OF_VARS))
    error = True
if NUMBER_OF_VARS != len(SCALE_FACTORS):
    print "not right number of scale factor values used. you need %s" % (str(NUMBER_OF_VARS))
    error = True
if NUMBER_OF_VARS != len(MISSING_VALUES):
    print "not right number of missing values used. you need %s" % (str(NUMBER_OF_VARS))
    error = True
for name in SHORT_NAMES: 
   if '(' not in name:
       print "please add a unit to your var %s enclosed into '()'" % (name)
       error = True
   
if error:
   print "----------------------------------"
   print "sorry this file '%s' can't be accepted" % (fn)
   
else:
   print "the file '%s' seems to be right" % (fn)