Index: /trunk/psModules/src/camera/pmFPAfile.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfile.c	(revision 19006)
+++ /trunk/psModules/src/camera/pmFPAfile.c	(revision 19007)
@@ -13,4 +13,5 @@
 #include "pmHDU.h"
 #include "pmFPA.h"
+#include "pmHDUUtils.h"
 #include "pmFPALevel.h"
 #include "pmFPAview.h"
@@ -182,133 +183,145 @@
     newName = psStringCopy(rule);
 
-    if (strstr (newName, "{FPA.OBS}") != NULL) {
-        char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.OBS");
-        if (name != NULL) {
+    if (strstr(newName, "{FPA.OBS}")) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.OBS");
+        if (name) {
             psStringSubstitute(&newName, name, "{FPA.OBS}");
         }
     }
-    if (strstr (newName, "{FPA.NAME}") != NULL) {
-        char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.NAME");
-        if (name != NULL) {
+    if (strstr(newName, "{FPA.NAME}")) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.NAME");
+        if (name) {
             psStringSubstitute(&newName, name, "{FPA.NAME}");
         }
     }
-    if (strstr (newName, "{CHIP.NAME}") != NULL) {
-        pmChip *chip = pmFPAviewThisChip (view, fpa);
-        if (chip != NULL) {
-            char *name = psMetadataLookupStr (NULL, chip->concepts, "CHIP.NAME");
-            if (name != NULL) {
+    if (strstr(newName, "{CHIP.NAME}")) {
+        pmChip *chip = pmFPAviewThisChip(view, fpa);
+        if (chip) {
+            char *name = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME");
+            if (name) {
                 psStringSubstitute(&newName, name, "{CHIP.NAME}");
             }
         }
     }
-    if (strstr (newName, "{CHIP.ID}") != NULL) {
-        pmChip *chip = pmFPAviewThisChip (view, fpa);
-        if (chip != NULL) {
-            char *name = psMetadataLookupStr (NULL, chip->concepts, "CHIP.ID");
-            if (name != NULL) {
+    if (strstr(newName, "{CHIP.ID}")) {
+        pmChip *chip = pmFPAviewThisChip(view, fpa);
+        if (chip) {
+            char *name = psMetadataLookupStr(NULL, chip->concepts, "CHIP.ID");
+            if (name) {
                 psStringSubstitute(&newName, name, "{CHIP.ID}");
             }
         }
     }
-    if (strstr (newName, "{CHIP.N}") != NULL) {
+    if (strstr(newName, "{CHIP.N}")) {
         char *name = NULL;
         if (view->chip < 0) {
-            psStringAppend (&name, "XX");
+            psStringAppend(&name, "XX");
         } else {
-            psStringAppend (&name, "%02d", view->chip);
+            psStringAppend(&name, "%02d", view->chip);
         }
         psStringSubstitute(&newName, name, "{CHIP.N}");
-        psFree (name);
-    }
-    if (strstr (newName, "{CHIP.NUM}") != NULL) {
+        psFree(name);
+    }
+    if (strstr(newName, "{CHIP.NUM}")) {
         char *name = NULL;
         if (view->chip < 0) {
-            psStringAppend (&name, "XX");
+            psStringAppend(&name, "XX");
         } else {
-            psStringAppend (&name, "%d", view->chip);
+            int chipNum = view->chip;   // Number of chip
+            // Potential correction for fortran (unit-indexed) numbering
+            pmChip *chip = pmFPAviewThisChip(view, fpa); // Chip of interest
+            pmHDU *hdu = pmHDUFromChip(chip); // Corresponding HDU
+            bool mdok;                  // Status of MD lookup
+            psMetadata *formats = psMetadataLookupMetadata(&mdok, hdu->format, "FORMATS"); // Special formats
+            if (mdok && formats) {
+                const char *format = psMetadataLookupStr(&mdok, formats, "CHIP.NUM"); // Format for CHIP.NUM
+                if (mdok && format && strcmp(format, "FORTRAN") == 0) {
+                    chipNum++;
+                }
+            }
+            psStringAppend(&name, "%d", chipNum);
         }
         psStringSubstitute(&newName, name, "{CHIP.NUM}");
-        psFree (name);
-    }
-    if (strstr (newName, "{CELL.NAME}") != NULL) {
-        pmCell *cell = pmFPAviewThisCell (view, fpa);
-        if (cell != NULL) {
-            char *name = psMetadataLookupStr (NULL, cell->concepts, "CELL.NAME");
-            if (name != NULL) {
+        psFree(name);
+    }
+    if (strstr(newName, "{CELL.NAME}")) {
+        pmCell *cell = pmFPAviewThisCell(view, fpa);
+        if (cell) {
+            char *name = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME");
+            if (name) {
                 psStringSubstitute(&newName, name, "{CELL.NAME}");
             }
         }
     }
-    if (strstr (newName, "{CELL.N}") != NULL) {
+    if (strstr(newName, "{CELL.N}")) {
         char *name = NULL;
         if (view->cell < 0) {
-            psStringAppend (&name, "XX");
+            psStringAppend(&name, "XX");
         } else {
-            psStringAppend (&name, "%02d", view->cell);
+            psStringAppend(&name, "%02d", view->cell);
         }
         psStringSubstitute(&newName, name, "{CELL.N}");
     }
-    if (strstr (newName, "{CELL.NUM}") != NULL) {
+    if (strstr(newName, "{CELL.NUM}")) {
         char *name = NULL;
         if (view->cell < 0) {
-            psStringAppend (&name, "XX");
+            psStringAppend(&name, "XX");
         } else {
-            psStringAppend (&name, "%d", view->cell);
+            int cellNum = view->cell;   // Number of cell
+            // Potential correction for fortran (unit-indexed) numbering
+            pmCell *cell = pmFPAviewThisCell(view, fpa); // Cell of interest
+            pmHDU *hdu = pmHDUFromCell(cell); // Corresponding HDU
+            bool mdok;                  // Status of MD lookup
+            psMetadata *formats = psMetadataLookupMetadata(&mdok, hdu->format, "FORMATS"); // Special formats
+            if (mdok && formats) {
+                const char *format = psMetadataLookupStr(&mdok, formats, "CELL.NUM"); // Format for CELL.NUM
+                if (mdok && format && strcmp(format, "FORTRAN") == 0) {
+                    cellNum++;
+                }
+            }
+            psStringAppend(&name, "%d", cellNum);
         }
         psStringSubstitute(&newName, name, "{CELL.NUM}");
     }
-    if (strstr (newName, "{EXTNAME}") != NULL) {
-        pmHDU *hdu = pmFPAviewThisHDU (view, fpa);
+    if (strstr(newName, "{EXTNAME}")) {
+        pmHDU *hdu = pmFPAviewThisHDU(view, fpa);
         if (hdu->extname && *hdu->extname) {
             psStringSubstitute(&newName, hdu->extname, "{EXTNAME}");
         }
     }
-    if (strstr (newName, "{FILTER}") != NULL) {
-        if (fpa != NULL) {
-            char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.FILTER");
-            if (name && *name) {
-                psStringSubstitute(&newName, name, "{FILTER}");
-            }
-        }
-    }
-    if (strstr (newName, "{FILTER.ID}") != NULL) {
-        if (fpa != NULL) {
-            char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.FILTERID");
-            if (name && *name) {
-                psStringSubstitute(&newName, name, "{FILTER.ID}");
-            }
-        }
-    }
-    if (strstr (newName, "{CAMERA}") != NULL) {
-        if (fpa != NULL) {
-            char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.INSTRUMENT");
-            if (name && *name) {
-                psStringSubstitute(&newName, name, "{CAMERA}");
-            }
-        }
-    }
-    if (strstr (newName, "{INSTRUMENT}") != NULL) {
-        if (fpa != NULL) {
-            char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.INSTRUMENT");
-            if (name && *name) {
-                psStringSubstitute(&newName, name, "{INSTRUMENT}");
-            }
-        }
-    }
-    if (strstr (newName, "{DETECTOR}") != NULL) {
-        if (fpa != NULL) {
-            char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.DETECTOR");
-            if (name && *name) {
-                psStringSubstitute(&newName, name, "{DETECTOR}");
-            }
-        }
-    }
-    if (strstr (newName, "{TELESCOPE}") != NULL) {
-        if (fpa != NULL) {
-            char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.TELESCOPE");
-            if (name && *name) {
-                psStringSubstitute(&newName, name, "{TELESCOPE}");
-            }
+    if (strstr(newName, "{FILTER}") && fpa) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.FILTER");
+        if (name && *name) {
+            psStringSubstitute(&newName, name, "{FILTER}");
+        }
+    }
+    if (strstr(newName, "{FILTER.ID}") && fpa) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.FILTERID");
+        if (name && *name) {
+            psStringSubstitute(&newName, name, "{FILTER.ID}");
+        }
+    }
+    if (strstr(newName, "{CAMERA}") && fpa) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.INSTRUMENT");
+        if (name && *name) {
+            psStringSubstitute(&newName, name, "{CAMERA}");
+        }
+    }
+    if (strstr(newName, "{INSTRUMENT}") && fpa) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.INSTRUMENT");
+        if (name && *name) {
+            psStringSubstitute(&newName, name, "{INSTRUMENT}");
+        }
+    }
+    if (strstr(newName, "{DETECTOR}") && fpa) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.DETECTOR");
+        if (name && *name) {
+            psStringSubstitute(&newName, name, "{DETECTOR}");
+        }
+    }
+    if (strstr(newName, "{TELESCOPE}") && fpa) {
+        char *name = psMetadataLookupStr(NULL, fpa->concepts, "FPA.TELESCOPE");
+        if (name && *name) {
+            psStringSubstitute(&newName, name, "{TELESCOPE}");
         }
     }
