简体   繁体   中英

How to print intervals of data in a for loop (if else statement)? Python Question

Hi everyone! I read in a CDF file with the date, time, latitude, and longitude of a satellite in orbit. I am trying to write up a txt file that saves the data every time the satellite hovers over an Arctic station (I have it's LAT and LON with a small error value).

I managed to save a txt file with the appropriate conditions, but it is way too much data to look through and outputs this:

2013-01-09 02:05:00 -57.097008 40.165134

2013-01-09 02:06:00 -57.29849 40.504826

2013-01-09 02:07:00 -57.49554 40.834732

2013-01-09 02:08:00 -57.68884 41.154243

2013-01-09 02:09:00 -57.881386 41.45857

etc.

Several years of data equals way too much to handle... Essentially, I want to find a way for the code to show intervals of time. So instead of minute by minute, it would be more like:

2013-09-01 02:00:00 ~ 2013-09-01 02:14:00

2013-10-05 04:23:00 ~ 2013-10-05 04:32:00

The code I used was:

From spacepy import cdf
import datetime
from numpy import *
import numpy as np

cdf = pycdf.CDF('/directory/file.cdf')
print(cdf)


TIME = cdf['EPOCH']
LAT = cdf['SouthBtrace_GM_LAT']
LON = cdf['SouthBTrace_GM_LON']    

#Loop Count
count = 0
    
with open("example.txt", "a") as testing:
    for i, j in zip(LAT, LON):
        if(-65.26 <= i <= -59.26 and 32.31 <= j <= 52.31):            
            count +=1
            n = count
            testing.write(str(TIME[n-1])+'\t'+str(i)+'\t'+str(j)+ '\n')
               
           
        else:
            count +=1

Apologies if my question is confusing. I am new to programming and never used stackoverflow either.

Edit:

>> type(TIME[0])
   datetime.datetime

Well, my thought would be that you add in a time parameter and an additional 'if' condition that's met only when the time difference is great enough (10 minutes, or 15 minutes, or whatever you want). So, like:

import datetime
delta = datetime.timedelta(seconds = 600) ## the time gap that you want to use
time_one = datetime.datetime(2020,9,16,5,26,0) ## some arbitrary starting value
with open("example.txt", "a") as testing:
    for i, j in zip(LAT, LON):
        if(-65.26 <= i <= -59.26 and 32.31 <= j <= 52.31):
            if TIME > time_one + delta: 
                count +=1
                n = count
                testing.write(str(TIME[n-1])+'\t'+str(i)+'\t'+str(j)+ '\n')
                time_one = TIME  ## resets the time_one value to the new time
        else:
            count +=1

That way, no new data will get written to the txt file unless the time value is at least 600 seconds (assuming the EPOCH value is in seconds ... if not, obviously use the appropriate units) later than the previous written entry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM