Input/output data with specific directory path name in Fortran - file-io

I am importing data from a directory '/Users/rob/Desktop/DISSERTATION/DSMCDATA/' into a Fortran code and exporting the same data with a different name into this same directory. The code is an .f90 file in a different directory '/Users/rob/Desktop/DISSERTATION/DSMCCODE/'. The code imports the data again to check if it was indeed exported into the correct directory. It is apparent that I am able to initially import the data from the specific directory without problems. However, I am unable to successfully export data into this specific directory.
program exporttest
implicit none
integer(8), parameter :: Stot= 2
integer(8), dimension(:), allocatable :: Nfp, Nf, Nfvp, Nfv
integer(4) :: rank, s
character(10) :: rankstring, sstring
character(*), parameter :: datadir= '/Users/rob/Desktop/DISSERTATION/DSMCDATA/'
character(50) :: NfTfile
type Specietype
integer(8), dimension(:), allocatable :: NfT, NfvT
end type Specietype
type(Specietype), dimension(:), allocatable :: SpecieT
allocate(SpecieT(Stot))
allocate(Nfp(Stot), Nfvp(Stot))
allocate(SpecieT(Stot)%NfT(1), SpecieT(Stot)%NfvT(1))
rank= 0
! import data
do s= 1, Stot, 1
write(sstring, '(I5)') s
allocate(Nf(1))
open (unit= 0, file= datadir // adjustl(adjustr(sstring) // ':' // 'Nfmat.bin'), &
status= 'old', form= 'unformatted', access= 'stream')
read(0) Nfp(s)
close(0)
Nf(1)= Nfp(s)
SpecieT(s)%NfT= Nf
deallocate(Nf)
end do
deallocate(Nfp)
write(*, *) 'NfT s1= ', SpecieT(1)%NfT(1)
write(*, *) 'NfT s2= ', SpecieT(2)%NfT(1)
! export data
write(rankstring, '(i10)') rank
do s= 1, Stot, 1
write(sstring, '(I5)') s
NfTfile= &
datadir // adjustl(adjustr(rankstring) // ':' // &
adjustl(adjustr(sstring) // ':' // 'NfTfort.bin'))
open(unit= 0, status= 'replace', file= NfTfile, form= 'unformatted', &
access= 'stream')
write(0) SpecieT(s)%NfT
close(0)
end do
! import exported data
do s= 1, Stot, 1
write(sstring, '(I5)') s
allocate(Nfv(1))
open (unit= 0, file= datadir // adjustl(adjustr(rankstring) // ':' // &
adjustl(adjustr(sstring) // ':' // 'NfTfort.bin')), &
status= 'old', form= 'unformatted', access= 'stream')
read(0) Nfvp(s)
close(0)
Nfv(1)= Nfvp(s)
SpecieT(s)%NfvT= Nfv
deallocate(Nfv)
end do
deallocate(Nfvp)
write(*, *) 'NfvT s1= ', SpecieT(1)%NfvT(1)
write(*, *) 'NfvT s2= ', SpecieT(2)%NfvT(1)
end program exporttest
I am compiling the code as follows:
mpif90 -O3 exporttest.f90 -o executablefile
and receiving the following output:
NfT s1= 1
NfT s2= 1
At line 68 of file exporttest.f90
Fortran runtime error: Cannot open file '/Users/rob/Desktop/DISSERTATION/DSMCDATA/0:1:NfTfort.bin': No such file or directory
Error termination. Backtrace:
#0 0x1052bee46
#1 0x1052bf718
#2 0x1052bfd7c
#3 0x10534005a
#4 0x1053406e3
#5 0x1050c59de
#6 0x1050c5c5e
It is apparent that the exported file '/Users/rob/Desktop/DISSERTATION/DSMCDATA/0:1:NfTfort.bin' is not being created in the directory '/Users/rob/Desktop/DISSERTATION/DSMCDATA/'. Any ideas why this is happening?
The problem lies in the final export section
open (unit= 0, file= datadir // adjustl(adjustr(rankstring) // ':' // &
adjustl(adjustr(sstring) // ':' // 'NfTfort.bin')), &
status= 'old', form= 'unformatted', access= 'stream')
read(0) Nfvp(s)
close(0)
Although the import section of the code demonstrates that the file declaration in the open statement is able to distinguish and correctly combine the directory path name string with the concatenation of running index strings, namely,
file= datadir // adjustl(adjustr(sstring) // ':' // 'Nfmat.bin')
The export section of the code here does not support that sort of string combination. The files are dumped into the preceding directory and are text edit files instead. I'm not sure why the string concatenation works correctly in the import section and not in the export section of the code.

A quick way around this is to ensure that the export open statement recognizes where the file path ends and the rest of the file name string begins. Done piece-wise, the export section of the code becomes:
! export data
write(rankstring, '(i10)') rank
do s= 1, Stot, 1
write(sstring, '(I5)') s
expstring= adjustl(adjustr(rankstring) // '_' // adjustl(adjustr(sstring) &
// '_'))
NfTfile= adjustl(adjustr(expstring) // adjustl(adjustr('NfTfort.bin')))
open(unit= 0, file= adjustl(adjustr(datadir) // adjustl(adjustr(NfTfile))), &
status= 'replace', form= 'unformatted', access= 'stream')
write(0) SpecieT(s)%NfT(1)
close(0)
end do
where character(10) :: expstring is defined in the preamble. The only draw-back with this method when exporting data to a separate directory is that datadir must be a directory before the one containing the code. Otherwise, a text file is created in the code directory with a file name that begins with the ending of the path name (namely the path name after the code directory). In this case, the same problem persists- it cannot distinguish where the path name ends and the file name begins and the extension is not recognized. At any rate, data may be successfully exported and accessed from a separate directory prior to the code directory with the above method, minding this caveat.

Related

nuitka fails to generate executable due to "The filename or extension is too long"

I've been trying to compile/generate a standalone executable (.exe) with nuitka but it fails every time with the message:
Nuitka:INFO:Total memory usage before running scons: 2.72 GB (2920177664 bytes):
scons: *** [main_executable.dist\main_executable.exe] The filename or extension is too long
I'm new to this programming but I think I've tried just about everything. I moved my *.py files to directory C:\main to shorten the path to no avail. I've rename the file to produce "main.exe" from "main_executable" to no avail.
My python is installed in here:
'C:\users\test\Anaconda3...'
I came across this function below to shorten the path but I have no idea how to implement it: (taken from http://code.activestate.com/recipes/286179-getshortpathname/)
Could you kindly help. Thanks.
def getShortPathName(filepath):
"Converts the given path into 8.3 (DOS) form equivalent."
import win32api, os
if filepath[-1] == "\\":
filepath = filepath[:-1]
tokens = os.path.normpath(filepath).split("\\")
if len(tokens) == 1:
return filepath
ShortPath = tokens[0]
for token in tokens[1:]:
PartPath = "\\".join([ShortPath, token])
Found = win32api.FindFiles(PartPath)
if Found == []:
raise WindowsError, 'The system cannot find the path specified: "%s"' % (PartPath)
else:
if Found[0][9] == "":
ShortToken = token
else:
ShortToken = Found[0][9]
ShortPath = ShortPath + "\\" + ShortToken
return ShortPath

How do I preserve the order of the dependencies?

I have the following code that opens files in a directory, runs spaCy NLP on them, and the outputs dependency parse info into a file in a new directory.
import spacy, os
nlp = spacy.load('en')
path1 = 'C:/Path/to/my/input'
path2 = '../output'
for file in os.listdir(path1):
with open(file, encoding='utf-8') as text:
txt = text.read()
doc = nlp(txt)
for sent in doc.sents:
f = open(path2 + '/' + file, 'a+')
for token in sent:
f.write(file + '\t' + str(token.dep_) + '\t' + str(token.head) + '\t' + str(token.right_edge) + '\n')
f.close()
The trouble is that this won't preserver the order of the dependencies in the output file. I can't seem to find any references to character positions in the API documentation.
The character index is at token.idx. The word index is at token.i. I know this isn't particularly intuitive.
Tokens also compare by position, so you could do:
for child in sent:
word1, word2 = sorted((child, child.head))
This would get you each dependency arc, arranged in document order. I'm not sure what you're trying to do with the right edge there, though, so I'm not sure if this does quite what you want.

Turn absolute file paths and line numbers in the tool output into hyperlinks

This is an example output:
/usr/local/bin/node /usr/local/bin/elm-make src/elm/Main.elm --output=builds/main.js
-- TYPE MISMATCH ---------------------------------------------- src/elm/Main.elm
The type annotation for `init` does not match its definition.
35| init : Maybe Route.Location -> ( Model, Cmd Msg )
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The type annotation is saying:
Maybe Route.Location -> ( { route : Maybe Route.Location }, Cmd Msg )
But I am inferring that the definition has this type:
Maybe Route.Location
-> ( { route : Maybe Route.Location -> Route.Model }, Cmd a )
Detected errors in 1 module.
Process finished with exit code 1
This is the regex that i came up with:
http://regexr.com/3egqu
However, creating output filter out of it like this:
doesn't work.
Thus far, I only know that the following works: ------ ($FILE_PATH$)
And it turns the file path into a link:
Help me find a way to include the line numbers into the links.
Here's what I've come up with;
First,
elm-make --report json
outputs the build errors in structured JSON;
$ elm-make --report json src/main.elm
[{"tag":"unused import","overview":"Module `Bootstrap.CDN` is unused.","details":"Best to remove it. Don't save code quality for later!","region":{"start":{"line":3,"column":1},"end":{"line":3,"column":28}},"type":"warning","file":"src/main.elm"}]
Now you can pipe that output through jq (see here). to reformat it into
elm make src/main.elm --report json --output ./public/app.js | \
jq '.[] | { type: .type, file: .file, line: .region.start.line|tostring, tag: .tag, column: .region.start.column|tostring, tag: .tag, details: .details }' | \
jq --raw-output '. | "[" + (.type|ascii_upcase) + "] " + .file + ":" + .line + ":" + .column + " " + .tag + " -- " + .details + "\n"'
that gives you a reformatted output;
[WARNING] src/main.elm:9:1 unused import -- Best to remove it. Don't save code quality for later!
[WARNING] src/main.elm:17:1 missing type annotation -- I inferred the type annotation so you can copy it into your code:
main : Program Never Model Main.Msg
Which you pick up in intellij using the format
$FILE_PATH$:$LINE$:$COLUMN$ $MESSAGE$
You then get to click on an error message to jump to the file, and the error text in a tooltip.

Passing file arguments to script in Lua

I need to read a input file which name I do not know.
I know that in C we can do this:
FILE *Ifile;
File *Ofile;
int main(int argc, char *argv[]){
// Input and Output files
Ifile = fopen(argv[1],"r");
Ofile = fopen(argv[2],"w");
(More code)
}
and then call "./cprogram <any file name>.txt <any file name>.txt"
Can I do something like this with .Lua scripts?
Yes, you can. From the documentation:
Before starting to run the script, lua collects all arguments in the command line in a global table called arg. The script name is stored at index 0, the first argument after the script name goes to index 1, and so on.
For example, you could do the following:
if #arg < 2 then
print ("usage: lua " .. arg[0] .. " <ifile> <ofile>")
return
end
local ifile = io.open(arg[1], "r")
local ofile = io.open(arg[2], "w")
if not ifile or not ofile then
print ("Error: could not open files")
return
end

ASSERT failure in QList<T>::at: "index out of range" in Qt 5

I want to generate *.png files from my directory randomly in qt using qrand() function.
to do that I have created a QStringList object so that I can store all files I have in my
directory.thinking that I have some list of files in my QStringList object when I run my application it crush.
I have also warning in my issue pane:> warning: unknown escape sequence: '\D' [enabled by default]
applicationPath = "C:\\Users\\Tekme\Documents\\QtProject\\4Toddler";
^
the reason I add the above warning issues is that, I am on window machine so I am thinking the problem can be path problem
the code is
QString MainWindow::randomIcon()
{
QStringList iconFileList;
QString searchPath = applicationPath + "\\icons";//applicationPath =
QDir directory = QDir(searchPath);
QStringList filters;
filters << "*.png";
directory.setNameFilters(filters);
iconFileList = directory.entryList(QDir::AllEntries);//i have 6 *.png files
int randomIndex = qrand() % iconFileList.count();
return iconFileList.at(randomIndex);//my application crash here
}
Even when I try to replace iconFileList.at(randomIndex) by iconFileList.at(2) it crush.
I am sure I have more than 2 files in my directory
The answer is in the error:
applicationPath = "C:\\Users\\Tekme\Documents\\QtProject\\4Toddler";
Should be
applicationPath = "C:\\Users\\Tekme\\Documents\\QtProject\\4Toddler";
You are missing an extra slash