Newer
Older
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
return (1, "MW", "Error writing file list.\n%s\nStopped. (%s)"%( e, \
getGMT(3)), 3)
if BState == STATE_STOP:
return (1, "YB", "Stopped by user. (%s)"%getGMT(3), 2)
if Msg == "bg":
msgLn(1, "W", "Wrote file list to %s"%Filesspec)
msgLn(0, "", "%s %s, %s %s on baler."%(fmti(FilesCount), \
sP(FilesCount, ("file", "files")), fmti(FilesBytes), \
sP(FilesBytes, ("byte", "bytes"))))
elif Msg == "bl":
logIt("Wrote file list to %s"%Filesspec)
logIt("%s %s, %s %s on baler."%(fmti(FilesCount), sP(FilesCount, \
("file", "files")), fmti(FilesBytes), sP(FilesBytes, \
("byte", "bytes"))))
return (0, FilesCount, FilesBytes)
# END: writeFilesTxt
# First day of the month for each non-leap year month MINUS 1. This will get
# subtracted from the DOY, so a DOY of 91, minus the first day of April 90
# (91-90) will leave the 1st of April. The 365 is the 1st of Jan of the next
# year.
PROG_FDOM = (0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365)
#############################
# BEGIN: q330yd2md(YYYY, DOY)
# LIB:q330yd2md():2013.023
# Converts YYYY,DOY to Month, Day. Faster than using using ydhmst2Time().
# Expects a 4-digit YYYY.
def q330yd2md(YYYY, DOY):
if DOY < 1 or DOY > 366:
return 0, 0
if DOY < 32:
return 1, DOY
elif DOY < 60:
return 2, DOY-31
if YYYY%4 != 0:
Leap = 0
elif YYYY%100 != 0 or YYYY%400 == 0:
Leap = 1
else:
Leap = 0
# Check for this special day.
if Leap == 1 and DOY == 60:
return 2, 29
# The PROG_FDOM values for Mar-Dec are set up for non-leap years. If it is a
# leap year and the date is going to be Mar-Dec (it is if we have made it this
# far), subtract Leap from the day.
DOY -= Leap
# We start through PROG_FDOM looking for dates in March.
Month = 3
for FDOM in PROG_FDOM[4:]:
# See if the DOY is less than the first day of next month.
if DOY <= FDOM:
# Subtract the DOY for the month that we are in.
return Month, DOY-PROG_FDOM[Month]
Month += 1
return 0, 0
# END: q330yd2md
##############################
# BEGIN: checkDTFile(Filespec)
# Reads the mini-seed file block headers to see if they are corrupted.
# Note Ret[0] values, so the caller can keep tabs. Also the return value
# has the file timerange added to the end.
def checkDTFile(Filespec):
Iam = stack()[0][3]
# Keep a list of station IDs that we come across. Should be just one.
StationIDs = []
# Keep a list of the channel names we find (should be just one for balers).
Channels = []
# The caller should be smater than this...but maybe not.
if isdir(Filespec):
return (8, "RW", "File %s is a directory."%basename(Filespec), 2, "", \
"")
except Exception, e:
return (1, "MW", str(e).strip(), 3, "", "")
# Determine the record size of the seed/miniseed file. This will chew up a
# little time, but different systems (like Nanometrics) uses different record
# sizes for different files for different sample rates. The balers may always
# be the same size, but you never know.
RecordSize = 0
Record = Fp.read(256)
# If the file is too small to determine it's record size then just go on.
if len(Record) < 256:
Fp.close()
if i == 0:
StaID = Record[8:13]
continue
if StaID == Record[8:13]:
if i == 1:
RecordSize = 256
elif i == 2:
RecordSize = 512
elif i == 4:
RecordSize = 1024
elif i == 8:
RecordSize = 2048
elif i == 16:
RecordSize = 4096
break
if RecordSize == 0:
Fp.close()
return (3, "MW", "Could not determine record size.", 3, "", "")
Fp.seek(0)
# Read the file in 10 record chunks to make it faster.
RecordsSize = RecordSize*10
RecordsRead = 0
FirstTime = "Z"
LastTime = ""
# Some may use this for messages.
NoOfRecords = int(getsize(Filespec)/RecordSize)
Records = Fp.read(RecordsSize)
# We're done with this file.
if len(Records) == 0:
break
RecordsRead += 10
Ptr = RecordSize*i
Record = Records[Ptr:Ptr+RecordSize]
# Need another set of Records.
if len(Record) < RecordSize:
break
ChanID = Record[15:18]
# The Q330 may create an "empty" file (all 00H) and then not finish filling it
# in. The read()s keep reading, but there's nothing to process. This detects
# that and returns. This may only happen in .bms-type data.
if ChanID == "\x00\x00\x00":
# I guess if a file is scrambled these could have 1000's of things in them.
# Both of them should only have one thing for baler files. Convert them to
# strings and then chop them off if they are long.
Stas = list2Str(StationIDs)
if len(Stas) > 12:
Stas = Stas[:12]+"..."
Chans = list2Str(Channels)
if len(Chans) > 12:
Chans = Chans[:12]+"..."
"FILE ENDS EMPTY. Times: %s to %s\n Recs: %d Stas: %s Chans: %s"% \
(FirstTime, LastTime, RecordsRead, Stas, Chans), 2, \
FirstTime, LastTime)
if ChanID not in Channels:
Channels.append(ChanID)
StaID = Record[8:13].strip()
if StaID not in StationIDs:
StationIDs.append(StaID)
Year, Doy, Hour, Mins, Secs, Tttt= unpack(">HHBBBxH", \
Record[20:30])
Month, Date = q330yd2md(Year, Doy)
DateTime = "%d-%02d-%02d %02d:%02d"%(Year, Month, Date, Hour, Mins)
if DateTime < FirstTime:
FirstTime = DateTime
if DateTime > LastTime:
LastTime = DateTime
Fp.close()
if RecordsRead == 0:
# Same as above.
Stas = list2Str(StationIDs)
if len(Stas) > 12:
Stas = Stas[:12]+"..."
Chans = list2Str(Channels)
if len(Chans) > 12:
Chans = Chans[:12]+"..."
if len(StationIDs) > 1:
"MULTIPLE STATIONS. Times: %s to %s\n Recs: %d Stas: %s Chans: %s"% \
(FirstTime, LastTime, RecordsRead, Stas, Chans), 3, \
FirstTime, LastTime)
"MULTIPLE CHANNELS. Times: %s to %s\n Recs: %d Stas: %s Chans: %s"% \
(FirstTime, LastTime, RecordsRead, Stas, Chans), 3, \
FirstTime, LastTime)
else:
return (0, "GB", \
"FILE OK. Times: %s to %s\n Recs: %d Stas: %s Chans: %s"% \
(FirstTime, LastTime, RecordsRead, Stas, Chans), 1, \
FirstTime, LastTime)
# END: checkDTFile
###############
# BEGIN: main()
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
# All of the action takes place here in one pass using if()'s.
FH_NAME = 0
FH_SIZE = 1
FH_FROM = 2
FH_TO = 3
Dirspec = ".%s"%sep
AbsDirspec = abspath(Dirspec)
if AbsDirspec.endswith(sep) == False:
AbsDirspec += sep
if ArgUCheck == True:
Ret = checkForUpdates("check")
exit(Ret)
if ArgUDown == True:
Ret = checkForUpdates("get")
exit(Ret)
DirspecSDR = ".%s%s.sdr%s"%(sep, TagID, sep)
Msgspec = Dirspec+TagID+".msg"
# Duplicating some stuff so the messages come out the way I want them to.
if exists(Msgspec) == False:
Fp = open(Msgspec, "w")
Fp.close()
logIt("%s %s"%(PROG_NAME, PROG_VERSION))
if ArgInfo == True:
logIt("Control computer IP address (maybe): %s"% \
gethostbyname(gethostname()))
exit(0)
logIt("Working directory: %s"%AbsDirspec)
logIt("Created %s"%Msgspec)
else:
logIt("%s %s"%(PROG_NAME, PROG_VERSION))
if ArgInfo == True:
logIt("Control computer IP address (maybe): %s"% \
gethostbyname(gethostname()))
exit(0)
logIt("Working directory: %s"%AbsDirspec)
logIt("Using %s"%Msgspec)
if ArgBadBlocks == True:
if exists(DirspecSDR) == False:
logIt("Done checking. No files have been offloaded.")
logIt("")
exit(0)
DFiles = listdir(DirspecSDR)
DFiles.sort()
FilesChecked = 0
FilesOK = 0
# These get loaded with the file names depending on the Ret[0] value coming
# from checkDTFile() for a summary.
FilesOpenErrors1 = []
FilesTooSmall2 = []
FilesRecSize3 = []
FilesEndEmpty4 = []
FilesNoData5 = []
FilesMultStas6 = []
FilesMultChans7 = []
FilesIsDir8 = []
# For an overall baler time range.
FirstFilesTime = "Z"
LastFilesTime = ""
# Loop through all of the file(s) the user specified, then loop through DFiles
# looking for matches.
for ArgSpecFile in ArgSpecFiles:
# Go through DFiles and turn each file into "" if it is checked, so we don't
# do it more than once if it matches more than one ArgSpecFile.
Checked = 0
DFile = DFiles[Index]
if len(DFile) == 0:
continue
if fnmatch(DFile, ArgSpecFile):
DFilespec = DirspecSDR+DFile
# checkDTFile() will also catch this. We'll still look for Rec[0]==8. Non-data
# files will probably come back with a Rec Size error.
if isdir(DFilespec):
continue
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
FirstTime = Ret[4]
LastTime = Ret[5]
# If something goes wrong the returned times will be "".
if FirstTime != "" and FirstTime < FirstFilesTime:
FirstFilesTime = FirstTime
if LastTime != "" and LastTime > LastFilesTime:
LastFilesTime = LastTime
if Ret[0] == 0:
FilesOK += 1
elif Ret[0] == 1:
FilesOpenErrors1.append(DFile)
elif Ret[0] == 2:
FilesTooSmall2.append(DFile)
elif Ret[0] == 3:
FilesRecSize3.append(DFile)
elif Ret[0] == 4:
FilesEndEmpty4.append(DFile)
elif Ret[0] == 5:
FilesNoData5.append(DFile)
elif Ret[0] == 6:
FilesMultStas6.append(DFile)
elif Ret[0] == 7:
FilesMultChans7.append(DFile)
elif Ret[0] == 8:
FilesIsDir8.append(DFile)
DFiles[Index] = ""
logIt("Done checking.")
logIt("")
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
logIt("Summary:")
logIt(" Date range: %s to %s"%(FirstFilesTime, LastFilesTime))
logIt("Files checked: %d"%FilesChecked)
logIt(" Files OK: %d"%FilesOK)
if len(FilesOpenErrors1) != 0:
logIt("File opening errors: %d"%len(FilesOpenErrors1))
for File in FilesOpenErrors1:
logIt(" "+File)
if len(FilesTooSmall2) != 0:
logIt("Files too small: %d"%len(FilesTooSmall2))
for File in FilesTooSmall2:
logIt(" "+File)
if len(FilesRecSize3) != 0:
logIt("Unknown record size: %d"%len(FilesRecSize3))
for File in FilesRecSize3:
logIt(" "+File)
if len(FilesEndEmpty4) != 0:
logIt("Ending empty: %d"%len(FilesEndEmpty4))
for File in FilesEndEmpty4:
logIt(" "+File)
if len(FilesNoData5) != 0:
logIt("No data: %d"%len(FilesNoData5))
for File in FilesNoData5:
logIt(" "+File)
if len(FilesMultStas6) != 0:
logIt("Multiple stations: %d"%len(FilesMultStas6))
for File in FilesMultStas6:
logIt(" "+File)
if len(FilesMultChans7) != 0:
logIt("Multiple channels: %d"%len(FilesMultChans7))
for File in FilesMultChans7:
logIt(" "+File)
elif len(FilesIsDir8) != 0:
logIt("Directories: %d"%len(FilesIsDir8))
for File in FilesIsDir8:
logIt(" "+File)
logIt("")
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# Get the basic info and make sure we are all on the same page. Do this each
# time we do anything.
Ret = getBalerHtm("bl", TagID, IPAddr)
if Ret[0] != 0:
logIt(Ret[2])
logIt("")
exit(1)
# Might as well check for this before we go to the trouble of getting the list
# of files (which could take a long time).
if ArgVerify == True:
if exists(DirspecSDR) == False:
logIt("Done verifying. No files have been offloaded.")
logIt("")
exit(0)
FWVers = Ret[1]
DSize = Ret[2]
NFiles = Ret[3]
Percent = Ret[4]
if ArgCheck == True:
Used = (Percent/100.0)*DSize
logIt("FWVers: %s DiskSize: %s"%(FWVers, fmti(DSize)))
logIt("%.1f%% of %s %s used."%(Percent, fmti(NFiles), sP(NFiles, \
("file", "files"))))
logIt("Done checking.")
logIt("")
exit(0)
# A List of Lists of the data files on the baler according to files.htm.
FHFiles = []
# files.htm can take a long time to build (it's made and sent on-the-fly).
if Percent < 50:
logIt("Getting files.htm...")
else:
logIt("Getting files.htm...(could take a while)")
Ret = getFilesHtm("", TagID, IPAddr)
if Ret[0] != 0:
logIt(Ret[2])
logIt("")
exit(1)
logIt("Got files.htm.")
FHFiles = Ret[1]
# Always write the whole list out to the file ./<TagID>files.htm.
Ret = writeFilesTxt("bl", Dirspec+TagID+"files.txt", FHFiles)
if Ret[0] != 0:
logIt(Ret[2])
logIt("")
exit(1)
if ArgList == True:
Count = 0
for File in FHFiles:
Count += 1
stdout.write("%d. %s %s to %s %d\n"%(Count, File[FH_NAME], \
File[FH_FROM], File[FH_TO], File[FH_SIZE]))
logIt("Done listing.")
exit(0)
# Go through FHFiles and match up by name and size the files that are already
# on the computer. Then go through the files in the directory and see if there
# are any that don't belong there.
if ArgVerify == True:
Here = 0
NotHere = 0
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
Name = FHFiles[Index][FH_NAME]
Size = FHFiles[Index][FH_SIZE]
if exists(DirspecSDR+Name) and getsize(DirspecSDR+Name) == Size:
Here += 1
else:
NotHere += 1
logIt("Already offloaded %s, not offloaded %s."%(fmti(Here), \
fmti(NotHere)))
DFiles = listdir(DirspecSDR)
Extras = 0
FHFiles2 = []
for FHFile in FHFiles:
if len(FHFile) == 0:
continue
FHFiles2.append(FHFile[FH_NAME])
for DFile in DFiles:
if DFile.startswith("."):
continue
if DFile not in FHFiles2:
logIt("Extra file: %s"%DFile)
Extras += 1
if Extras > 0:
logIt("%s non-data %s in data files directory."%(fmti(Extras), \
sP(Extras, ("file", "files"))))
logIt("Done verifying.")
logIt("")
exit(0)
logIt("Offloading from baler %s @ %s"%(TagID, IPAddr))
# Alter FHFiles as necessary and then fill up BFiles with the remains.
if ArgSpec == True:
logIt("Only offloading: %s"%list2Str(ArgSpecFiles))
for ArgSpecFile in ArgSpecFiles:
# For entries that are already [].
try:
if fnmatch(FHFiles[Index][FH_NAME], ArgSpecFile) == False:
FHFiles[Index] = []
# Don't list them. There could be a lot.
except IndexError:
continue
elif ArgSkip == True:
logIt("Excluding: %s"%list2Str(ArgSkipFiles))
for ArgSkipFile in ArgSkipFiles:
try:
if fnmatch(FHFiles[Index][FH_NAME], ArgSkipFile):
FHFiles[Index] = []
except IndexError:
continue
# For any of the offloads always throw out files that are already here.
try:
Filename = FHFiles[Index][FH_NAME]
Size = FHFiles[Index][FH_SIZE]
if exists(DirspecSDR+Filename) and \
getsize(DirspecSDR+Filename) == Size:
FHFiles[Index] = []
except:
continue
if ArgLSROffload == True:
logIt("Offloading low sample rate files.")
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
try:
Filename = FHFiles[Index][FH_NAME]
if Filename.find(".H") != -1 or Filename.find(".S") != -1:
FHFiles[Index] = []
except IndexError:
continue
# Now move everything left to BFiles.
BFiles = []
BFilesBytes = 0
for File in FHFiles:
if len(File) == 0:
continue
BFilesBytes += File[FH_SIZE]
BFiles.append(File)
BFilesToOff = len(BFiles)
if BFilesToOff == 0:
logIt("There are no files left in the list to offload.")
exit(0)
# Create this after we know if there is even going to be anything to offload.
if exists(DirspecSDR) == False:
makedirs(DirspecSDR)
logIt("Saving data files to %s (created)"%DirspecSDR)
else:
logIt("Saving data files to %s (exists)"%DirspecSDR)
logIt("Offloading %s %s, %s %s..."%(fmti(BFilesToOff), sP(BFilesToOff, \
("file", "files")), fmti(BFilesBytes), sP(BFilesBytes, ("byte", \
"bytes"))))
Count = 0
# Files offloaded.
OffFiles = 0
# Total bytes offloaded.
OffBytes = 0
# Bytes from the current file offloaded.
OffFileSize = 0
for File in BFiles:
OffFileSize = 0
Offloaded10 = 0
if len(File) == 0:
continue
Count += 1
Filename = File[FH_NAME]
BFileSize = File[FH_SIZE]
if BFileSize == -1:
logIt("%d/%d. Getting %s (?)..."%(Count, BFilesToOff, Filename))
else:
logIt("%d/%d. Getting %s (%s)..."%(Count, BFilesToOff, Filename, \
fmti(BFileSize)))
try:
urlretrieve("http://%s/%s"%(IPAddr, Filename), "%s%s"%(DirspecSDR, \
Filename), fileBlockRetrieved)
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
if Offloaded10 > 0:
stdout.write("\n")
logIt("ERROR: Retrieving file %s:"%Filename)
logIt("%s (got ~%s %s)"%(e, fmti(OffFileBytes), sP(OffFileBytes, \
("byte", "bytes"))))
logIt("")
exit(1)
if Offloaded10 > 0:
stdout.write("\n")
FileSize = getsize(DirspecSDR+Filename)
if BFileSize != -1:
if BFileSize != FileSize:
logIt("ERROR: Size error. %s: Baler %s, File %s"%(Filename, \
fmti(BFileSize), fmti(FileSize)))
logIt("")
exit(1)
# Same as above.
if FileSize < 4000:
logIt("STRANGE: File %s is only %s %s."%(Filename, fmti(FileSize), \
sP(FileSize, ("byte", "bytes"))))
OffFiles += 1
OffBytes += FileSize
logIt("Offloaded %s %s, %s %s.\a"%(fmti(OffFiles), sP(OffFiles, ("file", \
"files")), fmti(OffBytes), sP(OffBytes, ("byte", "bytes"))))
logIt("")
exit(0)
# END: main
# END PROGRAM: BLINE