sw_stop.py 1.62 KB
# sw_stop.py
# script to get SW Stop time from OMNI HOUR dataset (omni_hour_all)
# normally used in depotDECODER while updating OMNI
# arg : nc file name

from netCDF4 import Dataset
import numpy as np
from datetime import datetime, date, time, timedelta
import sys

def main():

	filename = sys.argv[1]

	data = Dataset(filename, "r", format="NETCDF4")

	density = np.array(data['N'][:])        
        densityIndex= np.array(np.where(~np.isnan(density)))
        maxdens = 0

        if densityIndex.shape[1] > 0 :
	   maxdens = densityIndex.max()
  
	bx = np.array(data['B'][:]).T[0]
        bxIndex = np.array(np.where(~np.isnan(bx)))
        maxbx = 0
        
        if bxIndex.shape[1] > 0 :
	   maxbx = bxIndex.max()
             
	stopIndex = np.minimum(maxdens,maxbx)
       
        if stopIndex == 0 :
           sys.exit()

	ddstopTime = np.array(data['Time'][:])[stopIndex]
	ddstopTime = ''.join(ddstopTime)
	# print ddstopTime

	dtstopTime = ddTime2Datetime([ddstopTime,])[0]

	dtstopTime = dtstopTime.isoformat()
	print dtstopTime

	# with open('OMNI_stop.txt','w') as omniStop:
	# 	omniStop.write(dtstopTime)
	# 	omniStop.close()

def ddTime2Datetime(ddTime):
    myDatetime = []
    for t in ddTime:
        year = int(t[0:4])
        day = int(t[4:7]) + 1
        myDate = datetime(year, 1, 1) + timedelta(day - 1)
        
        hour = int(t[7:9])
        minute = int(t[9:11])
        seconds = int(t[11:13])
        ms = int(t[13:16])*1000
        myTime = time(hour, minute,seconds,ms)
        
        myDatetime.append( datetime.combine(myDate, myTime) )
            
    return myDatetime

if __name__ == '__main__':
	main()