- Timestamp:
- Mar 21, 2016, 12:47:14 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r700 r701 9854 9854 typeinf = np.iinfo(np.int) 9855 9855 if (abs(np.float64(val)) > typeinf.max): 9856 print warnmsg 9857 print ' ' + fname + ': value to transform ', val,' overpasses type:', \ 9858 vtype,' limits!!' 9859 print ' Changing value to kind largest allowed value:', typeinf.max 9856 9860 newval = abs(np.float64(val))/np.float64(val) * np.int(typeinf.max) 9861 else: 9862 newval = np.int(val) 9857 9863 elif vtype == type(np.int16(1)): 9858 9864 typeinf = np.iinfo(np.int16) 9859 9865 if (abs(np.float64(val)) > typeinf.max): 9866 print warnmsg 9867 print ' ' + fname + ': value to transform ', val,' overpasses type:', \ 9868 vtype,' limits!!' 9869 print ' Changing value to kind largest allowed value:', typeinf.max 9860 9870 newval = abs(np.float64(val))/np.float64(val) * np.int16(typeinf.max) 9871 else: 9872 newval = np.int16(val) 9861 9873 elif vtype == type(np.int32(1)): 9862 9874 typeinf = np.iinfo(np.int32) 9863 9875 if (abs(np.float64(val)) > typeinf.max): 9876 print warnmsg 9877 print ' ' + fname + ': value to transform ', val,' overpasses type:', \ 9878 vtype,' limits!!' 9879 print ' Changing value to kind largest allowed value:', typeinf.max 9864 9880 newval = abs(np.float64(val))/np.float64(val) * np.int32(typeinf.max) 9881 else: 9882 newval = np.int32(val) 9865 9883 elif vtype == type(np.int64(1)): 9866 9884 newval = np.int64(val) … … 13612 13630 #netcdf_concatenation('mean_pluc.nc@all|mean_dtcon.nc@all') 13613 13631 13632 def count_cond(var, val, con): 13633 """ Function to count values of a variable which attain a condition 13634 [var]= variable 13635 [val]= value 13636 [con]= statistics/condition 13637 'eq': equal than [val] 13638 'neq': not equal than [val] 13639 'lt': less than [val] 13640 'le': less and equal than [val] 13641 'gt': greater than [val] 13642 'ge': greater and equal than [val] 13643 'in': inside the range [val[0]] <= [var] <= [val[1]] (where [val]=[val1, val2] 13644 'out': inside the range [val[0]] > [var]; [val[1]] < [var] (where [val]=[val1, val2] 13645 >>> vals = np.arange(100) 13646 >>> count_cond(vals,50,'eq') 13647 1 13648 >>> count_cond(vals,50,'neq') 13649 99 13650 >>> count_cond(vals,50,'lt') 13651 50 13652 >>> count_cond(vals,50,'le') 13653 51 13654 >>> count_cond(vals,50,'gt') 13655 49 13656 >>> count_cond(vals,50,'ge') 13657 50 13658 >>> count_cond(vals,[25,75],'in') 13659 51 13660 >>> count_cond(vals,[25,75],'out') 13661 49 13662 """ 13663 fname = 'count_cond' 13664 13665 cons = ['eq', 'neq', 'lt', 'le', 'gt', 'ge', 'in', 'out'] 13666 13667 if con == 'eq': 13668 Nvals = np.sum(var == val) 13669 elif con == 'neq': 13670 Nvals = np.sum(var != val) 13671 elif con == 'lt': 13672 Nvals = np.sum(var < val) 13673 elif con == 'le': 13674 Nvals = np.sum(var <= val) 13675 elif con == 'gt': 13676 Nvals = np.sum(var > val) 13677 elif con == 'ge': 13678 Nvals = np.sum(var >= val) 13679 elif con == 'in': 13680 if len(val) != 2: 13681 print errormsg 13682 print ' ' + fname + ": for condition '" + con + "' we must have two " + \ 13683 " values!!" 13684 print ' we have:', val 13685 quit(-1) 13686 Nvals = np.sum((var >= val[0])*(var <= val[1])) 13687 elif con == 'out': 13688 if len(val) != 2: 13689 print errormsg 13690 print ' ' + fname + ": for condition '" + con + "' we must have two " + \ 13691 " values!!" 13692 print ' we have:', val 13693 quit(-1) 13694 Nvals = np.sum((var < val[0])+(var > val[1])) 13695 else: 13696 print errormsg 13697 print ' ' + fname + ": condition '" + con + "' not ready!!" 13698 print ' only ready:', cons 13699 quit(-1) 13700 13701 return Nvals 13702 13614 13703 def field_stats(values, ncfile, varn): 13615 13704 """ Function to retrieve statistics from a field … … 13619 13708 'full': all statistics given variable 13620 13709 [fillVals]: ':' list of _fillValues ('None' for any) 13710 [countVals]: ':' list of Values@cond to use to count conditions ('None' for any) 13621 13711 [ncfile]= name of the netCDF file to use 13622 13712 [varn]= variable name to use ('all' for all variables) … … 13631 13721 13632 13722 13633 arguments = '[stats],[fillVals] '13723 arguments = '[stats],[fillVals],[coutnVals]' 13634 13724 check_arguments(fname,values,arguments,',') 13635 13725 13636 13726 stats=values.split(',')[0] 13637 13727 fillVals=values.split(',')[1] 13638 13728 countVals=values.split(',')[2] 13729 13730 # Fill Values 13639 13731 if fillVals == 'None': 13640 13732 fillV = None … … 13646 13738 fillV = [fillVals] 13647 13739 NfillV = 1 13740 13741 # Count Values 13742 if countVals == 'None': 13743 countV = None 13744 else: 13745 if countVals.find(':') != -1: 13746 countV = countVals.split(':') 13747 NcountV = len(countV) 13748 else: 13749 countV = [countVals] 13750 NcountV = 1 13648 13751 13649 13752 ncobj = NetCDFFile(ncfile, 'r') … … 13665 13768 field = objfield[:] 13666 13769 dtype = objfield.dtype 13667 print 'Lluis dtype:',dtype,'fillVals:',fillV 13770 13771 counts = {} 13772 if countVals is not None: 13773 for icV in range(NcountV): 13774 cV = countV[icV].split('@')[0] 13775 cC = countV[icV].split('@')[1] 13776 countval = retype(cV, dtype) 13777 counts[countV[icV]] = [count_cond(field, countval, cC), cC] 13778 13668 13779 if fillVals is not None: 13669 13780 for ifV in range(NfillV): 13670 print 'fV:', fillV[ifV]13671 13781 fillval = retype(fillV[ifV], dtype) 13672 13782 field = ma.masked_equal(field, fillval) … … 13686 13796 print ' Fstats mean2:', mean2v 13687 13797 print ' Fstats variability:', varv 13798 if countVals is not None: 13799 for icV in range(NcountV): 13800 print ' ' + fname + " N values '" + countV[icV][1] + "'", \ 13801 countV[icV][0], ':', counts[countV[icV]] 13688 13802 else: 13689 13803 print errormsg
Note: See TracChangeset
for help on using the changeset viewer.