Archive | code RSS for this section

A VGA font in a terminal emulator

How’d I do it? It was quite a while ago and I can’t remember, I can’t recall. I’m trying to reconstruct it…

I grew up in the IBM compatible, DOS era. That VGA font that you got by default in DOS still, to this day, looks ‘right’ to me on a terminal screen, even after years of using Linux and Mac OS X and what not. I know it is not a great font in numerous ways, but I am so familiar I don’t even see it. It’s invisible, like a good writing tool should be. So I wanted to use it in my terminal window on Debian. It’s not very hard, maybe. I can’t remember if these were all the things I needed to do…

I am using mrxvt because I like the simplicity of it. This is my very simple mrxvt configuration file, .mrxvtrc:

Mrxvt.sl: 65535
MRxvt.ps: true

Mrxvt.xft:                      1
Mrxvt.xftFont:                  Perfect DOS VGA 437 Win
Mrxvt.xftSize:                  16
Mrxvt.xftAntialias:             1

Mrxvt.saveLines:                10000

And I have aliased the command mrxvt to this string:

alias mrxvt='mrxvt-full -fade 50 -ps -title " " -tn rxvt'

So to get the VGA font, I first went and found a good X windows font, called “Perfect DOS VGA 437 Win”, which is a ttf (TrueType font) when I unzip the archive. I put the file (called Perfect_DOS_VGA_437_Win.ttf) in a subdirectory of my .fonts directory; I put it under ‘p’ for perfect:

$ ls ~/.fonts/p
Perfect_DOS_VGA_437_Win.ttf

And, on a modern Linux system, that seems to be all that is required. Except I do notice that my .fonts.conf file looks like this (also serves as an example of what the font looks like in action):

Perfect DOS VGA 437 Win font used in mrxvt managed by FLWM.

Perfect DOS VGA 437 Win font used in mrxvt managed by FLWM.

Notice that the <dir> line at the end lists my local ~/.fonts directory? I think that might be important, but like I said it was a long time ago and I’m not sure.

So there you have it. Finding Perfect_DOS_VGA_437_Win.ttf somewhere on the web and putting it in your ~/.fonts directory and making sure that that directory is listed in your ~/.fonts.conf file and then changing the font selection line in the config file for whichever terminal emulator you are using might or might not let you use a font you don’t like.

 

 

Fonty McFontface.

A little script to scan and OCR a bunch of pages

So this little script just uses scanimage, tesseract and vim to scan and process pages from my typewriter. It tries to produce sensible paragraphs, and outputs the results of multiple pages to a text file which can be read in and formatted using a word processor, such as LibreOffice.

It is an interactive script because I do not have a scanner fitted with a sheet feeder. To make it non-interactive, modify the scanimage line after reading the scanimage man page, and remove the line read Response. Nothing fancy, no error checking, no clean-up afterwards, no niceties. But it works pretty well, so far. If you want to use it, install any packages you need to to get scanimage, tesseract and vim to work, and cut and paste the below into a file in your path, and make the file executable.

cat type_ocr.sh
# /bin/bash
#
# type_ocr.sh v. 1.0
#
# Script to scan, ocr, process and concatenate pages, e.g. from a
# typewriter.
#
# D.J.Goossens, 14 July 2016. darren.goossens@gmail.com
#
# Start at 1001 so we can be (pretty!) sure all filenames have 4 digit
# numbers
#
# Create the output file.
echo This is type_ocr.sh v. 1.0
echo
echo Make sure you give it the output filename as a command line argument.
echo Ctrl-D escapes from the scanning, Ctrl-C quits elsewhere.
echo The resulting images and text files are not deleted.
echo They are of the form outXXXX.pnm and outXXXX.pnm.txt and
echo may be quite big.
echo
echo Hit Ctrl-C to exit now or Enter to continue.
read Response
echo 'Text file from type_ocr.sh v. 1.0' > $1
echo Processed `date` to $1 >> $1
echo 'Note: When it says "document 1001", treat it as document (page) 1'
scanimage --batch --batch-prompt --batch-start 1001 -p --mode=Gray --resolution=600
# Outputs are of the form out????.pnm. Loop over them
for f in out????.pnm;
do
tesseract $f $f
# The above produces out????.pnm.txt, which we can process,
# where first we replace double occurrences of newline with a placeholder
# string, then replace single occurrences with a space, then replace the
# placeholder with a return character (it is a trick of regular
# expressions that we search for \n (newline) but write \r (return) when
# we mess with the file).
vim -c "%s/\n\n/pLaCeHoLdErStRiNg/g" -c "wq" $f.txt
vim -c "%s/\n/ /g" -c "wq" $f.txt
vim -c "%s/pLaCeHoLdErStRiNg/\r/g" -c "wq" $f.txt
cat $f.txt >> $1
done
echo Try typing libreoffice $1 to see what you have got.
echo Setting paragraph formatting to indented and one and a
echo half space is a good start.

Your mileage may vary. Buyer beware. You get what you pay for. No guarantees implied or given. No warranty as far as possible. (Add here any other escape clauses you can think of.)

Because.

Very simple-minded automation of gnuplot

I have lots of datafiles I want to plot. gnuplot is scriptable. Since I don’t need any fancy output yet — this is data investigation, not manufacture of publication-quality diagrams — I can get multiple plots out quickly by writing a simple (simplish) couple of scripts.

I am sure people who know bash and perl and stuff can do this much better, but this works for me.

I have a wrapper script that simply consists of multiple calls to an inner script. It is here that I select the files I want to plot:

$ cat make_lots_of_plots.sh
./script_auto.sh r_0001_o_xy_TPA_21_Biso4p8_reread.inp
./script_auto.sh r_0001_o_yz_TPA_21_Biso4p8_reread.inp
./script_auto.sh r_0001_o_zx_TPA_21_Biso4p8_reread.inp
.
.
etc
.
.
./script_auto.sh r_0050_o_yz_TPA_21_Biso4p8_reread.inp
./script_auto.sh r_0050_o_zx_TPA_21_Biso4p8_reread.inp

I know I could do this in a single script, but I like to be able to call the inner one directly if I just want to make one plot. The inner script, script_auto.sh looks like this:

$ cat script_auto.sh
epsname=`basename $1 .inp`.eps
echo $epsname
scriptname=`basename $1 .inp`.gp
echo $scriptname
echo "set term postscript eps solid enhanced font 'Times,24'" > $scriptname
echo $epsname > temp1111
echo 'set output "' > temp2222
echo '"' > temp3333
paste -d '' temp2222 temp1111 temp3333 >> $scriptname
echo "set angles degrees; set polar" >> $scriptname
echo "set size square" >> $scriptname
echo "set xrange [-0.8:0.8]" >> $scriptname
echo "set yrange [-0.8:0.8]" >> $scriptname
echo $1 > temp1111
echo 'plot "' > temp2222
echo '" w l lw 3' > temp3333
paste -d '' temp2222 temp1111 temp3333 >> $scriptname
echo "set output" >> $scriptname
echo "set terminal x11" >> $scriptname
echo "quit" >> $scriptname
cat $scriptname
rm temp1111 temp2222 temp3333
gnuplot $scriptname
ls -ltrh $epsname

What’s going on here?

epsname=`basename $1 .inp`.eps
echo $epsname
scriptname=`basename $1 .inp`.gp
echo $scriptname

The bit above just uses basename to create the two filenames I need, one for the encapsulated postscript output, and one for the gnuplot script. Then I basically assemble the lines I want to see go into the gnuplot script. Mostly I can just echo stuff into the file, but I have various quote marks within quote marks, and (for simple-minded me) the solution that I know works is to go via the paste command, and write stuff out to little text files. I am sure there are much tidier ways to do this, but this works for me.

The line:

paste -d '' temp2222 temp1111 temp3333 >> $scriptname

causes the contents of the temporary files to be pasted together and put into the script, where the delimiter (‘-d‘) is the thing between the single quotes — which is nothing since the quotes are adjacent. This pastes stuff together with no gaps. The things being echoed into the file are gnuplot commands of various kinds.

Then I cat the script to the screen so I can see what it is doing (if I direct the output from the plotting script into a file, this lets me capture the commands I used), then I remove the temporary files, run gnuplot, and list the newly made file.

Clunky, but simple and effective. Useful changes could include outputting to pdf or some other format, and adding a step at the end (perhaps using pdfjoin) to combine all the resulting files in a single file.

Here is a png of the result. The gnuplot formatting could be made much nicer (title is too big, etc), but the overall process can always be improved, so I’m not worried about that. And the fact that it is a script means I can replot quickly when I know what the final format should be. For now I just want to see what I’ve got.

A simple plot from a diffuse scattering simulation, made using gnuplot.

A simple plot from a diffuse scattering simulation, made using gnuplot.

Whatever suits.

My little reminder scripty thing: zenity plus at.

I want to make little ‘notes to self’ so don’t forget stuff, and I want it on the command line and to be really simple. There are plenty of calendar tools for this, but I thought I would put together s tiny little thing for myself.

No dependencies or downloads (well, none for me, since my system already has zenity)… here is my little script, called ‘remind‘:

cat ~/bin/remind
echo 'DISPLAY=' > ~/.reminders/temp1remind
echo $DISPLAY > ~/.reminders/temp2remind
echo ' zenity --warning --text="' > ~/.reminders/temp3remind
echo $2 > ~/.reminders/temp4remind
echo '"' > ~/.reminders/temp5remind
paste -d '' ~/.reminders/temp[1-5]remind > ~/.reminders/"atjob$1"
rm ~/.reminders/temp[1-5]remind
chmod 700 ~/.reminders/"atjob$1"
cat ~/.reminders/"atjob$1"
at $1 -f ~/.reminders/"atjob$1"

So what this does is create a little file in the newly-made directory .reminders. The file name looks like atjobTIME where TIME is a time expression complying with the syntax of the at command. The idea is that the file will act as input to at.

The remind script above is stuck in my ~/bin directory and made executable. I call it like this (dollar is command prompt):

$ remind "12:52 tomorrow" "Lousy test"

(note the quotes, needed if the entry has any spaces or other tricky characters) and it creates a little file in .reminders (a directory created for this purpose) and then runs at, calling that job. So the little file looks like this:

$ cat .reminders/"atjob12:52 tomorrow"
DISPLAY=:0.0 zenity --warning --text="Lousy test"

And it creates a little zenity dialogue box that looks like this:

A little reminder.

A little reminder.

“Gee, wow.” Well, no, no wow at all, but it works. I suppose .reminders might fill up with tiny little files, but I can’t say it troubles me. I’ll clean them up some time. And I know all the piping stuff to little files is clumsy, but it works for me.

 

Asdfghjkl.

Fortran on the HPLX: Possible, but why would you?

I have an HP200LX. It is a real computer — a DOS 16-bit computer, yes, but one with the flexibility that comes with being able to run various (old) compilers. As a science-ist, I recommend and use Fortran, though preferably a modern variant. There are not many Fortran compilers that work on a 16-bit system, and I did not want to bother with a DOS extender on such a constrained environment. Of course, in truth running any language on this old thing is no more than a curiosity.

For the record, I’m writing this in an old DOS port of vi (calvin) on the LX.

On the Web one can find a thing called BC Fortran. It is in German. It is not terribly modern — essentially, FORTRAN 77 — but it works. This is how I used it on the LX.  Works in DOSBox as well.

1. Googled around for the archive bcf7713b.zip (‘Use of the compiler is free for non-commercial uses.’)

2. Copied it to my CF card in directory \BC.  See this post for how to use a Compact Flash card on a modern computer, if you do not have a card reader with a CF slot in it.

3. Put the card back into the LX. Turned on the LX.

4. Closed down the program manager (Menu -> File -> Terminate all).

5. The CF card is A: so changed to directory A:\BC.

6. Unzipped the archive.

7. For simplicity, I decided to simply copy all the Fortran files into the \BC directory, so I don’t need to worry about the path or anything.

8. I loaded the resident part (necessary for compiling and running programs; not doing this can lock up the LX such that neither the on/off button nor Ctrl-Alt-Del will work… a problem!).

LH BCRTSY.EXE

Only do this once; I recommend using command mem /c to look at what has already been loaded. Also, I do not know how to unload it (short of rebooting), or if that is even possible.  There used to be old DOS utilities for unloading arbitrary TSRs.

9. It is now ready. There is a compiling step followed by a linking step.

To compile file.f:

bcf -u file.f

To link, explicitly include the library on the command line:

bcl -o file.exe file lib

Here is a session, grabbed from a DOSBox window, not from the LX:

bcf -u short.f
BC-FORTRAN77 Compiler 1.3b1711, (c)1990 Andre Koestli, Stuttgart
File short.f
bcl -o short lib
BC-Linker 1.3b1711, (c)1990 Andre Koestli, Stuttgart
short.b             2996
lib.b              71948
       2 Commons    1600
      79 Module    74876
         frei     407220
      25 Module geloescht
       2 Commons    1600
      54 Module    58204
         frei     423892
      13 Module geloescht
       2 Commons    1600
      41 Module    44764
         frei     437332
       4 Module geloescht
       2 Commons    1600
      37 Module    42172
         frei     439924
       1 Module geloescht
       2 Commons    1600
      36 Module    41388
         frei     440708
short.exe          42416

 

Now, it seems to be Fortran 77 without any extensions (‘BCC-FORTRAN77 compiles the complete standard from ANSI/ISO’ — from ENGLISH.TXT, which is included in some of the archives); it does not have ‘end do’ for example, though it does have ‘endif’ and ‘else’.

FYI: ‘geloescht’ = ‘deleted’.

See http://folk.uio.no/hpl/scripting/doc/f77/tutorial/comments.html
See http://www.pcorner.com/list/MISC/BCF7713B.ZIP/ENGLISH.TXT/

This link gives a random number generator that works with the compiler: https://www.cisl.ucar.edu/zine/96/spring/articles/3.random-6.html

Even more pointless, possibly more nifty.

My new toy.

It’s here and on my desk… my new toy is a…number crunching computer for Monte Carlo modelling of materials.

Intel 6th-Gen i7 6700K SSD DDR4 4.0GHz CPU, 16GB DDR4 RAM, 2TB SATA III 6GB/s HDD,N600 Wireless Dual Band PCI-Express Network Adapter with 2 Antennae. (Just a cut and paste from the specs.)

It's just a box.

It’s just a box.

 

Ordered it from D&D Computer Technology Pty Ltd, and delivery was pretty quick. At my work the standard Linux ‘solution’ is RHEL, so it is running RHEL 6.7 (the IT guys here don’t like 7 — it uses the controversial systemd, for one thing…)

Wireless internet so I can put it wherever I want to.

Compared to our previous generation of boxen (4+ years old), it runs a fairly typical Monte Carlo simulation in 20m55s instead of 27m21s, which is a useful but not massive improvement, which is really the result of code that is really just a single, single-threaded process which results in it scaling more with the clock speed than anything else.

I’ve put LaTeX on the box, but I am going to manage it via TeXLive’s tlmgr rather than RHEL’s package management, so we’ll see how that works out…

 

At the other end of the spectrum.

Useless Utility of the Day: bclock.exe

This old DOS program has no use today. It is easy enough to run it in DOSBox of course, but why would you?

I just like the look of it.

Screen grab of BCLOCK.EXE, an almost analogue clock.

Screen grab of BCLOCK.EXE, an almost analogue clock.

 

The ‘almost analogue’ clock offers a novel way to watch time go past, with three bar charts, one for each of  hours, minutes and seconds. It comes with source code but the program needs DOS-specific files like DOS.H. So it cannot be just quickly recompiled for Linux/Windows as far as I know.

The top few lines of the C code.

The top few lines of the C code.

Finding it on the web is a bit hit-and-miss.  A bit of semi-random searching on terms like ‘BCLOCK.ZIP’ and ‘SIMTEL’ and ‘DOS CLOCKS” tends to turn it up in the archive ‘BCLOCK.ZIP‘. Something like this: http://www.filewatcher.com/m/bclock.zip.33143-0.html.  I suppose some of the files might be virus-ed, so be careful.

Recompiling for Linux, say, would be best done by a significant rewrite that uses some kind of cross platform graphics library.  My C coding is not up to that, I’ll admit, though it is a little project I have in the back of my mind.  The layout is rather amenable to sprinkling the display with vertical bars to indicate appointments or deadlines, and I can imagine it being quite a nifty little application.

Never happen.

Some Science: A Publication in Advances in Condensed Matter Physics

Advances in Condensed Matter Physics is an open access journal.  I have published in OA journals before, but I am pretty selective about them.  For example IUCrJ is is published by the IUCr, and I strongly believe that their name is a ‘guarantee of quality’ as good as any.  The other open access paper was in ISRN Materials Science,  which was more of an unknown quantity for me, and being a publisher who seems to have expanded via the OA model, the idea of sending work there initially filled me with skepticism.  This rather abated when it turned out they wanted a commissioned article and they were even prepared to pay me for it.  I am quite happy with how it turned out.  The picture below is not that paper but the new one.

 

Front page of a recent article in <i>Advances in Condensed Matter Physics</i>.

Front page of a recent article in Advances in Condensed Matter Physics (http://dx.doi.org/10.1155/2015/878463).

 

 

One slice of calculated diffuse scattering data from monoclinic 9-Chloro-10-methylanthracene.

One slice of calculated diffuse scattering data from monoclinic 9-Chloro-10-methylanthracene.

Recently, I got an email from Advances in Condensed Matter Physics asking me for an article.  Now, I delete several of these sorts of emails a day, and was about to delete this one when I thought, well, perhaps I should give the publisher — Hindawi, publishers of ISRN Materials Science — a second look.  The fact that they were prepared to commission something from me showed (of course) remarkable good taste, and the fact that they were prepared to pay for it (and did) suggested that they have an intention of climbing out of the ruck and mall of crappy OA publishers.

So I reread the email.  It appeared that they were prepared to waive the fee.  Good start.  I then went to the ACMP website and searched for the names and work of some scientists that I respect.  And they had indeed published there.  The journal has a proper impact factor, even if not especially high, and is not just indexed on google scholar (ie, not really indexed at all) but in proper databases like Web of Science. Okay, so it seems like a real journal.

There was an article I wanted to write, one of a very specific kind.  I have been developing and using the ZMC software for modelling diffuse scattering from molecular crystals — looking at subtle orderings in materials to improve understanding of structure and function.  The process is non-trivial, and not easy for the novice to get a  grip on.  So what I wanted to do was create an example of a simulation of a crystal, write a paper about it, then upload a bundle of files (as ‘additional material’) that would let a  user recreate the simulation.

This seemed like an ideal opportunity, because the software would then be available and not hidden behind a paywall.  Indeed, that is how it has worked out — the simulation (‘supplementary material’) can be downloaded by anyone by going to this page.

So not all OA publishers are alike, but it is important that a prospective author does their research into the journal and makes sure they are credible.

 

Avec.

Add a ‘close’ button to the twm title bar.

Some time ago I talked about a simple customisation of twm on cygwin. There was one more thing I wanted to add to the .twmrc file. This line:

### close window
RightTitleButton "/home/username/installs/backdrops/close4.xbm" = f.delete

All this does is add a button to the right of the title bar, using the bitmap in close4.xbm and attach to it the action f.delete. The result looks like this:

A little 'close window' button added to the title bar in twm.

A little ‘close window’ button added to the title bar in twm.

The icon for this is one I made myself.  twm icons are little xbm (X windows bit map) files.  The standard ones don’t always have the file name extension, and they live in

/usr/include/X11/bitmaps

or on cygwin on (64-bit) Windows:

C:\cygwin64\usr\include\X11\bitmaps

But by putting the full path into the file name, you can store them locally and not need to mess around in root.  The four buttons look like this: Close window button 1 for twm.  Close window button 2 for twm.  Close window button 3 for twm.  Close window button 4 for twm.

Now, I could not directly upload xbm files to this blog, so these images are gifs and will not work for twm. However, you can go here and get the icons in the file ‘close_buttons.zip‘. Alternatively, something like GIMP or ImageMagick can probably turn the gifs back into xbms — although I don’t know if they’ll have the right colour depth.

Then put them somewhere in your local filespace and put the path into your .twmrc file and there you have it, a neat little button to close the application/dialogue/whatever.

Blah blah de blah blah.

Today’s Useful Utility: SharpKeys

Getting rid of CAPSLOCK on Linux (assuming you’re using X) is really easy and can be done by a user without root access.

The SharpKeys screen.

The SharpKeys screen.

Not so simple in Windows, where you need to mess around with registry keys and the like. This can be done directly using regedit.exe, where if you dig down you can find the keyboard code for CLOCKSAP (003A) and futz around with it. I have a policy of not fooling about in the registry, any more than I like to play around as root on Linux, so I was hesitant to do this. If playing with the registry, it is also useful to have a detailed log file somewhere explaining to yourself what you did, and I can’t be bothered with that. I usually try to avoid leaning on downloaded utilities, and I always have my heart in my mouth when I let them access stuff, but I got sick of my typing chNGING TO CAPITALS WHEN I MEANT TO HIT THE ‘A’ key.

So I went here: http://sharpkeys.codeplex.com/ and downloaded and installed. See the screen grab above. Currently I have SLOKPACC recoded to give me ‘A’ since that it the key I am going for when I hit it by accident, but if that ends up producing too many instances of double-A in my text, I can set it to do nothing at all.

Oh, and the installer and stuff for SharpKeys may not mention Windows 10 directly, but I’ve used it with success on my Windows 10 netbook so it may be worth a try there too.

Non seq.