- Timestamp:
- Apr 24, 2019, 5:54:43 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r2465 r2468 34 34 # CDO_toCF: Function to pass a CDO output file to CF-conventions 35 35 # CFfile_creation: Operation to create a file folowing CF-conventions 36 # CFfile_fixTime: Operation to fix a netCDF file with a time axis folowing CF-conventions, but 37 # with a wrong set-up following a set of available operations 36 38 # CFmorzization: Function to provide a CF-compilation version of a variable within a file 37 39 # CFvars: Function to adapt a given variable and file following CF-standards … … 31504 31506 def CFfile_fixTime(values, ncfile, variable): 31505 31507 """ Operation to fix a netCDF file with a time axis folowing CF-conventions, but 31506 with a wrong set-up 31508 with a wrong set-up following a set of available operations 31507 31509 values= [operations] 31508 31510 [operations]: ';' list of names of operations to perform 31511 'setCalendar': re-set the calendar to 'gregorian' or alos know as 'standard' 31509 31512 'setRefDate',[YYYYMMDDHHMMSS]: re-set the reference date to [YYYYMMDDHHMMSS] 31510 'setCalendar': re-set the calendar to gregorian or standard 31513 'setTunits',[tunits]: re-set the temporal units. Accepted ones: 'weeks', 31514 'days', 'hours', 'minutes', 'seconds' 31511 31515 ncfile= name of the file to fix its time-axis 31512 31516 variable= name of the variable time 31513 31517 """ 31514 31518 fname = 'CFfile_fixTime' 31519 availop = ['setCalendar', "setRefDate,[YYYYMMDDHHMMSS]", "setTunits,[tunits]"] 31520 availTunits = ['weeks', 'days', 'hours', 'minutes', 'seconds'] 31515 31521 31516 31522 if values == 'h': … … 31523 31529 gen.check_arguments(fname,values,expectargs,':') 31524 31530 31525 operations = values.split(':')[0] 31531 # In case of multiple values 31532 #operations = values.split(':')[0].split(';') 31533 operations = values.split(';') 31526 31534 31527 31535 onc = NetCDFFile(ncfile,'a') 31528 if not onc.variables.has_key(variable): 31529 31536 varinfile(onc, ncfile, errormsg, '', variable) 31530 31537 31531 31538 otime = onc.variables[variable] 31532 31533 for op in operations: 31534 31535 31536 31539 timevs = otime[:] 31540 timeattrs = otime.ncattrs() 31541 if not gen.searchInlist(timeattrs,'units'): 31542 print errormsg 31543 print ' ' + fname + ": time variable '" + variable + "' without units !!" 31544 print ' a CF compilant time axis, must have units in format: [Tunits] ' + \ 31545 'since [YYYY]-[MM]-[DD] [HH]:[MM]:[SS]' 31546 quit(-1) 31547 tunits = otime.getncattr('units') 31548 ltunits = tunits.split(' ') 31549 if len(ltunits) < 3 or not gen.searchInlist(ltunits,'since'): 31550 print errormsg 31551 print ' ' + fname + ": wrong time units '" + tunits + "' !!" 31552 print ' a CF compilant time axis, must have units in format: [Tunits] ' + \ 31553 'since [YYYY]-[MM]-[DD] [HH]:[MM]:[SS]' 31554 quit(-1) 31555 if not gen.searchInlist(timeattrs,'calendar'): 31556 print warnmsg 31557 print ' ' + fname + ": time variable '" + variable + "' without calendar !!" 31558 print ' a CF compilant time axis, must have units with a calendar' 31559 print " imposing 'gregorian'" 31560 origcal = 'gregorian' 31561 else: 31562 origcal = otime.getncattr('calendar') 31563 31564 for opn in operations: 31565 if opn.count(',') != 0: 31566 lopn = opn.split(',') 31567 else: 31568 lopn = [opn] 31569 31570 # Fixing calendar 31571 if lopn[0] == 'setCalendar': 31572 refop = 'setCalendar' 31573 Nopreq = len(refop.split(',')) 31574 if len(lopn) != Nopreq: 31575 print errormsg 31576 print ' ' + fname + ": wrong number of terms for operation '" + \ 31577 lopn[0] + "' !!" 31578 print ' it requires:', Nopreq, 'and', len(lopn), 'were passed !' 31579 print " requested terms for the operation: '" + refpop + "'" 31580 quit(-1) 31581 print ' ' + fname + ": fixing calendar to 'gregorian'..." 31582 31583 newtimv = gen.change_CFcalendar(timevs, tunits, origcal) 31584 newtunits = tunits + '' 31585 31586 # Fixing reference date 31587 # python datetime only accepts dates >= 1900-Jan-1 31588 elif lopn[0] == 'setRefDate': 31589 refop = 'setRefDate,[YYYYMMDDHHMMSS]' 31590 Nopreq = len(refop.split(',')) 31591 if len(lopn) != Nopreq: 31592 print errormsg 31593 print ' ' + fname + ": wrong number of terms for operation '" + \ 31594 lopn[0] + "' !!" 31595 print ' it requires:', Nopreq, 'and', len(lopn), 'were passed !' 31596 print " requested terms for the operation: '" + refpop + "'" 31597 quit(-1) 31598 if len(lopn[1]) != len('YYYYMMDDHHMMSS'): 31599 print errormsg 31600 print ' ' + fname + ": wrong new reference date for operation '" + \ 31601 lopn[0] + "' !!" 31602 print ' it requires:', len('YYYYMMDDHHMMSS'), 'charactgers and', \ 31603 len(lopn[1]), 'were passed !' 31604 print " expected date format is 'YYYYMMDDHHMMSS'" 31605 quit(-1) 31606 31607 newRdate = gen.datetimeStr_conversion(lopn[1],'YmdHMS','Y-m-d H:M:S') 31608 print ' ' + fname + ": fixing Reference date to '" + newRdate + "'..." 31609 31610 newtimv, newtunits = gen.change_CFRefdate(timevs, tunits, newRdate) 31611 31612 # Fixing units 31613 elif lopn[0] == 'setTunits': 31614 refop = 'setTunits,[Tunits]' 31615 Nopreq = len(refop.split(',')) 31616 if len(lopn) != Nopreq: 31617 print errormsg 31618 print ' ' + fname + ": wrong number of terms for operation '" + \ 31619 lopn[0] + "' !!" 31620 print ' it requires:', Nopreq, 'and', len(lopn), 'were passed !' 31621 print " requested terms for the operation: '" + refpop + "'" 31622 quit(-1) 31623 if not gen.searchInlist(availTunits,lopn[1]): 31624 print errormsg 31625 print ' ' + fname + ": wrong new time-units '" + lopn[1] + "' !!" 31626 print ' avaialable ones:', availTunits 31627 quit(-1) 31628 31629 print ' ' + fname + ": fixing temporal units to '" + lopn[1] + "'..." 31630 newtimv, newtunits = gen.change_CFTunits(timevs, tunits, lopn[1]) 31631 31632 else: 31633 print errormsg 31634 print ' ' + fname + ": operation '" + lopn[0] + "' not ready !!" 31635 print ' available ones:', availop 31636 quit(-1) 31637 timevs = newtimv*1. 31638 tunits = newtunits + '' 31639 31640 # Changing values in file 31641 otime[:] = timevs 31642 otime.setncattr('units', tunits) 31643 31644 onc.sync() 31645 31646 # Adding PyNCplot transformation 31647 add_global_PyNCplot(onc, 'nc_var_tools', fname, '1.0') 31648 31649 onc.sync() 31650 31651 onc.close() 31652 print fname + ": successfull correction of time-axis of file '" + ncfile + "' !!" 31653 31654 return 31655 31656 #timesv = [56649.5, 56680, 56710.5, 56741, 56771.5, 56802.5, 56833, 56863.5, 56894, 56924.5] 31657 #dimt = len(timesv) 31658 #values='lon|360,lat|180,time|None@' + str(dimt) + ':global' 31659 #varsS='tas#time;lat;lon#f#None,time#time#f8#units|days!since!1850-01-01!00:00:00|S' 31660 #CFfile_creation(values, 'CFtest.nc', varsS) 31661 #fname = 'timev1850-2005.dat' 31662 #of = open(fname, 'w') 31663 #for tv in timesv: 31664 # of.write(str(tv) + '\n') 31665 #of.close() 31666 #setvar_asciivalues(fname, 'CFtest.nc', 'time') 31667 #values="setCalendar;setRefDate,19491201000000;setTunits,hours" 31668 #varaddattr('calendar|noleap', 'CFtest.nc', 'time') 31669 #CFfile_fixTime(values, 'CFtest.nc', 'time') 31537 31670 31538 31671 #quit()
Note: See TracChangeset
for help on using the changeset viewer.