Author Topic: Batch edit fits headers  (Read 7865 times)

Offline JeffSeivert

  • Newcomer
  • Posts: 7
Batch edit fits headers
« on: 2016 March 16 08:45:36 »
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


Offline rbotero

  • Newcomer
  • Posts: 36
    • http://www.astrobin.com/users/rbotero/
Re: Batch edit fits headers
« Reply #1 on: 2016 March 22 06:27:44 »
I use this one:  <link removed>

EDITED BY THE FORUM ADMINISTRATOR
Please do not advertise other applications on this forum.
« Last Edit: 2016 March 22 09:43:51 by Pleiades »

Offline oldwexi

  • PixInsight Guru
  • ****
  • Posts: 627
    • Astronomy Pages G.W.
Re: Batch edit fits headers
« Reply #2 on: 2016 March 22 09:38:30 »
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
« Last Edit: 2016 March 22 10:15:40 by oldwexi »

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Batch edit fits headers
« Reply #3 on: 2016 March 22 10:54:27 »
Since this is a recurring question, here is a little script to change a single keyword in a set of FITS files:

Code: [Select]
/*
 * 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:

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.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline oldwexi

  • PixInsight Guru
  • ****
  • Posts: 627
    • Astronomy Pages G.W.
Re: Batch edit fits headers
« Reply #4 on: 2016 March 22 13:25:33 »
For those having troubles with the script - like i do -
use the existing simple working solution
with
FitsHeader process
and Image Container.

Gerald
« Last Edit: 2016 March 22 13:34:11 by oldwexi »

Offline pfile

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 4729
Re: Batch edit fits headers
« Reply #5 on: 2016 March 22 14:41:17 »
Gerald, wouldn't that overwrite the entire fits header of each image in the ImageContainer?

rob

Offline oldwexi

  • PixInsight Guru
  • ****
  • Posts: 627
    • Astronomy Pages G.W.
Re: Batch edit fits headers
« Reply #6 on: 2016 March 22 16:05:44 »
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
« Last Edit: 2016 March 22 16:14:34 by oldwexi »

Offline oldwexi

  • PixInsight Guru
  • ****
  • Posts: 627
    • Astronomy Pages G.W.
Re: Batch edit fits headers
« Reply #7 on: 2016 March 22 16:28:26 »
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

Offline pfile

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 4729
Re: Batch edit fits headers
« Reply #8 on: 2016 March 22 21:34:04 »
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

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Batch edit fits headers
« Reply #9 on: 2016 March 23 01:46:52 »
What's the problem with the script? Have you tried it?
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Enzo De Bernardini

  • PTeam Member
  • PixInsight Addict
  • ***
  • Posts: 274
  • Resistance is futile.
    • Astronomí­a Sur
Re: Batch edit fits headers
« Reply #10 on: 2016 March 23 09:31:18 »
Hey! We are working on a (simple) friendly GUI based on Juan's script. Stay tuned  ;)

Offline oldwexi

  • PixInsight Guru
  • ****
  • Posts: 627
    • Astronomy Pages G.W.
Re: Batch edit fits headers
« Reply #11 on: 2016 March 23 11:32:56 »
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

Offline Alejandro Tombolini

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1267
    • Próxima Sur
Re: Batch edit fits headers
« Reply #12 on: 2016 March 23 15:48:09 »
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.

Offline JeffSeivert

  • Newcomer
  • Posts: 7
Re: Batch edit fits headers
« Reply #13 on: 2016 March 23 18:18:55 »
Thank you all for the discussion and solutions!  I will give them a try.

Thanks,

Jeff


Offline msmythers

  • PTeam Member
  • PixInsight Jedi
  • *****
  • Posts: 1178
    • astrobin
Re: Batch edit fits headers
« Reply #14 on: 2016 March 23 20:18:20 »
Alejandro

Could you give a link to the new script with interface.

Thanks
Mike