Netcdf error when calling http in scala - scala

I am trying to get http://dd.weather.gc.ca/ensemble/naefs/grib2/raw/12/018/CMC_naefs-geps-raw_RH_TGL_2m_latlon0p5x0p5_2018070712_P018_allmbrs.grib2 file with NetCDF:
def read(path: String): NetcdfDataset = {
NetcdfDataset.openDataset(path)
}
but I get
java.nio.file.InvalidPathException: Illegal char <:> at index 4:
http://dd.weather.gc.ca/ensemble/naefs/grib2/raw/12/018/CMC_naefs-geps-raw_RH_TGL_2m_latlon0p5x0p5_2018070712_P018_allmbrs.grib2
I have "edu.ucar" % "netcdfAll" % "4.6.3". What should I do to get this file? I already tried to load grib2 file from disk with this method and it goes OK.

It looks like the function NetcdfDataset.openDataset doesn't accept URLs, but only local paths. I suggest you download the .grib2 file to your computer and then pass the path to the downloaded file to the openDataset function,
e.g.
NetcdfDataset.openDataset("/home/kuba/Downloads/CMC_naefs-geps-raw_RH_TGL_2m_latlon0p5x0p5_2018070712_P018_allmbrs.grib2")

Related

Accept a specific type of file Matlab

I have a GUI using a Browe Button to search a file :
function Browse(app, event)
FileName,FilePath ]= uigetfile();
ExPath = fullfile(FilePath, FileName);
app.FileTextArea.Value = ExPath;
end
And i save the file Path in a Text Area.
I have another button that start a matlab script with the file path as parameter and so i would like to accept only a certain type of file (.ctm which is my own type of file) if possible like this :
if file is .ctm
do something
else
print('a .ctm file is needed')
Thanks for helping
There are two things you can do:
Display only the files with a certain extension with uigetfile()
[fileName, dataDir] = uigetfile('*.ctm', 'Select a *.ctm file', yourDefaultPth);
Verify that selected file has a .ctm extension
[data.dir,data.fileName,data.ext] = fileparts(fullfile(dataDir, fileName)); % dataDir and fileName from pt. 1
if strcmp(data.ext, '.ctm')
% do something
else
print('a .ctm file is needed')
end
Keep in mind that neither of the two will verify that the content of the file is the one you're expecting and if someone will manually modify extension of the file, your program will most likely crash. It's good for a start but if you want to do a more reliable check, you should verify that the content of the file is correct, not its extension.

Gatling where to place JSON files?

I'm having issues with loading a JSON file in Gatling. It works with an absolute path but not with a relative. Where should JSON files be stored? I've tried /home/dev/gatling-charts-highcharts-bundle-2.3.0/user-files/data but the file could not be found.
Piece of my code:
def addCredential(status_code: Option[Seq[Int]], username: Option[String]) = {
feed(random_user)
.exec(http("[POST] /users/[user]/credentials")
.post("/users/%s/credentials".format(username getOrElse "${username}"))
.body(RawFileBody("credential.json")).asJSON
.check(status.in(202, 404, 409)))
}
The file credential.json can be found if I give the absolute path but this is not optimal because several people use the simulations.
You can configure the folder where the bodies are located in gatling.conf.
directory {
bodies = user-files/bodies # Folder where bodies are located
}
Then you can put your file in the configured path /your-project/user-files/bodies/credential.json.

Using the same object from different PyTest testfiles?

im working with pytest right know. My Problem is that I need to use the same object generated in one test_file1.py in another test_file2.py which are in two different directories and invoked separately from another.
Heres the code:
$ testhandler.py
# Starts the first testcases
returnValue = pytest.main(["-x", "--alluredir=%s" % test1_path, "--junitxml=%s" % test1_path+"\\JunitOut_test1.xml", test_file1])
# Starts the second testcases
pytest.main(["--alluredir=%s" % test2_path, "--junitxml=%s" % test2_path+"\\JunitOut_test2.xml", test_file2])
As you can see the first one is critical, therefore I start it with -x to interrupt if there is an error. And --alluredir deletes the target directory before starting the new tests. Thats why I decided to invoke pytest twice in my testhandler.py (moreoften in the future maybe)
Here is are the test_files:
$ test1_directory/test_file1.py
#pytest.fixture(scope='session')
def object():
# Generate reusable object from another file
def test_use_object(object):
# use the object generated above
Note that the object is actually a class with parameters and functions.
$ test2_directory/test_file2.py
def test_use_object_from_file1():
# reuse the object
I tried to generate the object in the testhandler.py file and importing it to both testfiles. The problem was that the object was not excatly the same as in the testhandler.py or test_file1.py.
My question is now if there is a possibility to use excatly that one generated object. Maybe with a global conftest.py or something like that.
Thank you for your time!
By exactly the same you mean a similar object, right? The only way to do this is to marshal it in the first process and unmarshal it in the other process. One way to do it is by using json or pickle as marshaller, and pass the filename to use for the json/pickle file to be able to read the object back.
Here's some sample code, untested:
# conftest.py
def pytest_addoption(parser):
parser.addoption("--marshalfile", help="file name to transfer files between processes")
#pytest.fixture(scope='session')
def object(request):
filename = request.getoption('marshalfile')
if filename is None:
raise pytest.UsageError('--marshalfile required')
# dump object
if not os.path.isfile(filename):
obj = create_expensive_object()
with open(filename, 'wb') as f:
pickle.dump(f, obj)
else:
# load object, hopefully in the other process
with open(filename, 'rb') as f:
obj = pickle.load(f)
return obj

Cannot Use Relative Path in Scala.IO.Source

I am trying to read a CSV file into Scala. I can read fine using the absolute path, but would like to be able to use the relative path.
val filename = "sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
for (line <- Source.fromFile(filename).getLines()) { println(line) }
throws the error:
java.io.FileNotFoundException: sample_data\yahoo_finance\AAPL\AAPL_Historical.csv
(The system cannot find the path specified)
However:
val filename = "C:/Users/hansb/Desktop/Scala Project/src/main/" +
"resources/sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
for (line <- Source.fromFile(filename).getLines()) { println(line) }
works just fine.
My understanding was that scala.io.Source knew to look in the resources folder for the relative path.
What am I missing?
Working code using Phasmid's suggestion:
val relativePath = "/sample_data/yahoo_finance/AAPL/AAPL_Historical.csv"
val csv = getClass.getResource(relativePath)
for (line <- Source.fromURL(csv).getLines()){ println(line) }
This is one of the worst things about Java (and, thus, Scala). I imagine many millions of hours have been spent on this kind of problem.
If you want to get a resource from a relative path (i.e. from the class path) you need to treat the resource as a resource. So, something like the following:
getClass.getResource("AAPL_Historical.csv")
while yields a URL which can then convert into a Stream, or whatever. This form will expect to find the resource in the same (relative) folder as the class, but in the resources rather than scala directory.
If you want to put the resource into the top level of the resources folder, then use:
getClass.getResource("/AAPL_Historical.csv")
It may be that there is some other magic which works but I haven't found it.

How to implement "NSUserDefaults" functionality equivalent in Corona?

All I want is to save my user(player) highscores, and this information to persist between application(game) launches in Corona SDK (Lua). I do want it to work on iOS and Android nicely. My highscores data is actually two lua tables containing numbers.
What's the correct and easiest way to do it?
You may save the scores into a table, and then serialize it into json format text file.
local json=require("json")
local savefile="scores.json"
scores=
{
{
level=1,
status=0,
highscore=0,
},
{
level=2,
status=0,
highscore=0,
},
}
function getScore(filename, base)
-- set default base dir if none specified
if not base then
base = system.DocumentsDirectory
end
-- create a file path for corona i/o
local path = system.pathForFile(filename, base)
-- will hold contents of file
local contents
-- io.open opens a file at path. returns nil if no file found
local file = io.open(path, "r")
local scores
if file then
-- read all contents of file into a string
contents = file:read( "*a" )
if content ~= nil then
scores=json.decode(content)
end
io.close(file) -- close the file after using it
end
return scores
end
function saveScore(filename, base)
-- set default base dir if none specified
if not base then
base = system.DocumentsDirectory
end
-- create a file path for corona i/o
local path = system.pathForFile(filename, base)
-- io.open opens a file at path. returns nil if no file found
local file = io.open(path, "wb")
if file then
-- write all contents of file into a string
file:write(json.encode(scores))
io.close(file) -- close the file after using it
end
end
The global scores variable can be manipulated like a normal table, and when you want to load or save the scores table you can call the functions above.