/gdal-1.6.1-tcl_patched/swig/python/samples/fft.py

http://tcl-map.googlecode.com/ · Python · 139 lines · 83 code · 21 blank · 35 comment · 28 complexity · 5fac380b023cc37db17dba264b45baf9 MD5 · raw file

  1. #!/usr/bin/env python
  2. ###############################################################################
  3. # $Id: fft.py 13092 2007-11-26 21:12:22Z hobu $
  4. #
  5. # Project: GDAL Python samples
  6. # Purpose: Script to perform forward and inverse two-dimensional fast
  7. # Fourier transform.
  8. # Author: Andrey Kiselev, dron@remotesensing.org
  9. #
  10. ###############################################################################
  11. # Copyright (c) 2003, Andrey Kiselev <dron@remotesensing.org>
  12. #
  13. # Permission is hereby granted, free of charge, to any person obtaining a
  14. # copy of this software and associated documentation files (the "Software"),
  15. # to deal in the Software without restriction, including without limitation
  16. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. # and/or sell copies of the Software, and to permit persons to whom the
  18. # Software is furnished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included
  21. # in all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. # DEALINGS IN THE SOFTWARE.
  30. ###############################################################################
  31. try:
  32. from osgeo import gdal
  33. from osgeo.gdalconst import *
  34. import numpy as Numeric
  35. except ImportError:
  36. import gdal
  37. from gdalconst import *
  38. import Numeric
  39. import FFT
  40. import sys
  41. # =============================================================================
  42. def Usage():
  43. print 'Usage: fft.py [-inv] [-of out_format] [-ot out_type] infile outfile'
  44. print
  45. sys.exit( 1 )
  46. # =============================================================================
  47. # =============================================================================
  48. def ParseType(type):
  49. if type == 'Byte':
  50. return GDT_Byte
  51. elif type == 'Int16':
  52. return GDT_Int16
  53. elif type == 'UInt16':
  54. return GDT_UInt16
  55. elif type == 'Int32':
  56. return GDT_Int32
  57. elif type == 'UInt32':
  58. return GDT_UInt32
  59. elif type == 'Float32':
  60. return GDT_Float32
  61. elif type == 'Float64':
  62. return GDT_Float64
  63. elif type == 'CInt16':
  64. return GDT_CInt16
  65. elif type == 'CInt32':
  66. return GDT_CInt32
  67. elif type == 'CFloat32':
  68. return GDT_CFloat32
  69. elif type == 'CFloat64':
  70. return GDT_CFloat64
  71. else:
  72. return GDT_Byte
  73. # =============================================================================
  74. infile = None
  75. outfile = None
  76. format = 'GTiff'
  77. type = None
  78. transformation = 'forward'
  79. # Parse command line arguments.
  80. i = 1
  81. while i < len(sys.argv):
  82. arg = sys.argv[i]
  83. if arg == '-inv':
  84. transformation = 'inverse'
  85. if type == None:
  86. type = GDT_Float32
  87. elif arg == '-of':
  88. i = i + 1
  89. format = sys.argv[i]
  90. elif arg == '-ot':
  91. i = i + 1
  92. type = ParseType(sys.argv[i])
  93. set_type = 'yes'
  94. elif infile is None:
  95. infile = arg
  96. elif outfile is None:
  97. outfile = arg
  98. else:
  99. Usage()
  100. i = i + 1
  101. if infile is None:
  102. Usage()
  103. if outfile is None:
  104. Usage()
  105. if type == None:
  106. type = GDT_CFloat32
  107. indataset = gdal.Open( infile, GA_ReadOnly )
  108. out_driver = gdal.GetDriverByName(format)
  109. outdataset = out_driver.Create(outfile, indataset.RasterXSize, indataset.RasterYSize, indataset.RasterCount, type)
  110. for iBand in range(1, indataset.RasterCount + 1):
  111. inband = indataset.GetRasterBand(iBand)
  112. outband = outdataset.GetRasterBand(iBand)
  113. data = inband.ReadAsArray(0, 0)
  114. if transformation == 'forward':
  115. data_tr = FFT.fft2d(data)
  116. else:
  117. data_tr = FFT.inverse_fft2d(data)
  118. outband.WriteArray(data_tr)