python - Problem solving the max and min from an input file data -


i new python , need little python script named search_max.py.

it opens file "xyz" format , search min , max of each coord. problem when same awk script don't same resuts!!!

i wonder if there problem type of data or string operation or ... can me solve problem?

python script :

#!/usr/bin/python # -*- coding: iso-8859-15 -*- inputfile = "peamorphe.xyz" outputfile = "result.txt" # open input file infile = open(inputfile, "r") # read line 1 : number of atoms atomsno = infile.readline().rstrip('\n').split(" ") # read line 2 : name of system systemname = infile.readline().rstrip('\n') # read line 3 : initialisation min , max temp2 = infile.readline().rstrip('\n').split(" ") zmin = temp2[3] zmax = temp2[3] ymax = temp2[2] ymin = temp2[2] xmax = temp2[1] xmin = temp2[1] lineno = 3 print zmax, ymin, xmin # read other lines ligne in infile.readlines():     lineno = lineno + 1     # extraction , strip of data spaced " "     data = ligne.rstrip('\n\r').split(" ")     # conditions min , max     if data[1] < xmin:          xmin = data[1]          wclxmin = lineno     if data[1] > xmax:          xmax = data[1]         wclxmax = lineno     if data[2] < ymin:          ymin = data[2]         wclymin = lineno     if data[2] > ymax:          ymax = data[2]         wclymax = lineno     if data[3] < zmin:         zmin = data[3]         wclzmin = lineno     if data[3] > zmax:          zmax = data[3]          wclzmax = lineno  # evaluation of centers zcenter = float(zmax)-float(zmin) ycenter = float(ymax)-float(ymin) xcenter = float(xmax)-float(xmin)  # open input file infile = open(inputfile, "r") # read line 1 : number of atoms atomsno = infile.readline().rstrip('\n').split(" ") # read line 2 : name of system systemname = infile.readline().rstrip('\n') # read line 3 : initialisation min , max temp2 = infile.readline().rstrip('\n').split(" ") zmin = temp2[3] zmax = temp2[3] ymax = temp2[2] ymin = temp2[2] xmax = temp2[1] xmin = temp2[1] lineno = 3 print zmax, ymin, xmin # read other lines ligne in infile.readlines():     lineno = lineno + 1     # extraction , strip of data spaced " "     data = ligne.rstrip('\n\r').split(" ")     # conditions min , max     if data[1] < xmin:          xmin = data[1]          wclxmin = lineno     if data[1] > xmax:          xmax = data[1]         wclxmax = lineno     if data[2] < ymin:          ymin = data[2]         wclymin = lineno     if data[2] > ymax:          ymax = data[2]         wclymax = lineno     if data[3] < zmin:         zmin = data[3]         wclzmin = lineno     if data[3] > zmax:          zmax = data[3]          wclzmax = lineno  # evaluation of centers zcenter = float(zmax)-float(zmin) ycenter = float(ymax)-float(ymin) xcenter = float(xmax)-float(xmin) 

awk script :

#!/usr/bin/awk -f # xyz file begin{ xmax;xmin; zmax;zmin; ymax;ymin; xcent;ycent;zcent; xcent = (xmax-xmin)/2; ycent = (ymax-ymin)/2; zcent = (zmax-zmin)/2; print   "at start of script"; print   "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent;  print   "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent; print   "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent; print   ""; }  { if (xmax<$2) xmax = $2 if (xmin>$2) xmin = $2 if (ymax<$3) ymax = $3 if (ymin>$3) ymin = $3 if (zmax<$4) zmax = $4 if (zmin>$4) zmin = $4 }  end{ xcent = (xmax-xmin)/2; ycent = (ymax-ymin)/2; zcent = (zmax-zmin)/2; print   "at end of script"; print   "xmax = " xmax "; " "xmin = " xmin "; xcent = " xcent;  print   "ymax = " ymax "; " "ymin = " ymin "; ycent = " ycent; print   "zmax = " zmax "; " "zmin = " zmin "; zcent = " zcent} 

you can download input file here (14 days): peamorphe.xyz

thank in advance, exilien.

the reason why code not work expected given eumiro in above comment.

there far easier approach in python though: use numpy. example code maxima , minima of each column of file be

import numpy = numpy.loadtxt("peamorphe.xyz", skiprows=2, usecols=(1, 2, 3)) max_xyz = a.max(axis=0) min_xyz = a.min(axis=0) center = max_xyz - min_xyz 

these few lines of code script does, including parsing input file. if need indices of maxima , minima, can use a.argmax() , a.argmin().

looks quite bit easier, don't think?


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -