Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Quick update I found a way to solve this in webMathematica, using CollectStreams. But still, below problem leaves me puzzled, I would very much like to learn how this works.

Original

This question is motivated by the fact that I started to used webMathematica and observed a (to me) strange behavior there. I tried to isolate the problem, in plain Mathematica. I think it is rather closely related to this question, so I hope it is not considered a duplicate.

Setup We start with a fresh kernel and check the Streams:

Quit[]
Streams[]

{OutputStream["stdout", 1], OutputStream["stderr", 2]}

Next, we load some CountryData related info, such as:

CountryData["CH", "Population"]

7.595*10^6

Checking Streams again:

Streams[]

{OutputStream["stdout", 1], OutputStream["stderr", 2], InputStream[ "/Users/myusername/Library/Mathematica/Paclets/Repository/\ CountryData-8.0.62/Data/CountryData.wdx", 111]}

Ok, CountryaData opened a new stream to read the info from. All good.

Now we Close that stream manually:

Close[Last@Streams[]]

so we're back to

Streams[]

{OutputStream["stdout", 1], OutputStream["stderr", 2]}

and then we try to get the population a different country:

CountryData["FR", "Population"]

General::openx: InputStream[/Users/myusername/Library/Mathematica/Paclets/Repository/CountryData-8.0.62/Data/CountryData.wdx,111] is not open. >>

BinaryRead::openx: InputStream[/Users/myusername/Library/Mathematica/Paclets/Repository/CountryData-8.0.62/Data/CountryData.wdx,112] is not open. >>

`$Failed

and interestingly, this still works:

CountryData["CH", "Population"]

My Conclusion

It seems to me that on a fresh kernel, the first time we call CountryData (and e.g. ElementData, ...) it opens a stream and subsequently remembers that it has (should have) an open stream and tries to read from it. If that stream gets closed (which seems to happen in webMathematica, e.g. on reload of a page), you get $Failed.

My Question

Do you know where CountryData stores the stream it is associated with, in the open session? Is there a way to "reset" CountryData without quitting the kernel? Or can I add the lost stream manually? I tried various possiblities, such as:

OpenRead["/Users/myusername/Library/Mathematica/
   Paclets/Repository/CountryData-8.0.62/Data/CountryData.wdx"]

but I got the wrong serial number - and CountryData was still referring to the previous one (111 in above example).

Note Please let me know if the webMathematica code I used to play with is of interest (how to replicate etc)

share|improve this question

1 Answer 1

This seems to work. I think this sort of thing is probably dangerous -- well, unreliable.

file = DataPaclets`GetDataPacletResource["CountryData", "CountryData.wdx"]; 
DataPaclets`CountryDataDump`$StreamCache["CountryData"] = 
 OpenRead[file, BinaryFormat -> True]

I don't have webMathematica, so I can't test it on that.

share|improve this answer
    
thanks for that answer, I'll keep it open a bit longer to see if somebody else has another thought. I thought there must be an easy way to get this done, but it doesn't seem so. (and for webMathematica, I found a way to get it done, see the edited question). Thanks for the effort, +1! –  Pinguin Dirk Oct 14 '13 at 14:04
    
@PinguinDirk It would be nicer if Mma would issue a warning message and try to reopen it itself, perhaps with options to control the behavior. Perhaps they exist. –  Michael E2 Oct 14 '13 at 14:16
    
exactly. That sort of answer is what I was hoping for. Maybe somebody will know about it. Thanks! –  Pinguin Dirk Oct 14 '13 at 14:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.