Batch edit fits headers

JeffSeivert

Member
Nov 12, 2015
7
0
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

 

oldwexi

Well-known member
Mar 7, 2010
659
13
75
Austria
www.werbeagentur.org
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
 

Juan Conejero

PixInsight Staff
Sep 2, 2004
8,719
714
58
Valencia, Spain
pixinsight.com
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.
 
  • Like
Reactions: rgooday1

pfile

PTeam Member
Nov 23, 2009
7,792
488
Gerald, wouldn't that overwrite the entire fits header of each image in the ImageContainer?

rob
 

oldwexi

Well-known member
Mar 7, 2010
659
13
75
Austria
www.werbeagentur.org
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
 

oldwexi

Well-known member
Mar 7, 2010
659
13
75
Austria
www.werbeagentur.org
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
 
  • Like
Reactions: jkovach

pfile

PTeam Member
Nov 23, 2009
7,792
488
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
 

oldwexi

Well-known member
Mar 7, 2010
659
13
75
Austria
www.werbeagentur.org
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
 

enzodb

PixInsight Ambassador
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

  • Like
Reactions: jkovach

Martin Farmer

Active member
Feb 6, 2011
33
0
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

Martin Farmer

Active member
Feb 6, 2011
33
0
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