Skip to content
Snippets Groups Projects
get_GEOS.c 6.86 KiB
/*******************************************************************************
Description:

  Integer function get_GEOS.c
  Opens and reads selected meteorological variables from two GEOS files.
  The first file ('geos1') is for a time < 3 hours earlier than the target
    (granule) time.  The second file ('geos2') is for a time < 3 hours later.
  Variables are temporally interpolated to the target time.
  Read snow and ice cover values from GEOS land and ocean files, respectively.
  Surface temperature is then spatially interpolated.

  Called from collect_inputs.c

Input arguments:
  geos1            file name of earlier GEOS data time
  geos2            file name of later GEOS data time
  geos_lnd         file name of GEOS land properties file (for snow cover)
  geos_ocn         file name of GEOS ocean properties file (for ice cover)

Output arguments:
  none

Function output:
  int return_code  successful completion is zero, otherwise non-zero

  Output parameters stored in header file ancillary.h

Revision History:
  06/2021 R. Frey  Original version

Calls:
  read_GEOS.c
  get_granule_times.c
  get_geos_times.c
  get_ti_weights.c
  assign_geos_vals.c

Comments:
  Variables from the earlier GEOS time have names ending in "1" (e.g., 'sfct1').
  Variables from the later GEOS time have names ending in "2" (e.g., 'sfct2').
  These are stored in ancillary.h.


*******************************************************************************/

// Includes

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

//MVCCM includes
//#include "granule_geolocation.h"
//#include "granule.h"
#include "ancillary.h"

/******************************************************************************/

void get_GEOS(float *lat, float *lon, char *granule_start_time, char *anc_dir, char *geos1, char *geos2, char *geos_lnd,
              char *geos_ocn, char *geos_cnst, float *tpw, float *snowfr, float *icefr, float *geos_ocnfr,
              float *landicefr, float *sfct)

{

/******************************************************************************/

//  Declarations and initializations.

    extern int get_granule_times(char[64], float*);
    extern int get_geos_times(char[64], char[64], int*, int*);
    extern int get_ti_weights(float, int, int, float*);
    extern int get_ti_vars(float);
    extern int assign_geos_vals(float *, float *);
    extern int read_GEOS(char[256], int);
    extern int read_GEOS_lndocn(char[256], char[256]);
    extern int read_GEOS_constants(char[256]);

    char geos1_wpath[256];
    char geos2_wpath[256];
    char geosland_wpath[256];
    char geosocean_wpath[256];
    char geosconstants_wpath[256];
    char s1[64], s2[64];
    char search[2];
    char *token;
    int irt;
    int geoshr1;
    int geoshr2;
    int GEOS_time_index, ihr, imin;
    float targhr;
    float wt;

    int return_code = 0;

/******************************************************************************/

/*  Get complete file names. */
    strcpy(geos1_wpath,anc_dir);strcat(geos1_wpath,"/");strcat(geos1_wpath,geos1);
    printf("\nGEOS file1: %s\n", geos1_wpath);
    strcpy(geos2_wpath,anc_dir);strcat(geos2_wpath,"/");strcat(geos2_wpath,geos2);
    printf("GEOS file2: %s\n", geos2_wpath);
    strcpy(geosland_wpath,anc_dir);strcat(geosland_wpath,"/");strcat(geosland_wpath,geos_lnd);
    printf("GEOS LAND file: %s\n", geosland_wpath);
    strcpy(geosocean_wpath,anc_dir);strcat(geosocean_wpath,"/");strcat(geosocean_wpath,geos_ocn);
    printf("GEOS OCEAN file: %s\n", geosocean_wpath);
    strcpy(geosconstants_wpath,anc_dir);strcat(geosconstants_wpath,"/");strcat(geosconstants_wpath,geos_cnst);
    printf("GEOS CONSTANTS file: %s\n\n", geosconstants_wpath);

/******************************************************************************/

//  Read GEOS time-independent (constants) file.
//  Get land/sea tags (fraction ocean) and surface elevation (geopotential ht.).
    irt = read_GEOS_constants(geosconstants_wpath);

//  Read snow and ice fractions from GEOS land and ocean property files.
//  Variables listed in read_GEOS_lndocn.c
//  Pointers to GEOS snow and ice fractions are stored in ancillary.h.
    irt = read_GEOS_lndocn(geosland_wpath, geosocean_wpath);

//  Read selected variables from GEOS 3-hourly analysis files.
//  Variables listed in read_GEOS.c
//  Pointers to GEOS variables are stored in ancillary.h.

    GEOS_time_index = 1;
    irt = read_GEOS(geos1_wpath, GEOS_time_index);
    if(irt != 0) {
      printf("read_GEOS unsuccessful: %s\n", geos1_wpath);
     // return (-1);
    }

    GEOS_time_index = 2;
    irt = read_GEOS(geos2_wpath, GEOS_time_index);
    if(irt != 0) {
      printf("read_GEOS unsuccessful: %s\n", geos2_wpath);
     // return (-2);
    }

/******************************************************************************/

//  Time-interpolate GEOS grids (pointers to data in ancillary.h).

/******************************************************************************/

//  Get granule start date, hour, and minute.
    printf("GEOS interpolation target time from L1b: %s\n", granule_start_time);

    (void) strcpy(search, " ");
    token = strtok(granule_start_time, search);
    (void) strcpy(search, ":");
    token = strtok(NULL, search);
    ihr = atoi(token);
    token = strtok(NULL, search);
    imin = atoi(token);
    targhr = ihr + (imin / 60.0);
//  printf("hour, min: %d %d %f\n", ihr, imin, targhr);

/******************************************************************************/

//  Get GEOS times from file names, at least for now.
    (void) strcpy(s1, geos1);
    (void) strcpy(s2, geos2);
    irt = get_geos_times(s1, s2, &geoshr1, &geoshr2);

/******************************************************************************/

//  Calculate time-interpolation weights.
    irt = get_ti_weights(targhr, geoshr1, geoshr2, &wt);
    printf("Time-interpolation weights: %f %d %d %f\n", targhr, geoshr1, geoshr2, wt);

/******************************************************************************/

//  Perform time interplation. Pointers to inputs and outputs are stored in
//  ancillary.h.
    irt = get_ti_vars(wt);

/******************************************************************************/

//  Assign GEOS variable values to each pixel location in the input L1b granule.
//  Also spatially interpolates GEOS values to pixel locations if desired.
//  Pointers to inputs and outputs are stored in ancillary.h.
    irt = assign_geos_vals(lat, lon);

/******************************************************************************/

    tpw = grn_anc.tpw;
    snowfr = grn_anc.snowfr;
    icefr = grn_anc.icefr;
    geos_ocnfr = grn_anc.geos_ocnfr;
    landicefr = grn_anc.landicefr;
    sfct = grn_anc.sfct;

    int i;

    for (i=0; i<10; i++) {
        printf("i: %d\ttpw: %f\tsnowfr: %f\ticefr: %f\tgeos_ocnfr: %f\tlandicefr: %f\tsfct: %f\n",
                i, tpw[i], snowfr[i], icefr[i], geos_ocnfr[i], landicefr[i], sfct[i]);
    }
    // return ( return_code);

}