IS_IOSTAT_END error in Mac OSX (Fortran) - macos

I am writing a code in Fortran to read a list of properties from a file, and am using the IOSTAT function to skip invalid data. The relevant section of code is as follows:
do j=1,1000
read(22,*,IOSTAT=ios) step,T,K,U,Tot,P
If(IS_IOSTAT_END(ios)) Exit !exits loop if value is not number or end of file
IF(ios.ne.0) cycle
sumT=sumT+T
sumU=sumU+U
sumK = sumK + K
sumKsq = sumKsq + (K**2.d0)
end if
end do
This code has previously worked fine when running on Linux, but when running on Mac OSX I get the error message 'IS_IOSTAT_END has no implicit type'. Could somebody please explain how to correct this?
Thanks

The intrinsic procedure IS_IOSTAT_END(i) is defined since Fortran 2003. Sufficiently recent compiler version must be used.

Related

This program does not work from terminal but works in zerobrane

x = 10
io.write("Enter the name of the variable you want to be printed: ")
index = io.read()
f = loadstring("return " .. index)
print(f())
The above code gives this error when used in a terminal, but not when ran in zerobrane studio
main.lua:874: attempt to call a nil value (global 'loadstring')
stack traceback:
main.lua:874: in main chunk [C]: in ?
This is important because i am coding lua in emacs.
how can i correct this problem? Need help.
Your terminal version of Lua is higher than Lua in ZeroBrane.
Lua 5.3 removed loadstring and it exists as load now.

How can I hook to the command x in gdb?

I've tried hooking to other commands such as echo and it works well. But when it comes to hooking the x command, it fails. Here's the codes inside of my .gdbinit file.
set $pince_injection_failed = 1
set $pince_debugging_mode = 0
define hook-x
if $pince_injection_failed = 1
echo asdf
end
define hookpost-x
if $pince_debugging_mode = 0
echo zxcv
end
I'm aware that gdb doesn't accept aliases of a function for hooking. But x is already a full function isn't it? I couldn't find any aliases for it. I'm also doubting about it because a single character is too short for a command to be
I found the solution thanks to the Mark Plotnick. It seems like another fault of mine, I found out that there was a function that had a misplaced end, so all functions came after that function got ignored by gdb naturally.
define keks
set $lel=0
while($lel<10)
x/x 0x00400000
set $lel = $lel+1
end
Notice the missing end at the end of while loop

Lua - io.open() only up to 2 GB?

I am using a Lua script to determine the file size:
local filesize=0
local filePath = "somepath.bin"
local file,msg = io.open(filePath, "r")
if file then
filesize=file:seek("end")
file:close()
filePresent = true
end
However, this only seem to work for files up to 2GB. For larger files filesize is always nil. Is there any limitation on io.open? And if so, how could I work around this?
Running Lua 5.1.4 on Windows Server 2008 R2 64bit
The problem is not in io.open, but file:seek. You can check the error like this:
filesize, err = file:seek("end")
if not filesize then
print(err)
end
The error message is probably Invalid argument. That's because for files larger than 2GB, its size is over what 32-bit long can hold, which causes the C function fseek fail to work.
In POSIX systems, Lua uses fseeko which takes the size of off_t instead of long in fseek. In Windows, there's a _fseeki64 which I guess does similar job. If these are not available, fseek is used, and it would cause the problem.
The relevant source is liolib.c(Lua 5.2). As #lhf points out, in Lua 5.1, fseek is always used (source). Upgrading to Lua 5.2 could possibly solve the problem.
Internally, Lua uses the ISO C function long int ftell(FILE *stream); to determine the return value for file:seek(). A long int is always 32 bits on Windows, so you are out of luck here. If you can, you should use some external library to detect the file size -- I recommend luafilesystem.
On old Lua versions (where file:seek() is limited to 2Gb) you can ask cmd.exe to get file size:
function filesize(filename)
-- returns file size (or nil if the file doesn't exist or unable to open)
local command = 'cmd /d/c for %f in ("'..filename..'") do #echo(%~zf'
return tonumber(io.popen(command):read'*a')
end
print(filesize[[C:\Program Files\Windows Media Player\wmplayer.exe]])
--> 73728
print(filesize[[E:\City.of.the.Living.Dead.1980.720p.BluRay.x264.Skazhutin.mkv]])
--> 8505168882

IDL READFITS() syntax error

I'm trying to use the READFITS() function on IDL 8.3 on Mac 10.9.3
My input on the IDL promt:
readfits('image.fits',h, /EXTEN, /SILENT)
Result:
readfits('image.fits',h, /EXTEN, /SILENT)
^
% Syntax error.
*note: the '^' is below '/EXTEN'
Maybe it will help, so here is a link to the IDL help page on using READFITS() --> http://www.exelisvis.com/docs/readfits.html
I tried using the brackets like they show on that help page, but it still didn't work, so I'm stuck now. Didn't know if anyone here has experience reading .fits files in IDL.
ok, so it turns out the readfits procedure isn't included in IDL's original library, so I just had to download AstroLib (contains lots of useful astronomy procedures - including Readfits). The original syntax then worked.
I'm using IDL 8.2.2 on OS X 10.9.4.
Try keeping it simple first. Do these work?
readfits('image.fits')
readfits('image.fits', header)
Next try this:
readfits('image.fits', header, EXTEN_NO=0)
I suspect you really want extension number 0, not 1. See (e.g.) http://www.stsci.edu/documents/dhb/web/c02_datafiles.fm2.html.

How to get R script line numbers at error?

If I am running a long R script from the command line (R --slave script.R), then how can I get it to give line numbers at errors?
I don't want to add debug commands to the script if at all possible; I just want R to behave like most other scripting languages.
This won't give you the line number, but it will tell you where the failure happens in the call stack which is very helpful:
traceback()
[Edit:] When running a script from the command line you will have to skip one or two calls, see traceback() for interactive and non-interactive R sessions
I'm not aware of another way to do this without the usual debugging suspects:
debug()
browser()
options(error=recover) [followed by options(error = NULL) to revert it]
You might want to look at this related post.
[Edit:] Sorry...just saw that you're running this from the command line. In that case I would suggest working with the options(error) functionality. Here's a simple example:
options(error = quote({dump.frames(to.file=TRUE); q()}))
You can create as elaborate a script as you want on an error condition, so you should just decide what information you need for debugging.
Otherwise, if there are specific areas you're concerned about (e.g. connecting to a database), then wrap them in a tryCatch() function.
Doing options(error=traceback) provides a little more information about the content of the lines leading up to the error. It causes a traceback to appear if there is an error, and for some errors it has the line number, prefixed by #. But it's hit or miss, many errors won't get line numbers.
Support for this will be forthcoming in R 2.10 and later. Duncan Murdoch just posted to r-devel on Sep 10 2009 about findLineNum and setBreapoint:
I've just added a couple of functions to R-devel to help with
debugging. findLineNum() finds which line of which function
corresponds to a particular line of source code; setBreakpoint() takes
the output of findLineNum, and calls trace() to set a breakpoint
there.
These rely on having source reference debug information in the code.
This is the default for code read by source(), but not for packages.
To get the source references in package code, set the environment
variable R_KEEP_PKG_SOURCE=yes, or within R, set
options(keep.source.pkgs=TRUE), then install the package from source
code. Read ?findLineNum for details on how to tell it to search
within packages, rather than limiting the search to the global
environment.
For example,
x <- " f <- function(a, b) {
if (a > b) {
a
} else {
b
}
}"
eval(parse(text=x)) # Normally you'd use source() to read a file...
findLineNum("<text>#3") # <text> is a dummy filename used by
parse(text=)
This will print
f step 2,3,2 in <environment: R_GlobalEnv>
and you can use
setBreakpoint("<text>#3")
to set a breakpoint there.
There are still some limitations (and probably bugs) in the code; I'll
be fixing thos
You do it by setting
options(show.error.locations = TRUE)
I just wonder why this setting is not a default in R? It should be, as it is in every other language.
Specifying the global R option for handling non-catastrophic errors worked for me, along with a customized workflow for retaining info about the error and examining this info after the failure. I am currently running R version 3.4.1.
Below, I've included a description of the workflow that worked for me, as well as some code I used to set the global error handling option in R.
As I have it configured, the error handling also creates an RData file containing all objects in working memory at the time of the error. This dump can be read back into R using load() and then the various environments as they existed at the time of the error can be inspected interactively using debugger(errorDump).
I will note that I was able to get line numbers in the traceback() output from any custom functions within the stack, but only if I used the keep.source=TRUE option when calling source() for any custom functions used in my script. Without this option, setting the global error handling option as below sent the full output of the traceback() to an error log named error.log, but line numbers were not available.
Here's the general steps I took in my workflow and how I was able to access the memory dump and error log after a non-interactive R failure.
I put the following at the top of the main script I was calling from the command line. This sets the global error handling option for the R session. My main script was called myMainScript.R. The various lines in the code have comments after them describing what they do. Basically, with this option, when R encounters an error that triggers stop(), it will create an RData (*.rda) dump file of working memory across all active environments in the directory ~/myUsername/directoryForDump and will also write an error log named error.log with some useful information to the same directory. You can modify this snippet to add other handling on error (e.g., add a timestamp to the dump file and error log filenames, etc.).
options(error = quote({
setwd('~/myUsername/directoryForDump'); # Set working directory where you want the dump to go, since dump.frames() doesn't seem to accept absolute file paths.
dump.frames("errorDump", to.file=TRUE, include.GlobalEnv=TRUE); # First dump to file; this dump is not accessible by the R session.
sink(file="error.log"); # Specify sink file to redirect all output.
dump.frames(); # Dump again to be able to retrieve error message and write to error log; this dump is accessible by the R session since not dumped to file.
cat(attr(last.dump,"error.message")); # Print error message to file, along with simplified stack trace.
cat('\nTraceback:');
cat('\n');
traceback(2); # Print full traceback of function calls with all parameters. The 2 passed to traceback omits the outermost two function calls.
sink();
q()}))
Make sure that from the main script and any subsequent function calls, anytime a function is sourced, the option keep.source=TRUE is used. That is, to source a function, you would use source('~/path/to/myFunction.R', keep.source=TRUE). This is required for the traceback() output to contain line numbers. It looks like you may also be able to set this option globally using options( keep.source=TRUE ), but I have not tested this to see if it works. If you don't need line numbers, you can omit this option.
From the terminal (outside R), call the main script in batch mode using Rscript myMainScript.R. This starts a new non-interactive R session and runs the script myMainScript.R. The code snippet given in step 1 that has been placed at the top of myMainScript.R sets the error handling option for the non-interactive R session.
Encounter an error somewhere within the execution of myMainScript.R. This may be in the main script itself, or nested several functions deep. When the error is encountered, handling will be performed as specified in step 1, and the R session will terminate.
An RData dump file named errorDump.rda and and error log named error.log are created in the directory specified by '~/myUsername/directoryForDump' in the global error handling option setting.
At your leisure, inspect error.log to review information about the error, including the error message itself and the full stack trace leading to the error. Here's an example of the log that's generated on error; note the numbers after the # character are the line numbers of the error at various points in the call stack:
Error in callNonExistFunc() : could not find function "callNonExistFunc"
Calls: test_multi_commodity_flow_cmd -> getExtendedConfigDF -> extendConfigDF
Traceback:
3: extendConfigDF(info_df, data_dir = user_dir, dlevel = dlevel) at test_multi_commodity_flow.R#304
2: getExtendedConfigDF(config_file_path, out_dir, dlevel) at test_multi_commodity_flow.R#352
1: test_multi_commodity_flow_cmd(config_file_path = config_file_path,
spot_file_path = spot_file_path, forward_file_path = forward_file_path,
data_dir = "../", user_dir = "Output", sim_type = "spot",
sim_scheme = "shape", sim_gran = "hourly", sim_adjust = "raw",
nsim = 5, start_date = "2017-07-01", end_date = "2017-12-31",
compute_averages = opt$compute_averages, compute_shapes = opt$compute_shapes,
overwrite = opt$overwrite, nmonths = opt$nmonths, forward_regime = opt$fregime,
ltfv_ratio = opt$ltfv_ratio, method = opt$method, dlevel = 0)
At your leisure, you may load errorDump.rda into an interactive R session using load('~/path/to/errorDump.rda'). Once loaded, call debugger(errorDump) to browse all R objects in memory in any of the active environments. See the R help on debugger() for more info.
This workflow is enormously helpful when running R in some type of production environment where you have non-interactive R sessions being initiated at the command line and you want information retained about unexpected errors. The ability to dump memory to a file you can use to inspect working memory at the time of the error, along with having the line numbers of the error in the call stack, facilitate speedy post-mortem debugging of what caused the error.
First, options(show.error.locations = TRUE) and then traceback(). The error line number will be displayed after #

Resources