Changeset 2225 in lmdz_wrf
- Timestamp:
- Nov 9, 2018, 7:16:15 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r2224 r2225 28 28 ## e.g. # nc_var.py -o join_singlestation_obsfiles -S 'obs/sfc_CAM:OBSnetcdf' -v all 29 29 ## e.g. # nc_var.py -o join_sounding_obsfiles -S .:UWyoming_snd_1 -v all 30 ## e.g. # nc_var.py -o timemean -f selvar_new.nc -S 2:XLONG,XLAT -v T2 30 31 31 32 ## e.g. ccrc468-17 # ./nc_var.py -v time -f 123/CCRC_NARCliM_Sydney_All_1990-1999_pr10max.nc -o out -S 1:-1 -
trunk/tools/nc_var_tools.py
r2224 r2225 168 168 # TimeInf: Function to print all the information from the variable time 169 169 # time_information: Function to provide information about variable time 170 # timemean: Function to retrieve a time mean series from a multidimensional variable of a file 170 # timemean: Function to retrieve a series of time-statistics ('min', 'max', 'mean', 171 # 'mean2', 'stdv', 'quant','linregress','polregress') from a multidimensional 172 # variable of a file 171 173 # time_reset: Function to re-set the time axis of a file 172 174 # timeshiftvar: Function to temporaly shift a number of time-steps a given variable inside a netCDF file … … 3021 3023 3022 3024 attvar = times.ncattrs() 3023 if not searchInlist(attvar, 'units'):3025 if not gen.searchInlist(attvar, 'units'): 3024 3026 print errormsg 3025 3027 print ' spacemean: "time" does not have attribute: "units"' … … 3119 3121 var = ncf.variables[varn] 3120 3122 varinf = variable_inf(var) 3121 if searchInlist(varinf.attributes, 'scale_factor'):3123 if gen.searchInlist(varinf.attributes, 'scale_factor'): 3122 3124 scalefact = var.getncattr('scale_factor') 3123 3125 print ' spacemean: data has a scale factor of ', scalefact … … 3126 3128 scalefact = 1. 3127 3129 3128 if searchInlist(varinf.attributes, 'add_offset'):3130 if gen.searchInlist(varinf.attributes, 'add_offset'): 3129 3131 offset = var.getncattr('add_offset') 3130 3132 print ' spacemean: data has an offset of ', offset … … 3187 3189 varval = var[it,:,:] 3188 3190 percendone(it,dimt,5,'computed space statistics') 3189 varst = statsVal(varval)3191 varst = gen.statsVal(varval) 3190 3192 varstwgt = statsValWeigthed(varval, weightsv) 3191 3193 varstats[it,0] = varst.minv … … 3216 3218 varval = var[it,ik,:,:] 3217 3219 percendone(it*vardims[1]+ik,dimt*vardims[1],5,'computed space statistics') 3218 varst = statsVal(varval)3220 varst = gen.statsVal(varval) 3219 3221 varstwgt = statsValWeigthed(varval, weightsv) 3220 3222 varstats[it,ik,0] = varst.minv … … 3247 3249 3248 3250 percendone(it*vardims[1]*vardims[2]+ik*vardims[1]+il,dimt*vardims[1]*vardims[2],5,'computed space statistics') 3249 varst = statsVal(varval)3251 varst = gen.statsVal(varval) 3250 3252 varstwgt = statsValWeigthed(varval, weightsv) 3251 3253 varstats[it,ik,il,0] = varst.minv … … 3280 3282 for idim in range(Nvardnames): 3281 3283 rdim = vardimnames[idim] 3282 if not searchInlist(newdims, rdim):3284 if not gen.searchInlist(newdims, rdim): 3283 3285 if not rdim == 'time': 3284 3286 print ' spacemean: Adding dimension ' + rdim … … 3294 3296 # Checking fill value 3295 3297 ## 3296 if searchInlist(varattr, '_FillValue'):3298 if gen.searchInlist(varattr, '_FillValue'): 3297 3299 varfil = var._FillValue 3298 3300 else: … … 3322 3324 newvarattrs = newvar.ncattrs() 3323 3325 attrv = var.getncattr(attr) 3324 if not searchInlist(newvarattrs, attr):3326 if not gen.searchInlist(newvarattrs, attr): 3325 3327 if attr != 'scale_factor' and attr != 'add_offset' and attr != 'valid_range' \ 3326 3328 and attr != 'unpacked_valid_range' and attr != 'actual_range' : … … 3352 3354 newvar = ncfo.variables['time'] 3353 3355 newvarattrs = newvar.ncattrs() 3354 if searchInlist(newvarattrs, 'bounds'):3356 if gen.searchInlist(newvarattrs, 'bounds'): 3355 3357 if newvar.getncattr('bounds') == 'time_bnds': 3356 3358 ncf = NetCDFFile(ncfile,'r') … … 3379 3381 3380 3382 def timemean(values, ncfile, varn): 3381 """ Function to retrieve a time mean series from a multidimensional variable of a file 3382 values = power of the polynomial fitting with time to be applied 3383 ncfile = netCDF file name 3384 varn = variable name 3385 """ 3386 import datetime as dt 3387 import calendar as cal 3388 3389 powerv=int(values) 3390 ofile = 'timemean_' + varn + '.nc' 3391 varfil=1.e20 3392 statsn = ['min', 'max', 'mean', 'mean2', 'stdv', 'quant','linregress','polregress'] 3393 statslongn = ['minimum', 'maximum', 'mean', 'quadratic mean', 'standard deviation', 'quantiles', \ 3394 'linear regression', 'polynomial regression'] 3395 3396 ncf = NetCDFFile(ncfile,'r') 3397 3398 if not ncf.variables.has_key(varn): 3399 print errormsg 3400 print ' timemean: File "' + ncfile + '" does not have variable ' + varn + ' !!!!' 3401 print errormsg 3402 ncf.close() 3403 quit(-1) 3404 3405 times = ncf.variables['time'] 3406 timevals = times[:] 3407 3408 attvar = times.ncattrs() 3409 if not searchInlist(attvar, 'units'): 3410 print errormsg 3411 print ' timemean: "time" does not have attribute: "units"' 3412 ncf.close() 3413 quit(-1) 3414 else: 3415 units = times.getncattr('units') 3383 """ Function to retrieve a series of time-statistics ('min', 'max', 'mean', 3384 'mean2', 'stdv', 'quant','linregress','polregress') from a multidimensional 3385 variable of a file 3386 values = [powerfit]:[addvars] 3387 [powerfit]: power of the polynomial fitting with time to be applied 3388 [addvars]: ',' list of variables to be added at the final file from the original 3389 file ('all', for all variables) 3390 ncfile = netCDF file name 3391 varn = variable name 3392 """ 3393 fname = 'timemean' 3394 3395 # This is an old script!! too long !!! Sorry !! 3396 3397 import datetime as dt 3398 import calendar as cal 3416 3399 3417 txtunits = units.split(' ') 3418 tunits = txtunits[0] 3419 Srefdate = txtunits[len(txtunits) - 1] 3420 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 3400 if values == 'h': 3401 print fname + '_____________________________________________________________' 3402 print same_deltasign.__doc__ 3403 quit() 3404 3405 expectargs = '[powerfit]:[addvars]' 3406 gen.check_arguments(fname, values, expectargs, ':') 3407 3408 powerv = int(values.split(':')[0]) 3409 addvars = values.split(':')[1] 3410 3411 ofile = 'timemean_' + varn + '.nc' 3412 varfil=gen.fillValueF 3413 statsn = ['min', 'max', 'mean', 'mean2', 'stdv', 'quant','linregress','polregress'] 3414 statslongn = ['minimum', 'maximum', 'mean', 'quadratic mean', 'standard deviation', 'quantiles', \ 3415 'linear regression', 'polynomial regression'] 3416 3417 ncf = NetCDFFile(ncfile,'r') 3418 3419 if not ncf.variables.has_key(varn): 3420 print errormsg 3421 print ' timemean: File "' + ncfile + '" does not have variable ' + varn + ' !!!!' 3422 print errormsg 3423 ncf.close() 3424 quit(-1) 3425 3426 times = ncf.variables['time'] 3427 timevals = times[:] 3428 3429 attvar = times.ncattrs() 3430 if not gen.searchInlist(attvar, 'units'): 3431 print errormsg 3432 print ' timemean: "time" does not have attribute: "units"' 3433 ncf.close() 3434 quit(-1) 3435 else: 3436 units = times.getncattr('units') 3437 3438 txtunits = units.split(' ') 3439 tunits = txtunits[0] 3440 Srefdate = txtunits[len(txtunits) - 1] 3441 # Does reference date contain a time value [YYYY]-[MM]-[DD] [HH]:[MI]:[SS] 3421 3442 ## 3422 timeval = Srefdate.find(':')3423 3424 if not timeval == -1:3425 print ' timemean: refdate with time!'3426 refdate =datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate)3427 else:3428 refdate =dateStr_date(Srefdate)3429 3430 dimt = len(timevals)3431 realdates = np.zeros((dimt, 6), dtype=int)3432 print realdates.shape3443 timeval = Srefdate.find(':') 3444 3445 if not timeval == -1: 3446 print ' timemean: refdate with time!' 3447 refdate = gen.datetimeStr_datetime(txtunits[len(txtunits) - 2] + '_' + Srefdate) 3448 else: 3449 refdate = gen.dateStr_date(Srefdate) 3450 3451 dimt = len(timevals) 3452 realdates = np.zeros((dimt, 6), dtype=int) 3453 print realdates.shape 3433 3454 3434 3455 ## Not in timedelta 3435 # if tunits == 'years':3436 # for it in range(dimt):3437 # realdate = refdate + dt.timedelta(years=float(times[it]))3438 # realdates[it] = int(realdate.year)3439 # elif tunits == 'months':3440 # for it in range(dimt):3441 # realdate = refdate + dt.timedelta(months=float(times[it]))3442 # realdates[it] = int(realdate.year)3443 if tunits == 'weeks':3444 for it in range(dimt):3445 realdate = refdate + dt.timedelta(weeks=float(times[it]))3446 realdates[it,0] = int(realdate.year)3447 realdates[it,1] = int(realdate.month)3448 realdates[it,2] = int(realdate.day)3449 realdates[it,3] = int(realdate.hour)3450 realdates[it,4] = int(realdate.second)3451 realdates[it,5] = int(realdate.minute)3452 elif tunits == 'days':3453 for it in range(dimt):3454 realdate = refdate + dt.timedelta(days=float(times[it]))3455 realdates[it,0] = int(realdate.year)3456 realdates[it,1] = int(realdate.month)3457 realdates[it,2] = int(realdate.day)3458 realdates[it,3] = int(realdate.hour)3459 realdates[it,4] = int(realdate.second)3460 realdates[it,5] = int(realdate.minute)3461 elif tunits == 'hours':3462 for it in range(dimt):3463 realdate = refdate + dt.timedelta(hours=float(times[it]))3464 realdates[it,0] = int(realdate.year)3465 realdates[it,1] = int(realdate.month)3466 realdates[it,2] = int(realdate.day)3467 realdates[it,3] = int(realdate.hour)3468 realdates[it,4] = int(realdate.second)3469 realdates[it,5] = int(realdate.minute)3470 elif tunits == 'minutes':3471 for it in range(dimt):3472 realdate = refdate + dt.timedelta(minutes=float(times[it]))3473 realdates[it,0] = int(realdate.year)3474 realdates[it,1] = int(realdate.month)3475 realdates[it,2] = int(realdate.day)3476 realdates[it,3] = int(realdate.hour)3477 realdates[it,4] = int(realdate.second)3478 realdates[it,5] = int(realdate.minute)3479 elif tunits == 'seconds':3480 for it in range(dimt):3481 realdate = refdate + dt.timedelta(seconds=float(times[it]))3482 realdates[it,0] = int(realdate.year)3483 realdates[it,1] = int(realdate.month)3484 realdates[it,2] = int(realdate.day)3485 realdates[it,3] = int(realdate.hour)3486 realdates[it,4] = int(realdate.second)3487 realdates[it,5] = int(realdate.minute)3488 elif tunits == 'milliseconds':3489 for it in range(dimt):3490 realdate = refdate + dt.timedelta(milliseconds=float(times[it]))3491 realdates[it,0] = int(realdate.year)3492 realdates[it,1] = int(realdate.month)3493 realdates[it,2] = int(realdate.day)3494 realdates[it,3] = int(realdate.hour)3495 realdates[it,4] = int(realdate.second)3496 realdates[it,5] = int(realdate.minute)3497 else:3498 print errormsg3499 print ' timemean: time units "' + tunits + '" not ready!!!!'3500 ncf.close()3501 quit(-1)3502 3503 timesv = (realdates[:,0] - realdates[0,0])*12 + realdates[:,1]3504 3505 # Variable values (assuming time as first dimension)3456 # if tunits == 'years': 3457 # for it in range(dimt): 3458 # realdate = refdate + dt.timedelta(years=float(times[it])) 3459 # realdates[it] = int(realdate.year) 3460 # elif tunits == 'months': 3461 # for it in range(dimt): 3462 # realdate = refdate + dt.timedelta(months=float(times[it])) 3463 # realdates[it] = int(realdate.year) 3464 if tunits == 'weeks': 3465 for it in range(dimt): 3466 realdate = refdate + dt.timedelta(weeks=float(times[it])) 3467 realdates[it,0] = int(realdate.year) 3468 realdates[it,1] = int(realdate.month) 3469 realdates[it,2] = int(realdate.day) 3470 realdates[it,3] = int(realdate.hour) 3471 realdates[it,4] = int(realdate.second) 3472 realdates[it,5] = int(realdate.minute) 3473 elif tunits == 'days': 3474 for it in range(dimt): 3475 realdate = refdate + dt.timedelta(days=float(times[it])) 3476 realdates[it,0] = int(realdate.year) 3477 realdates[it,1] = int(realdate.month) 3478 realdates[it,2] = int(realdate.day) 3479 realdates[it,3] = int(realdate.hour) 3480 realdates[it,4] = int(realdate.second) 3481 realdates[it,5] = int(realdate.minute) 3482 elif tunits == 'hours': 3483 for it in range(dimt): 3484 realdate = refdate + dt.timedelta(hours=float(times[it])) 3485 realdates[it,0] = int(realdate.year) 3486 realdates[it,1] = int(realdate.month) 3487 realdates[it,2] = int(realdate.day) 3488 realdates[it,3] = int(realdate.hour) 3489 realdates[it,4] = int(realdate.second) 3490 realdates[it,5] = int(realdate.minute) 3491 elif tunits == 'minutes': 3492 for it in range(dimt): 3493 realdate = refdate + dt.timedelta(minutes=float(times[it])) 3494 realdates[it,0] = int(realdate.year) 3495 realdates[it,1] = int(realdate.month) 3496 realdates[it,2] = int(realdate.day) 3497 realdates[it,3] = int(realdate.hour) 3498 realdates[it,4] = int(realdate.second) 3499 realdates[it,5] = int(realdate.minute) 3500 elif tunits == 'seconds': 3501 for it in range(dimt): 3502 realdate = refdate + dt.timedelta(seconds=float(times[it])) 3503 realdates[it,0] = int(realdate.year) 3504 realdates[it,1] = int(realdate.month) 3505 realdates[it,2] = int(realdate.day) 3506 realdates[it,3] = int(realdate.hour) 3507 realdates[it,4] = int(realdate.second) 3508 realdates[it,5] = int(realdate.minute) 3509 elif tunits == 'milliseconds': 3510 for it in range(dimt): 3511 realdate = refdate + dt.timedelta(milliseconds=float(times[it])) 3512 realdates[it,0] = int(realdate.year) 3513 realdates[it,1] = int(realdate.month) 3514 realdates[it,2] = int(realdate.day) 3515 realdates[it,3] = int(realdate.hour) 3516 realdates[it,4] = int(realdate.second) 3517 realdates[it,5] = int(realdate.minute) 3518 else: 3519 print errormsg 3520 print ' timemean: time units "' + tunits + '" not ready!!!!' 3521 ncf.close() 3522 quit(-1) 3523 3524 timesv = (realdates[:,0] - realdates[0,0])*12 + realdates[:,1] 3525 3526 # Variable values (assuming time as first dimension) 3506 3527 ## 3507 var = ncf.variables[varn] 3508 varinf = variable_inf(var) 3509 3510 # varvals = var[:] 3511 vardims = varinf.dims 3512 varshape = varinf.Ndims 3513 vardimns = var.dimensions 3514 3515 ## print ' timemean: Shape of data: ',varshape, ':' , vardims 3516 dimt=vardims[0] 3517 vardnames = [] 3518 3519 if varshape == 1: 3520 print errormsg 3521 print ' timemean: You can not compute a time mean for a ', varshape, 'D var!!!' 3522 ncf.close() 3523 quit(-1) 3524 elif varshape == 2: 3525 dx=vardims[1] 3526 varstats = np.ones((5, dx), dtype=np.float64) 3527 varstats = varstats*varfil 3528 varquant = np.ones((21, dx), dtype=np.float64) 3529 varquant = varquant*varfil 3530 varlregress = np.ones((5, dx), dtype=np.float64) 3531 varlregress = varlregress*varfil 3532 varpregress = np.ones((powerv+1, dx), dtype=np.float64) 3533 varpregress = varpregress*varfil 3534 varpregressres = np.ones((dx), dtype=np.float64) 3535 varpregressres = varpregressres*varfil 3536 varpregresssingval = varpregress.copy() 3537 varpregresssingval = varpregresssingval*varfil 3538 vardnames.append(vardimns[1]) 3539 for ix in range(dx): 3540 if not varinf.FillValue is None: 3541 varvl = var[:,ix] 3542 varval = np.where(varvl == varinf.FillValue, None, varvl) 3543 else: 3544 varval = var[:,ix] 3545 percendone(ix,dx,5,'computed time statistics') 3546 3547 varst = statsVal(varval) 3548 var2st = stats2Val(timesv, varval, powerv) 3549 varstats[0,ix] = varst.minv 3550 varstats[1,ix] = varst.maxv 3551 varstats[2,ix] = varst.meanv 3552 varstats[3,ix] = varst.mean2v 3553 varstats[4,ix] = varst.stdv 3554 varquant[:,ix] = varst.quantilesv 3555 varlregress[:,ix] = var2st.linRegress 3556 varpregress[:,ix] = var2st.polRegress 3557 varpregressres[ix] = var2st.polRegressRes 3558 varpregresssingval[:,ix] = var2st.polRegressSingVal 3559 elif varshape == 3: 3560 dy=vardims[1] 3561 dx=vardims[2] 3562 varstats = np.ones((5,dy, dx), dtype=np.float64) 3563 varstats = varstats*varfil 3564 varquant = np.ones((21,dy, dx), dtype=np.float64) 3565 varquant = varquant*varfil 3566 varlregress = np.ones((5,dy, dx), dtype=np.float64) 3567 varlregress = varlregress*varfil 3568 varpregress = np.ones((powerv+1,dy, dx), dtype=np.float64) 3569 varpregress = varpregress*varfil 3570 varpregressres = np.ones((dy,dx), dtype=np.float64) 3571 varpregressres = varpregressres*varfil 3572 varpregresssingval = varpregress.copy() 3573 varpregresssingval = varpregresssingval*varfil 3574 vardnames.append(vardimns[1]) 3575 vardnames.append(vardimns[2]) 3576 for iy in range(dy): 3577 for ix in range(dx): 3528 var = ncf.variables[varn] 3529 varinf = variable_inf(var) 3530 3531 # varvals = var[:] 3532 vardims = varinf.dims 3533 varshape = varinf.Ndims 3534 vardimns = var.dimensions 3535 3536 ## print ' timemean: Shape of data: ',varshape, ':' , vardims 3537 dimt=vardims[0] 3538 vardnames = [] 3539 3540 if varshape == 1: 3541 print errormsg 3542 print ' timemean: You can not compute a time mean for a ', varshape, 'D var!!!' 3543 ncf.close() 3544 quit(-1) 3545 elif varshape == 2: 3546 dx=vardims[1] 3547 varstats = np.ones((5, dx), dtype=np.float64) 3548 varstats = varstats*varfil 3549 varquant = np.ones((21, dx), dtype=np.float64) 3550 varquant = varquant*varfil 3551 varlregress = np.ones((5, dx), dtype=np.float64) 3552 varlregress = varlregress*varfil 3553 varpregress = np.ones((powerv+1, dx), dtype=np.float64) 3554 varpregress = varpregress*varfil 3555 varpregressres = np.ones((dx), dtype=np.float64) 3556 varpregressres = varpregressres*varfil 3557 varpregresssingval = varpregress.copy() 3558 varpregresssingval = varpregresssingval*varfil 3559 vardnames.append(vardimns[1]) 3560 for ix in range(dx): 3561 if not varinf.FillValue is None: 3562 varvl = var[:,ix] 3563 varval = np.where(varvl == varinf.FillValue, None, varvl) 3564 else: 3565 varval = var[:,ix] 3566 3567 gen.percendone(ix,dx,5,'computed time statistics') 3568 3569 varst = gen.statsVal(varval) 3570 var2st = gen.stats2Val(timesv, varval, powerv) 3571 varstats[0,ix] = varst.minv 3572 varstats[1,ix] = varst.maxv 3573 varstats[2,ix] = varst.meanv 3574 varstats[3,ix] = varst.mean2v 3575 varstats[4,ix] = varst.stdv 3576 varquant[:,ix] = varst.quantilesv 3577 varlregress[:,ix] = var2st.linRegress 3578 varpregress[:,ix] = var2st.polRegress 3579 varpregressres[ix] = var2st.polRegressRes 3580 varpregresssingval[:,ix] = var2st.polRegressSingVal 3581 elif varshape == 3: 3582 dy=vardims[1] 3583 dx=vardims[2] 3584 varstats = np.ones((5,dy, dx), dtype=np.float64) 3585 varstats = varstats*varfil 3586 varquant = np.ones((21,dy, dx), dtype=np.float64) 3587 varquant = varquant*varfil 3588 varlregress = np.ones((5,dy, dx), dtype=np.float64) 3589 varlregress = varlregress*varfil 3590 varpregress = np.ones((powerv+1,dy, dx), dtype=np.float64) 3591 varpregress = varpregress*varfil 3592 varpregressres = np.ones((dy,dx), dtype=np.float64) 3593 varpregressres = varpregressres*varfil 3594 varpregresssingval = varpregress.copy() 3595 varpregresssingval = varpregresssingval*varfil 3596 vardnames.append(vardimns[1]) 3597 vardnames.append(vardimns[2]) 3598 for iy in range(dy): 3599 for ix in range(dx): 3578 3600 3579 if not varinf.FillValue is None:3580 varvl = var[:,iy,ix]3581 varval = np.where(varvl == varinf.FillValue, None, varvl)3582 else:3583 varval = var[:,iy,ix]3601 if not varinf.FillValue is None: 3602 varvl = var[:,iy,ix] 3603 varval = np.where(varvl == varinf.FillValue, None, varvl) 3604 else: 3605 varval = var[:,iy,ix] 3584 3606 3585 percendone(iy*dx+ix,dy*dx,5,'computed time statistics') 3586 varst = statsVal(varval) 3587 var2st = stats2Val(timesv, varval, powerv) 3588 varstats[0,iy,ix] = varst.minv 3589 varstats[1,iy,ix] = varst.maxv 3590 varstats[2,iy,ix] = varst.meanv 3591 varstats[3,iy,ix] = varst.mean2v 3592 varstats[4,iy,ix] = varst.stdv 3593 varquant[:,iy,ix] = varst.quantilesv 3594 varlregress[:,iy,ix] = var2st.linRegress 3595 varpregress[:,iy,ix] = var2st.polRegress 3596 varpregressres[iy,ix] = var2st.polRegressRes 3597 varpregresssingval[:,iy,ix] = var2st.polRegressSingVal 3598 elif varshape == 4: 3599 dz=vardims[1] 3600 dy=vardims[2] 3601 dx=vardims[3] 3602 varstats = np.ones((5, dz, dy, dx), dtype=np.float64) 3603 varstats = varstats*varfil 3604 varquant = np.ones((21, dz, dy, dx), dtype=np.float64) 3605 varquant = varquant*varfil 3606 varlregress = np.ones((5, dz, dy, dx), dtype=np.float64) 3607 varlregress = varlregress*varfil 3608 varpregress = np.ones((powerv+1, dz, dy, dx), dtype=np.float64) 3609 varpregress = varpregress*varfil 3610 varpregressres = np.ones((dz,dy,dx), dtype=np.float64) 3611 varpregressres = varpregressres*varfil 3612 varpregresssingval = varpregress.copy() 3613 varpregresssingval = varpregresssingval*varfil 3614 vardnames.append(vardimns[1]) 3615 vardnames.append(vardimns[2]) 3616 vardnames.append(vardimns[3]) 3617 for iz in range(dz): 3618 for iy in range(dy): 3619 for ix in range(dx): 3620 if not varinf.FillValue is None: 3621 varvl = var[:,iz,iy,ix] 3622 varval = np.where(varvl == varinf.FillValue, None, varvl) 3623 else: 3624 varval = var[:,iz,iy,ix] 3607 gen.percendone(iy*dx+ix,dy*dx,5,'computed time statistics') 3608 varst = gen.statsVal(varval) 3609 var2st = gen.stats2Val(timesv, varval, powerv) 3610 varstats[0,iy,ix] = varst.minv 3611 varstats[1,iy,ix] = varst.maxv 3612 varstats[2,iy,ix] = varst.meanv 3613 varstats[3,iy,ix] = varst.mean2v 3614 varstats[4,iy,ix] = varst.stdv 3615 varquant[:,iy,ix] = varst.quantilesv 3616 varlregress[:,iy,ix] = var2st.linRegress 3617 varpregress[:,iy,ix] = var2st.polRegress 3618 if len(var2st.polRegressRes) > 0: 3619 varpregressres[iy,ix] = var2st.polRegressRes 3620 varpregresssingval[:,iy,ix] = var2st.polRegressSingVal 3621 elif varshape == 4: 3622 dz=vardims[1] 3623 dy=vardims[2] 3624 dx=vardims[3] 3625 varstats = np.ones((5, dz, dy, dx), dtype=np.float64) 3626 varstats = varstats*varfil 3627 varquant = np.ones((21, dz, dy, dx), dtype=np.float64) 3628 varquant = varquant*varfil 3629 varlregress = np.ones((5, dz, dy, dx), dtype=np.float64) 3630 varlregress = varlregress*varfil 3631 varpregress = np.ones((powerv+1, dz, dy, dx), dtype=np.float64) 3632 varpregress = varpregress*varfil 3633 varpregressres = np.ones((dz,dy,dx), dtype=np.float64) 3634 varpregressres = varpregressres*varfil 3635 varpregresssingval = varpregress.copy() 3636 varpregresssingval = varpregresssingval*varfil 3637 vardnames.append(vardimns[1]) 3638 vardnames.append(vardimns[2]) 3639 vardnames.append(vardimns[3]) 3640 for iz in range(dz): 3641 for iy in range(dy): 3642 for ix in range(dx): 3643 if not varinf.FillValue is None: 3644 varvl = var[:,iz,iy,ix] 3645 varval = np.where(varvl == varinf.FillValue, None, varvl) 3646 else: 3647 varval = var[:,iz,iy,ix] 3625 3648 3626 percendone(iz*dy*dx+iy*dx+ix,dz*dx*dy,5,'computed time statistics') 3627 varst = statsVal(varval) 3628 var2st = stats2Val(timesv, varval, powerv) 3629 varstats[0,iz,iy,ix] = varst.minv 3630 varstats[1,iz,iy,ix] = varst.maxv 3631 varstats[2,iz,iy,ix] = varst.meanv 3632 varstats[3,iz,iy,ix] = varst.mean2v 3633 varstats[4,iz,iy,ix] = varst.stdv 3634 varquant[:,iz,iy,ix] = varst.quantilesv 3635 varlregress[:,iz,iy,ix] = var2st.linRegress 3636 varpregress[:,iz,iy,ix] = var2st.polRegress 3637 varpregressres[iz,iy,ix] = var2st.polRegressRes 3638 varpregresssingval[:,iz,iy,ix] = var2st.polRegressSingVal 3639 3640 elif varshape == 5: 3641 dn=vardims[1] 3642 dz=vardims[2] 3643 dy=vardims[3] 3644 dx=vardims[4] 3645 varstats = np.ones((5, dn, dz, dy, dx), dtype=np.float64) 3646 varstats = varstats*varfil 3647 varquant = np.ones((21, dn, dz, dy, dx), dtype=np.float64) 3648 varquant = varquant*varfil 3649 varlregress = np.ones((5, dn, dz, dy, dx), dtype=np.float64) 3650 varlregress = varlregress*varfil 3651 varpregress = np.ones((powerv+1, dn, dz, dy, dx), dtype=np.float64) 3652 varpregress = varpregress*varfil 3653 varpregressres = np.ones((dn,dz,dy,dx), dtype=np.float64) 3654 varpregressres = varpregressres*varfil 3655 varpregresssingval = varpregress.copy() 3656 varpregresssingval = varpregresssingval*varfil 3657 vardnames.append(vardimns[1]) 3658 vardnames.append(vardimns[2]) 3659 vardnames.append(vardimns[3]) 3660 vardnames.append(vardimns[4]) 3661 for iN in range(dn): 3662 for iz in range(dz): 3663 for iy in range(dy): 3664 for ix in range(dx): 3665 if not varinf.FillValue is None: 3666 varvl = var[:,iN,iz,iy,ix] 3667 varval = np.where(varvl == varinf.FillValue, None, varvl) 3649 gen.percendone(iz*dy*dx+iy*dx+ix,dz*dx*dy,5,'computed time statistics') 3650 varst = gen.statsVal(varval) 3651 var2st = gen.stats2Val(timesv, varval, powerv) 3652 varstats[0,iz,iy,ix] = varst.minv 3653 varstats[1,iz,iy,ix] = varst.maxv 3654 varstats[2,iz,iy,ix] = varst.meanv 3655 varstats[3,iz,iy,ix] = varst.mean2v 3656 varstats[4,iz,iy,ix] = varst.stdv 3657 varquant[:,iz,iy,ix] = varst.quantilesv 3658 varlregress[:,iz,iy,ix] = var2st.linRegress 3659 varpregress[:,iz,iy,ix] = var2st.polRegress 3660 varpregressres[iz,iy,ix] = var2st.polRegressRes 3661 varpregresssingval[:,iz,iy,ix] = var2st.polRegressSingVal 3662 3663 elif varshape == 5: 3664 dn=vardims[1] 3665 dz=vardims[2] 3666 dy=vardims[3] 3667 dx=vardims[4] 3668 varstats = np.ones((5, dn, dz, dy, dx), dtype=np.float64) 3669 varstats = varstats*varfil 3670 varquant = np.ones((21, dn, dz, dy, dx), dtype=np.float64) 3671 varquant = varquant*varfil 3672 varlregress = np.ones((5, dn, dz, dy, dx), dtype=np.float64) 3673 varlregress = varlregress*varfil 3674 varpregress = np.ones((powerv+1, dn, dz, dy, dx), dtype=np.float64) 3675 varpregress = varpregress*varfil 3676 varpregressres = np.ones((dn,dz,dy,dx), dtype=np.float64) 3677 varpregressres = varpregressres*varfil 3678 varpregresssingval = varpregress.copy() 3679 varpregresssingval = varpregresssingval*varfil 3680 vardnames.append(vardimns[1]) 3681 vardnames.append(vardimns[2]) 3682 vardnames.append(vardimns[3]) 3683 vardnames.append(vardimns[4]) 3684 for iN in range(dn): 3685 for iz in range(dz): 3686 for iy in range(dy): 3687 for ix in range(dx): 3688 if not varinf.FillValue is None: 3689 varvl = var[:,iN,iz,iy,ix] 3690 varval = np.where(varvl == varinf.FillValue, None, varvl) 3691 else: 3692 varval = var[:,iN,iz,iy,ix] 3693 3694 gen.percendone(iN*dy*dx*dz+iz*dy*dx+iy*dx+ix,dn*dz*dx*dy,5,'computed time statistics') 3695 varst = gen.statsVal(varval) 3696 var2st = gen.stats2Val(timesv, varval, powerv) 3697 varstats[0,iN,iz,iy,ix] = varst.minv 3698 varstats[1,iN,iz,iy,ix] = varst.maxv 3699 varstats[2,iN,iz,iy,ix] = varst.meanv 3700 varstats[3,iN,iz,iy,ix] = varst.mean2v 3701 varstats[4,iN,iz,iy,ix] = varst.stdv 3702 varquant[:,iN,iz,iy,iiN,x] = varst.quantilesv 3703 varlregress[:,iN,iz,iy,ix] = var2st.linRegress 3704 varpregress[:,iN,iz,iy,ix] = var2st.polRegress 3705 varpregressres[iN,iz,iy,ix] = var2st.polRegressRes 3706 varpregresssingval[:,iN,iz,iy,ix] = var2st.polRegressSingVal 3707 3708 else: 3709 print errormsg 3710 print ' timemean: ', varshape, ' shape of matrix not prepared !' 3711 ncf.close() 3712 quit(-1) 3713 3714 vardimnames = tuple(vardnames) 3715 # print ' shape of desired values: ', vardesiredvalues.shape 3716 # Creation of file 3717 ## 3718 3719 ncfo = NetCDFFile( ofile, 'w') 3720 3721 vartype = var.dtype 3722 varattr = var.ncattrs() 3723 3724 # Checking dimensions 3725 ## 3726 newdims = ncfo.dimensions 3727 Nvardnames = len(vardimnames) 3728 for idim in range(Nvardnames): 3729 rdim = vardimnames[idim] 3730 if not gen.searchInlist(newdims, rdim): 3731 if not rdim == 'time': 3732 ## print ' timemean: Adding dimension ' + rdim 3733 ncfo.sync() 3734 ncf.close() 3735 ncfo.close() 3736 fdimadd(ncfile + ',' + rdim, ofile) 3737 ncf = NetCDFFile(ncfile,'r') 3738 ncfo = NetCDFFile(ofile,'a') 3668 3739 else: 3669 varval = var[:,iN,iz,iy,ix] 3670 3671 percendone(iN*dy*dx*dz+iz*dy*dx+iy*dx+ix,dn*dz*dx*dy,5,'computed time statistics') 3672 varst = statsVal(varval) 3673 var2st = stats2Val(timesv, varval, powerv) 3674 varstats[0,iN,iz,iy,ix] = varst.minv 3675 varstats[1,iN,iz,iy,ix] = varst.maxv 3676 varstats[2,iN,iz,iy,ix] = varst.meanv 3677 varstats[3,iN,iz,iy,ix] = varst.mean2v 3678 varstats[4,iN,iz,iy,ix] = varst.stdv 3679 varquant[:,iN,iz,iy,iiN,x] = varst.quantilesv 3680 varlregress[:,iN,iz,iy,ix] = var2st.linRegress 3681 varpregress[:,iN,iz,iy,ix] = var2st.polRegress 3682 varpregressres[iN,iz,iy,ix] = var2st.polRegressRes 3683 varpregresssingval[:,iN,iz,iy,ix] = var2st.polRegressSingVal 3684 3685 else: 3686 print errormsg 3687 print ' timemean: ', varshape, ' shape of matrix not prepared !' 3688 ncf.close() 3689 quit(-1) 3690 3691 vardimnames = tuple(vardnames) 3692 # print ' shape of desired values: ', vardesiredvalues.shape 3693 # Creation of file 3694 ## 3695 3696 ncfo = NetCDFFile( ofile, 'w') 3697 3698 vartype = var.dtype 3699 varattr = var.ncattrs() 3700 3701 # Checking dimensions 3702 ## 3703 newdims = ncfo.dimensions 3704 Nvardnames = len(vardimnames) 3705 for idim in range(Nvardnames): 3706 rdim = vardimnames[idim] 3707 if not searchInlist(newdims, rdim): 3708 if not rdim == 'time': 3709 ## print ' timemean: Adding dimension ' + rdim 3710 ncfo.sync() 3711 ncf.close() 3712 ncfo.close() 3713 fdimadd(ncfile + ',' + rdim, ofile) 3714 ncf = NetCDFFile(ncfile,'r') 3715 ncfo = NetCDFFile(ofile,'a') 3716 else: 3717 print ' timemean: No time dimension!' 3740 print ' timemean: No time dimension!' 3718 3741 3719 3742 # Checking fill value 3720 3743 ## 3721 if searchInlist(varattr, '_FillValue'): 3722 varfil = var._FillValue 3723 else: 3724 varfil = False 3725 3726 Nstats = len(statsn) 3727 for ist in range(Nstats): 3728 newvardnames = [] 3729 if statsn[ist] == 'quant': 3730 print ist, statsn[ist]##, ': ', varquant[int(dimt/2),10] 3731 newdim = ncfo.createDimension('quant', 21) 3732 newvardnames.append('quant') 3733 newvardnames = newvardnames + list(vardnames) 3734 newvar = ncfo.createVariable(varn + statsn[ist], vartype, tuple(newvardnames), fill_value=varfil) 3735 newvar[:] = varquant 3736 elif statsn[ist] == 'linregress': 3737 print ist, statsn[ist]##, ': ', varquant[int(dimt/2),10] 3738 newdim = ncfo.createDimension('lregress', 5) 3739 newvar = ncfo.createVariable('lregressn', str, ('lregress',)) 3740 newvar[0] = 'slope' 3741 newvar[1] = 'intercept' 3742 newvar[2] = 'r_value' 3743 newvar[3] = 'p_value' 3744 newvar[4] = 'std_err' 3745 newvardnames.append('lregress') 3746 newvardnames = newvardnames + list(vardnames) 3747 newvar = ncfo.createVariable(varn + statsn[ist], 'f4', tuple(newvardnames), fill_value=varfil) 3748 newvar[:] = varlregress*1. 3749 3750 elif statsn[ist] == 'polregress': 3751 print ist, statsn[ist]##, ': ', varquant[int(dimt/2),10] 3752 newdim = ncfo.createDimension('pregress', powerv+1) 3753 newvar = ncfo.createVariable('pregressn', str, ('pregress',)) 3754 for ip in range(powerv+1): 3755 newvar[ip]='coefficient**' + str(powerv-ip) 3756 newvardnames.append('pregress') 3757 newvardnames = newvardnames + list(vardnames) 3758 newvar = ncfo.createVariable(varn + statsn[ist], 'f4', tuple(newvardnames), fill_value=varfil) 3759 newvar[:] = varpregress*1. 3760 newvar.setncattr('power',powerv) 3761 newvar.setncattr('values','Polynomial coefficients, highest power first') 3762 newvar = ncfo.createVariable(varn + statsn[ist] + '_Residual', vartype, tuple(vardnames), fill_value=varfil) 3763 newvar[:] = varpregressres 3764 newvar.setncattr('power',powerv) 3765 newvar.setncattr('values','Polynomial residuals') 3766 newvar = ncfo.createVariable(varn + statsn[ist] + '_VandermondeSingularVector', vartype, tuple(newvardnames), fill_value=varfil) 3767 newvar[:] = varpregresssingval 3768 newvar.setncattr('power',powerv) 3769 newvar.setncattr('values','Polynomial coefficients, highest power first') 3744 if gen.searchInlist(varattr, '_FillValue'): 3745 varfil = var._FillValue 3770 3746 else: 3771 print ist, statsn[ist]##, ': ', varstats[int(dimt/2),ist] 3772 if statsn[ist] == 'mean' or statsn[ist] == 'stdv' or statsn[ist] == 'mean2' or statsn[ist] == 'polregress_Residual' \ 3773 or statsn[ist] == 'polregress_VandermondeSingularVector' and searchInlist(varattr, 'scale_factor'): 3774 newvar = ncfo.createVariable(varn + statsn[ist], 'f4', vardimnames, fill_value=varfil) 3775 if varshape == 2: 3776 newvar[:] = varstats[ist,:]*1. 3777 elif varshape == 3: 3778 newvar[:] = varstats[ist,:,:]*1. 3779 elif varshape == 4: 3780 newvar[:] = varstats[ist,:,:,:]*1. 3781 elif varshape == 5: 3782 newvar[:] = varstats[ist,:,:,:,:]*1. 3783 else: 3784 newvar = ncfo.createVariable(varn + statsn[ist], vartype, vardimnames, fill_value=varfil) 3785 if varshape == 2: 3786 newvar[:] = varstats[ist,:] 3787 elif varshape == 3: 3788 newvar[:] = varstats[ist,:,:] 3789 elif varshape == 4: 3790 newvar[:] = varstats[ist,:,:,:] 3791 elif varshape == 5: 3792 newvar[:] = varstats[ist,:,:,:,:] 3793 3794 newvar = ncfo.variables[varn + statsn[ist]] 3795 for attr in varattr: 3796 newvarattrs = newvar.ncattrs() 3797 attrv = var.getncattr(attr) 3798 if not searchInlist(newvarattrs, attr): 3799 if attr == 'scale_factor' or attr == 'add_offset' or attr == 'valid_range' \ 3800 or attr == 'unpacked_valid_range' or attr == 'actual_range' : 3801 if not statsn[ist] == 'mean' and not statsn[ist] == 'stdv' and not statsn[ist] == 'mean2' and not \ 3802 statsn[ist] == 'linregress' and not statsn[ist] == 'polregress' \ 3803 and not statsn[ist] == 'polregress_Residual' and not \ 3804 statsn[ist] == 'polregress_VandermondeSingularVector': 3805 newvar.setncattr(attr, attrv) 3806 else: 3807 newvar.setncattr(attr, attrv) 3747 varfil = False 3748 3749 Nstats = len(statsn) 3750 for ist in range(Nstats): 3751 newvardnames = [] 3752 if statsn[ist] == 'quant': 3753 print ist, statsn[ist]##, ': ', varquant[int(dimt/2),10] 3754 newdim = ncfo.createDimension('quant', 21) 3755 newvardnames.append('quant') 3756 newvardnames = newvardnames + list(vardnames) 3757 newvar = ncfo.createVariable(varn + statsn[ist], vartype, tuple(newvardnames), fill_value=varfil) 3758 newvar[:] = varquant 3759 elif statsn[ist] == 'linregress': 3760 print ist, statsn[ist]##, ': ', varquant[int(dimt/2),10] 3761 newdim = ncfo.createDimension('lregress', 5) 3762 newvar = ncfo.createVariable('lregressn', str, ('lregress',)) 3763 newvar[0] = 'slope' 3764 newvar[1] = 'intercept' 3765 newvar[2] = 'r_value' 3766 newvar[3] = 'p_value' 3767 newvar[4] = 'std_err' 3768 newvardnames.append('lregress') 3769 newvardnames = newvardnames + list(vardnames) 3770 newvar = ncfo.createVariable(varn + statsn[ist], 'f4', tuple(newvardnames), fill_value=varfil) 3771 newvar[:] = varlregress*1. 3772 3773 elif statsn[ist] == 'polregress': 3774 print ist, statsn[ist]##, ': ', varquant[int(dimt/2),10] 3775 newdim = ncfo.createDimension('pregress', powerv+1) 3776 newvar = ncfo.createVariable('pregressn', str, ('pregress',)) 3777 for ip in range(powerv+1): 3778 newvar[ip]='coefficient**' + str(powerv-ip) 3779 newvardnames.append('pregress') 3780 newvardnames = newvardnames + list(vardnames) 3781 newvar = ncfo.createVariable(varn + statsn[ist], 'f4', tuple(newvardnames), fill_value=varfil) 3782 newvar[:] = varpregress*1. 3783 newvar.setncattr('power',powerv) 3784 newvar.setncattr('values','Polynomial coefficients, highest power first') 3785 newvar = ncfo.createVariable(varn + statsn[ist] + '_Residual', vartype, tuple(vardnames), fill_value=varfil) 3786 newvar[:] = varpregressres 3787 newvar.setncattr('power',powerv) 3788 newvar.setncattr('values','Polynomial residuals') 3789 newvar = ncfo.createVariable(varn + statsn[ist] + '_VandermondeSingularVector', vartype, tuple(newvardnames), fill_value=varfil) 3790 newvar[:] = varpregresssingval 3791 newvar.setncattr('power',powerv) 3792 newvar.setncattr('values','Polynomial coefficients, highest power first') 3793 else: 3794 print ist, statsn[ist]##, ': ', varstats[int(dimt/2),ist] 3795 if statsn[ist] == 'mean' or statsn[ist] == 'stdv' or statsn[ist] == 'mean2' or statsn[ist] == 'polregress_Residual' \ 3796 or statsn[ist] == 'polregress_VandermondeSingularVector' and gen.searchInlist(varattr, 'scale_factor'): 3797 newvar = ncfo.createVariable(varn + statsn[ist], 'f4', vardimnames, fill_value=varfil) 3798 if varshape == 2: 3799 newvar[:] = varstats[ist,:]*1. 3800 elif varshape == 3: 3801 newvar[:] = varstats[ist,:,:]*1. 3802 elif varshape == 4: 3803 newvar[:] = varstats[ist,:,:,:]*1. 3804 elif varshape == 5: 3805 newvar[:] = varstats[ist,:,:,:,:]*1. 3806 else: 3807 newvar = ncfo.createVariable(varn + statsn[ist], vartype, vardimnames, fill_value=varfil) 3808 if varshape == 2: 3809 newvar[:] = varstats[ist,:] 3810 elif varshape == 3: 3811 newvar[:] = varstats[ist,:,:] 3812 elif varshape == 4: 3813 newvar[:] = varstats[ist,:,:,:] 3814 elif varshape == 5: 3815 newvar[:] = varstats[ist,:,:,:,:] 3816 3817 newvar = ncfo.variables[varn + statsn[ist]] 3818 for attr in varattr: 3819 newvarattrs = newvar.ncattrs() 3820 attrv = var.getncattr(attr) 3821 if not gen.searchInlist(newvarattrs, attr): 3822 if attr == 'scale_factor' or attr == 'add_offset' or attr == 'valid_range' \ 3823 or attr == 'unpacked_valid_range' or attr == 'actual_range' : 3824 if not statsn[ist] == 'mean' and not statsn[ist] == 'stdv' and not statsn[ist] == 'mean2' and not \ 3825 statsn[ist] == 'linregress' and not statsn[ist] == 'polregress' \ 3826 and not statsn[ist] == 'polregress_Residual' and not \ 3827 statsn[ist] == 'polregress_VandermondeSingularVector': 3828 newvar.setncattr(attr, attrv) 3829 else: 3830 newvar.setncattr(attr, attrv) 3808 3831 3809 newvar.setncattr('cell_methods', 'time ' + statslongn[ist] + ' all period in file ' + str(dimt) + ' time steps') 3810 3811 ncfo.sync() 3812 ncfo.close() 3813 3814 fvaradd(ncfile + ',lon', ofile) 3815 fvaradd(ncfile + ',lat', ofile) 3816 3817 fgaddattr(ncfile, ofile) 3818 3819 print ' timemean: File "' + ofile + '" as time mean of "' + varn + '" has been created' 3820 3821 return 3832 newvar.setncattr('cell_methods', 'time ' + statslongn[ist] + ' all period in file ' + str(dimt) + ' time steps') 3833 3834 ncfo.sync() 3835 allvars = ncfo.variables.keys() 3836 3837 ncfo.close() 3838 3839 if addvars == 'all': 3840 varns = allvars 3841 else: 3842 varns = gen.str_list(addvars, ',') 3843 3844 for varn in varns: 3845 fvaradd(ncfile + ',' + varn, ofile) 3846 3847 fgaddattr(ncfile, ofile) 3848 3849 print ' timemean: File "' + ofile + '" as time mean of "' + varn + '" has been created' 3850 3851 return 3822 3852 3823 3853 def flipdim(values, filename, varn): … … 25307 25337 if values == 'h': 25308 25338 print fname + '_____________________________________________________________' 25309 print te pmoral_stats.__doc__25339 print temporal_stats.__doc__ 25310 25340 quit() 25311 25341
Note: See TracChangeset
for help on using the changeset viewer.