Batch edit fits headers

I have a bunch of darks that mistakenly have the OBJECT keyword as M1.  Is there a way to batch edit the keyword for a group of files rather than one at a time?

Thanks,

Jeff

 
I use this one:  <link removed>

EDITED BY THE FORUM ADMINISTRATOR
Please do not advertise other applications on this forum.
 
Hi Jeff!
No need to do software jumping and loose data precision if you have PI.
PI can cover all your processing needs.

Your solution:
Create an Image Container in wich you load the images with the OBJECT Name 'M1' in the Fits Header !!

Load ONLY one of the images with "M1" into your Workspace.
Select  File / Fits Header

At the bottom set
Name:  OBJECT
Value:  Dark
Comment: Updated by me

Click the line with OBJECT 'M1'
Click the Replace button  (on the bottom of the FITSHeader process window)
        This changes the value of OBJECT in the FITSHeader process window to 'Dark'.

Move and drop the blue triangle of the FITSHeader process to your Workspace
      this get you a FitsHeader process icon on the Workspace.

Finally move the blue triangle of the Image Container with all your M1 images and drop it
over the FitsHeader process icon

All now new created images will be replaced in the Fits Header with the text you entered.

Gerald
 
Since this is a recurring question, here is a little script to change a single keyword in a set of FITS files:

Code:
/*
 * Script to change a keyword value in a set of FITS files.
 */
var inputDirectory = "";
var outputDirectory = "";
var keywordName = "";
var oldKeywordValue = "";
var newKeywordValue = "";

// ----------------------------------------------------------------------------

if ( inputDirectory.length == 0 )
   throw new Error( "Must specify an input directory" ); 
if ( outputDirectory.length == 0 )
   throw new Error( "Must specify an output directory" ); 
if ( keywordName.length == 0 )
   throw new Error( "Must specify a keyword name" ); 
if ( oldKeywordValue.length == 0 || newKeywordValue.length == 0 )
   throw new Error( "Must specify old and new keyword values" ); 

if ( !inputDirectory.endsWith( '/' ) )
   inputDirectory += '/';
if ( !outputDirectory.endsWith( '/' ) )
   outputDirectory += '/';

for ( let D = searchDirectory( inputDirectory + "*.fit", false/*recursive*/ ), i = 0; i < D.length; ++i )
{
   let w = ImageWindow.open( D[i] );
   let k = w[0].keywords;
   let found = false;
   for ( let j = 0; j < k.length; ++j )
   {
      k[j].trim();
      if ( k[j].name == keywordName )
         if ( k[j].strippedValue == oldKeywordValue )
         {
            k[j].value = newKeywordValue;
            found = true;
            break;
         }
   }
   if ( found )
   {
      console.writeln( "<end><cbr>" + D[i] );
      w[0].keywords = k;
      w[0].saveAs( outputDirectory + File.extractNameAndSuffix( D[i] ),
                   false/*queryOptions*/,
                   false/*allowMessages*/,
                   false/*strict*/,
                   true/*verifyOverwrite*/ );
   }
   w[0].forceClose();
}

Just enter the appropriate script parameters and run it from Script Editor. For example:

Code:
var inputDirectory = "/path/to/images";
var outputDirectory = "/path/to/images/test";
var keywordName = "OBJECT";
var oldKeywordValue = "M1";
var newKeywordValue = "XXX";

This would change all OBJECT keyword values from 'M1' to 'XXX' in all existing FITS files on a folder '/path/to/images' (replace it with the path to an actual folder on your system). The output directory must exist, and if it is the same as the input directory, existing files will be replaced (you will be asked for confirmation in such case). I have made a few tests and the script works well; however, use it at your own risk, of course. Everybody: feel free to improve it and/or adapt it to your specific needs.
 
For those having troubles with the script - like i do -
use the existing simple working solution
with
FitsHeader process
and Image Container.

Gerald
 
Gerald, wouldn't that overwrite the entire fits header of each image in the ImageContainer?

rob
 
In my first test it looked okay.
After your note i tried it again and got an overwrite
of all selected Image with the sam FitsHeader.

Have to check again - could be i was NOT precise enough...

Gerald
 
Hi Rob!
You are right. The ImageContainer trick would
overwrite all other data in the FITS Header including the OBJECT data also !!!

So, i have to apologize for not testing my advice 100% correct.

For the case to get the darks re named to dark in the "object" Fits Column it would be good enough,
but when the script will work it should be better.

Ash on my head.

Gerald
 
OK thanks for checking it - i was fairly certain it would all be overwritten but had not tried it in a long time. was not sure what would happen...

rob
 
Juan Conejero said:
What's the problem with the script? Have you tried it?
Hi Juan!
The issue was between chair and keyboard...

First didnt enter the pathname in script. After first run with error abend
i couldnot restart script with F9. Had to remove and reload.
You never can imagine how some users like me are ticking.
Sorry
Gerald
 
Hi, the script with the interface has been uploaded to PixInsight Resources repository. If you do not have it already configured see here how to add the repository and download the script.

Saludos, Alejandro.
 
Hi Mike,

You can download it (and others) by setting a new repository directly on PI (see Alejandro's link), or if you prefer, I'm attaching the zipped script for download.

By the way, script's credits are for Juan, I just added the GUI. To all: feel free to change the script to improve the GUI or add some features.

Greetings,

Enzo.
 

Attachments

  • BatchEditFITSSingleKeyword.png
    BatchEditFITSSingleKeyword.png
    11.3 KB · Views: 373
  • BatchEditFITSSingleKeyword.zip
    2.5 KB · Views: 350
Hello All,

I have been following this closely with interest. I have a lot of flat files that don't have any filter type in the fits header so tonight I tried the script.
It failed as my entry for old keyword value is blank (see screen capture) and the net result it did not work.
I tried with another key word value like OBSERVER and this had a value already and I could change this successfully with the script.

So it wouldn't work with blank fields - any ideas gents

Thanks for the efforts all with this development.

Regards
Martin
 

Attachments

  • Screen Shot 2016-03-24 at 21.55.17.png
    Screen Shot 2016-03-24 at 21.55.17.png
    48.9 KB · Views: 191
Hi Martin, please test the script variation attached. I have removed the requirement of that value, but I'm not sure you have some unexpected behavior...

Greetings,

Enzo.
 

Attachments

  • BatchEditFITSSingleKeyword_101.zip
    2.5 KB · Views: 361
Hello Enzo,

Yes I can confirm that this latest version you have kindly posted works with blank entries as a value you need to change.
I have very quickly modified my collection of Flat file headers to now include the filter.


Thank you for your efforts, and indeed all who have contributed.

Martin
 
Back
Top