Changeset 308 in lmdz_wrf
- Timestamp:
- Feb 26, 2015, 11:12:54 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r307 r308 2647 2647 self.attributes = attvar 2648 2648 if not searchInlist(attvar, 'units'): 2649 print errormsg 2650 print ' cls_time_information: time variable "', tname, '" does not have attribute: "units"' 2651 quit(-1) 2649 print warnmsg 2650 print ' cls_time_information: time variable "', tname, \ 2651 '" does not have attribute: "units"' 2652 units = None 2652 2653 else: 2653 2654 units = times.getncattr('units') 2654 2655 2655 self.unitsval = units 2656 2657 if not searchInlist(attvar, 'calendar'): 2658 print warnmsg 2659 print ' cls_time_information: time variable "', tname, '" does not have attribute: "calendar"' 2660 self.calendar = '-' 2661 else: 2662 self.calendar = times.getncattr('calendar') 2656 self.unitsval = units 2657 2658 if not searchInlist(attvar, 'calendar'): 2659 print warnmsg 2660 print ' cls_time_information: time variable "', tname, \ 2661 '" does not have attribute: "calendar"' 2662 self.calendar = '-' 2663 else: 2664 self.calendar = times.getncattr('calendar') 2663 2665 2664 txtunits = units.split(' ')2665 self.units = txtunits[0]2666 Srefdate = txtunits[len(txtunits) - 1]2666 txtunits = units.split(' ') 2667 self.units = txtunits[0] 2668 Srefdate = txtunits[len(txtunits) - 1] 2667 2669 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 2668 2670 ## 2669 timeval = Srefdate.find(':')2670 2671 if not timeval == -1:2671 timeval = Srefdate.find(':') 2672 2673 if not timeval == -1: 2672 2674 # print ' refdate with time!' 2673 self.refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate) 2674 self.Urefdate = txtunits[len(txtunits) - 2] + ' ' + Srefdate 2675 else: 2676 self.refdate = datetimeStr_datetime(Srefdate + '_00:00:00') 2677 self.Urefdate = Srefdate 2678 2679 self.Srefdate = self.refdate.strftime("%Y%m%d%H%M%S") 2680 timev = times[:] 2681 2682 self.dimt = times.shape[0] 2683 self.dt = timev[1]-timev[0] 2684 self.firstTu = timev[0] 2685 self.firstTt = timeref_datetime(self.refdate, self.firstTu, self.units) 2686 self.firstTS = self.firstTt.strftime("%Y%m%d%H%M%S") 2687 self.firstTm = timeref_datetime_mat(self.refdate, self.firstTu, self.units) 2688 self.lastTu = timev[self.dimt-1] 2689 self.lastTt = timeref_datetime(self.refdate, self.lastTu, self.units) 2690 self.lastTS = self.lastTt.strftime("%Y%m%d%H%M%S") 2691 self.lastTm = timeref_datetime_mat(self.refdate, self.lastTu, self.units) 2675 self.refdate = datetimeStr_datetime(txtunits[len(txtunits) - 2]+\ 2676 '_' + Srefdate) 2677 self.Urefdate = txtunits[len(txtunits) - 2] + ' ' + Srefdate 2678 else: 2679 self.refdate = datetimeStr_datetime(Srefdate + '_00:00:00') 2680 self.Urefdate = Srefdate 2681 2682 self.Srefdate = self.refdate.strftime("%Y%m%d%H%M%S") 2683 timev = times[:] 2684 2685 self.dimt = times.shape[0] 2686 self.dt = timev[1]-timev[0] 2687 self.firstTu = timev[0] 2688 self.firstTt = timeref_datetime(self.refdate, self.firstTu,self.units) 2689 self.firstTS = self.firstTt.strftime("%Y%m%d%H%M%S") 2690 self.firstTm = timeref_datetime_mat(self.refdate, self.firstTu, \ 2691 self.units) 2692 self.lastTu = timev[self.dimt-1] 2693 self.lastTt = timeref_datetime(self.refdate, self.lastTu, self.units) 2694 self.lastTS = self.lastTt.strftime("%Y%m%d%H%M%S") 2695 self.lastTm = timeref_datetime_mat(self.refdate, self.lastTu, \ 2696 self.units) 2697 2698 return 2692 2699 2693 2700 def time_information(ncfu, tname): … … 11739 11746 """ Function to return a slice of a given variable according to values to its 11740 11747 dimensions 11741 slice_variable(varobj, dims )11748 slice_variable(varobj, dimslice) 11742 11749 varobj= object wit the variable 11743 11750 dimslice= [[dimname1]:[value1]|[[dimname2]:[value2], ...] pairs of dimension … … 11746 11753 * -1: all along the dimension 11747 11754 * -9: last value of the dimension 11748 * [beg] :[end] slice from [beg] to [end]11755 * [beg]@[end] slice from [beg] to [end] 11749 11756 """ 11750 11757 fname = 'slice_variable' … … 11769 11776 dimcutv = dimsl[idc].split(':')[1] 11770 11777 if vardims[idd] == dimcutn: 11771 posfrac = dimcutv.find(' :')11778 posfrac = dimcutv.find('@') 11772 11779 if posfrac != -1: 11773 inifrac = int(dimcutv.split(' :')[0])11774 endfrac = int(dimcutv.split(' :')[1])11780 inifrac = int(dimcutv.split('@')[0]) 11781 endfrac = int(dimcutv.split('@')[1]) 11775 11782 varvalsdim.append(slice(inifrac,endfrac)) 11776 11783 dimnslice.append(vardims[idd]) … … 13416 13423 13417 13424 return 13425 13426 def ncstediffp(values, ncfile, varn): 13427 """ Function to compute differencies between time-steps (deacumulate) a netCDF file 13428 new variables labelled [varn] + 'de'will be added to the file 13429 values= [tdimn],[tvarn] 13430 tdimn: name of the dimension time 13431 tvarn: name of the variable time 13432 ncfile= netCDF file to replace something 13433 varn= ',' list of variables to deaccumulate ('all' for all) 13434 ncstediffp('Time,Times', '/home/lluis/PY/wrfout_d01_2001-11-11_00:00:00.tests', 'Q2') 13435 """ 13436 fname = 'ncstediffp' 13437 13438 if values == 'h': 13439 print fname + '_____________________________________________________________' 13440 print ncstediffp.__doc__ 13441 quit() 13442 13443 expectargs = '[tdimn],[tvarn]' 13444 13445 check_arguments(fname,len(expectargs.split(',')),values,',',expectargs) 13446 13447 tdimn = values.split(',')[0] 13448 tvarn = values.split(',')[1] 13449 13450 onc = NetCDFFile(ncfile, 'a') 13451 13452 if varn.find(',') != -1: 13453 if varn != 'all': 13454 varns = varn.slipt(',') 13455 else: 13456 varns = onc.variables 13457 else: 13458 varns = [varn] 13459 13460 otdim = onc.dimensions[tdimn] 13461 otvar = onc.variables[tvarn] 13462 timeinf = cls_time_information(onc, tvarn) 13463 13464 dimt = len(otdim) 13465 13466 print ' ' + fname + ': deaccumulating _______' 13467 for vn in varns: 13468 print " '" + vn + "'" 13469 ovar = onc.variables[vn] 13470 varinf = variable_inf(ovar) 13471 13472 if not searchInlist(varinf.dimns, tdimn): 13473 print warnmsg 13474 print ' ' + fname + ": variable '" + vn + "' has not time dimension '"+\ 13475 tdimn + "' !!" 13476 print ' skipping it' 13477 else: 13478 slicefwd, dfwd = slice_variable(ovar, tdimn + ':1@'+str(dimt)) 13479 slicebck, dbck = slice_variable(ovar, tdimn + ':0@'+str(dimt-1)) 13480 13481 print slicefwd[:,15,15] - slicebck[:,15,15] 13482 13483 deaccum = np.zeros((varinf.dims), dtype=varinf.dtype) 13484 ddeaccum = [] 13485 for idv in range(varinf.Ndims): 13486 if varinf.dimns[idv] == tdimn: 13487 ddeaccum.append(slice(1,varinf.dims[idv])) 13488 else: 13489 ddeaccum.append(slice(0,varinf.dims[idv])) 13490 13491 deaccum[tuple(ddeaccum)] = slicefwd - slicebck 13492 13493 if not searchInlist(onc.variables, vn + 'de'): 13494 if varinf.FillValue is not None: 13495 newvar = onc.createVariable(vn + 'de', varinf.dtype, \ 13496 tuple(varinf.dimns), fill_value = varinf.FillValue) 13497 else: 13498 newvar = onc.createVariable(vn + 'de', varinf.dtype, \ 13499 tuple(varinf.dimns)) 13500 newvar[:] = deaccum 13501 else: 13502 devar = onc.variables[vn + 'de'] 13503 devar = deaccum 13504 13505 newattr = set_attribute(newvar, 'deaccumulated', 'True') 13506 if timeinf.dt is not None: 13507 newattr = set_attributek(newvar, 'time_step_diff', timeinf.dt, \ 13508 'npfloat') 13509 13510 for attrn in varinf.attributes: 13511 attrv = ovar.getncattr(attrn) 13512 newattr = set_attribute(newvar, attrn, attrv) 13513 13514 onc.sync() 13515 13516 onc.close() 13517 13518 return 13519 ncstediffp('Time,Times', '/home/lluis/PY/wrfout_d01_2001-11-11_00:00:00.tests', 'Q2') 13520 quit() 13418 13521 13419 13522 #quit()
Note: See TracChangeset
for help on using the changeset viewer.