root/trunk/EBaporate/ebaporate.c

Revision 449, 5.0 kB (checked in by hazelsct, 6 years ago)

Separated shorts.c into new file, documented it. Other doc changes.

  • Property svn:keywords set to Author Date Id Revision
Line 
1/***************************************
2  $Header$
3
4  This file has the ebaporate front-end to the code, which inputs a PPM file
5  with a hearth image and integrates a function over it, such as the
6  evaporation rate or Al/Ti ratio.
7***************************************/
8
9
10#include <stdio.h>
11#include <string.h>
12#include <math.h>
13
14#define COLS 1     /* X cell size */
15#define ROWS 1     /* Y cell size */
16#define THRESH 37  /* Minimum color value to be considered in Intens-T curve */
17#define COLOR 2    /* Color being used by the function (1red, 2green, 3blue) */
18#define POWER 1    /* Power of output, 2 accentuates high-temp differences */
19
20/* Clausius-Clapeyron constants */
21/* Al/Ti, for gamma calculation */
22#define AA (16450-23200)
23#define BB (12.36-11.74)
24#define CC (-1.023+0.66)
25#define DD 0.
26#define CONST 1.33216 /* =sqrt(Mti/Mal) */
27/* V/Ti, for gamma calculation
28#define AA (26900-23200)
29#define BB (10.12-11.74)
30#define CC (0.33+0.66)
31#define DD (-.265)
32#define CONST 0.974292887 /* =sqrt (Mv/Mal) */
33
34#define PI 3.141592653589793 /* 'Nuff said */
35
36
37/*++++++++++++++++++++++++++++++++++++++
38  The usual main() function.
39
40  int argc Argument count.
41
42  char *argv[] Argument values.
43  ++++++++++++++++++++++++++++++++++++++*/
44
45void main (int argc, char *argv[])
46{
47  int i, j, n=0, rowd, cold;
48  double *rower, total=0., last, func(), fTH, f255;
49  unsigned char insup [80], *outs;
50  FILE *ppmout;
51
52  /*+ Get the top of the file, verify PPM, and determine rows and columns. +*/
53  scanf ("%s", insup);
54  if (strcmp (insup, "P6"))
55    exit (printf ("Not PPM.\n"), 1);
56
57  scanf ("%s", insup);
58  if (insup [0] == '#')
59    {
60      while (insup [1] != 13 && insup [1] != 10)
61        scanf ("%c", insup+1);
62      scanf ("%s", insup);
63    }
64
65  sscanf (insup, "%d", &cold);
66  scanf ("%d", &rowd);
67  scanf ("%d", &i);
68  printf ("Picture rows: %d, columns: %d, bytemask:%d\n", rowd, cold, i);
69  scanf ("%c", insup+1);
70
71  /*+ Open the output temperature map file. +*/
72  if ((ppmout = fopen ("map.ppm", "w")) == NULL)
73    exit (printf ("No ppm file!\n"), 1);
74  fprintf (ppmout, "P5\n# CREATOR: Adam Powell's ebaporate software\n");
75  fprintf (ppmout, "%d %d\n255\n",
76          (cold+COLS-1)/COLS, (rowd+ROWS-1)/ROWS);
77
78  /*+ Allocate memory for everything. +*/
79  if ((rower = (double *)malloc (sizeof (double)*((cold+COLS-1)/COLS))) == NULL)
80    exit (printf ("Not enough memory...\n"));
81  if ((outs = (char *)malloc (sizeof (char)*((cold+COLS-1)/COLS))) == NULL)
82    exit (printf ("Not enough memory...\n"));
83  for (j=0; j<(cold+COLS-1)/COLS; j++)
84    rower [j]=0.;
85
86  /*+ The big loop: read the input PPM file, calculate the local function value
87     and add it to the total, and store the output PPM. +*/
88  for (i=0; i<rowd; i++)
89    {
90      for (j=0; j<cold; j++)
91        {
92          scanf ("%c%c%c", insup+1, insup+2, insup+3);
93          if (insup [COLOR] < THRESH)
94            rower [j/COLS] = 256. * ROWS * COLS;
95          rower [j/COLS] += insup [COLOR];
96        }
97      fTH = func ((double)THRESH);
98      f255=func (255.);
99      if (i%ROWS == ROWS-1 || i == rowd-1)
100        for (j=0;j<(cold+COLS-1)/COLS;j++)
101          {
102            last = rower [j] /= ROWS*COLS;
103            if (rower [j] < 256)
104              {
105                total += func (rower [j]);
106                n++;
107                outs [j] = (unsigned char)
108                  (220. - 220.*pow ((func (rower [j])-fTH)/(f255-fTH), POWER));
109              }
110            else
111              {
112                outs [j] = 255;
113              }
114            fprintf (ppmout, "%c", outs [j]);
115            rower [j] = 0.;
116          }
117    }
118  fclose (ppmout);
119
120  /* Make temperature bar ppm */
121  printf ("Making Tbar...\n");
122  if ((ppmout = fopen ("tbar.ppm", "w")) == NULL)
123    exit (printf ("No Tbar file!\n"), 1);
124  fprintf (ppmout, "P5\n# CREATOR: Adam's evapometer software\n216 20\n255\n");
125  for (i=0; i<20;i++)
126    {
127      for (j=0; j<24;j++)
128        fprintf (ppmout, "%c", 255);
129      for (j=24; j<216;j++)
130        fprintf (ppmout, "%c", (unsigned char)
131                 (220.-220.*pow(((double)j-24.)/192., POWER)));
132    }
133  fclose (ppmout);
134
135  printf ("Min, max function values: %lf %lf\n", fTH, f255);
136  printf ("(Last rower: %.2lf; last chars: %d %d %d)\n", last,
137          *(insup+1), *(insup+2), *(insup+3));
138  printf ("Average functional value: %le, based on %d out of %d cells.\n\n",
139          total/n, n, ((cold+COLS-1)/COLS)*((rowd+ROWS-1)/ROWS));
140}
141
142
143/*++++++++++++++++++++++++++++++++++++++
144  This function converts color values (0-255) into aluminum activity
145  coefficients.  It can easily be modified to give evaporation rates, and at
146  some point there will be a function pointer to allow multiple functions to
147  handle this in different ways.
148
149  double func It returns evaporation rate, activity coefficient, etc.
150
151  double color Color value to evaluate.
152  ++++++++++++++++++++++++++++++++++++++*/
153
154double func (double color)
155{
156  double temp, vp;
157  temp = 288.31 * log (color*23.02927);          /* Temperature, green-based */
158  /* temp = 322.58 * log (color*3.0794)+273;       /* Temperature, red-based */
159  vp = exp (log (10.) * (-AA/temp + BB + CC*log10 (temp) + DD*.001*temp));
160  /* Vap press */
161  vp *= CONST;
162  /*vp *= .04788*101300./760./sqrt (2*PI*0.04788*8.314*temp); /* 101300./760.*6.2213647e-07/sqrt (temp); */
163  return (temp); }
Note: See TracBrowser for help on using the browser.