How to extract readable values from .grib2 - grib

I have installed wgrib2 and degrib but cannot figure out how to extract values (human readable) for a specifig lat/lng.
degrib multi_1.nww3.t00z.grib2 -P -pnt -33.883,18.254
gives me:
SWPER, [s], 201403290000, 201403300900, 9999.000
WVDIR, [Degree true], 201403290000, 201403300900, 9999.000
SWDIR, [Degree true], 201403290000, 201403300900, 9999.000
...
Thanks!

Just in case someone wants to do this and they don't have degrib and wgrib2 installed, you can extract a point directly with cdo and output to a netcdf file:
cdo -f nc remapnn,lon=18.254/lat=-33.883 in.grb out.nc
"remapnn" means nearest neighbour remapping and this thus extracts the nearest gridpoint to the desired lon/lat without interpolation. You can then examine those values with
ncdump out.nc

ok. i found the "mistake": the command ist correct and works if there is data for the given point. 9999.000 is the output if there is no data for the given point.. maybe it helps someone

Related

I am trying get three parameters of x, y, and z from this script but all it does is return one value that is not the dimensions I am looking for?

I am new to perl and i am trying to get the dimensions which i think are given from the GeometricCenter method in this perl script. I used this script and it ran and returned only one value which i believe is a value called the gyration which is a paramter that helps determine the xyz dimensions. I thought this script would of returned the dimensions but it did not. Anyone know?
so i tried printing the values in the array xyz which i thought they were in. I tried this by using say but it said i cannot use say on an undefined value
This is the actual problem.
https://github.com/michal-brylinski/eboxsize/blob/master/eBoxSize-1.1.pl
As far as I can tell, your question boils down to "how do I print the values of an array?".
The answer depends on how you want the results formatted, but in the simple case of space-separated numbers, you can just do
print "#geo_center\n";

Getting Variables from a data structure and creating a matrix from those variables

I have a data structure which has data points named Vel1 to Vel1520. However, when I apply Uorder = orderfields(MeanU_Velocity); the variables put in the order Vel1 Vel10 Vel100 Vel1000 Vel1001 Vel1002 etc. Is there any way to sort the data structure such that it lists the variables from 1 to 1520 in ascending order? Regards, Jer
An easy fix to this is to always use the same number of digits. 0001, 0002, ..., 0010, ..., 1520
instead of num2str(42), try sprintf('Vel%04d', 42). This prints formatted text to a string. %04d is a special code that says: fill with zeros, reserve 4 places, print an integer number. Have a look at the documentation and look at matlabs formatted strings tutorial for more comprehensive examples.

Get a random file in Fish shell

I'm translating this Zsh function to Fish
function random_quote() {
QUOTE_FILES=( $PREFS_ROOT/quotes/* )
cat $QUOTE_FILES[$RANDOM%$#QUOTE_FILES+1]
}
Here's what I've got so far:
function random_quote
set QUOTE_FILES $PREFS_ROOT/quotes/*
cat $QUOTE_FILES[$RANDOM%$#QUOTE_FILES+1]
end
The cat line needs fixing still. I know that RANDOM should be replaced by random, but I'm not sure how to do the rest.
Just found out it's really simple, random itself supports getting a random entry from a list!
just use cat (random choice $QUOTE_FILES)
it also works without a variable random choice /path/to/some/folder/*
or with a bunch of arguments random choice option1 option2 option3
you can find the documentation for random here:
https://fishshell.com/docs/current/cmds/random.html
How about
function random_quote
set -l QUOTE_FILES $PREFS_ROOT/quotes/*
set -l n (math 'scale=0;'(random)'%'(count $QUOTE_FILES)'+1')
cat $QUOTE_FILES[$n]
end
This answer is outdated. Please follow #Niklas's correct one.

Find all differences between .mat files

I am looking for a way to list the differences between two .mat files, something that can be usefull for many people.
Though I searched everywhere I could think of, I have not found anything that meets my requirements:
Pick 2 mat files
Find the differences
Save them properly
The closest I have come is visdiff. As long as I stay within matlab, it will allow me to browse the differences, but when I save the result it only shows me the top level.
Here is a simplified example of what my files typically look like:
a = 6;
b.c.d = 7;
b.c.e = 'x';
save f1
f = a;
clear a
b.c.e = 'y';
save f2
visdiff('f1.mat','f2.mat')
If I click here on b, I can find the difference. However if I run this and use 'file>save', I am not able to click on b. Thus I still don't know what has been changed.
Note: I don't have Simulink
Hence my question is:
How can I show all differences between 2 mat files to someone without Matlab
Here are the answers that I personally consider to be most suitable for different situations:
Answer for users with Simulink
General answer
Answer displaying all value differences
Find all differences between mat files without MATLAB?
You can find the differences between HDF5 based .mat files with the HDF5 Tools.
Example
Let me shorten your MATLAB example and assume you create two mat files with
clear ; a = 6 ; b.c = 'hello' ; save -v7.3 f1
clear ; a = 7 ; b.e = 'world' ; save -v7.3 f2
Outside MATLAB use
h5ls -v -r f1.mat
to get a listing about the kind of data included f1.mat:
Opened "f1.mat" with sec2 driver.
/ Group
Location: 1:96
Links: 1
/a Dataset {1/1, 1/1}
Attribute: MATLAB_class scalar
Type: 6-byte null-terminated ASCII string
Data: "double"
Location: 1:2576
Links: 1
Storage: 8 logical bytes, 8 allocated bytes, 100.00% utilization
Type: native double
/b Group
Attribute: MATLAB_class scalar
Type: 6-byte null-terminated ASCII string
Data: "struct"
Location: 1:800
Links: 1
/b/c Dataset {5/5, 1/1}
Attribute: H5PATH scalar
Type: 2-byte null-terminated ASCII string
Data: "/b"
Attribute: MATLAB_class scalar
Type: 4-byte null-terminated ASCII string
Data: "char"
Attribute: MATLAB_int_decode scalar
Type: native int
Data: 2
Location: 1:1832
Links: 1
Storage: 10 logical bytes, 10 allocated bytes, 100.00% utilization
Type: native unsigned short
Use of
h5ls -d -r f1.mat
returns the values of the stored data:
/ Group
/a Dataset {1, 1}
Data:
(0,0) 6
/b Group
/b/c Dataset {5, 1}
Data:
(0,0) 104, 101, 108, 108, 111
The data 104, 101, 108, 108, 111 represents the word hello, which can be seen with
h5ls -d -r f1.mat | tail -1 | awk '{FS=",";printf("%c%c%c%c%c \n",$2,$3,$4,$5,$6)}'
You can get the same listing for f2.mat and compare the two outputs with the tool of your choice.
Comparison also works directly with HDF5 Tools. To compare the two numbers a from both files use
h5diff -r f1.mat f2.mat /a
which will show you the values and their difference
dataset: </a> and </a>
size: [1x1] [1x1]
position a a difference
------------------------------------------------------------
[ 0 0 ] 6 7 1
1 differences found
attribute: <MATLAB_class of </a>> and <MATLAB_class of </a>>
0 differences found
Remarks
There are a few more commands and options in the HDF5 Tools, which may help to get your real problem solved.
Binary distributions are available for Linux and Windows from The HDF Group. For OS X you can get them installed via MacPorts. If needed there is also a GUI: HDFView.
If you have simulink you can use Simulink.saveVars to generate an m-file that upon execution creates the same variables in work space:
a = 6;
b.c.d = 7;
b.c.e = 'x';
Simulink.saveVars('f1');
f = a;
clear a
b.c.e = 'y';
Simulink.saveVars('f2');
visdiff('f1.m','f2.m')
as illustrated in this sctreenshot
Note that by default it limits the number of elements in arrays to 1000 and you can increase it to 10000. Arrays larger than that limit will be saved in a separate mat-file.
UPDATE: From R2014a a new function similar to Simulink.saveVars has been added to MATLAB. see matlab.io.saveVariablesToScript
This is only part of the answer, but maybe it helps.
You could use gencode, a Matlab function that generates Matlab code from a variable such that running the code reproduces the variable. You do this for all of the variables in each mat-file (takes some programming, but should be doable) and put the results in different .m-files.
Then you use a standard text comparison tool (maybe even visdiff) to compare the .m-files.
There are several good tools to compare XML-Files, this I would proceed this way:
Download struct2xml.m
Load both matfiles
Export each with struct2xml
compare, using XMLSpy or similar
Simple general answer, without displaying value differences
Due to the insight I gained from the answers of #BHF, #Daniel R and #Dennis Jaheruddin, I have managed to find a simple scalable solution:
[fs1, fs2, er] = comp_struct(load('f1.mat'),load('f2.mat'))
Note that it works for .mat containing an arbritrary number of variables.
This uses the Compare Structures - File Exchange submission.
Answer for small files, displaying all value differences
Based on the suggestion by #A. Donda I have tried to use gencode to create a variable for everything.
Though it works for my toy example, it is quite slow and tells me that I exceed the allowed amount of variables for my real .mat files.
Anyway, for those who are looking for something that works with small files, I will post this option:
wList=who;
for iLoop = 1:numel(wList)
eval(['generated_' wList{iLoop} '= gencode(' wList{iLoop} ');'])
for jLoop = 1:numel(eval(['generated_' wList{iLoop}]))
eval(['generated_' wList{iLoop} '_' num2str(jLoop) '= generated_' wList{iLoop} '(' num2str(jLoop) ');' ])
end
end
Though it may work, I don't feel like this is the best way to go.
General answer, without displaying value differences
Due to the insight I gained from the answers of #BHF and #Daniel R I have managed to find a reasonably scalable solution.
Step 1: Save all variables from each files as a single struct
This uses the Save workspace to struct - File Exchange submission.
Here are the steps to take assuming you want to compare f1.mat and f2.mat:
clear
load f1
myStruct1 = ws2struct;
save myStruct1 myStruct1
clear
load f2
myStruct2 = ws2struct;
save myStruct2 myStruct2
clear
load myStruct1
load myStruct2
Step 2: Compare the structs
This uses the Compare Structures - File Exchange submission
Given that you want to compare myStruct1 and myStruct2 you can simply call:
[fs1, fs2, er] = comp_struct(myStruct1,myStruct2)
I was positively surprised at how readable the list of differences in er is, here is the output for the example that was used in the question:
er =
's2 is missing field a'
's1(1).b(1).c(1).e and s2(1).b(1).c(1).e do not match'
Note that it will not show values, from a technical point of view it is probably not too hard to change the m file if value difference displays are desirable. However, especially if there are some big matrices I suppose this could result in problematic output.

Pick random lines from file with fixed seed (pseudo-random)

I would like to randomly pick some lines (e.g.20) from a file and print it into another but I want to have a seed fixed so that I get the same output if input file is the same.
The examples I've found that pick several lines, their output is different everytime
e.g:
perl -e '$f="inputfile";$_=`wc -l $f`;#l=split( );$r=int rand(#l[0]);system("head -n$r $f|tail -20")'> outputfile
And those that talk about fixed seed and pseudo-random are just for printing numbers, not extracting lines from files, or just extract a single line. Is there a command for unix or some code in perl or similar? (sort -R, --random- & shuf didn't work (using Mac OS X 10.5.8)).
You can set the seed via srand(); (for example. srand(5)) to get a fixed seed for rand.