Set Title in postscript header, produced by IDL - ps

How to produce an postscript output in IDL such that the title window (i.e. the Title field in the header of the postscript file) is set to the filename or to an arbitrary string? By default, it is set to "Graphics produced by IDL":
%%Title: Graphics produced by IDL
Is is possible to change this from within IDL, without using scripts after the .ps is produced?
For reference, one may produce a .ps file in the following way.
PRO test_2
device,decomposed=0
set_plot, 'ps'
device,filename="~/filename.ps",/isolatin1,xsize=8.,ysize=10.5,inches=1,$
xoffset=0.25,yoffset=0.25,landscape=0
!p.font=0
device, /helvetica, font_size=7 ; a classic sans-serif font
;
x = indgen(100)
plot, x, sin(x)
;
device,/close
set_plot,'x'
!p.thick=1 & !p.charthick=1 & !p.font=-1 & !x.thick=1 & !y.thick=1
END

This is the nice thing about IDL - there is always a way!
After opening the PS-file with set_plot, do the following:
IDL> printf, 100, "%!PS-Adobe-3.0"
IDL> printf, 100, "%% Created with: IDL> "+(recall_commands())(0)
IDL> printf, 100, "%", form='(A0,$)'
The 1st line makes a new declaration that this is a PS-file. Check that this is the actual PS-version that your IDL version creates.
The 2nd line is your comment - this prints the command you used before opening the PS-file.
The 3rd line out-comments the PS-file declaration that IDL will print next. So there'll be two of those lines, with the second being a comment by way of the resulting "%%".
You might want to check that the LUN of PS-files is indeed 100.
Good luck!

You could open it like a text file, find the line containing %%Title:, modify it, and write the new file out. I don't think there's an easier way to do it without using external programs.

Related

How to do an incremental read of binary files

TL;DR: can I do an incremental read of binary files with Red or Rebol?
I would like to use Red to process some large (13MB to 2GB) structured binary files (Kurzweil synthesizer files). I've used other languages (C, Go, Tcl, Ruby, Dart) to walk through these files, and now I'd like to do the same with Red or Rebol.
Is there a way to incrementally read binary files, byte by byte? All I see is read/binary which seems to slurp the entire file at once (or a part of a file).
I'll need to jump around a little bit, too (either peek at the next byte, or skip to the end of a section, or skip past variable length strings to the start of data).
(Yes, I could make some helpers that tracked the position and used read/part/seek.)
I would like to make a call to the low level OS read/seek if that is possible - something new to learn.
This is on macos, but a portable solution would be great.
Thanks!
PS: "open/read %abc" gives an error "*** Script Error: open does not allow file! for its port argument", even though the help message say the port argument is "port [port! file! url! block!]"
Rebol has ports for that, which are planned for 0.7.0 release in Red. So, current I/O is very basic and buffer-only, and open is a preliminary stub.
I would like to make a call to the low level OS read/seek if that is possible - something new to learn.
You can leverage Rebol or Red/System FFI as a learning excercise.
Here is how you would do it in Rebol:
>> file: open/direct/binary %file.dat
>> until [none? probe copy/part file 20]
>> close file
#{732F7072696E74657253657474696E6773312E62}
#{696E504B01022D00140006000800000021006149}
#{0910890100001103000010000000000000000000}
...
#{000000006A290000646F6350726F70732F617070}
#{2E786D6C504B0506000000000D000D0068030000}
#{292C00000000}
none
first file or pick file 1 will return the next byte value (integer!)
This even works with text files: open/lines/direct, in that case copy/part file 20 will return 20 lines, or you can use pick file 1 or first file to get the next line.
Soon this will be available on Red too.

Adjust figure size in Scilab with xs2pdf function

I have written lines of Scilab code which generate a matrix. It is a function whose argument is a vector containing two positive integers and that returns a matrix of size the values of the vector, according to some algorithm. The function also exports the matrix to a figure in LaTeX style, thanks to the prettyprint function.
I would like that figure to be exported to a PDF file, for which I used the function xs2pdf. It works almost fine. The problem is, when serving its intended purpose, the function generates a matrix of size around 40x40, and it never fits on the page. It seems to me like the PDF document created is not even A4.
I didn't include the entire code, all you need to know is that the code generates a matrix named z, and then I have the lines :
//just for this post
z=rand(40,40)
//export to figure
A=prettyprint(z) ;
clf ;
xstring(0,0,A) ;
//export to PDF
xs2pdf(0, '_path_to_pdf_file') ;
The matrix z is created here in order to simulate the matrix that my programme actually generates. If you run this code, having filled in the '_path_to_pdf_file' bit, do you get a decent PDF output?
I could reproduced the same problem. Sometimes the PDF output is not even generated, and Scilab returns an error.
One workaround is to make Scilab create a new TeX file and compile it with pdflatex outside Scilab. The good part is that you can run everything from the same Scilab script. Of course, you'll need a LaTeX distribution installed.
r = 40; c = 40;
z = rand(r,c);
A = prettyprint(z) ;
texfile = "\documentclass{standalone}" + ...
"\usepackage{graphics}" + ...
"\usepackage{amsmath}" + ...
"\setcounter{MaxMatrixCols}{"+ string(c) +"}" + ...
"\begin{document}" + ...
A + ...
"\end{document}"
filename = "matrix.tex";
write(filename,texfile) //write() cannot overwrite a file
dos("pdflatex " + filename) //use unix() instead of dos() in case you're not on Windows
I don't know if you have any knowledge of LaTeX, so I should make a few notes:
The output goes to current Scilab directory. All auxiliary files produced by LaTeX will also be created there.
It uses the standalone class, which crops the PDF output exactly to whatever is described in the .tex file. In this case, only the matrix is printed, with no margins. To use this class, you need the standalone package for LaTeX.
prettystring() outputs the matrix using pmatrix environment, which requires the amsmath package, thus you need this one installed too.
The line \setcounter{MaxMatrixCols}{c} is needed in case you have a matrix with more than 10 columns.
Here is the output:

Rename ttf/woff/woff2 file to PostScript Font Name with Script

I am a typographer working with many fonts that have incorrect or incomplete filenames. I am on a Mac and have been using Hazel, AppleScript, and Automator workflows, attempting to automate renaming these files*. I require a script to replace the existing filename of ttf, woff, or woff2 files in Finder with the font's postscriptName. I know of tools (fc-scan/fontconfig, TTX, etc) which can retrieve the PostScript name-values I require, but lack the programming knowhow to code a script for my purposes. I've only managed to setup a watched directory that can run a script when any files matching certain parameters are added.
*To clarify, I am talking about changing the filename only, not the actual names stored within the font. Also I am open to a script of any compatible language or workflow of scripts if possible, e.g. this post references embedding AppleScript within Shell scripts via osascript.
StackExchange Posts I've Consulted:
How to get Fontname from OTF or TTF File?
How to get PostScript name of TTF font in OS X?
How to Change Name of Font?
Automate Renaming Files in macOS
Others:
https://github.com/dtinth/JXA-Cookbook/wiki/Using-JavaScript-for-Automation
https://github.com/fonttools/fonttools
https://github.com/devongovett/fontkit
https://www.npmjs.com/package/rename-js
https://opentype.js.org/font-inspector.html
http://www.fontgeek.net/blog/?p=343
https://www.lantean.co/osx-renaming-fonts-for-free
Edit: Added the following by request.
1) Screenshot of a somewhat typical webfont, illustrating how the form fields for font family and style names are often incomplete, blank, or contain illegal characters.
2) The woff file depicted (also, as base64).
Thank you all in advance!
Since you mentioned Automator in your question, I thought I'd try and solve this while using that to rename the file, along with standard Mac bash to get the font name. Hopefully, it beats learning a whole programming language.
I don't know what your workflow is so I'll leave any deviations to you but here is a method to select a font file and from Services, rename the file to the font's postscript name… based on Apple's metadata, specifically "com_apple_ats_name_postscript". This is one of the pieces of data retrieved using 'mdls' from the Terminal on the font file. To focus on the postscript name, grep the output for name_postscript. For simplicity here, I'll exclude the path to the selected file.
Font Name Aquisition
So… running this command…
mdls GenBkBasBI.ttf | grep -A1 name_postscript
… generates this output, which contains FontBook's Postscript name. The 'A1' in grep returns the found line and the first line after, which is the one containing the actual font name.
com_apple_ats_name_postscript = (
"GentiumBookBasic-BoldItalic"
Clean this up with some more bash (tr, tail)…
tr -d \ | tail -n 1 | tr -d \"
In order, these strip spaces, all lines excepting the last, and quotation marks. So for the first 'tr' instance, there is an extra space after the backslash.
In a single line, it looks like this…
mdls GenBkBasBI.ttf | grep -A1 name_postscript | tr -d \ | tail -n 1 | tr -d \"
…and produces this…
GentiumBookBasic-BoldItalic
Now, here is the workflow that includes the above bash command. I got the idea for variable usage from the answer to this question…
Apple Automator “New PDF from Images” maintaining same filename
Automator Workflow
Automator Workflow screenshot
At the top; Service receives selected 'files or folders' in 'Finder'.
Get Selected Finder Items
This (or Get Specified…) is there to allow testing. It is obviated by using this as a Service.
Set Value of Variable (File)
This is to remember which file you want to rename
Run Shell Script
This is where we use the bash stuff. The $f is the selected/specified file. I'm running 'zsh' for whatever reason. You can set it to whatever you're running, presumably 'bash'.
Set Value of Variable (Text)
Assign the bash output to a variable. This will be used by the last action for the new filename.
Get Value of Variable (File)
Recall the specified/selected file to rename.
Rename Finder Items: Name Single Item
I have it set to 'Basename only' so it will leave the extension alone. Enter the 'Text' variable from action 4 in here.

Moving the "cursor" back a line for stdout

I have a little command line tool (written in Objective C, runs under MacOS) that tracks changes to folders and applies rules to files. This tool also informs the user about the progress. It says like:
"Found 3 files of type Z and applied rule"
"Found 6 files of typ x and applied rules"
Currently, the tool outputs the feedback as an endless list but this does not look very handy. What I'm after is a solution to only type the line per file type once and then update the number in the terminal if the tool finds another file of that type. Very similar to how "top" under Unix gives the feedback.
However, to do so, I'll need to move the cursor in the terminal backwards to the beginning of the line and also one or multiple lines backwards.
Is this possible and does anybody know, how to do so?
Thanks
Norbert

Trying to read in a .gda file to IDL

I am trying to read in a .gda file into IDL for plotting purposes. I am not familiar with the format and my research indicates it is an unformatted binary data file type. Anyways, here is what I am doing:
pro omidi_contour
openr, 1, 'data.gda'
a = fltarr(128,128,128)
readu, 1, a
close, 1
end
However when I look at the variable definition at the left panel of IDL, it indicates that a is 'undefined'. When I try to print:
print, a[0,0,0]
I get:
Variable is undefined: A
How can I solve this?
I found out that there was nothing wrong with my program. It was reading the right values from the file. However, the IDL "forgot" the value of the variables once the program was done. Solution: DO not run this as a program i.e. remove the following lines:
pro omidi_contour
end
This makes the code to run as if each line were typed into the IDL prompt and IDL does remember the values this time round.