1 /*
2      cmap_close.c: close map file
3      Copyright (C) 2001  CCLRC, Charles Ballard
4 
5      This library is free software: you can redistribute it and/or
6      modify it under the terms of the GNU Lesser General Public License
7      version 3, modified in accordance with the provisions of the
8      license to address the requirements of UK law.
9 
10      You should have received a copy of the modified GNU Lesser General
11      Public License along with this library.  If not, copies may be
12      downloaded from http://www.ccp4.ac.uk/ccp4license.php
13 
14      This program is distributed in the hope that it will be useful,
15      but WITHOUT ANY WARRANTY; without even the implied warranty of
16      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17      GNU Lesser General Public License for more details.
18 */
19 #include <string.h>
20 #include <math.h>
21 #include <stdarg.h>
22 #include "cmaplib.h"
23 #include "cmap_header.h"
24 #include "cmap_labels.h"
25 #include "cmap_errno.h"
26 
27 /*! Close the file.
28  In write mode the header is output, along with the machine
29  stamp.  In read mode the file is just closed.
30  Write mode supports ways of updating the map statistics (
31  only active for FLOAT32).
32  /param mfile (CMMFile *)
33  /return void */
ccp4_cmap_close(CMMFile * mfile)34 void ccp4_cmap_close(CMMFile *mfile)
35 {
36   int i;
37 
38   if ( mfile == NULL)
39     return;
40 
41   if (ccp4_file_is_write(mfile->stream) ) {
42     if ( mfile->data_mode == FLOAT32) {
43       switch (mfile->close_mode) {
44       case 1:
45         break;
46       case 2:
47 	mfile->stats.offset = 0.0f;
48       case 0:
49       default:
50         if (mfile->stats.total != 0) {
51           mfile->stats.mean /= mfile->stats.total;
52           mfile->stats.rms /= mfile->stats.total;
53           mfile->stats.rms -= mfile->stats.mean*mfile->stats.mean;
54           mfile->stats.rms = (mfile->stats.rms > 0) ? sqrt(mfile->stats.rms) : 0;
55           mfile->stats.mean += (double) mfile->stats.offset;
56         }
57         break;
58       }
59     }
60     write_mapheader(mfile);
61     write_maplabels(mfile);
62     ccp4_file_warch(mfile->stream);
63   }
64   ccp4_file_close(mfile->stream);
65   for (i=0 ; i != mfile->labels.number ; i++)
66     if (mfile->labels.labels[i] != NULL)
67       free(mfile->labels.labels[i]);
68   free(mfile);
69 }
70 
71 /*! Set the close mode:
72     0: calculate based on stored values (default)
73     1: just dump the current values
74  /param mfile (CMMFile *)
75  /param mask (unsigned int) close mode
76  /return void */
ccp4_cmap_closemode(CMMFile * mfile,unsigned int mask)77 void ccp4_cmap_closemode(CMMFile *mfile, unsigned int mask)
78 {
79   mfile->close_mode = mask;
80 }
81