Help in applescript; multiple data from 2 txt files to create image file

Discussion in 'Mac Programming' started by LERsince1991, Nov 14, 2011.

  1. LERsince1991, Nov 14, 2011
    Last edited: Nov 14, 2011

    LERsince1991 macrumors 65816

    Joined:
    Jul 24, 2008
    Location:
    UK
    #1
    Hi!

    So I'm working on a way to visually map data from different text files with lots of numbers in (generated from data logging apps from an iPhone) and to use these numbers to create an image file.

    For example:
    (the actual files I'll be using will probably have about 1000-5000 values)


    text file 1; location
    ______________

    53.226700 -0.544238 65.000000 2011-11-14 18:41:00 +0000
    53.227048 -0.544065 78.480770 2011-11-14 18:41:00 +0000
    53.227048 -0.544065 78.480770 2011-11-14 18:41:00 +0000
    53.227048 -0.544065 78.480770 2011-11-14 18:41:00 +0000
    53.226883 -0.544247 65.000000 2011-11-14 18:41:00 +0000
    53.226883 -0.544247 65.000000 2011-11-14 18:41:00 +0000
    ______________
    (longitude) (latitude) (accuracy in m) (date) (time) (not sure what this is lol)



    Text file 2; magnitude
    ______________

    Magnitude = 61.3
    Time = 06:42:39 PM
    Date = 2011-11-14
    ----------------------
    Magnitude = 70.8
    Time = 06:42:39 PM
    Date = 2011-11-14
    ----------------------
    Magnitude = 70.1
    Time = 06:42:39 PM
    Date = 2011-11-14
    ----------------------
    Magnitude = 66.8
    Time = 06:42:39 PM
    Date = 2011-11-14
    ______________



    and heres what I need to do;

    - create image file with transparent background
    - set pixel [x=line 1, longitude, from desktop/location.txt], [Y=line 1, latitude, from desktop/location.txt]
    - set pixel value; greyscale colour; [text file 2, first magnitude value]%
    (or maybe always use black colour and set the opacity with the magnitude from text file 2)
    - then continue this process with the next values in each file.

    I've been learning applescript very slowly but its taking way too long as this is for an architectural project to visualise values onto a map. I will take the output of this script and then scale it over a satellite map to show this layer. If anyone is interested I'm mapping values such as EMF magnitude in micro tesla and its part of my student project.

    Hope someone can help make this a reality!
    Thanks!
    Luke :)

    edit; theres a few tricky things here as some of the numbers aren't positive and some will only change slightly e.g., long and lat values. I'm guessing that the script will need to set the range and find the largest value of each variable and set that as 100% and then the same with the smallest values setting them as the 0% values (or something). I don't really have a clue. :(

    edit 2; Just had another thought, if applescript can't make the image file it could open a new photoshop file and use the longitude and latitude as x,y mouse arrow co-ordinates and then click. Making the image in photoshop with a style I could define in photoshop before hand, i.e. i can change the brush style creating different output styles. but thats not important.
     
  2. sero macrumors member

    Joined:
    Aug 28, 2008
  3. LERsince1991 thread starter macrumors 65816

    Joined:
    Jul 24, 2008
    Location:
    UK
    #3
    hi, I've taken a long look at gnu plot but haven't got a clue how to install it. I can't code and have no clue about this. Could someone please explain 1. how to install the program 'gnuplot' and 2. how to use it for what I need please? :/

    Thanks,
    luke
     
  4. LERsince1991 thread starter macrumors 65816

    Joined:
    Jul 24, 2008
    Location:
    UK
    #4
    also whilst I'm working on getting gnuplot to work for me can someone please help me with a script that can combine some of the data form the 2 text files into one please? I'm going to print this huge document off as part of the project research to show where the visual has came from.

    Lets call the first document "location"
    and make its contents

    53.226700 -0.544238 65.000000 2011-11-14 18:41:00 +0000
    53.227048 -0.544065 78.480770 2011-11-14 18:41:00 +0000
    53.227048 -0.544065 78.480770 2011-11-14 18:41:00 +0000
    53.227048 -0.544065 78.480770 2011-11-14 18:41:00 +0000
    53.226883 -0.544247 65.000000 2011-11-14 18:41:00 +0000
    53.226883 -0.544247 65.000000 2011-11-14 18:41:00 +0000

    I want just the longitude and latitude - the first 2 numbers
    eg
    53.226883 -0.544247

    then call the second document "magnitude"
    and its contents are;

    Magnitude = 61.3
    Time = 06:42:39 PM
    Date = 2011-11-14
    ----------------------
    Magnitude = 70.8
    Time = 06:42:39 PM
    Date = 2011-11-14
    ----------------------
    Magnitude = 70.1
    Time = 06:42:39 PM
    Date = 2011-11-14
    ----------------------
    Magnitude = 66.8
    Time = 06:42:39 PM
    Date = 2011-11-14

    I just want to extract the magnitude values.

    so the output document would be;
    (long1)tab(lat1)tab(magnitude1)
    (long2)tab(lat2)tab(magnitude2)
    (long3)tab(lat3)tab(magnitude3)
    (long4)tab(lat4)tab(magnitude4)

    and look like this (but with 5000 lines of data);

    53.226700 -0.544238 61.3
    53.227048 -0.544065 70.8
    53.227048 -0.544065 70.1
    53.227048 -0.544065 66.8
    etc........

    I'm getting proper stressed about this work now, bit out of my comfort zone but I REALLY want to do this. Would be amazing if I could get some help please

    Luke
     
  5. LERsince1991 thread starter macrumors 65816

    Joined:
    Jul 24, 2008
    Location:
    UK
    #5
    Please? Even merging the text files is a start :)
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    I don't know AppleScript well, but I doubt that you could do this kind of a merge in AppleScript.

    I do know Ruby, which is preinstalled on Mac OS X. So here's a Ruby script that will merge your two files.

    merge.rb
    Code:
    # Define a subroutine that will read a line
    # from the location file and extract the
    # longitude and latitude
    
    def read_location_file(location_file)
      longitude = nil
      latitude = nil
      line = location_file.gets
      if line =~ / ^ \s* (-?[0-9.]+) \s+ (-?[0-9.]+) /x
        longitude = $1
        latitude = $2
      end
      return longitude, latitude
    end
    
    
    # Store and check the command-line args
    
    location_file_path = ARGV[0]
    magnitude_file_path = ARGV[1]
    output_file_path = ARGV[2]
    if location_file_path.nil? || magnitude_file_path.nil? || output_file_path.nil?
      STDERR.puts 'missing command-line argument(s)'
      STDERR.puts "usage: ruby #{$PROGRAM_NAME} location_file_path magnitude_file_path output_file_path"
      exit 1
    end
    
    puts "Merging #{location_file_path} and #{magnitude_file_path} into #{output_file_path}"
    
    
    # Open the location and magnitude files
    
    File.open(location_file_path) do |location_file|
      File.open(magnitude_file_path) do |magnitude_file|
        File.open(output_file_path, 'w') do |output_file|
        
          magnitude = nil
    
        
          # Read in each line of the magnitude file
        
          magnitude_file.each do |magnitude_file_line|
                
            case magnitude_file_line
            
        
              # If the current line is magnitude = 123.34,
              # store this magnitude for later.
            
              when / ^ \s* magnitude \s* = \s* ([0-9.]*) \s* $ /ix
                magnitude = $1
            
            
              # If the current line is ------------------,
              # read the next line of the location file
              # and output the magnitude, longitude, and latitude.
            
              when / ^ \s* \-+ \s* $ /x
                longitude, latitude = read_location_file(location_file)
                output_file.puts "#{longitude}\t#{latitude}\t#{magnitude}"
                magnitude = nil
                    
            end
          
          end
        
        
          # After the last line of the magnitude file is read and
          # if the magnitude file didn't end with a line of dashes,
          # read the (last) line of the location file and
          # output the last magnitude, longitude and latitude.
        
          unless magnitude.nil?
            longitude, latitude = read_location_file(location_file)
            output_file.puts "#{longitude}\t#{latitude}\t#{magnitude}"
          end
    
        end # File.open(output_file_path, 'w')
      end # File.open(magnitude_file_path)
    end # File.open(location_file_path)
    
    
    puts 'Merge complete'
    
    You could run this by saving the code into a file called merge.rb in the same folder as your longitude and latitude files.

    Then open the Terminal app. Enter the following command:
    Code:
    cd "folder path"
    
    where folder path in the command above is the path to the folder you put merge.rb into. For example, if you put it in a folder called Stuff inside your Downloads folder, the command would be cd "~/Download/Stuff"

    Then, assuming you input files are called location and magnitude and you want the output to be put into a file called merged, you enter the following command:
    Code:
    ruby merge.rb location magnitude merged
    
     
  7. LERsince1991 thread starter macrumors 65816

    Joined:
    Jul 24, 2008
    Location:
    UK
    #7
    wow thats awesome thank you so much!
    I'll try it out now :)

    Luke
     
  8. LERsince1991, Nov 16, 2011
    Last edited: Nov 16, 2011

    LERsince1991 thread starter macrumors 65816

    Joined:
    Jul 24, 2008
    Location:
    UK
    #8
    Edit, done it :)
    when I was typing in terminal the 2nd command it needed the .txt on the end. even if I deleted the extension in the name of the files. Awesome! will have to get the data in and merge them now.

    Anyone any tips on how to even install GNUplot please? :)

    Luke
     
  9. sero macrumors member

    Joined:
    Aug 28, 2008
    #9
    Wirelessly posted (Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3)

    The easiest way would probably be to install macports or fink and then install gnuplot through that.
     
  10. LERsince1991, Nov 17, 2011
    Last edited: Nov 17, 2011

    LERsince1991 thread starter macrumors 65816

    Joined:
    Jul 24, 2008
    Location:
    UK
    #10
    I think I've installed mac ports with the lion download and install package from the macports site.

    And I've downloaded the necessary files for GNUplot.
    I read all the read me files and other text files included in gnu plot but haven't got a clue what I'm doing.

    I started some command that installs cairo and pango but it took ages and ages and was using 100% cpu so I quit it after about an hour and a half or more.

    edit; I hope its not destroying my computer :/

    edit2; I think I have gnu plot installed now, I can do "plot sin(x)" successfully so thats a start

    Anyone able to provide a command to plot my data into points or something?
     

Share This Page