Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with executing shell commands in the loop for each file

    Hi,

    I've a set of GPH files that I would like to convert to the PostScript and subsequently, PNG format (on the lines of issues discussed here). I drafted a simple code to read a set of *.gph files via the fs (ssc) module. When I attempt to run the code below, Stata returns an error: unexpected end of file.
    Code:
    /* == Convert Files to PS from GPH == */
    
    // Set the file directory
    local graphdir "C:\where I keep my GPH files"
    cd `graphdir'
    
    // Postscript settings
    graph set ps pagesize custom
    graph set ps pagewidth 11
    graph set ps pageheight 8
    graph set ps tmargin 0
    graph set ps lmargin 0
    graph set ps logo off
    graph set ps fontface Helvetica
    
    // List files
    fs *.gph
    
    // Export to PS
    foreach gf in `r(files)' {
        graph use "`gf'"
        graph export "`gf'.ps", replace
        #delimit ;
        shell  "C:\gs9.15\bin\gswin64c.exe"
            -dSAFER -dBATCH -dNOPAUSE
            -r100 -g1100x800 -sDEVICE=png16m
            -dGraphicsAlphaBits=4 -dTextAlphaBits=4
            -sOutputFile="`gf'" "`gf'.ps";
    }
    This is what happens when I attempt to run the lines:

    Code:
    . // Export to PS
    . foreach gf in `r(files)' {
      2.         graph use "`gf'"
      3.         graph export "`gf'.ps", replace
      4.         #delimit ;
    delimiter now ;
    .         shell  "C:\gs9.15\bin\gswin64c.exe"
    >                 -dSAFER -dBATCH -dNOPAUSE
    >                 -r100 -g1100x800 -sDEVICE=png16m
    >                 -dGraphicsAlphaBits=4 -dTextAlphaBits=4
    >                 -sOutputFile="`gf'" "`gf'.ps";
      5. }
    
    unexpected end of file
    Last edited by Konrad Zdeb; 18 Nov 2014, 02:21. Reason: Colours.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    I don't know what happens when you change the delimiter within a loop, but did you try changing it back right before the final "}"

    Comment


    • #3
      Hi,
      thanks for showing the interest. It seems like interesting things happen.
      Code:
      . // Export to PS
      . foreach gf in `r(files)' {
        2.         graph use "`gf'"
        3.         graph export "`gf'.ps", replace
        4.         #delimit ;
      delimiter now ;
      .         shell  "C:\gs9.15\bin\gswin64c.exe"
      >                 -dSAFER -dBATCH -dNOPAUSE
      >                 -r100 -g1100x800 -sDEVICE=png16m
      >                 -dGraphicsAlphaBits=4 -dTextAlphaBits=4
      >                 -sOutputFile="`gf'" "`gf'.ps";
        5.         #delimit cr
      delimiter now cr
      . }
      output-file suffix "gph.ps" not recognized
          specify correct suffix or specify as() option
      r(198);
      
      end of do-file
      
      r(198);
      
      .
      How Stata treats local macros embedded within the shell command? My naive hope was that it would treat them in the same way if not embedded within the shell command.
      Kind regards,
      Konrad
      Version: Stata/IC 13.1

      Comment


      • #4
        I'm guessing that -graph export- is getting confused. Your filelist contains names like this.gph, that.gph, the_next.gph. You then ask -graph export- to export those graphs as this.gph.ps, that.gph.ps, etc. While that doesn't strike me as unreasonable, -graph export- can't seem to handle that: it parses the filename extension as beginning after the first dot and can't recognize gph.ps as a file-type. So you have to add a little code to take the .gph.'s out.

        Code:
         foreach gf in `r(files)' {
                  graph use "`gf'"
                  local gf_stub: subinstr local gf ".gph" ""
                   graph export "`gf_stub'.ps", replace
                  #delimit ;
        
                 shell  "C:\gs9.15\bin\gswin64c.exe"
                        -dSAFER -dBATCH -dNOPAUSE
                         -r100 -g1100x800 -sDEVICE=png16m
                         -dGraphicsAlphaBits=4 -dTextAlphaBits=4
                         -sOutputFile="`gf_stub'" "`gf_stub'.ps";
                  #delimit cr
        
         }
        should do the trick.

        If you actual want the files to be named *.gph.ps, I think you'll need to do that separately later on.

        Note: I changed the references to `gf' to `gf_stub' in your gswin64c command as well. I'm not familiar with gswin64c, so I don't know if that was the right thing to do or not. But I'll leave it to you to fix that if it was wrong.

        Comment


        • #5
          Clyde,

          Thank you very much for your comments. The loop runs and creates the files, however, without the PNG extension. Following Huebler's post, I altered code adding the PNG extension but the created files are without the PNG extension.

          Code:
          // List files
          fs *.gph
          
          // Export to PS
          foreach gf in `r(files)' {
                    graph use "`gf'"
                    local gf_stub: subinstr local gf ".gph" ""
                     graph export "`gf_stub'.ps", replace
                    #delimit ;
          
                   shell  "C:\gs9.15\bin\gswin64c.exe"
                          -dSAFER -dBATCH -dNOPAUSE
                           -r100 -g1100x800 -sDEVICE=png16m
                           -dGraphicsAlphaBits=4 -dTextAlphaBits=4
                           -sOutputFile="`gf_stub.png'" "`gf_stub'.ps";
                    #delimit cr
          
           }
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment


          • #6
            how about just adding another shell command:

            Code:
            shell rename "`gfstub'"  "`gfstub'.png";
            it's a kludge, but should do the job.



            Comment


            • #7
              I think the output part of your shell call should be
              Code:
              -sOutputFile="`gf_stub'.png"
              The gf_stub part is the only part that's a local macro so it's the only part that should be in the macro single quotes.

              Comment


              • #8
                Hi,

                thanks for getting back to me.

                Originally posted by Sarah Edgington View Post
                I think the output part of your shell call should be
                Code:
                -sOutputFile="`gf_stub'.png"
                The gf_stub part is the only part that's a local macro so it's the only part that should be in the macro single quotes.
                It seemed logical to me, but when I tried it I arrived at the same error:

                Code:
                . // Export to PS
                . foreach gf in `r(files)' {
                  2.         graph use "`gf'"
                  3.         graph display, xsize(11) ysize(8)
                  4.         local gf_stub: subinstr local gf ".gph" ""
                  5.         graph export "`gf'.ps", replace
                  6.         #delimit ;
                delimiter now ;
                .         shell  "C:\gs9.15\bin\gswin64c.exe"
                >                 -dSAFER -dBATCH -dNOPAUSE
                >                 -r100 -g1100x800 -sDEVICE=png16m
                >                 -dGraphicsAlphaBits=4 -dTextAlphaBits=4
                >                 -sOutputFile="`gf_stub'.png" "`gf_stub'.ps";
                  7.         #delimit cr
                delimiter now cr
                . }
                output-file suffix "gph.ps" not recognized
                    specify correct suffix or specify as() option

                Originally posted by ben earnhart View Post
                how about just adding another shell command:
                Code:
                shell rename "`gfstub'" "`gfstub'.png";
                it's a kludge, but should do the job.
                when I tried version with the shell command, I also get an error.

                Code:
                . // Export to PS
                . foreach gf in `r(files)' {
                  2.         graph use "`gf'"
                  3.         graph display, xsize(11) ysize(8)
                  4.         local gf_stub: subinstr local gf ".gph" ""
                  5.         graph export "`gf'.ps", replace
                  6.         #delimit ;
                delimiter now ;
                .         shell  "C:\gs9.15\bin\gswin64c.exe"
                >                 -dSAFER -dBATCH -dNOPAUSE
                >                 -r100 -g1100x800 -sDEVICE=png16m
                >                 -dGraphicsAlphaBits=4 -dTextAlphaBits=4
                >                 -sOutputFile="`gf_stub" "`gf_stub'.ps";
                  7.         shell rename "`gfstub'"  "`gfstub'.png";
                  8.         #delimit cr
                delimiter now cr
                . }
                output-file suffix "gph.ps" not recognized
                    specify correct suffix or specify as() option
                Kind regards,
                Konrad
                Version: Stata/IC 13.1

                Comment


                • #9
                  Weird! The shell command to rename it is separate, independent, and should not interact with the Ghostscript command in any way. I did notice that your command that did run had a .`gfstub,png' where it was ignoring the .png part and Sarah pointed out why it should have problems, but it ran. Maybe restore that piece to the non-nonsensical but working syntax? Logically, it shouldn't get output, since the macro you called with the old syntax doesn't exist...

                  Comment


                  • #10
                    Thanks for showing the interest. I finally got it to work, the code is below:

                    Code:
                    /* == Convert Files to PS from GPH == */
                    
                    // Set the file directory
                    local graphdir "C:\where I keep those *.gph files\"
                    cd `graphdir'
                    
                    // Postscript settings
                    graph set ps pagesize custom
                    graph set ps pagewidth 11
                    graph set ps pageheight 8
                    graph set ps tmargin 0
                    graph set ps lmargin 0
                    graph set ps logo off
                    graph set ps fontface Helvetica
                    
                    // List files
                    fs *.gph
                    
                    foreach gf in `r(files)' {
                        graph use "`gf'"
                        graph display, xsize(11) ysize(8)
                        local gf_stub: subinstr local gf ".gph" ""
                        graph export "`gf_stub'.ps", replace
                        #delimit ;
                        shell  "C:\gs9.15\bin\gswin64c.exe"
                            -dSAFER -dBATCH -dNOPAUSE
                            -r100 -g1100x800 -sDEVICE=png16m
                            -dGraphicsAlphaBits=4 -dTextAlphaBits=4
                            -sOutputFile="`gf_stub'" "`gf_stub'.ps";
                        #delimit cr
                        local subfile = "`gf_stub'" + ".png"
                        !rename "`gf_stub'" "`subfile'"
                    }
                    Kind regards,
                    Konrad
                    Version: Stata/IC 13.1

                    Comment

                    Working...
                    X