Stand With Ukraine

Thursday, October 9, 2014

Bash background processes, function and local variables example

#!/bin/bash
#This simple code demonstrates how to define a function, local and global variables in bash.
#And most importantly, how to launch background processes and wait for their termination
gvar="Glob value"
ncalls=0
avar="Test"
# Note for ksh users: in ksh to mark a function definition you should use either function keyword
# or () after the function name, not both
function a_proc(){
echo ${gvar}
local avar=$1 #ksh users: use 'typeset' instead of 'local'
echo "Input: ${avar}"
sleep 100
avar=$(date)
echo "Output: ${avar}"
ncalls=$((${ncalls} + 1))
}
a_proc "$(date)" &
a_proc "$(date)" &
a_proc "$(date)" &
a_proc "$(date)" &
a_proc "$(date)" &
a_proc "$(date)" &
wait
#Although I am trying to update ${ncalls} assuming that this will change my global variable, but ...
#This is because I am launching the function in background, which works in a subshell,
#so global variables won't be updated
echo "ncalls=${ncalls}"
echo "avar=${avar}"
##But look what happens with ncalls when launched in foreground
a_proc "$(date)"
echo "ncalls=${ncalls}"
##Conclusion: the local keyword (for bash, use typeset for ksh) can be useful if you want to make sure that you are not
##changing a global variable in your function

Friday, September 19, 2014

Youtube videos in chromium on OpenSuse 13.1

Recently I got tired and fed up with ubuntu/kubuntu and decided to go back to Opensuse 13.1. I installed it and almost everything works OK now. I do still have some troubles reanimating WIFI after sleep (I have to go to network configuration and enter my password for selected network). But this post is about video, so if you have chromium installed but youtube videos do not play then the following might help you solve your issue:
    sudo zypper rm chromium-ffmpeg
The above will remove chromium-ffmpeg and will install chromium-ffmpegsumo. If you do not have chromium-ffmpegsumo installed then install it:
    sudo zypper in chromium-ffmpegsumo
Hope this will help someone to make opensuse more enjoyable. Anyway even with these 2 annoyances I like opensuse more than ubuntu. (Maybe because I have not used it long enough yet:))

Tuesday, January 21, 2014

netcdf4-python copying variables from one netcdf file to another

This has arisen as a problem of converting NETCDF4 to NETCDF3_CLASSIC which is still used by some old software. I did try nc4tonc3 provided with netCDF4 but that one also wanted the input file in NETCDF4_CLASSIC format... That is why I wrote this little script to actually copy variables and dimensions. There were no attributes in the files, so I did not care to handle attributes.

# -*- coding: utf-8 -*-
from netCDF4 import Dataset
#input file
dsin = Dataset("crop.nc")
#output file
dsout = Dataset("crop.nc3", "w", format="NETCDF3_CLASSIC")
#Copy dimensions
for dname, the_dim in dsin.dimensions.iteritems():
print dname, len(the_dim)
dsout.createDimension(dname, len(the_dim) if not the_dim.isunlimited() else None)
# Copy variables
for v_name, varin in dsin.variables.iteritems():
outVar = dsout.createVariable(v_name, varin.datatype, varin.dimensions)
print varin.datatype
# Copy variable attributes
outVar.setncatts({k: varin.getncattr(k) for k in varin.ncattrs()})
outVar[:] = varin[:]
# close the output file
dsout.close()
view raw Converter.py hosted with ❤ by GitHub
From a comment by Nikolay Koldunov, there is a utility for this tasks nccopy ...