Beispiel #1
0
static void
cmap1_init1( void )
{
    PLFLT i[4], h[4], l[4], s[4];

    i[0] = 0;           /* left boundary */
    i[1] = 0.45;        /* just before center */
    i[2] = 0.55;        /* just after center */
    i[3] = 1;           /* right boundary */

    h[0] = 260;         /* hue -- low: blue-violet */
    h[1] = 260;         /* only change as we go over vertex */
    h[2] = 20;          /* hue -- high: red */
    h[3] = 20;          /* keep fixed */

#if 1
    l[0] = 0.5;         /* lightness -- low */
    l[1] = 0.0;         /* lightness -- center */
    l[2] = 0.0;         /* lightness -- center */
    l[3] = 0.5;         /* lightness -- high */
#else
    plscolbg( 255, 255, 255 );
    l[0] = 0.5;         /* lightness -- low */
    l[1] = 1.0;         /* lightness -- center */
    l[2] = 1.0;         /* lightness -- center */
    l[3] = 0.5;         /* lightness -- high */
#endif
    s[0] = 1;           /* maximum saturation */
    s[1] = 1;           /* maximum saturation */
    s[2] = 1;           /* maximum saturation */
    s[3] = 1;           /* maximum saturation */

    c_plscmap1l( 0, 4, i, h, l, s, NULL );
}
PlPlotWidget::PlPlotWidget(QWidget *parent) :
    QWidget(parent)
{
    setAttribute( Qt::WA_DeleteOnClose );

    plot = new QtExtWidget( parent->width(), parent->height(), parent ); //this should fit the widget box

    plmkstrm( &strm );// One window = One plot widget = one stream
    plsdev( "extqt" );
    plsetqtdev( plot );
    plinit();

    plot->setBackgroundColor(255,255,255,1);
    //resize( 400, 320 );
    pladv( 0 );
    unsigned int col = 255;
    plscolbg(col,col,col);
}
Beispiel #3
0
int
main( int argc, char *argv[] )
{
    PLFLT xmin0, xmax0, ymin0, ymax0, zxmin0, zxmax0, zymin0, zymax0;
    PLFLT xmin, xmax, ymin, ymax, zxmin, zxmax, zymin, zymax;
    PLFLT xmid, ymid, wx, wy;
    PLFLT mar0, aspect0, jx0, jy0, ori0;
    PLFLT mar, aspect, jx, jy, ori;
    PLINT win, level2, digmax, digits, compression1, compression2;
    PLFLT xp0, yp0;
    PLINT xleng0, yleng0, xoff0, yoff0;
    PLFLT xp1, yp1;
    PLINT xleng1, yleng1, xoff1, yoff1;
    PLFLT xp2, yp2;
    PLINT xleng2, yleng2, xoff2, yoff2;
    PLINT fam0, num0, bmax0;
    PLINT fam1, num1, bmax1;
    PLINT fam2, num2, bmax2;
    PLINT r0, g0, b0;
    PLFLT a0;
    PLINT r, g, b;
    PLFLT a;
    PLINT r1[] = { 0, 255 };
    PLINT g1[] = { 255, 0 };
    PLINT b1[] = { 0, 0 };
    PLFLT a1[] = { 1.0, 1.0 };
    int   status;
    char  fnam[256];

    // Parse and process command line arguments

    status = 0;

    (void) plparseopts( &argc, argv, PL_PARSE_FULL );

    // Test setting / getting familying parameters before plinit
    // Save values set by plparseopts to be restored later.
    plgfam( &fam0, &num0, &bmax0 );
    fam1  = 0;
    num1  = 10;
    bmax1 = 1000;
    plsfam( fam1, num1, bmax1 );

    // Retrieve the same values?
    plgfam( &fam2, &num2, &bmax2 );
    printf( "family parameters: fam, num, bmax = %d %d %d\n", fam2, num2, bmax2 );
    if ( fam2 != fam1 || num2 != num1 || bmax2 != bmax1 )
    {
        fputs( "plgfam test failed\n", stderr );
        status = 1;
    }
    // Restore values set initially by plparseopts.
    plsfam( fam0, num0, bmax0 );

    // Test setting / getting page parameters before plinit
    // Save values set by plparseopts to be restored later.
    plgpage( &xp0, &yp0, &xleng0, &yleng0, &xoff0, &yoff0 );
    xp1    = 200.;
    yp1    = 200.;
    xleng1 = 400;
    yleng1 = 200;
    xoff1  = 10;
    yoff1  = 20;
    plspage( xp1, yp1, xleng1, yleng1, xoff1, yoff1 );

    // Retrieve the same values?
    plgpage( &xp2, &yp2, &xleng2, &yleng2, &xoff2, &yoff2 );
    printf( "page parameters: xp, yp, xleng, yleng, xoff, yoff = %f %f %d %d %d %d\n", xp2, yp2, xleng2, yleng2, xoff2, yoff2 );
    if ( xp2 != xp1 || yp2 != yp1 || xleng2 != xleng1 || yleng2 != yleng1 ||
         xoff2 != xoff1 || yoff2 != yoff1 )
    {
        fputs( "plgpage test failed\n", stderr );
        status = 1;
    }
    // Restore values set initially by plparseopts.
    plspage( xp0, yp0, xleng0, yleng0, xoff0, yoff0 );

    // Test setting / getting compression parameter across plinit.
    compression1 = 95;
    plscompression( compression1 );

    // Initialize plplot
    plinit();

    // Test if device initialization screwed around with the preset
    // compression parameter.
    plgcompression( &compression2 );
    printf( "Output various PLplot parameters\n" );
    printf( "compression parameter = %d\n", compression2 );
    if ( compression2 != compression1 )
    {
        fputs( "plgcompression test failed\n", stderr );
        status = 1;
    }


    // Exercise plscolor, plscol0, plscmap1, and plscmap1a to make sure
    // they work without any obvious error messages.
    plscolor( 1 );
    plscol0( 1, 255, 0, 0 );
    plscmap1( r1, g1, b1, 2 );
    plscmap1a( r1, g1, b1, a1, 2 );

    plglevel( &level2 );
    printf( "level parameter = %d\n", level2 );
    if ( level2 != 1 )
    {
        fputs( "plglevel test failed.\n", stderr );
        status = 1;
    }

    pladv( 0 );

    xmin0 = 0.01;
    xmax0 = 0.99;
    ymin0 = 0.02;
    ymax0 = 0.49;
    plvpor( xmin0, xmax0, ymin0, ymax0 );
    plgvpd( &xmin, &xmax, &ymin, &ymax );
    printf( "plvpor: xmin, xmax, ymin, ymax = %f %f %f %f\n", xmin, xmax, ymin, ymax );
    if ( xmin != xmin0 || xmax != xmax0 || ymin != ymin0 || ymax != ymax0 )
    {
        fputs( "plgvpd test failed\n", stderr );
        status = 1;
    }
    xmid = 0.5 * ( xmin + xmax );
    ymid = 0.5 * ( ymin + ymax );

    xmin0 = 0.2;
    xmax0 = 0.3;
    ymin0 = 0.4;
    ymax0 = 0.5;
    plwind( xmin0, xmax0, ymin0, ymax0 );
    plgvpw( &xmin, &xmax, &ymin, &ymax );
    printf( "plwind: xmin, xmax, ymin, ymax = %f %f %f %f\n", xmin, xmax, ymin, ymax );
    if ( xmin != xmin0 || xmax != xmax0 || ymin != ymin0 || ymax != ymax0 )
    {
        fputs( "plgvpw test failed\n", stderr );
        status = 1;
    }

    // Get world coordinates for middle of viewport
    plcalc_world( xmid, ymid, &wx, &wy, &win );
    printf( "world parameters: wx, wy, win = %f %f %d\n", wx, wy, win );
    if ( fabs( wx - 0.5 * ( xmin + xmax ) ) > 1.0E-5 || fabs( wy - 0.5 * ( ymin + ymax ) ) > 1.0E-5 )
    {
        fputs( "plcalc_world test failed\n", stderr );
        status = 1;
    }

    // Retrieve and print the name of the output file (if any).
    // This goes to stderr not stdout since it will vary between tests and
    // we want stdout to be identical for compare test.
    plgfnam( fnam );
    if ( fnam[0] == '\0' )
    {
        printf( "No output file name is set\n" );
    }
    else
    {
        printf( "Output file name read\n" );
    }
    fprintf( stderr, "Output file name is %s\n", fnam );

    // Set and get the number of digits used to display axis labels
    // Note digits is currently ignored in pls[xyz]ax and
    // therefore it does not make sense to test the returned
    // value
    plsxax( 3, 0 );
    plgxax( &digmax, &digits );
    printf( "x axis parameters: digmax, digits = %d %d\n", digmax, digits );
    if ( digmax != 3 )
    {
        fputs( "plgxax test failed\n", stderr );
        status = 1;
    }

    plsyax( 4, 0 );
    plgyax( &digmax, &digits );
    printf( "y axis parameters: digmax, digits = %d %d\n", digmax, digits );
    if ( digmax != 4 )
    {
        fputs( "plgyax test failed\n", stderr );
        status = 1;
    }

    plszax( 5, 0 );
    plgzax( &digmax, &digits );
    printf( "z axis parameters: digmax, digits = %d %d\n", digmax, digits );
    if ( digmax != 5 )
    {
        fputs( "plgzax test failed\n", stderr );
        status = 1;
    }

    mar0    = 0.05;
    aspect0 = PL_NOTSET;
    jx0     = 0.1;
    jy0     = 0.2;
    plsdidev( mar0, aspect0, jx0, jy0 );
    plgdidev( &mar, &aspect, &jx, &jy );
    printf( "device-space window parameters: mar, aspect, jx, jy = %f %f %f %f\n", mar, aspect, jx, jy );
    if ( mar != mar0 || jx != jx0 || jy != jy0 )
    {
        fputs( "plgdidev test failed\n", stderr );
        status = 1;
    }

    ori0 = 1.0;
    plsdiori( ori0 );
    plgdiori( &ori );
    printf( "ori parameter = %f\n", ori );
    if ( ori != ori0 )
    {
        fputs( "plgdiori test failed\n", stderr );
        status = 1;
    }

    xmin0 = 0.1;
    ymin0 = 0.2;
    xmax0 = 0.9;
    ymax0 = 0.8;
    plsdiplt( xmin0, ymin0, xmax0, ymax0 );
    plgdiplt( &xmin, &ymin, &xmax, &ymax );
    printf( "plot-space window parameters: xmin, ymin, xmax, ymax = %f %f %f %f\n", xmin, ymin, xmax, ymax );
    if ( xmin != xmin0 || ymin != ymin0 || xmax != xmax0 || ymax != ymax0 )
    {
        fputs( "plgdiplt test failed\n", stderr );
        status = 1;
    }

    zxmin0 = 0.1;
    zymin0 = 0.1;
    zxmax0 = 0.9;
    zymax0 = 0.9;
    plsdiplz( zxmin0, zymin0, zxmax0, zymax0 );
    plgdiplt( &zxmin, &zymin, &zxmax, &zymax );
    printf( "zoomed plot-space window parameters: xmin, ymin, xmax, ymax = %f %f %f %f\n", zxmin, zymin, zxmax, zymax );
    if ( fabs( zxmin - ( xmin + ( xmax - xmin ) * zxmin0 ) ) > 1.0E-5 ||
         fabs( zymin - ( ymin + ( ymax - ymin ) * zymin0 ) ) > 1.0E-5 ||
         fabs( zxmax - ( xmin + ( xmax - xmin ) * zxmax0 ) ) > 1.0E-5 ||
         fabs( zymax - ( ymin + ( ymax - ymin ) * zymax0 ) ) > 1.0E-5 )
    {
        fputs( "plsdiplz test failed\n", stderr );
        status = 1;
    }

    r0 = 10;
    g0 = 20;
    b0 = 30;
    plscolbg( r0, g0, b0 );
    plgcolbg( &r, &g, &b );
    printf( "background colour parameters: r, g, b = %d %d %d\n", r, g, b );
    if ( r != r0 || g != g0 || b != b0 )
    {
        fputs( "plgcolbg test failed\n", stderr );
        status = 1;
    }

    r0 = 20;
    g0 = 30;
    b0 = 40;
    a0 = 0.5;
    plscolbga( r0, g0, b0, a0 );
    plgcolbga( &r, &g, &b, &a );
    printf( "background/transparency colour parameters: r, g, b, a = %d %d %d %f\n", r, g, b, a );
    if ( r != r0 || g != g0 || b != b0 || a != a0 )
    {
        fputs( "plgcolbga test failed\n", stderr );
        status = 1;
    }

    plend();

    exit( status );
}
/*
 * Plot specified x/y coordinates, x as int32_t and y as float, with an arbitrary number
 * of separate lines, as lines and/or points.
 * Output is PDF file, location specified by 'fileName'.
 */
int plplotLines(
    PlplotSetup *setup,
    uint32_t numSamples,	/* size of ix[] and fy[] arrays */
    uint32_t numLines,		/* size of lineDef[] array */
    int32_t *ix,			/* numSamples */
    LineDef *lineDef,		/* numLines */
    const char *graphName,
    const char *fileName,
    bool plotPoints,		/* true: plot points */
    bool plotLine,			/* true: plot line */
    bool skipTrailZeroes)	/* don't plot zero Y values at end of graph */
{
    if(setup == NULL) {
        printf("***plplotLine: setup required\n");
        return -1;
    }

    char tmpFile[TEMP_FN_LEN];
    makeTempFile(tmpFile, TEMP_FN_LEN);

    PLFLT fx[numSamples];
    PLFLT minX = setup->minX;
    PLFLT maxX = setup->maxX;
    PLFLT minY = setup->minY;
    PLFLT maxY = setup->maxY;

    for(uint32_t dex=0; dex<numSamples; dex++) {
        fx[dex] = ix[dex];
    }

    const char *yName = "";
    if(setup->yAxisName) {
        yName = setup->yAxisName;
    }
    const char *xName = "";
    if(setup->xAxisName) {
        xName = setup->xAxisName;
    }

    plsdev ("psc");

    /* standard: background white, foreground (axes, labels, etc.) black */
    plscolbg(PLOT_WHITE);
    plscol0(1, PLOT_BLACK);

    plsfnam(tmpFile);
    plsdiori(1.0);						// portrait
    plinit();
    plenv(minX, maxX, minY, maxY, 0, 0);
    pllab(xName, yName, graphName);

    for(uint32_t dex=0; dex<numLines; dex++) {
        uint32_t thisSamples = numSamples;
        if(skipTrailZeroes) {
            while((lineDef[dex].fy[thisSamples-1] == 0.0) && (thisSamples > 0)) {
                thisSamples--;
            }
            if(thisSamples == 0) {
                printf("***plplotLines: Warning: line with all zeroes skipped\n");
                continue;
            }
        }
        plotOneLine(thisSamples, fx,  &lineDef[dex], plotPoints, plotLine);
    }

    plend();

    int ourRtn = psToPdf(tmpFile, fileName);
    unlink(tmpFile);
    return ourRtn;
}
/*
 * Plot a histogram of prebinned data. X values are int32_t's, and the corresponding
 * Y values - the counts for each X - are uint32_t's.
 */
int plplotBins(
    uint32_t numBins,
    const int32_t *x,			/* numBins of X values, monotonically increasing */
    const uint32_t *y,			/* numBins of Y values for each associated X */
    const char *graphName,
    const char *fileName,
    const char *xAxisName,		/* optional */
    const char *yAxisName)		/* optional */
{
    char tmpFile[TEMP_FN_LEN];
    makeTempFile(tmpFile, TEMP_FN_LEN);

    PLINT totalBins = numBins + 2;

    /* PLFLT array of sample values */
    PLFLT *xf = (PLFLT *)malloc(totalBins * sizeof(PLFLT));

    /* these two will have Y values of zero */
    xf[0] = x[0] - 1;
    xf[totalBins - 1] = x[numBins-1] + 1;

    const int32_t *ip = x;
    PLFLT *op = xf + 1;
    for(uint32_t dex=0; dex<numBins; dex++) {
        *op++ = *ip++;
    }

    /* PLFLT array of bins */
    PLFLT *yf = (PLFLT *)malloc(totalBins * sizeof(PLFLT));
    yf[0] = 0.0;
    yf[totalBins - 1]  = 0.0;
    const uint32_t *uip = y;
    op = yf + 1;
    for(uint32_t dex=0; dex<numBins; dex++) {
        *op++ = *uip++;
    }

    /* get max Y value */
    uint32_t maxY = 0;
    uip = y;
    for(uint32_t dex=0; dex<numBins; dex++) {
        uint32_t currY = *uip++;
        if(currY > maxY) {
            maxY = currY;
        }
    }

    const char *yName = yAxisName ? yAxisName : "";
    const char *xName = xAxisName ? xAxisName : "";

#if	HIST_COLOR
    plsdev ("psc");
    plscolbg(255, 255, 255);			/* white background */
#else
    plsdev ("ps");
#endif
    plsfnam(tmpFile);
    plsdiori(1.0);						// portrait
    plinit();
    plenv(xf[0], xf[totalBins - 1], 0, maxY, 0, 0);

#if	HIST_COLOR
    /* can we alter colors of lines and the spaces inside the histograms? */
    plscolbg(255, 0, 0);				/* red background */
    plscol0(1, 255, 0, 0);				/* red foreground - no effect */
#endif

    pllab(xName, yName, graphName);

    plbin(totalBins, xf, yf, PL_BIN_CENTRED);
    plend();

    free(xf);
    free(yf);

    int ourRtn = psToPdf(tmpFile, fileName);
    unlink(tmpFile);
    return ourRtn;
}
/*
 * Plot a histogram showing the number of occurences of each possible
 * value of 'samples'.
 */
int plplotHist(
    const int32_t *samples,
    uint32_t numSamples,
    const char *graphName,
    const char *fileName,
    const char *xAxisName,		/* optional */
    const char *yAxisName)		/* optional */
{
    char tmpFile[TEMP_FN_LEN];
    makeTempFile(tmpFile, TEMP_FN_LEN);

    /* First determine the range, i.e. the number of bins */
    int32_t minSamp = samples[0];
    int32_t maxSamp = samples[0];
    for(uint32_t dex=0; dex<numSamples; dex++) {
        int32_t s = samples[dex];
        if(s < minSamp) {
            minSamp = s;
        }
        if(s > maxSamp) {
            maxSamp = s;
        }
    }

    /* When we specify PL_BIN_CENTRED, the min and max values are half the normal width */
    minSamp--;
    maxSamp++;

    PLINT numBins = maxSamp - minSamp + 1;

    /* One array containing the sample values, x */
    PLFLT *x = (PLFLT *)malloc(numBins * sizeof(PLFLT));
    int32_t binNum = minSamp;
    for(uint32_t dex=0; dex<(uint32_t)numBins; dex++) {
        x[dex] = binNum++;
    }

    /* Now make and fill the bins proper */
    PLFLT *y = (PLFLT *)malloc(numBins * sizeof(PLFLT));
    for(uint32_t dex=0; dex<(uint32_t)numBins; dex++) {
        y[dex] = 0;
    }
    PLFLT maxY = 0.0;

    for(uint32_t dex=0; dex<numSamples; dex++) {
        int32_t s = samples[dex];
        PLFLT *yp = y + s - minSamp;
        *yp += 1.0;
        if(*yp > maxY) {
            maxY = *yp;
        }
    }

    const char *yName = yAxisName ? yAxisName : "";
    const char *xName = xAxisName ? xAxisName : "";

#if	HIST_COLOR
    plsdev ("psc");
    plscolor(1);
    plscolbg(255, 255, 255);			/* white background */
#else
    plsdev ("ps");
#endif
    plsfnam(tmpFile);
    plsdiori(1.0);						// portrait
    plinit();
    plenv(minSamp, maxSamp, 0, maxY, 0, 0);

#if	HIST_COLOR
    /* can we alter colors of lines and the spaces inside the histograms? */
    plscolbg(255, 0, 0);				/* red background */
    plscol0(1, 255, 0, 0);				/* red foreground - no effect */
#endif

    pllab(xName, yName, graphName);

    plbin(numBins, x, y, PL_BIN_CENTRED);
    plend();

    free(x);
    free(y);

    int ourRtn = psToPdf(tmpFile, fileName);
    unlink(tmpFile);
    return ourRtn;
}
Beispiel #7
0
int main() {                                                    
  
  // http://cdiac.ornl.gov/pns/current_ghg.html
  const double c_co2 = 390;
  const double c_n2o = 0.32;
  const double c_co = 0.1;
  const double c_ch4 = 1.775;
  const double c_o2 = 20.9e+4;
  
  
  const double cp=1004;  /* J/kg K */
  const double deltat=3600.0*4.0;  /* s */
  const double Ra=287; /* J/kg K */

  int timesteps = 0;
  int ilev=0;
  int ilyr=0;
  const int nlyr=20;  /* Number of Layers */
  const int nlev=nlyr+1;
  int instabil=FALSE;

  int status;
  
  
  

  const double deltap=PSURF/nlyr;
  const double kappa=Ra/cp;
  
  double *p=calloc(nlev,sizeof(double));
  double *T=calloc(nlyr,sizeof(double));
  double *theta=calloc(nlev,sizeof(double));
  double *plyr=calloc(nlyr,sizeof(double));
  double *z=calloc(nlev,sizeof(double));
  double *zlyr=calloc(nlyr,sizeof(double));
  double *deltazlyr=calloc(nlyr,sizeof(double));
  double *deltazlev=calloc(nlev,sizeof(double));
  double *h2o=calloc(nlyr,sizeof(double));
  double *o3=calloc(nlyr,sizeof(double));
  
  double ***dtaumol;
  double **wgt;
  double *wavelength;
  int nbnd=0;
  int *nch;
  int iv;
  int iq;
  
  double *tmplev=calloc(nlev, sizeof(double)); /* temporary vector with length lev */
  double *tmplyr=calloc(nlyr, sizeof(double)); /* temporary vector with length lyr */
  
  double *eup=calloc(nlev, sizeof(double));
  double *edn=calloc(nlev, sizeof(double));
  double *euptmp=calloc(nlev, sizeof(double));
  double *edntmp=calloc(nlev, sizeof(double));
  double *edirtmp=calloc(nlev, sizeof(double));
  double *enet=calloc(nlyr, sizeof(double));

  double *deltaTday=calloc(nlyr, sizeof(double));
  double *deltaT=calloc(nlyr, sizeof(double));
  
  //------------- Define variables for Rodents---------------//
  
  const double gr_albedo=0.3; /* 1 */
  
  const int mu_counterlimit = (int)(24.0 * 3600 / deltat);
  double* const mu_0 = calloc(mu_counterlimit, sizeof(double));
  int mu_counter;

  // --- start mu_0
  {
  double mu_weight = 0;

  for(mu_counter=0; mu_counter < mu_counterlimit; mu_counter++) {
    mu_0[mu_counter] = cos( 2.0*M_PI / mu_counterlimit * (mu_counter + mu_counter+1) / 2.0 );
    if(mu_0[mu_counter] > 0)
      mu_weight += mu_0[mu_counter];
  }
  // weight needs to be adjusted such that it is 0.5 for half of the steps (day-only)
  mu_weight = mu_weight / (mu_counterlimit/2.0 * 0.5);

  for(mu_counter=0; mu_counter < mu_counterlimit; mu_counter++) {
    mu_0[mu_counter] = mu_0[mu_counter] / mu_weight;
    printf("mu_0[%d] = %e\n", mu_counter, mu_0[mu_counter]);
  }
  mu_counter = 0;
  
  }
  // --- end mu_0
  
  double* S_0;
  double* omega_0=calloc(nlyr,sizeof(double));
  double* gassy=calloc(nlyr,sizeof(double));
  double* f=calloc(nlyr,sizeof(double));
  
  
  for(ilyr=0; ilyr<nlyr; ilyr++) {
   omega_0[ilyr] = 0.f;
   gassy[ilyr] = 0; //0.85f;
   f[ilyr] = 0; //0.8f;
  }
  
  {
    double *temp1, *temp2, *temp3;
    int temp4;
      status=read_4c_file("mstrnx.data/solar.dat", &temp1, &temp2, &temp3, &S_0, &temp4);
      if (status !=0) {
      printf("Error reading Solar.dat\n");
      return EXIT_FAILURE;
      }
      
      free(temp1);
      free(temp2);
      free(temp3);
  } 
  
  //--------------------- Plot color -------------------------//

#ifndef _NOPLOT
  plscolbg (255, 255, 255);   /* background color white */
  plscol0  (15, 0, 0, 0);     /* set color 15 to black  */
  plsdev ("xwin"); /* if not called, the user is asked! */
  plinit ();
  
  plssub(2,2);
#endif
  
  //--------------------- Calculate pressure for layers and levels -------------//

  for(ilev=0; ilev<nlev; ilev++) {                              /* Calculation of the Pressure at the Levels p[ilev] */
    p[ilev]=PSURF*ilev/(nlev-1);
  }
    
  for(ilyr=0; ilyr<nlyr; ilyr++) {                            /* Calculation of the Pressure in the Layers*/
    plyr[ilyr]= 0.5*(p[ilyr]+p[ilyr+1]);
  }
   
  
  //----------------------- Reading afglus ------------------------//
  
  {
    
    double *ztemp;
    double *ptemp;
    double *Ttemp;
    double *h2otemp;
    double *o3temp;

    int status;
    int ntemp;
    int itemp;
    unsigned int mintemp = 0;
    unsigned int mintemp2 = 0;
    
    status = read_5c_file("mstrnx.data/afglus.atm", &ztemp, &ptemp, &Ttemp, &h2otemp, &o3temp, &ntemp);
    if (status !=0) {
      printf("Error reading Temperature profile\n");
      return EXIT_FAILURE;
    }

    /*
      for (itemp=0; itemp<ntemp; itemp++) {
      printf("itemp = %d, ztemp = %f, ptemp = %f, Ttemp = %f, h2otemp = %f, o3temp = %f\n", itemp, ztemp[itemp], ptemp[itemp], Ttemp[itemp], h2otemp[itemp], o3temp[itemp]);
      }
    */  
    
    //-------------------- Interpolate T, h2o and o3 to layers --------------//
    
    for (ilyr=0; ilyr<nlyr; ilyr++) {
      // printf("ilev = %d, searching for %f\n", ilyr, plyr[ilyr]);
      for (itemp=0; itemp<ntemp; itemp++) {
	if ( abs(ptemp[mintemp]- plyr[ilyr])>abs(ptemp[itemp]-plyr[ilyr])) {
	  mintemp=itemp;
	}
      }
      if (ptemp[mintemp]<plyr[ilyr]) {
	mintemp2 = mintemp+1;
      }
      else {
	mintemp2 = mintemp-1;
      }
      // printf("\tmintemp  = %d,\tp = %f\n", mintemp, ptemp[mintemp]);
      // printf("\tmintemp2 = %d,\tp = %f\n", mintemp2, ptemp[mintemp2]);

      const double weight2 = abs(ptemp[mintemp]-plyr[ilyr]);
      const double weight1 = abs(ptemp[mintemp2]-plyr[ilyr]);
      const double norm = weight1 + weight2;
      
      /* T in levels?!?! */
      T[ilyr] = Ttemp[mintemp]*weight1 + Ttemp[mintemp2]*weight2;
      T[ilyr] /= norm;
      
      h2o[ilyr] = h2otemp[mintemp]*weight1 + h2otemp[mintemp2]*weight2;
      h2o[ilyr] /= norm;

      o3[ilyr] = o3temp[mintemp]*weight1 + o3temp[mintemp2]*weight2;
      o3[ilyr] /= norm;
    }
  }
  
  
  for (ilyr=0; ilyr<nlyr; ilyr++)  {
    printf("ilyr = %d, plyr = %f,\tT = %f,\th2o = %f,\to3 = %f\n", ilyr, plyr[ilyr], T[ilyr], h2o[ilyr], o3[ilyr]);
  }
  
  /* 
     for (ilyr=0; ilyr<nlyr; ilyr++)  {
     printf("T[%d] = %f\n", ilyr, T[ilyr]);
     }
     for (ilyr=0; ilyr<nlyr; ilyr++)  {
     printf("o3[%d] = %f\n", ilyr, o3[ilyr]);
     }
     for (ilyr=0; ilyr<nlyr; ilyr++)  {
     printf("h2o[%d] = %f\n", ilyr, h2o[ilyr]);
     }
  */
  
   
  
  
  //--------------------------------- Starting while-loop ----------------------------------//
  //----------------------------------------------------------------------------------------//
  
  while (timesteps*deltat<TIME_MAX) {
    // printf("\nNew time %d: T = %f\n", (int)(timesteps*deltat), T[nlyr-1]);
    timesteps++;
  
    for(ilev=0; ilev<nlev; ilev++) {                    /* Reseting edn, eup and enet */
      edn[ilev] = 0;
      eup[ilev] = 0;
      if(ilev < nlyr) //enet is defined for ilyr only
        enet[ilev]=0;
    }
    
    
    //--------------------- Calculate z ----------------------//
    
    for (ilyr=0;ilyr<nlyr; ilyr++) {                         /* z for layers */
      deltazlyr[ilyr]=(Ra*T[ilyr]*deltap)/(plyr[ilyr]*g);  
    }
      
    zlyr[nlyr-1]=0; //check this one time
    for (ilyr=nlyr-2; ilyr >= 0; ilyr--) {
      zlyr[ilyr]=zlyr[ilyr+1]+deltazlyr[ilyr];
    }
      
     
    for (ilev=0;ilev<nlev-1; ilev++) {                      /* z for levels */
      deltazlev[ilev]=(Ra*T[ilev]*deltap)/(plyr[ilev]*g);  
    }
      
    z[nlev-1]=0;
    for (ilev=nlev-2; ilev >= 0; ilev--) {
      z[ilev]=z[ilev+1]+deltazlev[ilev];
     
    }
      
      
      
    //--------------------- Use k-distribution ---------------------//
      
    /* mu_0 is defined for a 8-part cycle of the earth, i.e. a timestep of 3 hours! */
    if(++mu_counter == mu_counterlimit)
      mu_counter = 0;

    //calculate it every two days!
    if((timesteps-1) % 10 == 0)
      //warning, wavelength is in nm!
      status = ck_mstrnx (z, plyr, T, h2o, o3, nlev, /*lyrflag*/ 1, c_co2, c_n2o, c_co, c_ch4, c_o2, &dtaumol, &wgt, &wavelength, &nbnd, &nch);
    
    for(iv=0; iv<nbnd; iv++) {
      //printf("iv = %d, wavelength = %e, S0 = %e\n", iv, wavelength[iv], S_0[iv]);
	  
      
      //------------- Use schwarzschild and sum edn eup --------//
	
      
      for(iq=0; iq<nch[iv]; iq++) {
	schwarzschild2(dtaumol[iv][iq], T, nlev, T[nlyr-1], edntmp, euptmp, wavelength[iv]*1e-6,wavelength[iv+1]*1e-6, tmplev, tmplyr);	

	for(ilev=0; ilev<nlev; ilev++) {
	  edn[ilev] += edntmp[ilev]*wgt[iv][iq];
	  eup[ilev] += euptmp[ilev]*wgt[iv][iq];
	  //printf("ilev = %d, iv = %d of %d, iq = %d of %d, wgt = %e, edntmp = %e, euptmp = %e\n", ilev, iv, nbnd, iq, nch[iv], wgt[iv][iq], edntmp[ilev], euptmp[ilev]);
	}
	
	//------------------- Include Rodents---------------------//  
	if(mu_0[mu_counter] > 0) {
	  rodents_solar(nlyr, dtaumol[iv][iq], omega_0, gassy, f, S_0[iv], mu_0[mu_counter], gr_albedo, edntmp, euptmp, edirtmp);
	  
	  for(ilev=0; ilev<nlev; ilev++) {
	    edn[ilev] += (edntmp[ilev]+edirtmp[ilev])*wgt[iv][iq];
	    eup[ilev] += euptmp[ilev]*wgt[iv][iq];
	    //printf("ilev = %d, iv = %d of %d, iq = %d of %d, wgt = %e, edntmp = %e, euptmp = %e, edirtmp = %e\n", ilev, iv, nbnd, iq, nch[iv], wgt[iv][iq], edntmp[ilev], euptmp[ilev], edirtmp[ilev]);
	  }
	}
      }
    }
     
    //---------------- Calculate enet -----------------------// 
    
    for(ilyr=0; ilyr<nlyr-1; ilyr++) {
      enet[ilyr] = eup[ilyr+1] + edn[ilyr] - eup[ilyr] - edn[ilyr+1];
     
    }

    for (ilev=0; ilev<nlev; ilev++) {
      //printf("ilev = %d, eup = %f, edn = %f\n", ilev, eup[ilev], edn[ilev]);
    }

    //---------------- Calculate temperature gain per layer -----------//
    
    for (ilyr=0; ilyr<nlyr-1; ilyr++)  {
      deltaT[ilyr]=(enet[ilyr]*g*deltat)/((deltap*100.0)*cp);
      T[ilyr] +=deltaT[ilyr];
      //printf("dT[ilyr] = %f\n", ilyr, deltaT[ilyr]);
    }
  
    //----------------Calculate Temperature gain for ground layer ---------//
    
    deltaT[nlyr-1]=((edn[nlyr-1]-eup[nlyr-1])*g*deltat)/((deltap*100.0)*cp);
    T[nlyr-1] += deltaT[nlyr-1];


    //---------- Convert T to theta ------------//
    
    for (ilyr=0; ilyr<nlyr; ilyr++) {                           
      theta[ilyr]=pow(PSURF/plyr[ilyr], kappa)*T[ilyr];
      //printf("%d %f\n", ilyr, theta[ilyr]);
    }
    
    
    //------------ Convection -------------------//
    
    instabil = FALSE;
    
    for (ilyr=0; ilyr<nlyr;ilyr++) {                            /* Testing for Instability */
      if (theta[ilyr+1]>theta[ilyr]) {
	instabil = TRUE;
	break;
      }
    }

    if (instabil) {                                             /* Convection - Sorting of Layers according to theta */
      qsort (theta, nlyr, sizeof(double), sortfunc);
      //  printf ("Konvektion :-)\n");
    }
    
    for (ilyr=0; ilyr<nlyr; ilyr++) {                           /* Conversion from theta to T */
      T[ilyr] = theta[ilyr] /  (pow(PSURF/plyr[ilyr], kappa));

      //printf("%d %f\n", ilyr, theta[ilyr]);
    }

    
    //-------------------- Print every x timestep ------------//
    
    if(timesteps % (int)(10) == 0) {
      printf ("timestep %d\n", timesteps);

      /* Printing time in readable format
       * In order to remove days, hours or minutes
       * just add // in front of the appropriate line
       */
      int time = timesteps*deltat;
      printf("time ");
      printf("%dd = ", (int)(time / 3600 / 24));
      printf("%dh = ", (int)(time / 3600 ));
      printf("%dmin = ", (int)(time / 60 ));
      printf("%ds\n", time);

      printf(" Tsurf=%f\n", T[nlyr-1]);

      for (ilyr=0; ilyr<nlyr; ilyr++) {
	deltaTday[ilyr]=deltaT[ilyr]*86.4/2;
      }

      plotall(nlyr, T, plyr, zlyr, deltaTday);

      /* for (ilyr=0; ilyr<nlyr; ilyr++) { */
      /*   printf("ilyr %d, z=%f,  plyr=%f,theta=%f, T=%f\n", ilyr, z[ilyr], plyr[ilyr],theta[ilyr], T[ilyr]); */
      /* } */

      for (ilyr=0; ilyr<nlyr; ilyr++) {
      	printf("p%f, edn%f, eup%f, enet%f\n", p[ilyr], edn[ilyr], eup[ilyr], enet[ilyr]);
      }
      printf("p%f, edn%f, eup%f\n", p[ilyr], edn[ilyr], eup[ilyr]);
    }
   
  }                                     

  free(tmplyr);
  free(tmplev);
  //----------------- End of while-loop---------------------//
  
  printf("\nTime %d: T = %f\n", (int)(timesteps*deltat), T[nlyr-1]);
  for (ilyr=0; ilyr<nlyr; ilyr++) {
    printf("%d %f\n", ilyr, T[ilyr]);
  }

  printf("\nTsurf=%f\n", T[nlyr-1]);

  return 0;
}
Beispiel #8
0
int plp_draw(double *signal, int *signal_lengths, int ylog_scale) {
	int count;
	int i,j;
	int col;
	int dowind;

	for (i=0;i<2*NOF_INPUT_ITF;i++) {
		if (signal_lengths[i] > INPUT_MAX_SAMPLES) {
			moderror_msg("plplot buffer configured for %d samples but received %d in signal %d\n",
					INPUT_MAX_SAMPLES,signal_lengths[i],i);
			return -1;
		}
	}

	dowind=0;
	xmax=-1;
	for(i=0;i<2*NOF_INPUT_ITF;i++) {
		if (signal_lengths[i]) {
			dowind=1;
			xmax = (PLFLT) MAX(xmax,signal_lengths[i]);
			for (j=0;j<signal_lengths[i];j++) {
				ymin = (PLFLT) MIN(ymin,signal[i*INPUT_MAX_SAMPLES+j]);
				ymax = (PLFLT) MAX(ymax,signal[i*INPUT_MAX_SAMPLES+j]);
			}
		}
	}

	if (!dowind) {
		xmin=0;
		xmax=100;
		ymin=-1;
		ymax=1;
	}

	plclear();

	plscolbg(255, 255, 255);
	plvsta();
	plwid(1);
	plwind(xmin, xmax, ymin*1.1, ymax*1.1);

	plcol0(1);
	if (ylog_scale) {
		plbox(logaxis_x, 0., 0, logaxis_y, 0., 0);
	} else {
		plbox(axis_x, 0., 0, axis_y, 0., 0);
	}
	plcol0(4);
	plbox("g", 0, 0, "g", 0, 0);
	plcol0(1);
	pllab(xlabel, ylabel, "");

	draw_legend();

	plwid(4);
	col=3;
	for (i=0;i<2*NOF_INPUT_ITF;i++) {
		if (signal_lengths[i]) {
			plcol0(line_colors[i]);
			plline(signal_lengths[i], t, &signal[i*INPUT_MAX_SAMPLES]);
			col++;
			if (col==4) col++;
		}
	}

	plflush();                      // force an update of the tk driver

}