从Python代码到工作可执行文件(缩小网格文件程序)

时间:2013-03-02 17:39:44

标签: python syntax

我之前在Invalid Syntax error in Python Code I copied from the Internet发布了一个关于语法错误的问题。幸运的是,由于你的缘故,我的问题得到了解决。但是现在没有语法错误我发现自己无助,因为我现在不知道该怎么做这个代码。正如我所说的那样,3年前我已经完成了一些基本的Python培训,但人脑似乎忘记了这么快。

所以简而言之,我需要将某些文件的网格分辨率降低到一半,而且我一直在寻找一种方法来实现它。幸运的是,我发现一些python代码似乎完全符合我的要求。代码是这样的:

#!/bin/env python
# -----------------------------------------------------------------------------
# Reduce grid data to a smaller size by averaging over cells of specified
# size and write the output as a netcdf file.  xyz_origin and xyz_step
# attributes are adjusted.
#
# Syntax: downsize.py <x-cell-size> <y-cell-size> <z-cell-size>
#                       <in-file> <netcdf-out-file>
#
import sys

import Numeric

from VolumeData import Grid_Data, Grid_Component

# -----------------------------------------------------------------------------
#
def downsize(mode, cell_size, inpath, outpath):

  from VolumeData import fileformats
  try:
    grid_data = fileformats.open_file(inpath)
  except fileformats.Uknown_File_Type as e:
    sys.stderr.write(str(e))
    sys.exit(1)

  reduced = Reduced_Grid(grid_data, mode, cell_size)

  from VolumeData.netcdf.netcdf_grid import write_grid_as_netcdf
  write_grid_as_netcdf(reduced, outpath)

# -----------------------------------------------------------------------------
# Average over cells to produce reduced size grid object.
#
# If the grid data sizes are not multiples of the cell size then the
# final data values along the dimension are not included in the reduced
# data (ie ragged blocks are not averaged).
#
class Reduced_Grid(Grid_Data):

  def __init__(self, grid_data, mode, cell_size):

    size = map(lambda s, cs: s / cs, grid_data.size, cell_size)
    xyz_origin = grid_data.xyz_origin
    xyz_step = map(lambda step, cs: step*cs, grid_data.xyz_step, cell_size)
    component_name = grid_data.component_name
    components = []
    for component in grid_data.components:
      components.append(Reduced_Component(component, mode, cell_size))

    Grid_Data.__init__(self, '', '', size, xyz_origin, xyz_step,
                       component_name, components)

# -----------------------------------------------------------------------------
# Average over cells to produce reduced size grid object.
#
class Reduced_Component(Grid_Component):

  def __init__(self, component, mode, cell_size):

    self.component = component
    self.mode = mode
    self.cell_size = cell_size
    Grid_Component.__init__(self, component.name, component.rgba)

  # ---------------------------------------------------------------------------
  #
  def submatrix(self, ijk_origin, ijk_size):

    ijk_full_origin = map(lambda i, cs: i * cs, ijk_origin, self.cell_size)
    ijk_full_size = map(lambda s, cs: s*cs, ijk_size, self.cell_size)
    values = self.component.submatrix(ijk_full_origin, ijk_full_size)

    if mode == 'ave':
      m = average_down(values, self.cell_size)

我把它保存为.py文件,当我双击它时,命令提示符出现一个毫秒然后消失。我设法截取了该命令提示符,其中显示“无法使用'bin / env python创建进程”C:\ Users ........... py“。

我想要做的是能够使用代码告诉我使用的语法来缩小尺寸:

# Syntax: downsize.py <x-cell-size> <y-cell-size> <z-cell-size>
    #                       <in-file> <netcdf-out-file>

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

不要通过双击来运行该文件。通过打开一个新shell运行该文件,然后键入.py文件的路径(或只是父目录的cd),然后输入要传递的参数。例如:

python downsize.py 1 2 3 foo bar