-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpypismtools.py
1036 lines (856 loc) · 28.8 KB
/
pypismtools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
pypismtools: Tools to evaluate PISM parameter studies
pypismtools is a module to facilitate evaluation of PISM parameter
studies. It mainly comprises two classes, Observation and Experiment,
which act as containers for observational data and PISM model
simulations, along with helper functions. The experiment class
determines information about an experiment from the netcdf file
directly, especially from the "pism_overrides" flag. Such information
can then be used for labeling, plotting, evaluation, etc. The indend
is to provide a robust tool to evaluate data, and to avoid common mistakes
such as mis-labeling plots. Additional functions include routines to
permute (netcdf) dimension, convert units using udunits, to estimate
trends, and to import GMT colormaps.
"""
__author__ = "Andy Aschwanden"
import numpy as np
import pylab as plt
from netCDF4 import Dataset as CDF
from pyproj import Proj
from osgeo import gdal
from osgeo import osr
# FIXME: how to provide DEBUG flag to module
DEBUG = None
class GeoTIFF(object):
"""
A class to read a GeoTIFF
Parameters
----------
filename: a valid geotiff file
"""
def __init__(self, file_name):
self.file_name = file_name
try:
print(("\n opening GeoTIFF file %s" % file_name))
self.gtiff = gdal.Open(file_name)
except:
print(("could not open file %s" % file_name))
self.RasterArray = self.gtiff.ReadAsArray()
self.gtiff_projection = self.gtiff.GetProjection()
osr_gtiff = osr.SpatialReference()
osr_gtiff.ImportFromWkt(self.gtiff_projection)
self.proj4 = osr_gtiff.ExportToProj4()
geoT = self.gtiff.GetGeoTransform()
pxwidth = self.gtiff.RasterXSize
pxheight = self.gtiff.RasterYSize
ulx = geoT[0]
uly = geoT[3]
rezX = geoT[1]
rezY = geoT[5]
rx = ulx + pxwidth * rezX
ly = uly + pxheight * rezY
self.width = np.abs(pxwidth * rezX)
self.height = np.abs(pxheight * rezY)
self.center_x = ulx + pxwidth * rezX / 2
self.center_y = uly + pxheight * rezY / 2
self.easting = np.arange(ulx, rx + rezX, rezX)
self.northing = np.arange(ly, uly - rezY, -rezY)
self.X, self.Y = np.meshgrid(self.easting, self.northing)
p_osr_gtiff = Proj(self.proj4)
self.lon_0, self.lat_0 = p_osr_gtiff(self.center_x, self.center_y, inverse=True)
self.lon, self.lat = p_osr_gtiff(self.X, self.Y, inverse=True)
def get_dims(nc):
"""
Gets dimensions from netcdf instance
Parameters:
-----------
nc: netCDF instance
Returns:
--------
xdim, ydim, zdim, tdim: dimensions
"""
# a list of possible x-dimensions names
xdims = ["x", "x1"]
# a list of possible y-dimensions names
ydims = ["y", "y1"]
# a list of possible z-dimensions names
zdims = ["z", "z1"]
# a list of possible time-dimensions names
tdims = ["t", "time"]
xdim = None
ydim = None
zdim = None
tdim = None
# assign x dimension
for dim in xdims:
if dim in list(nc.dimensions.keys()):
xdim = dim
# assign y dimension
for dim in ydims:
if dim in list(nc.dimensions.keys()):
ydim = dim
# assign z dimension
for dim in zdims:
if dim in list(nc.dimensions.keys()):
zdim = dim
# assign time dimension
for dim in tdims:
if dim in list(nc.dimensions.keys()):
tdim = dim
return xdim, ydim, zdim, tdim
def get_projection_from_file(nc):
"""
Gets a Proj projection instance from a pointer to a netCDF file
Parameters
----------
nc : a netCDF object instance
Returns
-------
p : Proj4 projection instance
"""
from pyproj import Proj
# First, check if we have a global attribute 'proj4'
# which contains a Proj4 string:
try:
p = Proj(str(nc.proj4))
print("Found projection information in global attribute proj4, using it")
except:
try:
p = Proj(str(nc.projection))
print("Found projection information in global attribute projection, using it")
except:
try:
# go through variables and look for 'grid_mapping' attribute
for var in list(nc.variables.keys()):
if hasattr(nc.variables[var], "grid_mapping"):
mappingvarname = nc.variables[var].grid_mapping
print(('Found projection information in variable "%s", using it' % mappingvarname))
break
var_mapping = nc.variables[mappingvarname]
p = Proj(
proj="stere",
ellps=var_mapping.ellipsoid,
datum=var_mapping.ellipsoid,
units="m",
lat_ts=var_mapping.standard_parallel,
lat_0=var_mapping.latitude_of_projection_origin,
lon_0=var_mapping.straight_vertical_longitude_from_pole,
x_0=var_mapping.false_easting,
y_0=var_mapping.false_northing,
)
except:
print("No mapping information found, return empy string.")
p = ""
return p
def add_inner_title(ax, title, loc, size=None, **kwargs):
"""
Adds an inner title to a given axis, with location loc.
from http://matplotlib.sourceforge.net/examples/axes_grid/demo_axes_grid2.html
"""
from matplotlib.offsetbox import AnchoredText
from matplotlib.patheffects import withStroke
if size is None:
size = dict(size=plt.rcParams["legend.fontsize"])
at = AnchoredText(title, loc=loc, prop=size, pad=0.0, borderpad=0.5, frameon=False, **kwargs)
ax.add_artist(at)
return at
def get_golden_mean():
"""
Returns golden mean (sqrt(5) - 1.0) / 2.0
"""
return (np.sqrt(5) - 1.0) / 2.0
def set_mode(mode, aspect_ratio=0.95):
"""
Set the print mode, i.e. document and font size. Options are:
- onecol: width=80mm, font size=8pt. Appropriate for 1-column figures
- twocol: width=160mm, font size=8pt. Default.
Appropriate for 2-column figures
- medium: width=121mm, font size=7pt.
- small_font: width=121mm, font size=6pt.
- height: height=2.5in.
- small: width=80mm, font size=6pt
- presentation: width=85mm, font size=10pt. For presentations.
"""
linestyle = "-"
def set_onecol():
"""
Define parameters for "publish" mode and return value for pad_inches
"""
fontsize = 6
lw = 0.5
markersize = 2
fig_width = 3.15 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.5,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"legend.fontsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.30
def set_small():
"""
Define parameters for "publish" mode and return value for pad_inches
"""
fontsize = 6
lw = 0.5
markersize = 2
fig_width = 3.15 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.5,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"legend.fontsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"lines.markeredgewidth": 0.2,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.20
def set_72mm():
"""
Define parameters for "72mm" mode and return value for pad_inches
"""
fontsize = 6
markersize = 3
lw = 0.7
fig_width = 2.8 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.35,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"legend.fontsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.20
def set_50mm():
"""
Define parameters for "72mm" mode and return value for pad_inches
"""
fontsize = 5
markersize = 2.5
lw = 0.6
fig_width = 2.0 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.3,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"legend.fontsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.10
def set_medium():
"""
Define parameters for "medium" mode and return value for pad_inches
"""
fontsize = 8
markersize = 3
lw = 0.75
fig_width = 3.15 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.5,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"legend.fontsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.10
def set_small_font():
"""
Define parameters for "small_font" mode and return value for pad_inches
"""
fontsize = 6
markersize = 2
lw = 0.6
fig_width = 3.15 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.5,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"legend.fontsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.10
def set_large_font():
"""
Define parameters for "large_font" mode and return value for pad_inches
"""
fontsize = 10
markersize = 9
lw = 0.75
fig_width = 6.2 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.5,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"legend.fontsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.20
def set_presentation():
"""
Define parameters for "presentation" mode and return value
for pad_inches
"""
fontsize = 8
lw = 1.5
markersize = 3
fig_width = 6.64 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.75,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"legend.fontsize": fontsize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.2
def set_twocol():
"""
Define parameters for "twocol" mode and return value for pad_inches
"""
fontsize = 7
lw = 0.75
markersize = 3
fig_width = 6.3 # inch
fig_height = aspect_ratio * fig_width # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.5,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"legend.fontsize": fontsize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.35
def set_height():
"""
Define parameters for "twocol" mode and return value for pad_inches
"""
fontsize = 8
lw = 1.1
markersize = 1.5
fig_height = 2.5 # inch
fig_width = fig_height / aspect_ratio # inch
fig_size = [fig_width, fig_height]
params = {
"backend": "ps",
"axes.linewidth": 0.65,
"lines.linewidth": lw,
"axes.labelsize": fontsize,
"font.size": fontsize,
"xtick.labelsize": fontsize,
"ytick.labelsize": fontsize,
"lines.linestyle": linestyle,
"lines.markersize": markersize,
"legend.fontsize": fontsize,
"font.size": fontsize,
"figure.figsize": fig_size,
}
plt.rcParams.update(params)
return lw, 0.025
if mode == "onecol":
return set_onecol()
elif mode == "small":
return set_small()
elif mode == "medium":
return set_medium()
elif mode == "72mm":
return set_72mm()
elif mode == "50mm":
return set_50mm()
elif mode == "small_font":
return set_small_font()
elif mode == "large_font":
return set_large_font()
elif mode == "presentation":
return set_presentation()
elif mode == "twocol":
return set_twocol()
elif mode == "height":
return set_height()
else:
print(("%s mode not recognized, using onecol instead" % mode))
return set_twocol()
def trend_estimator(x, y):
"""
Trend estimator
Simultaneous estimation of bias, trend, annual, semi-annual and
161-day sinusoid (alias period S2 tide errors).
Parameters
----------
x, y : array_like, x must have units "years"
Returns
-------
x : ndarray
The solution (or the result of the last iteration for an unsuccessful
call).
cov_x : ndarray
Uses the fjac and ipvt optional outputs to construct an
estimate of the jacobian around the solution. ``None`` if a
singular matrix encountered (indicates very flat curvature in
some direction). This matrix must be multiplied by the
residual standard deviation to get the covariance of the
parameter estimates -- see curve_fit.
infodict : dict
a dictionary of optional outputs with the key s::
- 'nfev' : the number of function calls
- 'fvec' : the function evaluated at the output
- 'fjac' : A permutation of the R matrix of a QR
factorization of the final approximate
Jacobian matrix, stored column wise.
Together with ipvt, the covariance of the
estimate can be approximated.
- 'ipvt' : an integer array of length N which defines
a permutation matrix, p, such that
fjac*p = q*r, where r is upper triangular
with diagonal elements of nonincreasing
magnitude. Column j of p is column ipvt(j)
of the identity matrix.
- 'qtf' : the vector (transpose(q) * fvec).
mesg : str
A string message giving information about the cause of failure.
ier : int
An integer flag. If it is equal to 1, 2, 3 or 4, the solution was
found. Otherwise, the solution was not found. In either case, the
optional output variable 'mesg' gives more information.
Notes
-----
Code snipplet provided by Anthony Arendt, March 13, 2011.
Uses scipy.optimize.leastsq, see documentation of
scipy.optimize.leastsq for details.
"""
try:
from scipy import optimize
except:
print("scipy.optimize not found. Please install.")
exit(1)
def fitfunc(p, x):
return (
p[0]
+ p[1] * x
+ p[2] * np.cos(2.0 * np.pi * (x - p[3]) / 1.0)
+ p[4] * np.cos(2.0 * np.pi * (x - p[5]) / 0.5)
+ p[6] * np.cos(2.0 * np.pi * (x - p[7]) / 0.440794)
)
def errfunc(p, x, y):
return fitfunc(p, x) - y
p0 = [0.0, -80.0, 40.0, 0.0, 10.0, 0.0, 1.0, 0.0]
return optimize.leastsq(errfunc, p0[:], args=(x, y), full_output=1)
def colorList():
"""
Returns a list with colors, e.g for line plots. etc.
"""
colors = [
"#084594", # dark blue
"#FF7F00", # orange
"#984EA3", # violet
"#E41A1C", # red
"#4DAF4A", # green
"#377EB8", # light blue
"#FB9A99", # light red
"#FB9A99", # light orange
"#CAB2D6", # light violet
"brown",
"pink",
]
return colors
def gmtColormap(fileName, log_color=False, reverse=False):
"""
Import a CPT colormap from GMT.
Parameters
----------
fileName : a cpt file.
Example
-------
>>> cdict = gmtColormap("mycolormap.cpt")
>>> gmt_colormap = colors.LinearSegmentedColormap("my_colormap", cdict)
Notes
-----
This code snipplet modified after
http://www.mail-archive.com/[email protected]/msg09547.html
"""
import colorsys
import os
try:
try:
f = open(fileName)
except:
# Check if it's a colormap provided in colormaps/
basedir, fname = os.path.split(__file__)
my_file = os.path.join(basedir, "colormaps", fileName)
f = open(my_file)
except:
print("file ", fileName, "not found")
return None
lines = f.readlines()
f.close()
x = []
r = []
g = []
b = []
colorModel = "RGB"
for l in lines:
ls = l.split()
if l[0] == "#":
if ls[-1] == "HSV":
colorModel = "HSV"
continue
else:
continue
if ls[0] == "B" or ls[0] == "F" or ls[0] == "N":
pass
else:
x.append(float(ls[0]))
r.append(float(ls[1]))
g.append(float(ls[2]))
b.append(float(ls[3]))
xtemp = float(ls[4])
rtemp = float(ls[5])
gtemp = float(ls[6])
btemp = float(ls[7])
x.append(xtemp)
r.append(rtemp)
g.append(gtemp)
b.append(btemp)
if reverse:
r.reverse()
g.reverse()
b.reverse()
x = np.array(x, np.float32)
r = np.array(r, np.float32)
g = np.array(g, np.float32)
b = np.array(b, np.float32)
if colorModel == "HSV":
for i in range(r.shape[0]):
rr, gg, bb = colorsys.hsv_to_rgb(r[i] / 360.0, g[i], b[i])
r[i] = rr
g[i] = gg
b[i] = bb
if colorModel == "HSV":
for i in range(r.shape[0]):
rr, gg, bb = colorsys.hsv_to_rgb(r[i] / 360.0, g[i], b[i])
r[i] = rr
g[i] = gg
b[i] = bb
if colorModel == "RGB":
r = r / 255.0
g = g / 255.0
b = b / 255.0
if log_color:
xNorm = np.zeros((len(x),))
xNorm[1::] = np.logspace(-1, 0, len(x) - 1)
xNorm[1::-2] /= 4
else:
xNorm = (x - x[0]) / (x[-1] - x[0])
red = []
blue = []
green = []
for i in range(len(x)):
red.append([xNorm[i], r[i], r[i]])
green.append([xNorm[i], g[i], g[i]])
blue.append([xNorm[i], b[i], b[i]])
colorDict = {"red": red, "green": green, "blue": blue}
return colorDict
def smooth(x, window_len=11, window="hanning"):
"""
Smooth the data using a window with requested size (running mean,
moving average, low pass filtering).
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
Parameters
----------
x : array_like, the input signal
window_len : the dimension of the smoothing window; should be an odd integer
window : the type of window from "flat", "hanning", "hamming",
"bartlett", "blackman" flat window will produce a moving average smoothing.
Returns
-------
y : the smoothed signal
Example
-------
t = np.linspace(-2,2,0.1)
x = np.sin(t) + np.randn(len(t))*0.1
y = smooth(x)
See also
--------
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman,
numpy.convolve
scipy.signal.lfilter
Notes
-----
Downloaded from http://www.scipy.org/Cookbook/SignalSmooth.
TODO
----
the window parameter could be the window itself if an array instead
of a string
"""
if x.ndim != 1:
raise ValueError("smooth only accepts 1 dimension arrays.")
if x.size < window_len:
raise ValueError("Input vector needs to be bigger than window size.")
if window_len < 3:
return x
if not window in ["flat", "hanning", "hamming", "bartlett", "blackman"]:
raise ValueError("Window is one of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")
s = np.r_[2 * x[0] - x[window_len:1:-1], x, 2 * x[-1] - x[-1:-window_len:-1]]
if window == "flat": # moving average
w = np.ones(window_len, "d")
else:
w = eval("np." + window + "(window_len)")
y = np.convolve(w / w.sum(), s, mode="same")
return y[window_len - 1 : -window_len + 1]
def fftsmooth(x, window_len=11, window="hanning"):
"""
Smooth the data using a window with requested size (running mean,
moving average, low pass filtering).
This method is based on the convolution of a scaled window with the signal.
The signal is prepared by introducing reflected copies of the signal
(with the window size) in both ends so that transient parts are minimized
in the begining and end part of the output signal.
Parameters
----------
x : array_like, the input signal
window_len : the dimension of the smoothing window; should be an odd integer
window : the type of window from "flat", "hanning", "hamming",
"bartlett", "blackman" flat window will produce a moving average smoothing.
Returns
-------
y : the smoothed signal
Example
-------
t = np.linspace(-2,2,0.1)
x = np.sin(t) + np.randn(len(t))*0.1
y = smooth(x)
See also
--------
numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman,
numpy.convolve
scipy.signal.lfilter
Notes
-----
Downloaded from http://www.scipy.org/Cookbook/SignalSmooth, but replaced
np.convovle with faster scipy.signal.fftconvolve
TODO
----
the window parameter could be the window itself if an array instead
of a string
"""
from scipy.signal import fftconvolve
if x.ndim != 1:
raise ValueError("smooth only accepts 1 dimension arrays.")
if x.size < window_len:
raise ValueError("Input vector needs to be bigger than window size.")
if window_len < 3:
return x
if not window in ["flat", "hanning", "hamming", "bartlett", "blackman"]:
raise ValueError("Window is one of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")
s = np.r_[2 * x[0] - x[window_len:1:-1], x, 2 * x[-1] - x[-1:-window_len:-1]]
if window == "flat": # moving average
w = np.ones(window_len, "d")
else:
w = eval("np." + window + "(window_len)")
y = fftconvolve(w / w.sum(), s, mode="same")
return y[window_len - 1 : -window_len + 1]
def get_rmse(a, b, N, w=None):
"""
Returns the (weighted) root mean square error of differences between a and b.
Parameters
----------
a, b : array_like
N : number of valid values
w : weights
Returns
-------
rmse : scalar
"""
if w is None:
w = np.ones_like(a)
c = (a.ravel() - b.ravel()) / w.ravel()
if isinstance(c, np.ma.MaskedArray):
return np.sqrt(np.linalg.norm(np.ma.compressed(c), 2) ** 2.0 / N)
else:
return np.sqrt(np.linalg.norm(c, 2) ** 2.0 / N)
def get_avg(a, b, N, relative=False):
"""
Returns the average difference between a and b.
Parameters
----------
a,b : array_like
N : number of values
Returns
-------
avg : scalar
Notes
-----
The average is the sum of elements of the difference (a - b)
divided by the number of elements N.
"""
if relative is False:
c = a.ravel() - b.ravel()
else:
c = a.ravel() - b.ravel() / b.ravel()
if isinstance(c, np.ma.MaskedArray):
return np.linalg.norm(np.ma.compressed(c), 1) / N
else:
return np.linalg.norm(c, 1) / N
def unit_converter(data, inunit, outunit):
"""
Unit converter. Takes an (numpy) array, valid udunits inunits and outunits
as strings, and returns the array in outunits.
Parameters
----------
data : array_like
inunit : string
unit to convert from, must be UDUNITS-compatible string
outunit : string
unit to conver to, must be UDUNITS-compatible string
Returns
-------
out : array_like
Example
-------
>>> import numpy as np
>>> c = Converter("kg","Gt")
>>> out = c(np.array([1,2])*1e12)
>>> out = array([ 1., 2.])
"""
inunit = str(inunit)
outunit = str(outunit)
if isinstance(data, np.ma.MaskedArray):
mask = data.mask
else:
mask = None
data = np.array(data)
if not (inunit == outunit):
try:
try:
from cf_units import Unit
in_unit = Unit(inunit)
out_unit = Unit(outunit)
outdata = in_unit.convert(data, out_unit)
except:
from udunits2 import Converter, System, Unit
sys = System()
c = Converter((Unit(sys, inunit), Unit(sys, outunit)))
outdata = c(data)
except:
print("Neither cf_units or udunits2 module found, you're on your own.")
c = 1.0 / 1e3
outdata = c * data
else:
outdata = data