Index: /trunk/psModules/src/concepts/pmConceptsStandard.c
===================================================================
--- /trunk/psModules/src/concepts/pmConceptsStandard.c	(revision 23624)
+++ /trunk/psModules/src/concepts/pmConceptsStandard.c	(revision 23625)
@@ -33,4 +33,20 @@
 break;
 
+
+// Format type for time
+typedef enum {
+    TIME_FORMAT_ISO,                    // Date stored ISO (YYYY-MM-DD)
+    TIME_FORMAT_BACKWARDS,              // Date stored backwards (DD-MM-YYYY)
+    TIME_FORMAT_USA,                    // Date stored in USA order (MM-DD-YYYY)
+    TIME_FORMAT_JD,                     // Date stored as JD
+    TIME_FORMAT_MJD,                    // Date stored as MJD
+} conceptTimeFormatType;
+
+// Format for time
+typedef struct {
+    conceptTimeFormatType format;       // Format type for time
+    bool separate;                      // Date and time stored separately?
+    bool pre2000;                       // Year is pre-2000 (two digits only)?
+} conceptTimeFormat;
 
 
@@ -630,4 +646,45 @@
 }
 
+static conceptTimeFormat conceptGetTimeFormat(const char *name, // Concept name ("CELL.TIME" or "FPA.TIME")
+                                              const psMetadata *cameraFormat // Camera format
+    )
+{
+    conceptTimeFormat time;               // Time format, to return
+    time.format = TIME_FORMAT_ISO;
+    time.separate = false;
+    time.pre2000 = false;
+
+    bool mdok = true;                   // Status of MD lookup
+    psMetadata *formats = psMetadataLookupMetadata(&mdok, cameraFormat, "FORMATS"); // The formats
+    if (mdok && formats) {
+        psString format = psMetadataLookupStr(&mdok, formats, name); // The formats for eg, CELL.TIME
+        if (mdok && format && strlen(format) > 0) {
+            psList *formatList = psStringSplit(format, " ,;", false); // List of formats specified
+            psListIterator *formatListIter = psListIteratorAlloc(formatList, PS_LIST_HEAD, false); // Iterator
+            while ((format = psListGetAndIncrement(formatListIter))) {
+                if (strcasecmp(format, "SEPARATE") == 0) {
+                    time.separate = true;
+                } else if (strcasecmp(format, "USA") == 0) {
+                    time.format = TIME_FORMAT_USA;
+                } else if (strcasecmp(format, "BACKWARDS") == 0) {
+                    time.format = TIME_FORMAT_BACKWARDS;
+                } else if (strcasecmp(format, "PRE2000") == 0) {
+                    time.pre2000 = true;
+                } else if (strcasecmp(format, "MJD") == 0) {
+                    time.format = TIME_FORMAT_MJD;
+                } else if (strcasecmp(format, "JD") == 0) {
+                    time.format = TIME_FORMAT_JD;
+                } else {
+                    psWarning("Unrecognised FORMATS option for %s: %s --- ignored.", name, format);
+                }
+            }
+            psFree(formatListIter);
+            psFree(formatList);
+        }
+    }
+
+    return time;
+}
+
 // Determine the corresponding TIMESYS for one of the TIME concepts
 static psTimeType conceptGetTimesysForTime(const char *name, // Concept name ("CELL.TIME" or "FPA.TIME")
@@ -734,81 +791,10 @@
     psTimeType timeSys = conceptGetTimesysForTime(pattern->name, fpa, chip, cell); // Time system
 
-    // Work out how the time is represented
-    bool separateTime = false;
-    bool usaTime = false;               // Is the time specified in USA (MM-DD-YYYY) order?
-    bool backwardsTime = false;         // Is the time specified in backwards (DD-MM-YYYY) order?
-    bool yearFirst = false;            // Is the time specified in yearFirst (YYYY-MM-DD) order?
-    bool pre2000Time = false;           // Is the time specified pre-2000 (where the year only has two digits)
-    bool mjdTime = false;               // Is the time specified a MJD?
-    bool jdTime = false;                // Is the time specified a JD?
-
-    // Get format
-    bool mdok;                          // Status of MD lookup
-    psMetadata *formats = psMetadataLookupMetadata(&mdok, cameraFormat, "FORMATS");
-    if (!mdok || !formats) {
-        psError(PS_ERR_UNKNOWN, true, "Unable to find FORMATS in camera configuration.\n");
-        return NULL;
-    }
-
-    psString timeFormat = psMetadataLookupStr(&mdok, formats, pattern->name);
-    if (!mdok || strlen(timeFormat) == 0) {
-        psError(PS_ERR_UNKNOWN, true, "Unable to find %s in FORMATS.\n", pattern->name);
-        return NULL;
-    }
-
-    // Parse the time format
-    // why should more than one be allowed??
-    psList *timeFormats = psStringSplit(timeFormat, " ,;", false); // List of the format options
-    psListIterator *timeFormatsIter = psListIteratorAlloc(timeFormats, PS_LIST_HEAD, false); // Iter
-    while ((timeFormat = psListGetAndIncrement(timeFormatsIter))) {
-        if (strcasecmp(timeFormat, "SEPARATE") == 0) {
-            separateTime = true;
-        } else if (strcasecmp(timeFormat, "USA") == 0) {
-            usaTime = true;
-            backwardsTime = false;
-            yearFirst = false;
-            jdTime = false;
-            mjdTime = false;
-        } else if (strcasecmp(timeFormat, "BACKWARDS") == 0) {
-            backwardsTime = true;
-            usaTime = false;
-            yearFirst = false;
-            jdTime = false;
-            mjdTime = false;
-        } else if (strcasecmp(timeFormat, "YEAR.FIRST") == 0) {
-            yearFirst = true;
-            usaTime = false;
-            backwardsTime = false;
-            jdTime = false;
-            mjdTime = false;
-        } else if (strcasecmp(timeFormat, "PRE2000") == 0) {
-            pre2000Time = true;
-        } else if (strcasecmp(timeFormat, "MJD") == 0) {
-            mjdTime = true;
-            jdTime = false;
-            yearFirst = false;
-            backwardsTime = false;
-            usaTime = false;
-        } else if (strcasecmp(timeFormat, "JD") == 0) {
-            jdTime = true;
-            mjdTime = false;
-            yearFirst = false;
-            backwardsTime = false;
-            usaTime = false;
-        } else {
-            psError(PS_ERR_UNKNOWN, true, "Unrecognised FORMATS option for %s: %s --- "
-                    "ignored.\n", pattern->name, timeFormat);
-            psFree(timeFormatsIter);
-            psFree(timeFormats);
-            return NULL;
-        }
-    }
-    psFree(timeFormatsIter);
-    psFree(timeFormats);
+    conceptTimeFormat timeFormat = conceptGetTimeFormat(pattern->name, cameraFormat); // Format for time
 
     psTime *time = NULL;                // The time
     switch (concept->type) {
       case PS_DATA_LIST: {
-          if (!separateTime) {
+          if (!timeFormat.separate) {
               psWarning ("DATE and TIME stored separately, but not specified in format\n");
           }
@@ -837,19 +823,25 @@
               return NULL;
           }
-          if (backwardsTime) {
-              // Need to switch days and years
-              int temp = day;
-              day = year;
-              year = temp;
-          }
-          if (usaTime) {
-              // Need to switch everything around.... Yanks!
-              int temp = day;
-              day = month;
-              month = year;
-              year = temp;
+          switch (timeFormat.format) {
+            case TIME_FORMAT_BACKWARDS: {
+                // Need to switch days and years
+                int temp = day;
+                day = year;
+                year = temp;
+                break;
+            }
+            case TIME_FORMAT_USA: {
+                // Need to switch everything around.... Yanks!
+                int temp = day;
+                day = month;
+                month = year;
+                year = temp;
+                break;
+            }
+            default:
+              break;
           }
           if (year < 100) {
-              if (pre2000Time) {
+              if (timeFormat.pre2000) {
                   year += 1900;
               } else {
@@ -898,26 +890,38 @@
       case PS_DATA_STRING: {
           psString timeString = concept->data.V;   // String with the time
-          if (jdTime) {
-              double timeValue = strtod (timeString, NULL);
-              time = psTimeFromJD(timeValue);
-          } else if (mjdTime) {
-              double timeValue = strtod (timeString, NULL);
-              time = psTimeFromMJD(timeValue);
-          } else {
-              // It's ISO
-              time = psTimeFromISO(timeString, timeSys);
-          } // Interpreting the time string
+          switch (timeFormat.format) {
+            case TIME_FORMAT_JD: {
+                double timeValue = strtod (timeString, NULL);
+                time = psTimeFromJD(timeValue);
+                break;
+            }
+            case TIME_FORMAT_MJD: {
+                double timeValue = strtod (timeString, NULL);
+                time = psTimeFromMJD(timeValue);
+                break;
+            }
+            case TIME_FORMAT_ISO: {
+                // It's ISO
+                time = psTimeFromISO(timeString, timeSys);
+                break;
+            }
+            default:
+              psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Unable to interpret time string: %s", timeString);
+              return NULL;
+          }
           break;
       }
       case PS_TYPE_F32: {
           double timeValue = (double)concept->data.F32;
-          if (jdTime) {
+          switch (timeFormat.format) {
+            case TIME_FORMAT_JD:
               time = psTimeFromJD(timeValue);
-          } else if (mjdTime) {
+              break;
+            case TIME_FORMAT_MJD:
               time = psTimeFromMJD(timeValue);
-          } else {
-              psError(PS_ERR_UNKNOWN, true, "Not sure how to parse %s (%f) --- trying JD\n",
-                      pattern->name, timeValue);
-              time = psTimeFromJD(timeValue);
+              break;
+            default:
+              psError(PS_ERR_UNKNOWN, true, "Unable to interpret time %s (%f)", pattern->name, timeValue);
+              return NULL;
           }
           break;
@@ -925,12 +929,14 @@
       case PS_TYPE_F64: {
           double timeValue = (double)concept->data.F64;
-          if (jdTime) {
+          switch (timeFormat.format) {
+            case TIME_FORMAT_JD:
               time = psTimeFromJD(timeValue);
-          } else if (mjdTime) {
+              break;
+            case TIME_FORMAT_MJD:
               time = psTimeFromMJD(timeValue);
-          } else {
-              psError(PS_ERR_UNKNOWN, true, "Not sure how to parse %s (%f) --- trying JD\n",
-                      pattern->name, timeValue);
-              time = psTimeFromJD(timeValue);
+              break;
+            default:
+              psError(PS_ERR_UNKNOWN, true, "Unable to interpret time %s (%f)", pattern->name, timeValue);
+              return NULL;
           }
           break;
@@ -946,8 +952,13 @@
     }
 
-    if (jdTime || mjdTime) {
+    // Set the time system appropriately
+    switch (timeFormat.format) {
+      case TIME_FORMAT_JD:
+      case TIME_FORMAT_MJD:
         conceptSetTimesysForTime(pattern->name, fpa, chip, cell, PS_TIME_TAI);
-    } else {
+        break;
+      default:
         time->type = timeSys;
+        break;
     }
 
@@ -1211,64 +1222,7 @@
     psTimeConvert(time, timeSys);
 
-    // Work out the format
-    bool separateTime = false;          // Are the date and time stored separately?
-    bool pre2000Time = false;           // Is the year in pre-2000 format (two digits only)?
-    bool backwardsTime = false;         // Is the date stored backwards (DD-MM-YYYY)?
-    bool usaTime = false;               // Is the date stored in USA order (MM-DD-YYYY)?
-    bool jdTime = false;                // Is the date stored as a JD?
-    bool mjdTime = false;               // Is the date stored as a MJD?
-    bool yearFirst = false;
-
-    bool mdok = true;                   // Status of MD lookup
-    psMetadata *formats = psMetadataLookupMetadata(&mdok, cameraFormat, "FORMATS"); // The formats
-    if (mdok && formats) {
-        psString format = psMetadataLookupStr(&mdok, formats, concept->name); // The formats for eg, CELL.TIME
-        if (mdok && format && strlen(format) > 0) {
-            psList *formatList = psStringSplit(format, " ,;", false); // List of formats specified
-            psListIterator *formatListIter = psListIteratorAlloc(formatList, PS_LIST_HEAD, false); // Iterator
-            while ((format = psListGetAndIncrement(formatListIter))) {
-                if (strcasecmp(format, "SEPARATE") == 0) {
-                    separateTime = true;
-                } else if (strcasecmp(format, "USA") == 0) {
-                    usaTime = true;
-                    backwardsTime = false;
-                    jdTime = false;
-                    mjdTime = false;
-                } else if (strcasecmp(format, "BACKWARDS") == 0) {
-                    backwardsTime = true;
-                    usaTime = false;
-                    mjdTime = false;
-                    jdTime = false;
-                } else if (strcasecmp(format, "YEAR.FIRST") == 0) {
-                    yearFirst = true;
-                    usaTime = false;
-                    backwardsTime = false;
-                    jdTime = false;
-                    mjdTime = false;
-                } else if (strcasecmp(format, "PRE2000") == 0) {
-                    pre2000Time = true;
-                } else if (strcasecmp(format, "MJD") == 0) {
-                    mjdTime = true;
-                    usaTime = false;
-                    backwardsTime = false;
-                    jdTime = false;
-                    separateTime = false;
-                } else if (strcasecmp(format, "JD") == 0) {
-                    jdTime = true;
-                    usaTime = false;
-                    backwardsTime = false;
-                    mjdTime = false;
-                    separateTime = false;
-                } else {
-                    psWarning("Unrecognised FORMATS option for %s: %s --- "
-                              "ignored.\n", concept->name, format);
-                }
-            }
-            psFree(formatListIter);
-            psFree(formatList);
-        }
-    }
-
-    if (separateTime) {
+    conceptTimeFormat timeFormat = conceptGetTimeFormat(concept->name, cameraFormat); // Format for time
+
+    if (timeFormat.separate) {
         // We're working with two separate headers --- construct a list with the date and time separately
         psString dateTimeString = psTimeToISO(time); // String representation
@@ -1280,24 +1234,28 @@
         // Need to format the strings....
         // XXX: Couldn't be bothered doing these right now
-        if (pre2000Time) {
+        if (timeFormat.pre2000) {
             psError(PS_ERR_UNKNOWN, true, "Don't you realise it's the twenty-first century?\n");
             return NULL;
         }
-        if (backwardsTime) {
-            int day, month, year;
-            psTrace ("psModules.concepts", 5, "ISO time has year first, convert to DD-MM-YYYY");
-            sscanf (dateString, "%d-%d-%d", &year, &month, &day);
-            sprintf (dateString, "%02d-%02d-%04d", day, month, year);
-            // XXX fix this for str length
-        }
-        if (usaTime) {
-            int day, month, year;
-            psTrace ("psModules.concepts", 5, "ISO time has year first, convert to MM-DD-YYYY");
-            sscanf (dateString, "%d-%d-%d", &year, &month, &day);
-            sprintf (dateString, "%02d-%02d-%04d", month, day, year);
-            // XXX fix this for str length
-        }
-        if (yearFirst) {
-            psTrace ("psModules.concepts", 5, "ISO time has year first, no adjustment needed");
+
+        switch (timeFormat.format) {
+          case TIME_FORMAT_BACKWARDS: {
+              int day, month, year;
+              psTrace ("psModules.concepts", 5, "ISO time has year first, convert to DD-MM-YYYY");
+              sscanf (dateString, "%d-%d-%d", &year, &month, &day);
+              sprintf (dateString, "%02d-%02d-%04d", day, month, year);
+              // XXX fix this for str length
+              break;
+          }
+          case TIME_FORMAT_USA: {
+              int day, month, year;
+              psTrace ("psModules.concepts", 5, "ISO time has year first, convert to MM-DD-YYYY");
+              sscanf (dateString, "%d-%d-%d", &year, &month, &day);
+              sprintf (dateString, "%02d-%02d-%04d", month, day, year);
+              // XXX fix this for str length
+              break;
+          }
+          default:
+            break;
         }
 
@@ -1313,25 +1271,31 @@
         psListAdd(dateTime, PS_LIST_TAIL, timeItem);
 
-        psMetadataItem *item = psMetadataItemAllocPtr(concept->name, PS_DATA_LIST, concept->comment,
-                                                      dateTime);
-        psFree (dateItem);
-        psFree (timeItem);
-        psFree (dateTime);
+        psMetadataItem *item = psMetadataItemAllocPtr(concept->name, PS_DATA_LIST,
+                                                      concept->comment, dateTime);
+        psFree(dateItem);
+        psFree(timeItem);
+        psFree(dateTime);
         return item;
     }
-    if (jdTime) {
-        double jd = psTimeToMJD(time);
-        return psMetadataItemAllocF64(concept->name, concept->comment, jd);
-    }
-    if (mjdTime) {
-        double mjd = psTimeToMJD(time);
-        return psMetadataItemAllocF64(concept->name, concept->comment, mjd);
-    }
-
-    // If we've gotten this far, so it's straight ISO.
-    psString dateTimeString = psTimeToISO(time); // String representation
-    psMetadataItem *item = psMetadataItemAllocStr(concept->name, concept->comment, dateTimeString);
-    psFree(dateTimeString);
-    return item;
+
+    switch (timeFormat.format) {
+      case TIME_FORMAT_JD: {
+          double jd = psTimeToMJD(time);
+          return psMetadataItemAllocF64(concept->name, concept->comment, jd);
+      }
+      case TIME_FORMAT_MJD: {
+          double mjd = psTimeToMJD(time);
+          return psMetadataItemAllocF64(concept->name, concept->comment, mjd);
+      }
+      default: {
+          // If we've gotten this far, it's straight ISO.
+          psString dateTimeString = psTimeToISO(time); // String representation
+          psMetadataItem *item = psMetadataItemAllocStr(concept->name, concept->comment, dateTimeString);
+          psFree(dateTimeString);
+          return item;
+      }
+    }
+
+    return NULL;
 }
 
