Changeset 1627 in lmdz_wrf
- Timestamp:
- Sep 10, 2017, 9:02:02 PM (8 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r1597 r1627 16 16 ## e.g. # nc_var.py -o varaddattrk -f SouthAm_Climzones.nc -S 'longitude_of_central_meridian|-60.0|R' -v Mercator 17 17 ## e.g. # nc_var.py -o setvar_asciivalues -S 'nombres_zonas.dat' -f SouthAm_Climzones.nc -v regionn 18 ## e.g. # nc_var.py -o WRF_to_newCF -f wrfout_d01_1995-01-01_00\:00\:00 -S 'XLONG:XLAT:Times:19491201000000:minutes' -v QVAPOR,T2,Q2 18 19 19 20 ## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1 … … 167 168 # WRF_CFxtime_creation: Function to add a CF-convention time unit in a WRF file using variable 'XTIME' 168 169 # WRF_toCF: Function to pass a WRF original file to CF-conventions 170 # WRF_to_newCF: Function to pass a WRF original file to new CF-conventions file 169 171 170 172 operations=['addDim', 'addVar', 'addvals', 'CDO_toCF', 'chdimname', 'changevartype', \ … … 199 201 'vrattr', 'WRF_d0Nref', \ 200 202 'WRF_CFlonlat_creation', 'WRF_CFtime_creation', 'WRF_CFxtime_creation', \ 201 'list_operations', 'WRF_toCF' ]203 'list_operations', 'WRF_toCF', 'WRF_to_newCF'] 202 204 203 205 ### Options … … 491 493 elif oper == 'WRF_toCF': 492 494 ncvar.WRF_toCF(opts.values, opts.ncfile) 495 elif oper == 'WRF_to_newCF': 496 ncvar.WRF_to_newCF(opts.values, opts.ncfile, opts.varname) 493 497 else: 494 498 print errormsg -
trunk/tools/nc_var_tools.py
r1626 r1627 18537 18537 print fname + ": successful written of '" + ofile + "' !!" 18538 18538 18539 def WRF_to_newCF(values, ncfile, variables): 18540 """ Function to pass a WRF original file to new CF-conventions file 18541 values= [wlonn]:[wlatn]:[wtimen]:[refDateTime]:[Tunits] 18542 [wlonn]: WRF longitude var name (it might come from WPS!) 18543 [wlatn]: WRF latitude var name (it might come from WPS!) 18544 [wtimen]: WRF time var name 18545 [refDateTime]: date of reference for times (in [YYYY][MM][DD][HH][MI][SS] format) 18546 [Tunits]: units for time variable 18547 ncfile= file to transform 18548 variables= ',' list of variables to transform ('all', for all variables) 18549 """ 18550 fname = 'WRF_to_newCF' 18551 18552 if values == 'h': 18553 print fname + '_____________________________________________________________' 18554 print WRF_to_newCF.__doc__ 18555 quit() 18556 18557 arguments = '[wlonn]:[wlatn]:[wtimen]:[refDateTime]:[Tunits]' 18558 gen.check_arguments(fname, values, arguments, ':') 18559 18560 ofile = 'CF_WRF.nc' 18561 18562 # New names for certain dimensions 18563 dimsrename = {'west_east': 'lon', 'south_north': 'lat', 'Time':'time'} 18564 18565 # Variables to not be included 18566 varsNO = ['XLONG', 'XLONG_M', 'XLAT', 'XLAT_M', 'Times'] 18567 18568 onewnc = NetCDFFile(ofile, 'w') 18569 18570 wlonn = values.split(':')[0] 18571 wlatn = values.split(':')[1] 18572 wtimen = values.split(':')[2] 18573 refDateTime = values.split(':')[3] 18574 Tunits = values.split(':')[4] 18575 18576 wrfnc = NetCDFFile(ncfile, 'r') 18577 18578 if variables == 'all': 18579 varns = wrnc.variables.keys() 18580 else: 18581 varns = gen.str_list(variables, ',') 18582 18583 # lon, lat 18584 if not gen.searchInlist(wrfnc.variables.keys(),wlonn): 18585 print errormsg 18586 print ' ' + fname + ": file '" + ncfile + "' has not lon variable '" + \ 18587 wlonn + "' !!!" 18588 quit(-1) 18589 18590 if not gen.searchInlist(wrfnc.variables.keys(),wlatn): 18591 print errormsg 18592 print ' ' + fname + ": file '" + ncfile + "' has not lat variable '" + \ 18593 wlatn + "' !!!" 18594 quit(-1) 18595 18596 if not gen.searchInlist(wrfnc.dimensions.keys(),'west_east'): 18597 print errormsg 18598 print ' ' + fname + ": file '" + ncfile + "' has not 'west_east' dimension !!!" 18599 quit(-1) 18600 18601 if not gen.searchInlist(wrfnc.dimensions.keys(),'south_north'): 18602 print errormsg 18603 print ' ' + fname + ": file '" + ncfile + "' has not 'south_north' dimension !!!" 18604 quit(-1) 18605 18606 lonobj = wrfnc.variables[wlonn] 18607 lonv = lonobj[:] 18608 18609 latobj = wrfnc.variables[wlatn] 18610 latv = latobj[:] 18611 18612 if len(lonv.shape) == 3: 18613 cflons = np.zeros((lonv.shape[1],lonv.shape[2]), dtype=np.float) 18614 cflons = np.where(lonv[0,:,:] < 0., lonv[0,:,:] + 360., lonv[0,:,:]) 18615 cflats = np.zeros((latv.shape[1],latv.shape[2]), dtype=np.float) 18616 cflats = latv[0,:,:] 18617 dimx = lonv.shape[2] 18618 dimy = lonv.shape[1] 18619 elif len(lonv.shape) == 2: 18620 cflons = np.zeros((lonv.shape), dtype=np.float) 18621 cflons = np.where(lonv < 0., lonv + 360., lonv) 18622 cflats = latv 18623 dimx = lonv.shape[1] 18624 dimy = lonv.shape[0] 18625 elif len(lonv.shape) == 1: 18626 cflons = np.zeros((lonv.shape), dtype=np.float) 18627 cflons = np.where(lonv < 0., lonv + 360., lonv) 18628 cflats = latv 18629 dimx = lonv.shape[0] 18630 dimy = latv.shape[0] 18631 else: 18632 print errormsg 18633 print ' ' + fname + ": original longitude variable '" + varn + "' shape:" + \ 18634 lonv.shape + "not ready !!!" 18635 quit(-1) 18636 18637 # Creation of CF-dimensions 18638 newdim = onewnc.createDimension('lon', dimx) 18639 newdim = onewnc.createDimension('lat', dimy) 18640 newdim = onewnc.createDimension('time', None) 18641 18642 # dimension-variables 18643 newvar = onewnc.createVariable('lon','f8',('lat','lon')) 18644 newattr = basicvardef(newvar, 'longitude','Longitude','degrees_East') 18645 newvar[:] = cflons 18646 newattr = set_attribute(newvar, 'axis', 'X') 18647 newattr = set_attribute(newvar, '_CoordinateAxisType', 'Lon') 18648 18649 newvar = onewnc.createVariable('lat','f8',('lat','lon')) 18650 newattr = basicvardef(newvar, 'latitude','Latitude','degrees_North') 18651 newvar[:] = cflats 18652 newattr = set_attribute(newvar, 'axis', 'Y') 18653 newattr = set_attribute(newvar, '_CoordinateAxisType', 'Lat') 18654 18655 onewnc.sync() 18656 18657 # CFing time 18658 if not gen.searchInlist(wrfnc.variables.keys(), wtimen): 18659 print errormsg 18660 print ' ' + fname + ": file '" + ncfile + "' has not time variable '" + \ 18661 wtimen + "' !!!" 18662 quit(-1) 18663 18664 if not gen.searchInlist(wrfnc.dimensions.keys(), 'Time'): 18665 print errormsg 18666 print ' ' + fname + ": file '" + ncfile + "' has not 'Time' dimension !!!" 18667 quit(-1) 18668 18669 timeobj = wrfnc.variables[wtimen] 18670 timev = timeobj[:] 18671 18672 fdates = wrfnc.variables['Times'][:] 18673 cftimes = [] 18674 for it in range(fdates.shape[0]): 18675 wrfdates = gen.datetimeStr_conversion(fdates[it,:], 'WRFdatetime', 'matYmdHMS') 18676 cftimes.append(gen.realdatetime1_CFcompilant(wrfdates, refDateTime, Tunits)) 18677 18678 yrref=refDateTime[0:4] 18679 monref=refDateTime[4:6] 18680 dayref=refDateTime[6:8] 18681 horref=refDateTime[8:10] 18682 minref=refDateTime[10:12] 18683 secref=refDateTime[12:14] 18684 18685 tunit = Tunits + ' since ' + yrref + '/' + monref + '/' + dayref + ' ' + \ 18686 horref + ':' + minref + ':' + secref 18687 18688 newvar = onewnc.createVariable('time','f8',('time')) 18689 newattr = basicvardef(newvar, 'time','Time',tunit) 18690 newvar[:] = cftimes 18691 newattr = set_attribute(newvar, 'axis', 'T') 18692 newattr = set_attribute(newvar, '_CoordinateAxisType', 'Time') 18693 newattr = set_attribute(newvar, 'calendar', 'gregorian') 18694 18695 onewnc.sync() 18696 18697 # Removing not desired variables 18698 for vrmn in varsNO: 18699 if gen.searchInlist(varns, vrmn): varns.remove(vrmn) 18700 18701 # Looping on variables 18702 for vn in varns: 18703 newvars = onewnc.variables.keys() 18704 newdims = list(onewnc.dimensions) 18705 if not gen.searchInlist(newvars,vn): 18706 varobj = wrfnc.variables[vn] 18707 varattrs = gen.variables_values(vn) 18708 vvatrs = varobj.ncattrs() 18709 18710 newvard = [] 18711 for dimn in varobj.dimensions: 18712 if dimsrename.has_key(dimn): 18713 newvard.append(dimsrename[dimn]) 18714 else: 18715 newvard.append(str(dimn)) 18716 if not gen.searchInlist(newdims, dimn): add_dims(wrfnc,onewnc,[str(dimn)]) 18717 18718 18719 cfn = varattrs[0] 18720 stn = varattrs[1] 18721 lonn = varattrs[4].replace('|',' ') 18722 un = varattrs[5].replace('|',' ') 18723 18724 newvar = onewnc.createVariable(cfn, nctype(varobj.dtype), tuple(newvard)) 18725 newvar[:] = varobj[:] 18726 18727 newattr = set_attribute(newvar, 'standard_name', stn) 18728 newattr = set_attribute(newvar, 'long_name', lonn) 18729 # Original attributes 18730 for attrn in vvatrs: 18731 attrv = varobj.getncattr(attrn) 18732 if attrn == 'units': 18733 if un != attrv: 18734 print warnmsg 18735 print ' ' + fname + ": WRF variable '" + vn + "' has different " + \ 18736 " units '" + attrv + "' than CF '" + un + "' !!" 18737 print ' keeping WRF ones' 18738 newattr = set_attribute(newvar, 'units', attrv) 18739 else: 18740 newattr = set_attribute(newvar, 'units', un) 18741 18742 newattr = set_attribute(newvar, 'coordinates', 'lon lat') 18743 onewnc.sync() 18744 18745 onewnc.close() 18746 wrfnc.close() 18747 18748 print fname + ": succesfull creation of file '" + ofile + "' !!" 18749 18750 return 18751 18539 18752 def WRF_toCF(values, ncfile): 18540 18753 """ Function to pass a WRF original file to CF-conventions
Note: See TracChangeset
for help on using the changeset viewer.