Changeset 2177 in lmdz_wrf for trunk/tools
- Timestamp:
- Oct 11, 2018, 1:50:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r2176 r2177 57 57 58 58 ####### Content 59 # advance_date: Function to advance a date object a certain given units of time 59 60 # advance_matDate: Function to advance matrix-date with a matrix-increment 60 61 # angle_DegMinSec: Function to transform an angle to Degrees Minutes Seconds … … 14371 14372 # etime = tv[timeslc[1]] 14372 14373 # print islc, ':', itime, '-', etime, '<>', tvals, '=', tstat[islc] 14373 14374 14374 14375 def Latin_Greek(char): 14375 14376 """ Function to pass an ASCII latin character to its equivalent greek … … 14452 14453 return vals 14453 14454 14455 def advance_date(date, Tunit, NTunits): 14456 """ Function to advance a date a certain given units of time 14457 date: datetime object to advance 14458 Tunit: units of time (one from 'years', 'months', 'days', 'minutes', 'hours', 14459 'seconds') 14460 NTunits: number of [Tunit] to advance 14461 >>> advance_date(dt.datetime(1976,02,17,8,30,0), 'seconds', 25) 14462 1976-02-17 08:30:25 14463 >>> advance_date(dt.datetime(1976,02,17,8,30,0), 'seconds', 75) 14464 1976-02-17 08:31:15 14465 >>> advance_date(dt.datetime(1976,02,17,8,30,0), 'seconds', 2500) 14466 1976-02-17 09:11:40 14467 >>> advance_date(dt.datetime(1976,02,17,8,30,0), 'seconds', 3600*20) 14468 1976-02-18 04:30:00 14469 >>> advance_date(dt.datetime(1976,02,17,8,30,0), 'seconds', 24*3600*100) 14470 1976-05-27 08:30:00 14471 >>> advance_date(dt.datetime(1976,02,17,8,30,0), 'seconds', 24*3600*1000) 14472 1978-11-13 08:30:00 14473 """ 14474 import datetime as dt 14475 fname = 'advance_date' 14476 14477 availtunits = ['years', 'months', 'days', 'minutes', 'hours', 'seconds'] 14478 14479 mdate = [date.year, date.month, date.day, date.hour, date.minute, date.second] 14480 14481 if Tunit == 'years': 14482 idate = 0 14483 elif Tunit == 'months': 14484 idate = 1 14485 elif Tunit == 'days': 14486 idate = 2 14487 elif Tunit == 'hours': 14488 idate = 3 14489 elif Tunit == 'minutes': 14490 idate = 4 14491 elif Tunit == 'seconds': 14492 idate = 5 14493 else: 14494 print errormsg 14495 print ' ' + fname + ": time units '" + uints + "' not ready !!" 14496 print ' available ones:', availtunits 14497 quit(-1) 14498 14499 thresholds = [100000, 13, days_month(mdate[0],mdate[1])+1, 24, 60, 60] 14500 mins = [-100000., 1., 1., 0., 0., 0.] 14501 14502 14503 ndate = mdate + [] 14504 ndate[idate] = ndate[idate] + NTunits 14505 14506 for idd in range(5,0,-1): 14507 if ndate[idd] >= thresholds[idd]: 14508 Nthres = int(ndate[idd]/thresholds[idd]) 14509 14510 if idd == 2 and Nthres > 1: 14511 TOTthres = thresholds[idd] - 1 14512 year = ndate[0] 14513 mon = ndate[1] 14514 while TOTthres < ndate[idd]: 14515 if mon + 1 > 12: 14516 mon = 1 14517 year = year + 1 14518 else: 14519 mon = mon + 1 14520 dmon = days_month(year,mon) 14521 if TOTthres + dmon > ndate[idd]: break 14522 else: 14523 TOTthres = TOTthres + dmon 14524 ndate[idd] = ndate[idd] - TOTthres 14525 ndate[1] = mon 14526 ndate[0] = year 14527 else: 14528 ndate[idd] = int(np.max([ndate[idd] - thresholds[idd]*Nthres*1., \ 14529 mins[idd]])) 14530 ndate[idd-1] = ndate[idd-1] + Nthres 14531 14532 newdate = dt.datetime(year=ndate[0], month=ndate[1], day=ndate[2], \ 14533 hour=ndate[3], minute=ndate[4], second=ndate[5]) 14534 14535 return newdate 14536 14537 def generate_CFtimes(datei, datef, units, freq, dateref='19491201000000'): 14538 """ Function to generate CFtimes for a given period, frequency and units 14539 datei: initial date of the period ([YYYY][MM][DD][HH][MI][SS] format) 14540 datef: final date of the period ([YYYY][MM][DD][HH][MI][SS] format) 14541 units: units of time (one from 'years', 'months', 'days', 'minutes', 'hours', 14542 'seconds') 14543 Tfreq: frequency of time-steps in number of [units] time 14544 dateref: reference date ([YYYY][MM][DD][HH][MI][SS] format) 14545 """ 14546 import datetime as dt 14547 fname = 'generate_CFtimes' 14548 14549 availtunits = ['years', 'months', 'days', 'minutes', 'hours', 'seconds'] 14550 14551 idate = dt.datetime.strptime(datei, '%Y%m%d%H%M%S') 14552 edate = dt.datetime.strptime(datef, '%Y%m%d%H%M%S') 14553 refdate = dt.datetime.strptime(dateref, '%Y%m%d%H%M%S') 14554 refdateS = refdate.strftime("%Y-%m-%d %H:%M:%S") 14555 14556 delta = idate - refdate 14557 # Python version depending 14558 if searchInlist(dir(delta), 'total_seconds'): 14559 diffT = delta.total_seconds() 14560 else: 14561 diffT = delta.days*24*3600. + delta.seconds 14562 14563 cftimes = [diffT] 14564 14565 date = idate 14566 while date < edate: 14567 date = advance_date(date, units, freq) 14568 14569 delta = date - refdate 14570 # Python version depending 14571 if searchInlist(dir(delta), 'total_seconds'): 14572 diffT = delta.total_seconds() 14573 else: 14574 diffT = delta.days*24*3600. + delta.seconds 14575 cftimes.append(diffT) 14576 print 'Lluis:', date, ':', diffT, 'frq:', freq 14577 14578 delta = edate - refdate 14579 # Python version depending 14580 if searchInlist(dir(delta), 'total_seconds'): 14581 diffT = delta.total_seconds() 14582 else: 14583 diffT = delta.days*24*3600. + delta.seconds 14584 14585 cftimes.append(diffT) 14586 timeu = units + ' since ' + refdateS 14587 14588 return cftimes, timeu 14589 14590 print generate_CFtimes('20021201000000', '20030228000000', 'days', 1) 14591 14454 14592 #quit() 14455 14593
Note: See TracChangeset
for help on using the changeset viewer.