Re: Command line arguments [message #42530] |
Fri, 11 February 2005 18:42  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Mr. No Address writes:
> If I understand your first question above correctly, I'm compiling
> first because "Way back in the day" I was never able to successfully run
> code without compiling it first. I'm sure I was doing something wrong,
No doubt. :-)
This article might be useful to you:
http://www.dfanning.com/tips/namefiles.html
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|
Re: Command line arguments [message #42531 is a reply to message #42530] |
Fri, 11 February 2005 14:52   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
"Mr. No Address" <no_given_address@landofthelost.net> writes:
...
> PRO mentor, file ; I added "file"
>
> Now I seem to have a Perl problem in that I think Perl is executing
> the lines after
>
> print IDL "mentor, '$filepath'\n";
>
> before it writes the idl.ps file. Seems I ran into this way back when too.
...
Yes, that's true. Perl will not wait for IDL, it just stuffs
characters into a pipe. You will have to make some other kind of
locking mechanism. Like having perl wait until a certain file is
created, etc.
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: Command line arguments [message #42532 is a reply to message #42531] |
Fri, 11 February 2005 14:19   |
Mr. No Address
Messages: 11 Registered: February 2005
|
Junior Member |
|
|
Michael Wallace wrote:
>> I would like to compile the program once outside the loop and then pass
>> the file using an argument instead grabbing the file from an environment
>> variable. Can I do something like this?
>>
>> print IDL "mentor, $filepath\n";
>
>
> Yes.
>
> Maybe I don't understand what you're doing, but why are you 1.)
> explicitly compiling the command your going to run instead of letting
> IDL automatically compile it? and 2.) why don't you write a wrapper in
> IDL itself that handles the looping and you just call the wrapper the
> one time instead of calling the same IDL command multiple times?
Thanks for the reply. With your post and Craig's above I was able to
piece together what I was doing wrong. Still have problems, but I think
that scope is now Perl. In any case, to address your questions above...
If I understand your first question above correctly, I'm compiling
first because "Way back in the day" I was never able to successfully run
code without compiling it first. I'm sure I was doing something wrong,
but I got things to work this way and went with it. On #2, This time
around I set out to write everything in IDL, but knowing a bit of Perl
and not really knowing IDL, it was too easy to fall back on my Perl
skills. Most of that had to do with the way Perl handles time, e.g.,
timegm and gmtime.
Cheers,
Gary
>
> My Perl is rusty, but a while back I wrote a Python wrapper to simulate
> command line arguments. I think I posted it in the newsgroup some time
> ago, but I couldn't find the old thread.
>
> The first argument to the program below is the name of the IDL command
> to execute. The remainder of the arguments will be fed to the IDL
> command named in the first argument. I have defined my IDL programs
> that need to be run on the command line to include the keyword ARGS. The
> additional arguments are passed into the IDL command using this
> keyword. By doing this, I can use this one wrapper for all command line
> programs, but the argument list is not bound to something specific.
>
> If I want to quickly do something and the program I want to run doesn't
> have an ARGS keyword, I can just put the entire IDL command I want to
> run in the first argument. Just quote the entire command so that it's
> interpreted as a single argument.
>
> -Mike
>
>
> #!/usr/bin/env python
>
> import os
> import sys
>
> # Usage statement
> usage = "usage: %s idlprog args" %os.path.basename(sys.argv[0])
>
> # Check that the name of the IDL program was provided
> if len(sys.argv) < 2:
> print usage
> else:
> fd = os.popen('idl', 'w')
>
> # If extra arguments are given, pass them via the ARGS keyword
> if len(sys.argv) < 3:
> fd.write(sys.argv[1])
> else:
> fd.write(sys.argv[1] + ', ARGS = ' + `sys.argv[2:]`)
>
> fd.close()
|
|
|
Re: Command line arguments [message #42533 is a reply to message #42532] |
Fri, 11 February 2005 14:11   |
Mr. No Address
Messages: 11 Registered: February 2005
|
Junior Member |
|
|
Mr. No Address wrote:
> Craig Markwardt wrote:
>
>> "Mr. No Address" <no_given_address@landofthelost.net> writes:
>>
>>> Way back in the day I posted a very similar question. Someone gave me a
>>> solution using environment variables that has worked fine all this
>>> time, but now I'm updating things and I'm seeking a cleaner solution.
>>> Here we go...
>>>
>>> I have a Perl program that I compile and run an IDL program from. I'm
>>> now looping through several hundred times and it seems wasteful to
>>> compile the IDL program every loop.
>>>
>>> # The relevant Perl code...
>>> $ENV{"file"}="$filepath";
>>> open(IDL, "|/usr/local/bin/idl") || die "Can't open IDL: $!";
>>> print IDL ".Compile mentor \n";
>>> print IDL "mentor \n";
>>> close IDL;
>>>
>>> # The relevant IDL code...
>>> PRO mentor
>>> file=GETENV('file')
>>>
>>> I would like to compile the program once outside the loop and then pass
>>> the file using an argument instead grabbing the file from an environment
>>> variable. Can I do something like this?
>>>
>>> print IDL "mentor, $filepath\n";
>>
>>
>>
>> You need to put quotation marks around it, as in:
>>
>> print IDL "mentor, '$filepath'\n";
>>
>> That has worked for me in the past.
>
>
> Thanks for the reply Craig. How do I assign $filepath to a variable in
> the IDL code?
OK, I figured out this part of it. I changed the first line in the IDL
file to:
PRO mentor, file ; I added "file"
Now I seem to have a Perl problem in that I think Perl is executing the
lines after
print IDL "mentor, '$filepath'\n";
before it writes the idl.ps file. Seems I ran into this way back when too.
Thanks,
Gary
|
|
|
|
Re: Command line arguments [message #42535 is a reply to message #42534] |
Fri, 11 February 2005 13:17   |
Michael Wallace
Messages: 409 Registered: December 2003
|
Senior Member |
|
|
> I would like to compile the program once outside the loop and then pass
> the file using an argument instead grabbing the file from an environment
> variable. Can I do something like this?
>
> print IDL "mentor, $filepath\n";
Yes.
Maybe I don't understand what you're doing, but why are you 1.)
explicitly compiling the command your going to run instead of letting
IDL automatically compile it? and 2.) why don't you write a wrapper in
IDL itself that handles the looping and you just call the wrapper the
one time instead of calling the same IDL command multiple times?
My Perl is rusty, but a while back I wrote a Python wrapper to simulate
command line arguments. I think I posted it in the newsgroup some time
ago, but I couldn't find the old thread.
The first argument to the program below is the name of the IDL command
to execute. The remainder of the arguments will be fed to the IDL
command named in the first argument. I have defined my IDL programs
that need to be run on the command line to include the keyword ARGS.
The additional arguments are passed into the IDL command using this
keyword. By doing this, I can use this one wrapper for all command line
programs, but the argument list is not bound to something specific.
If I want to quickly do something and the program I want to run doesn't
have an ARGS keyword, I can just put the entire IDL command I want to
run in the first argument. Just quote the entire command so that it's
interpreted as a single argument.
-Mike
#!/usr/bin/env python
import os
import sys
# Usage statement
usage = "usage: %s idlprog args" %os.path.basename(sys.argv[0])
# Check that the name of the IDL program was provided
if len(sys.argv) < 2:
print usage
else:
fd = os.popen('idl', 'w')
# If extra arguments are given, pass them via the ARGS keyword
if len(sys.argv) < 3:
fd.write(sys.argv[1])
else:
fd.write(sys.argv[1] + ', ARGS = ' + `sys.argv[2:]`)
fd.close()
|
|
|
Re: Command line arguments [message #42536 is a reply to message #42535] |
Fri, 11 February 2005 12:46   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
"Mr. No Address" <no_given_address@landofthelost.net> writes:
> Way back in the day I posted a very similar question. Someone gave me a
> solution using environment variables that has worked fine all this
> time, but now I'm updating things and I'm seeking a cleaner solution.
> Here we go...
>
> I have a Perl program that I compile and run an IDL program from. I'm
> now looping through several hundred times and it seems wasteful to
> compile the IDL program every loop.
>
> # The relevant Perl code...
> $ENV{"file"}="$filepath";
> open(IDL, "|/usr/local/bin/idl") || die "Can't open IDL: $!";
> print IDL ".Compile mentor \n";
> print IDL "mentor \n";
> close IDL;
>
> # The relevant IDL code...
> PRO mentor
> file=GETENV('file')
>
> I would like to compile the program once outside the loop and then pass
> the file using an argument instead grabbing the file from an environment
> variable. Can I do something like this?
>
> print IDL "mentor, $filepath\n";
You need to put quotation marks around it, as in:
print IDL "mentor, '$filepath'\n";
That has worked for me in the past.
Good luck!
Craig
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
|
Re: Command line arguments [message #42732 is a reply to message #42530] |
Mon, 21 February 2005 09:56  |
JD Smith
Messages: 850 Registered: December 1999
|
Senior Member |
|
|
On Fri, 11 Feb 2005 19:42:40 -0700, David Fanning wrote:
> Mr. No Address writes:
>
>> If I understand your first question above correctly, I'm compiling
>> first because "Way back in the day" I was never able to successfully run
>> code without compiling it first. I'm sure I was doing something wrong,
>
> No doubt. :-)
>
> This article might be useful to you:
>
> http://www.dfanning.com/tips/namefiles.html
You'd be surprised how many people I know compile their routines by
hand, not once, but *twice*, just for good measure. This is what a
lifetime of IRAF can do to a person.
JD
|
|
|