
  /* Convert the output of MTZtovarious to other formats */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define HKL 0
#define FOB 1
#define FIN 2
#define SCA 3
#define CNS 4


  /* Case-insensitive strcmp */

int cistrcmp (const char *s1, const char *s2)
{
  while (*s1 && toupper (*s1) == toupper (*s2))
  { 
    s1++;
    s2++;
  }

  return toupper (*s1) - toupper (*s2);
}


int main (int argc, char * argv [])
{
  int h, k, l, isym;

  double F [3], S [3], I [3], SI [3], T;

  char line [512], *c;

  int anomalous, mode; 
  
  anomalous = 0;

  mode = HKL;

  while (--argc)
  {
    c = *++argv;

    if (*c == '-')
    {
      c++;

      if (cistrcmp (c, "anomalous") == 0)

        anomalous = 1;
    
      if (cistrcmp (c, "shelx") == 0 ||
          cistrcmp (c, "hkl")   == 0)

        mode = HKL;
    
      if (cistrcmp (c, "xplor") == 0 ||
          cistrcmp (c, "fob")   == 0)

        mode = FOB;
    
      if (cistrcmp (c, "xtalview") == 0 ||
          cistrcmp (c, "fin")      == 0)

        mode = FIN;

      if (cistrcmp (c, "scalepack") == 0 ||
          cistrcmp (c, "sca")       == 0)

        mode = SCA;

      if (cistrcmp (c, "cns") == 0)

        mode = CNS;
    }
  }

  if (mode == FOB)
  {
    if (anomalous)
  
      printf (" ANOMALOUS=TRUE\n");

    else

      printf (" ANOMALOUS=FALSE\n");

    printf (" DECLARE NAME=FOBS    DOMAIN=RECIPROCAL   TYPE=COMP END\n");
    printf (" DECLARE NAME=SIGMA   DOMAIN=RECIPROCAL   TYPE=REAL END\n");
    printf (" DECLARE NAME=TEST    DOMAIN=RECIPROCAL   TYPE=INTE END\n");
  }

  if (mode == CNS)
  {
    if (anomalous)
  
      printf (" ANOMalous=TRUE\n");

    else

      printf (" ANOMalous=FALSe\n");

    printf (" DECLare NAME=IOBS    DOMAIN=RECIprocal   TYPE=REAL END\n");
    printf (" DECLare NAME=ISIGMA  DOMAIN=RECIprocal   TYPE=REAL END\n");
    printf (" DECLare NAME=FOBS    DOMAIN=RECIprocal   TYPE=REAL END\n");
    printf (" DECLare NAME=SIGMA   DOMAIN=RECIprocal   TYPE=REAL END\n");
    printf (" DECLare NAME=TEST    DOMAIN=RECIprocal   TYPE=INTE END\n");
  }

  if (mode == SCA)
  {
    printf ("    0\n");
  }

  while (gets (line))
  {
    S [2] = SI [2] = 2E6;
    
    T = 1;

    sscanf (line, " %d %d %d %d %lf %lf %lf %lf %lf %lf"
                              " %lf %lf %lf %lf %lf %lf %lf",
                    &h, &k, &l, &isym, &F [0], &S  [0], 
                                       &F [1], &S  [1], 
                                       &F [2], &S  [2],
                                       &I [0], &SI [0],
                                       &I [1], &SI [1],
                                       &I [2], &SI [2], &T); 

        /* Valid combinations are: Centric:    isym 0  mask 6
                                   Noncentric: isym 0  mask 0
                                               isym 1  mask 6
                                               isym 2  mask 6 */

    if ((mode == HKL && SI [2] < 1E6) ||
        (mode == SCA && SI [2] < 1E6) ||
        (mode == CNS && SI [2] < 1E6) ||
        (mode == FOB && S  [2] < 1E6) ||
        (mode == FIN && S  [2] < 1E6))
    {
      int mask, c;

      mask = 0;

      for (c = 0; c < 3; c++)

        if ((mode == HKL && (I [c] == 0 && SI [c] == 0)) ||
            (mode == SCA && (I [c] == 0 && SI [c] == 0)) ||
            (mode == CNS && (I [c] == 0 && SI [c] == 0)) ||
            (mode == FOB && (F [c] == 0 && S  [c] == 0)) ||
            (mode == FIN && (F [c] == 0 && S  [c] == 0)))

          mask |= (1 << c);

      if (mask == 4 || mask == 2)
      
        mask = 6;

      if (!anomalous)
      {
        isym = 0;
        
        mask |= 6;
      }


        /* Valid combinations are: Centric:    isym 0  mask 6
                                   Noncentric: isym 0  mask 0
                                               isym 1  mask 6
                                               isym 2  mask 6 */

      if ((isym == 0 && mask == 6) ||
          (isym == 0 && mask == 0) ||
          (isym == 1 && mask == 6) ||
          (isym == 2 && mask == 6))
      {
        if (mask == 6)
        {
          if (isym == 0 || isym == 1)
          {
            F  [1] = F  [0];
            I  [1] = I  [0];
            S  [1] = S  [0];
            SI [1] = SI [0];

            F  [2] = 0;
            I  [2] = 0;
            S  [2] = 9999;
            SI [2] = 9999;

            mask = 4;
          }
          else
          {
            F  [1] = 0;
            I  [1] = 0;
            S  [1] = 9999;
            SI [1] = 9999;

            F  [2] = F  [0];
            I  [2] = I  [0];
            S  [2] = S  [0];
            SI [2] = SI [0];

            mask = 2;
          }
        }

        if (mode == HKL)
        
          for (c = 1; c < 3; c++)
          {
            if ((mask & (1 << c)) == 0)
            {
              printf ("%4d%4d%4d", h, k, l);

              if (I [c] > -999.99 && I [c] <  9999.99)

                printf ("%8.2f", I [c]);

              else

                printf ("%8.0f", I [c]);

              if (SI [c] > -999.99 && SI [c] <  9999.99)

                printf ("%8.2f", SI [c]);

              else

                printf ("%8.0f", SI [c]);
                
              printf ("%4d\n",  1 - 2 * (T < 1));
            }

            h = -h;
            k = -k;
            l = -l;
          }

        if (mode == SCA)
        {
          printf ("%4d%4d%4d", h, k, l);

          for (c = 1; c < 3; c++)

            if ((mask & (1 << c)) == 0)
            {
              if (I [c] > -999.99 && I [c] <  9999.99)

                printf ("%8.2f", I [c]);

              else

                printf ("%8.0f", I [c]);

              if (SI [c] > -999.99 && SI [c] <  9999.99)

                printf ("%8.2f", SI [c]);

              else

                printf ("%8.0f", SI [c]);
            }
            else
            
              if (c < 2 || isym != 0)
              {
                printf ("       0       0");
              }
          
          printf ("\n");
        }

        if (mode == CNS)
        {
          for (c = 1; c < 3; c++)
          {
            if ((mask & (1 << c)) == 0)

              printf (" INDEX=%4d%4d%4d IOBS=%10.2f ISIGMA=%10.2f FOBS=%10.2f SIGMA=%10.2f TEST= %1d\n",
                        h, k, l, I [c], SI [c], F [c], S [c], T < 1);

            h = -h;
            k = -k;
            l = -l;
          }
        }

        if (mode == FOB)
        {
          for (c = 1; c < 3; c++)
          {
            if ((mask & (1 << c)) == 0)

              printf (" INDEX=%4d%4d%4d FOBS=%10.2f SIGMA=%10.2f TEST= %1d\n",
                        h, k, l, F [c], S [c], T < 1);

            h = -h;
            k = -k;
            l = -l;
          }
        }

        if (mode == FIN)
        
          printf (" %4d %4d %4d %8.3f %8.3f %8.3f %8.3f\n", 
                    h, k, l, F [1], S [1], F [2], S [2]);
      }
    }
  }

  if (mode == HKL)
  
    printf ("   0   0   0    0.00    0.00   1\n");
    
  return 0;
}
