Smooth function: Difference between revisions

From SPEDAS Wiki
Jump to navigation Jump to search
 
Line 32: Line 32:
== Python code: pySPEDAS ==
== Python code: pySPEDAS ==


<pre>
<syntaxhighlight lang="python">


import pytplot
import pytplot
Line 61: Line 61:
# Run the example code
# Run the example code
ex_test_smooth()
ex_test_smooth()
</pre>
</syntaxhighlight>

Latest revision as of 02:47, 29 December 2023

Compare IDL code to python code for smoothing data.

IDL code: SPEDAS

<syntaxhighlight lang="idl"> pro test_smooth

 ; smooth data
 
 t = [1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.]
 y = [3., 5., 8., 15., 20., 1., 2., 3., 4., 5., 6., 4.]
 store_data, 'original', data={x:t, y:y}
 tsmooth2, 'original', 5, newname = 'smooth'
 ylim, 'original', 0, 20
 ylim, 'smooth', 0, 20
 tplot, ['original', 'smooth']
 
 get_data, 'original', data=d0
 print, 'Original data:', d0.y
 get_data, 'smooth', data=d
 print, 'Smooth data:', d.y
 
 ; Results:
 ;Original data:    3.00000      5.00000      8.00000      15.0000      20.0000      1.00000      2.00000      3.00000      4.00000      5.00000      6.00000      4.00000
 ;Smooth data:      3.00000      5.00000      10.2000      9.80000      9.20000      8.20000      6.00000      3.00000      4.00000      4.40000      6.00000      4.00000
 

end </syntaxhighlight>

Python code: pySPEDAS

<syntaxhighlight lang="python">

import pytplot from pyspedas.analysis.tsmooth import tsmooth


def ex_test_smooth():

   t = [1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.]
   y = [3., 5., 8., 15., 20., 1., 2., 3., 4., 5., 6., 4.]
   pytplot.store_data('original', data={'x': t, 'y': y})
   tsmooth('original', width=5, new_names='smooth', preserve_nans=1)
   pytplot.tplot(['original', 'smooth'])
   d0 = pytplot.get_data('original')
   print('Original data: ', d0[1])
   d = pytplot.get_data('smooth')
   print('Smooth data: ', d[1])
   """
   Results:
   Original data:[ 3., 5., 8.,   15., 20., 1.,  2., 3., 4., 5.,  6.,  4.]
   Smooth data:  [ 3., 5., 10.2, 9.8, 9.2, 8.2  6., 3., 4., 4.4, 6.,  4.]
   """
  1. Run the example code

ex_test_smooth() </syntaxhighlight>