Author Topic: Calculation/Conversion with Text Files  (Read 9747 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Calculation/Conversion with Text Files
« Reply #15 on: March 19, 2010, 09:44:11 AM »
Ok thanks... I am moving along. Here is how I have it and it worked.

awesome, lucky guess, glad you're on your way
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #16 on: March 19, 2010, 11:51:04 AM »
How do I apply something like this: (setq Conv3 (* 100.0 (/ B (- 100.0 A)))) where A and B are lists. Both lists have real numbers in them so I think I don't have to do any data type conversion. I tried this:
Code: [Select]
(if (/= (car ListfileM) null)
    (setq Conv3 (* 100 (/ (car listfileA) (- 100 (car ListfileM)))))
    (prompt "nUnable to convert")
  )

=============AND THIS=========
Code: [Select]
  (setq A (car ListfileM))
  (setq B (car ListfileA))
  (setq Conv3 (* 100.0 (/ B (- 100.0 A))))
.
Just to see if I can get it to work with the first elements...Nothing
============== Acad Map 3D 2009================

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Calculation/Conversion with Text Files
« Reply #17 on: March 19, 2010, 12:55:47 PM »
Rolling eyes? wtf, I did something wrong?

No not at all - I'm in agreement with Kerry  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Calculation/Conversion with Text Files
« Reply #18 on: March 19, 2010, 01:10:55 PM »
Ziko,

Bear in mind that you are reading strings from the files, these will need to be converted to Reals before proceeding with your calculations, then back to strings to write to the output.

Lee

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Calculation/Conversion with Text Files
« Reply #19 on: March 19, 2010, 01:17:16 PM »
One tip.  If you are processing a list ( or more than one list ), and you want a list returned, then look at ' mapcar '.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #20 on: March 19, 2010, 04:25:39 PM »
I got this to work
Code: [Select]
  (setq Output (mapcar '(lambda (x y)
   (* x y) 
   )              
'(1 2 3 4)  '(6 6 6 6)
)
          )
But not this
 
Code: [Select]
(setq Output (mapcar '(lambda (x y)
                (* 100 (/ x (- 100 y)))
  )              
'(1 2 3 4)  '(6 6 6 6)
)
    )
I get (0 0 0 0). It's probably not a simple plug in. Any more pointers?
============== Acad Map 3D 2009================

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Calculation/Conversion with Text Files
« Reply #21 on: March 19, 2010, 04:31:21 PM »
Beware of the difference between Integers and Reals

Code: [Select]
(setq Output (mapcar '(lambda (x y)
                (* 100. (/ x (- 100. y)))
  )              
'(1 2 3 4)  '(6 6 6 6)
)
    )

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #22 on: March 19, 2010, 08:19:35 PM »
Thanks all, this works
Code: [Select]
(setq Output (mapcar '(lambda (x y) (* (/ x (- 100.0 y)) 100.0)) ListA ListM))
============== Acad Map 3D 2009================

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #23 on: August 04, 2010, 10:34:12 AM »
A while back I got help putting this together. I now have time to improve it.  It does what I needed it to do, which is (A x B = C) where A, B, C are text files and 'x' is a formula.  It works as

follows.

1-copy the first 6 lines of A or B to new file, C (indentical in both A and B)
2-Start applying formula at the 7th lines of both A and B and write to C starting at 7th line.
3-strip out  of C anything that is negative or zero (Formula applied to text in files: need help on how to stop calculation where it says "Make file" in A and B  and copy everything to C after that

line)

I would like to see other ways of getting this done if anyone has the time to have a go at it. Alan suggested I use a subroutine to open/read/close a file and return the data; I am not sure how to

apply this when I need to read from two separate files... will be glad of you (Alan) could explain little more. I have attached the complete text files for anyone who would have a go at it. You

could simply provide an alternative routine to do this without going through my routine. It gets the job done for me but I am sure there better ways of doing this.

Would like to incoporate error trap but I am not sure how to set it up. Whether when user selects wrong file type, or exist before selecting file etc...Thanks for you continued help.

 
Code: [Select]
(defun C:Conv (/) ;Localize later


;===============Tell User To Select Moisture First====================

  (alert
    "1- Select Moisture Grid File First \n2- Select Ash Grid File Second "
  )

;======Read first file to list======================================

  (if (setq M_Open
    (getfiled "Select Moisture_Grid" "" "txt" 16)
      )
    (setq M_Read (open M_Open "r")) ; read text file
    (prompt "\n...Unable to open file."); tell use if unable to get textfile
  )
  (while (Setq M_Line1 (read-line M_Read)) ; read textfile to list
    (Setq M_Listfile (cons M_Line1 M_Listfile))
; create list of data read
  )
  (setq M_List
(reverse
  (mapcar 'atof M_Listfile)
)
  )
;Convert to real for calculations

  (repeat 6 (setq M_list (cdr M_List))) ; remove first 6 lines

;===========Read Second file to list==============================

  (if (setq M_OpenA (getfiled "Select Ash_Grid" "" "txt" 16))
    (setq M_ReadA (open M_OpenA "r"))
    (prompt "\n...Unable to open file.")
  )
  (while
    (Setq M_Line1A
  (read-line M_ReadA)
    )
     (Setq M_ListfileA
   (cons M_Line1A M_ListfileA)
     )
  )
  (setq M_ListA
(reverse
  (mapcar 'atof M_ListfileA);Convert to real for calculations
)
  )


  (repeat 6 (setq M_listA (cdr M_ListA))) ; remove first 6 lines

;===Create Output file and Add Coordinates and grid sizes======

  (setq M_Output (open "C:\\Projects\\Output.txt" "w"))
  (setq M_List_out (reverse M_Listfile))
  (setq m 0)
  (while (< m 6); use repeat
    (setq M_line (nth m M_List_out))
    (write-line M_line M_Output)
    (setq m (1+ m))
  )
  (close M_Output)

;==================== Process Files==========================================

    (setq M_Outfile
  (mapcar '(lambda (x y) (* (/ x (- 100.0000 y)) 100.0000))
  M_List
  M_ListA
  )
    )

  (setq M_Outfile1
(vl-remove-if 'Zerop (vl-remove-if 'Minusp M_Outfile))
  )

  (setq M_Output (open "C:\\Projects\\Output.txt" "a"))
  (setq M_Outfile1 (mapcar 'rtos M_Outfile1))
; Convert to string for output
  (while (setq M_O_Line (car M_Outfile1))
    (write-line M_O_Line M_Output)
    (setq M_Outfile1 (cdr M_Outfile1))
  )

  (close M_Output)

  (princ "\nFinished...File Converted")
  (princ)

)

« Last Edit: August 04, 2010, 11:07:22 AM by ziko30 »
============== Acad Map 3D 2009================

efernal

  • Bull Frog
  • Posts: 206
Re: Calculation/Conversion with Text Files
« Reply #24 on: August 04, 2010, 11:38:23 AM »
(setq Convert (atof (* 100 (/ A_Line1 (- 100 M_Line1)))))
    (write-line Convert Converted)

Convert is not a string, so use PRINC instead...

e.fernal
e.fernal

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #25 on: August 04, 2010, 11:46:37 AM »
(setq Convert (atof (* 100 (/ A_Line1 (- 100 M_Line1)))))
    (write-line Convert Converted)

Convert is not a string, so use PRINC instead...

e.fernal

Thanks e.fernal... I did not want to start a new thread, although I see that might cause some misunderstanding... but what you have noted has already been taken care. I have something that works in my last post. I just want to see different versions of the same process. You can use the attached text files if you have the time to have a go.
============== Acad Map 3D 2009================

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calculation/Conversion with Text Files
« Reply #26 on: August 05, 2010, 01:35:54 PM »
My revision but no subroutine. See how the cond is used to end the routine if any failures occur.
Still needs an error handler though.

Code: [Select]
(defun C:Conv (/           M           M_LINE      M_LINE1     M_LINE1A    M_LIST      M_LISTA
               M_LISTFILE  M_LISTFILEA M_LIST_OUT  M_OPEN      M_OPENA     M_OUTFILE   M_OUTFILE1
               M_OUTPUT    M_O_LINE    M_READ      M_READA     X           Y
              )
  ;;===============Tell User To Select Moisture First====================
  (alert "1- Select Moisture Grid File First \n2- Select Ash Grid File Second ")
  (cond
    ;;======Read first file to list======================================
    ((not
       (and
         (setq M_Open (getfiled "Select Moisture_Grid" "" "txt" 16))
         (setq M_Read (open M_Open "r")) ; read text file
         (progn
           (while (setq M_Line1 (read-line M_Read)) ; read textfile to list
             (setq M_Listfile (cons M_Line1 M_Listfile)) ; create list of data read
           )
           (setq M_List (reverse (mapcar 'atof M_Listfile)))  ; Convert to real for calculations
           (repeat 6 (setq M_list (cdr M_List))) ; remove first 6 lines
         )
       )
     )
     (prompt "\n...Unable to open file.") ; tell use if unable to get textfile
    )
    ;;===========Read Second file to list==============================
    ((not
       (and
         (setq M_OpenA (getfiled "Select Ash_Grid" "" "txt" 16))
         (setq M_ReadA (open M_OpenA "r"))
         (progn
           (while (setq M_Line1A (read-line M_ReadA))
             (setq M_ListfileA (cons M_Line1A M_ListfileA))
           )
           (setq M_ListA (reverse (mapcar 'atof M_ListfileA))) ;Convert to real for calculations
           (repeat 6 (setq M_listA (cdr M_ListA))) ; remove first 6 lines
         )
       )
     )
     (prompt "\n...Unable to open file.")
    )
    ;;===Create Output file and Add Coordinates and grid sizes======
    ((not
       (and
         (setq M_Output (open "C:\\Projects\\Output.txt" "w"))
         ;;(setq M_Output (open "Output.txt" "w"))
         (setq M_List_out (reverse M_Listfile))
         (setq m 0)
         (progn
           (while (< m 6)               ; use repeat
             (setq M_line (nth m M_List_out))
             (write-line M_line M_Output)
             (setq m (1+ m))
           )
           ;;==================== Process Files==========================================
           (setq M_Outfile
                  (mapcar '(lambda (x y) (* (/ x (- 100.0 y)) 100.0)) M_List M_ListA)
           )
           (setq M_Outfile1 (vl-remove-if 'zerop (vl-remove-if 'minusp M_Outfile)))
           (setq M_Outfile1 (mapcar 'rtos M_Outfile1)) ; Convert to string for output
           (while (setq M_O_Line (car M_Outfile1))
             (write-line M_O_Line M_Output)
             (setq M_Outfile1 (cdr M_Outfile1))
           )
           (null (close M_Output))
           (princ "\nFinished...File Converted")
         )
       )
     )
     (prompt "\n...Unable to open output file.")
    )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #27 on: August 05, 2010, 02:43:50 PM »
Thanks for the time CAB... I am going to look at it with my autolisp help files open to see how COND, AND, NOT, NULL are being applied in your revision.  I also can see how your formatting helps in following the routine. Up to now, I 've felt until it works, no need to make it pretty, but I think that is not he right approach.
============== Acad Map 3D 2009================

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calculation/Conversion with Text Files
« Reply #28 on: August 05, 2010, 05:18:50 PM »
What happens to this data from file 'A'?
Code: [Select]
Make File
Method: Inverse Distance
Strata: CO_EG E_MOIS_AR
Modeling Method: Inverse Distance
Search Radius: 10000.0 Max Samples: 20 Min Quadrant: 0 Max Quadrant: 20
Pinch Out:  OFF
Conformance:  OFF
Northing     Easting      Value        Point    Source
416306.400   1847269.240  Point KE-41-2005 Not Used
413646.530   1845818.530  Point KE-40-2005 Not Used
414752.500   1846111.160  Point KE-39-2005 Not Used
415615.370   1845844.340  Point KE-38-2005 Not Used
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calculation/Conversion with Text Files
« Reply #29 on: August 05, 2010, 05:48:28 PM »
Another version:
Code: [Select]
(defun C:Conv (/           M           M_LINE      M_LINE1     M_LINE1A    M_LISTA      M_LISTB
               M_LISTFILE  M_LISTFILEA M_LIST_OUT  M_OPEN      M_OPENA     M_OUTFILE   M_OUTFILE1
               M_OUTPUT    M_O_LINE    M_READ      M_READA     6_lines
              )
;;  return a list of text string for each line in the file
(defun ReadFile (fname / fn ln AllLines)
  (if (setq fn (findfile fname)) ; only if the file is found
    (progn ; fn contains the full path
      (setq fn (open fn "r")) ; open file for reading
      (while (setq ln (read-line fn)) ; read all the lines in the file
(setq AllLines (cons ln AllLines))
      )
      (close fn) ; close the open file handle
    )
  )
  (reverse AllLines)
)
 
  ;;===============Tell User To Select Moisture First====================
  (alert "1- Select Moisture Grid File First \n2- Select Ash Grid File Second ")
  (cond
    ;;======Read first file to list======================================
    ((not
       (and
         (setq M_Open (getfiled "Select Moisture_Grid" "" "txt" 16))
         (setq M_ListA (ReadFile M_Open))      ; read textfile to list
         (repeat 6 (setq 6_lines (cons (car M_ListA) 6_lines)
                         M_listA  (cdr M_ListA))) ; remove first 6 lines
         (setq M_ListA (mapcar (function (lambda(x) (distof x 2))) M_ListA)) ; Convert to real for calculations
         (setq M_ListA (vl-remove-if 'listp M_ListA))
       )
     )
     (prompt "\n...Unable to open file.") ; tell use if unable to get textfile
    )
    ;;===========Read Second file to list==============================
    ((not
       (and
         (setq M_Open (getfiled "Select Ash_Grid" "" "txt" 16))
         (setq M_ListB (ReadFile M_Open))     ; read textfile to list
         (repeat 6 (setq M_listB (cdr M_ListB)))  ; remove first 6 lines
         (setq M_ListB (mapcar (function (lambda(x) (distof x 2))) M_ListB)) ; Convert to real for calculations
         (setq M_ListB (vl-remove-if 'listp M_ListB))
       )
     )
     (prompt "\n...Unable to open file.")
    )
    ;;===Create Output file and Add Coordinates and grid sizes======
    ((not
       (and
         (setq M_Output (open "C:\\Projects\\Output.txt" "w"))
         (progn
           (mapcar (function (lambda (x) (write-line x M_Output))) (reverse 6_lines))
           (setq M_Outfile
                  (mapcar
                    (function
                      (lambda (x y) (* (/ x (- 100.0 y)) 100.0))) M_ListA M_ListB)
           )
           (mapcar (function (lambda(x) (write-line (rtos x 2) M_Output))) M_Outfile)
           (null (close M_Output))
           (princ "\nFinished...File Converted")
         )
       )
     )
     (prompt "\n...Unable to open output file.")
    )
  )
  (princ)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.