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

0 Members and 1 Guest are viewing this topic.

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #30 on: August 05, 2010, 11:36:01 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


If I can, I would like to copy it to C (output file), but if I lose it that is not a problem.
============== Acad Map 3D 2009================

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #31 on: August 05, 2010, 11:41:49 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.


THIS IS THE WAY I UNDERSTAND THE CODE
=================================
"COND" has 3 tests & 3 expressions to execute:

Test

Code: [Select]
((not
        (and .....
                .....     
        )
)


On failure execute expression
Code: [Select]
(Prompt "\n... Unable to open file)


"AND"
Should return T when all arguments are true


===========================

"NOT"
make sure I don't have NIL here? But am I checking whether a file is selected or am I checking if any of the variables within that expression have something in them ? I am finding the "NOT" function confusing.

Working backwards....
Code: [Select]
AND returns T, NOT returns NIL, COND executes PROMPT ??? I am thinking when it works they all return T
         

============== Acad Map 3D 2009================

ziko30

  • Newt
  • Posts: 51
Re: Calculation/Conversion with Text Files
« Reply #32 on: August 05, 2010, 11:43:47 PM »
Another version:


I will take the weekend to study this one... appreciate your time.
============== Acad Map 3D 2009================

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calculation/Conversion with Text Files
« Reply #33 on: August 06, 2010, 12:48:26 AM »
Working backwards....
Code: [Select]
AND returns T, NOT returns NIL, COND executes PROMPT ??? I am thinking when it works they all return T
 

Almost correct:
AND returns T, NOT returns NIL, therefore  COND  does not execute the PROMPT, just goes on to the next COND test. 8-)

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 #34 on: August 06, 2010, 12:50:00 AM »
If I can, I would like to copy it to C (output file), but if I lose it that is not a problem.
In the next version I'll add this back to the output file.
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 #35 on: August 06, 2010, 10:30:13 AM »
You need to test this routine as I don't have time this morning.
Pseudo Code
Quote
**  Do this twice
>>>>>>>>>>>
Get file choice from user
Read the file
Remove first 6 lines (save them from file A)
Split remaining list into two list, data and non-data (file A only)
Quit if there is a problem
<<<<<<<<<<<<<

Then test data list length & option to quit
Test file length & option to quit

Open output file, quit if there is a problem
Calculate combined data
Write first 6 lines
Write new data
Write non-data
Close and all done


Code: [Select]
(defun C:Conv (/ 6_lines flag flength1 flength2 list_suffix m_lista m_listb m_open
               m_outfile m_output raw_data tmp1 tmp2
              )
  ;;  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))
         (princ "\n>>>   Reading Moisture File   <<<\n")
         (setq Raw_Data (ReadFile M_Open))      ; read textfile to list
         (setq flength1 (length Raw_Data))
         (repeat 6 (setq 6_lines (cons (car Raw_Data) 6_lines) ; save the first 6 lines
                         Raw_Data  (cdr Raw_Data))) ; remove first 6 lines
         ;;  split Raw_Data into two lists
         (setq flag t) ; this avoids the real conversion once one failure occurs
         (not ; prevent the WHILE from stopping the process
           (while (setq tmp1 (car Raw_Data))
             (if (and flag (setq tmp2 (distof tmp1 2))) ; got a real number
               (setq M_ListA (cons tmp2 M_ListA)) ; save it
               (setq List_Suffix (cons tmp1 List_Suffix)
                     flag nil)
             )
             (setq Raw_Data (cdr Raw_Data))
           )
         )
       )
     )
     (prompt "\n...Unable to open Moisture file.") ; tell use if unable to get textfile
    )
    ;;===========Read Second file to list==============================
    ((not
       (and
         (setq M_Open (getfiled "Select Ash_Grid" "" "txt" 16))
         (princ "\n>>>   Reading Ash_Grid File   <<<\n")
         (setq Raw_Data (ReadFile M_Open))     ; read textfile to list
         (setq flength2 (length Raw_Data))
         (repeat 6 (setq Raw_Data (cdr Raw_Data)))  ; remove first 6 lines
         (setq flag t) ; this avoids the real conversion once one failure occurs
         (not ; prevent the WHILE from stopping the process
           (while (setq tmp1 (car Raw_Data))
             (if (and flag (setq tmp2 (distof tmp1 2))) ; got a real number
               (setq M_ListB (cons tmp2 M_ListB)) ; save it
               (setq flag nil)
             )
             (setq Raw_Data (cdr Raw_Data))
           )
         )
       )
     )
     (prompt "\n...Unable to open Ash_Grid file.")
    )

    ((and  ;  Option to quit if number of file entries do not match
       (/= (length M_ListA) (length M_ListB))
       (null (initget "Yes No")) ; return T
       (= (getkword "\nUnequal # of data entries, Process anyway? [Yes/No] <Yes>: ") "No")
     )
     (prompt "\n...User quit. Files entries not equal.")
    )
    
    ((and  ;  Option to quit if number of file entries do not match
       (/= flength1 flength2)
       (null (initget "Yes No")) ; return T
       (= (getkword "\nUnequal file length, Process anyway? [Yes/No] <Yes>: ") "No")
     )
     (prompt "\n...User quit. Files entries not equal.")
    )
    
    ;;===Create Output file and Add Coordinates and grid sizes======
    ;;  write the first 6 lines, then the data, then the suffix lines
    ((not
       (and
         (princ "\n>>>   Creating Output,txt file   <<<\n")
         (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)
           (mapcar (function (lambda(x) (write-line x M_Output))) (reverse List_Suffix))
           (null (close M_Output))
           (princ "\nFinished...File Converted")
           (princ (strcat "\n" (itoa (length M_ListA)) " data points processed."))
         )
       )
     )
     (prompt "\n...Unable to open output file.")
    )
  )
  (princ)
)
« Last Edit: August 06, 2010, 05:11:41 PM by CAB »
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 #36 on: August 06, 2010, 03:18:28 PM »
You need to test this routine as I don't have time this morning.


Works great CAB... thanks a bunch.  I will study your code and may have some specific question. Your explanation helped on the "NOT" function.
============== Acad Map 3D 2009================

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Calculation/Conversion with Text Files
« Reply #37 on: August 06, 2010, 04:35:20 PM »
Glad it works for you.

Questions welcome. :-)
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 #38 on: August 06, 2010, 05:12:27 PM »
Oops, corrected code above.
Needed a space between YesNo like this
(null (initget "Yes No")) ; return T
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.