示例#1
0
文件: hc_misc.c 项目: andreww/hc
void lonlatpv2cv(HC_PREC lon, float lat, 
		 HC_PREC *polar_vec,HC_PREC *cart_vec)
{
  HC_PREC theta,phi;
  theta = LAT2THETA((HC_HIGH_PREC)lat);
  phi   = LON2PHI((HC_HIGH_PREC)lon);
  thetaphipv2cv(theta,phi,polar_vec,cart_vec);
}
示例#2
0
int main(int argc, char **argv)
{
  struct ggrd_master *ggrd;
  double time,vr[4],vphi[4],vtheta[4],dtrange;
  double xloc[3];
  static int order = 3;
  hc_boolean calc_derivatives ;
  double lon,lat,age;
  int mode;

  mode = 1;


  switch(mode){
  case 1:
    if(argc>1)
      ggrd_grdinfo(argv[1]);
    break;
  case 2:
    /* 
       initialize velocity structure
    */
    ggrd = (struct ggrd_master *)calloc(1,sizeof(struct ggrd_master));
    ggrd_init_master(ggrd);
    ggrd->v.history = TRUE;		/* expect history */
    ggrd->age_control = TRUE;		/* expect seafloor age files */
    ggrd->age_bandlim = 900;
    /* 
       read in velocities 
    */
    if(ggrd_read_vel_grids(ggrd,1.0,FALSE,TRUE,
			   "/home/walter/becker/data/plates/past/clb/hall/",FALSE)){
      fprintf(stderr,"error opening grids\n");
      exit(-1);
    }
    
    if(argc>1)
      sscanf(argv[1],"%lf",&time);
    else
      time = 0.0;
    dtrange = 1.0;			/* transition width, in Ma */
    
    fprintf(stderr,"%s: using time %g\n",argv[0],time);
    
    calc_derivatives = FALSE;
    
    xloc[HC_R] = HC_ND_RADIUS(0.0);
    for(lat=-89;lat<=89;lat+=2)
      for(lon=0;lon<=358;lon+=2){
	//lon=270;lat=-15;{
	xloc[HC_THETA] = LAT2THETA(lat);
	xloc[HC_PHI] = LON2PHI(lon);
	/* 
	   interpolate
	*/
	if(ggrd_find_vel_and_der(xloc,time,dtrange,ggrd,
				 order,calc_derivatives,
				 TRUE,vr,vtheta,vphi))
	  exit(-1);
	if(interpolate_seafloor_ages(xloc[HC_THETA], 
				     xloc[HC_PHI],time,ggrd, &age))
	  exit(-1);
	
	fprintf(stdout,"%11g %11g\t%11g %11g %11g\t%11g\n",lon,lat,vphi[0],-vtheta[0],vr[0],age);
      }
    break;			/* end mode 2 */
  }
  return 0;
}
示例#3
0
文件: sh_syn.c 项目: geodynamics/hc
int main(int argc, char **argv)
{
  int type,lmax,shps,ilayer,nset,ivec,i,j,npoints,nphi,ntheta;
  /* 
     switches 
  */
  hc_boolean verbose = TRUE, short_format = FALSE ,short_format_ivec = FALSE ,binary = FALSE;
  int regular_basis = 0;
  HC_PREC w,e,s,n,dx,dy;
  /*  */
  FILE *in;
  HC_PREC *data,*theta,*phi;
  /* spacing for reg_ular output */
  HC_PREC dphi,x,y,dtheta;
  HC_PREC fac[3] = {1.,1.,1.},zlabel;
  SH_RICK_PREC *dummy;
  struct sh_lms *exp;
  dx = 1.0;
  w=0;e=360.;s=-90;n=90;
  if(argc > 1){
    if((strcmp(argv[1],"-h")==0)||(strcmp(argv[1],"--help")==0)||(strcmp(argv[1],"-help")==0))
      argc = -1000;
    else{
      sscanf(argv[1],"%i",&i);
      if(i)
	short_format = TRUE;
    }
  } 
  if(argc > 2){
    sscanf(argv[2],"%i",&i);
    if(i)
      short_format_ivec = TRUE;
  }
  if(argc > 3){
    sscanf(argv[3],HC_FLT_FORMAT,&w);
    if(w == 999)
      regular_basis = -1;
    else
      regular_basis = 1;
  }
  if(argc > 4)
    sscanf(argv[4],HC_FLT_FORMAT,&e);
  if(argc > 5)
    sscanf(argv[5],HC_FLT_FORMAT,&s);
  if(argc > 6)
    sscanf(argv[6],HC_FLT_FORMAT,&n);
   if(argc > 7)
    sscanf(argv[7],HC_FLT_FORMAT,&dx);
  if(argc > 8)
    sscanf(argv[8],HC_FLT_FORMAT,&dy);
  else
    dy = dx;
  if((argc > 9)|| (argc < 0)){
    fprintf(stderr,"usage: %s [short_format, %i] [short_ivec, %i] [w, %g] [e, %g] [s, %g] [n, %g] [dx, %g] [dy, dx] (in that order)\n",
	    argv[0],short_format,short_format_ivec,(double)w,(double)e,(double)s,(double)n,(double)dx);
    fprintf(stderr,"short_format:\n\t0: expects regular format with long header\n");
    fprintf(stderr,"\t1: expects short format with only lmax in header\n\n");
    fprintf(stderr,"short_ivec:\n\t0: for short format, expect AB for scalar expansion\n");
    fprintf(stderr,"\t1: for short format, expect poloidal toroidal AP BP AT BT for vector expansion\n\n");
    fprintf(stderr,"w,e,...\n\tif none of those are set, will use Gauss latitudes and FFT divided longitudes dependening on lmax\n");
    fprintf(stderr,"\tif w is set to anything but 999, will switch to regular spaced output with -Rw/e/s/n -Idx/dy type output\n");
    fprintf(stderr,"\tif w is set to 999, will read lon lat in deg from \"tmp.lonlat\", and expand on those locations\n\n");
    fprintf(stderr,"The output format will depend on the type of SH input.\n");
    fprintf(stderr,"\tfor scalara: lon lat scalar if a single SH is read in, else lon lat zlabel scalar.\n");
    fprintf(stderr,"\tfor vectors: lon lat v_theta v_phi if a single SH is read in, else lon lat zlabel v_theta v_phi.\n\n\n");
    exit(-1);
  }
  if(verbose)
    fprintf(stderr,"%s: waiting to read spherical harmonic coefficients from stdin (use %s -h for help)\n",
	    argv[0],argv[0]);
  while(sh_read_parameters_from_stream(&type,&lmax,&shps,&ilayer,&nset,
				       &zlabel,&ivec,stdin,short_format,
				       binary,verbose)){
    if(short_format_ivec){
      ivec = 1;
      shps = 2;
    }
    if(verbose)
      fprintf(stderr,"%s: converting lmax %i ivec: %i at z: %g\n",
	      argv[0],lmax,ivec,(double)zlabel);

    /* input and init */
    sh_allocate_and_init(&exp,shps,lmax,type,ivec,verbose,((regular_basis != 0)?(1):(0)));
    sh_read_coefficients_from_stream(exp,shps,-1,stdin,binary,fac,verbose);
    if(regular_basis == 1){
      /* 
	 regular basis output on regular grid
      */
      if(verbose)
	fprintf(stderr,"sh_syn: using regular spaced grid with -R%g/%g/%g/%g -I%g/%g spacing\n",
		(double)w,(double)e,(double)s,(double)n,(double)dx,(double)dy);
      if((w > e)||(s>n)||(s<-90)||(s>90)||(n<-90)||(n>90)){
	fprintf(stderr,"%s: range error\n",argv[0]);
	exit(-1);
      }

      if((ivec) && (s == -90)&&(n == 90)){
	s += dy/2;
	n -= dy/2;
	fprintf(stderr,"sh_syn: vector fields: adjusting to -R%g/%g/%g/%g\n",
		(double)w,(double)e,(double)s,(double)n);
      }
      /*  */
      dphi = DEG2RAD(dx);
      nphi = ((e-w)/dx) + 1;
      dtheta = DEG2RAD(dy);
      ntheta = ((n-s)/dy) + 1;
      npoints = nphi * ntheta;

      /*  */
      hc_vecalloc(&phi,nphi,"sh_shsyn");
      hc_vecalloc(&theta,ntheta,"sh_shsyn");
      for(x=LON2PHI(w),i=0;i < nphi;i++,x += dphi)
	phi[i] = x;
      for(y = LAT2THETA(n),j=0;j < ntheta;y += dtheta,j++)
	theta[j] = y;
      hc_vecalloc(&data,npoints * shps,"sh_shsyn data");
      /* compute the expansion */
      sh_compute_spatial_reg(exp,ivec,FALSE,&dummy,
			     theta,ntheta,phi,nphi,data,
			     verbose,FALSE);
      /* output */
      sh_print_reg_spatial_data_to_stream(exp,shps,data,
					  (nset>1)?(TRUE):(FALSE),
					  zlabel, theta,ntheta,
					  phi,nphi,stdout);
    }else if(regular_basis == -1){
      /* output on locations input lon lat file */
      if(verbose)
	fprintf(stderr,"sh_syn: reading locations lon lat from stdin for expansion\n");
      npoints = 0;
      hc_vecalloc(&phi,1,"sh_syn");
      hc_vecalloc(&theta,1,"sh_syn");
      in = fopen("tmp.lonlat","r");
      if(!in){
	fprintf(stderr,"sh_syn: error, could not open tmp.lonlat for reading lon lat locations\n");
	exit(-1);
      }
      while(fscanf(in,HC_TWO_FLT_FORMAT,&dphi,&dtheta)==2){
	phi[npoints] = LON2PHI(dphi);
	theta[npoints] = LAT2THETA(dtheta);
	npoints++;
	hc_vecrealloc(&phi,npoints+1,"sh_syn");
	hc_vecrealloc(&theta,npoints+1,"sh_syn");
      }
      if(verbose)
	fprintf(stderr,"sh_syn: read %i locations lon lat from tmp.lonlat for expansion\n",npoints);
      fclose(in);
      hc_vecalloc(&data,npoints * shps,"sh_shsyn data");
      sh_compute_spatial_irreg(exp,ivec,theta,phi,npoints,data,verbose);
      sh_print_irreg_spatial_data_to_stream(exp,shps,data,(nset>1)?(TRUE):(FALSE),
					    zlabel,theta,phi,npoints,stdout);
     }else{			/* use the built in spatial basis (Gaussian) */
      /* expansion */
      hc_vecalloc(&data,exp[0].npoints * shps,"sh_syn");
      sh_compute_spatial(exp,ivec,FALSE,&dummy,data,verbose);
      /* output */
      sh_print_spatial_data_to_stream(exp,shps,data,(nset>1)?(TRUE):(FALSE),
				      zlabel,stdout);
    }
    free(exp);free(data);
  }

  return 0;
}
示例#4
0
int main(int argc, char **argv)
{
  int ilayer,nvsol,ndsol=0,mode,shps,loop,i1,i2,nlat,nlon,
    ivec,lc,ndata,ndata_all,ndata_d,npoints,i,j,
    poff,shps_read=0,shps_read_d=0;
  FILE *in;
  struct sh_lms *vsol=NULL,*dsol=NULL;
  struct hcs *model;
  HC_PREC zlabel;
  hc_boolean binary_in = TRUE, verbose = FALSE,read_dsol=FALSE;
  HC_PREC *data,*plm=NULL,*xpos,*xvec,lon,lat,theta,phi,xtmp[3],pvec[3],
    *xscalar;
  HC_PREC polar_base[9];
  hc_struc_init(&model);
  /* 
     deal with parameters
  */
  ilayer = 0;
  mode = 1;
  switch(argc){
  case 3:
    sscanf(argv[2],"%i",&ilayer);
    break;
  case 4:
    sscanf(argv[2],"%i",&ilayer);
    sscanf(argv[3],"%i",&mode);
    break;
  case 5:
    sscanf(argv[2],"%i",&ilayer);
    sscanf(argv[3],"%i",&mode);
    read_dsol = TRUE;
    break;
  default:
    fprintf(stderr,"%s: usage\n%s sol.file layer [mode,%i] [scalar.sol]\n\n",
	    argv[0],argv[0],mode);
    fprintf(stderr,"extracts spatial solution (velocity or stress, v) from output file sol.file\n");
    fprintf(stderr,"         if scalar.sol argument is given, will also read in a scalar for VTK output\n");
    fprintf(stderr,"layer: 1...nset\n");
    fprintf(stderr,"\tif ilayer= 1..nset, will print one layer\n");
    fprintf(stderr,"\t          -1, will select nset (the top layer)\n");
    fprintf(stderr,"\t          -2, will print all layers\n");
    fprintf(stderr,"mode: 1...4\n");
    fprintf(stderr,"\tif mode = 1, will print lon lat z v_r \n");
    fprintf(stderr,"\t          2, will print lon lat z v_theta v_phi \n");
    fprintf(stderr,"\t          3, will print lon lat z v_r v_theta v_phi\n");
    fprintf(stderr,"\t          4, will print the depth levels of all layers\n");
    fprintf(stderr,"\t          5, compute all depth levels (set ilayer=-2) and write VTK file, ASCII\n");
    fprintf(stderr,"\t          6, compute all depth levels (set ilayer=-2) and write VTK file, BINARY\n");
    exit(-1);
    break;
  }
  if((mode == 4)||(mode==5)||(mode==6))
    ilayer = -2;
  /* 
     read in velocity/traction solution
  */
  in = hc_fopen(argv[1],"r","hc_extract_spatial");
  shps_read = hc_read_sh_solution(model,&vsol,in,binary_in,verbose);
  fclose(in);
  nvsol = model->nradp2 * shps_read;
  /* 
     deal with selection
  */
  loop = 0;
  if(ilayer == -1)
    ilayer = model->nradp2;
  else if(ilayer == -2){
    ilayer = model->nradp2;
    loop =1;
  }
  if((ilayer < 1)||(ilayer > model->nradp2)){
    fprintf(stderr,"%s: ilayer (%i) out of range, use 1 ... %i\n",
	    argv[0],ilayer,model->nradp2);
    exit(-1);
  }
  /* set up layer bounds */
  if(loop){
    i1=0;i2=model->nradp2-1;
  }else{
    i1=ilayer-1;i2 = i1;
  }
  /* detect number of expansions */
  if(mode == 1){
    shps = 1;			/* r */
  }else if(mode == 2){
    shps = 2;			/* theta,phi */
  }else if((mode == 3)||(mode == 5)||(mode==6)){
    shps = 3;			/* r,theta,phi */
  }else{
    shps = 1;
  }
  if(shps > shps_read){
    fprintf(stderr,"%s: solution file only had %i expansions, mode %i requests %i\n",
	    argv[0],shps_read,mode,shps);
    exit(-1);
  }
  /* 
     density solution or other scalar
  */
  if(read_dsol){
    if((mode != 5)&&(mode != 6))
      HC_ERROR("hc_extract_spatial","error, only mode 5 and  can handle scalar input");
    in = hc_fopen(argv[4],"r","hc_extract_spatial");
    shps_read_d = hc_read_sh_solution(model,&dsol,in,binary_in,
				    verbose);
    fclose(in);
    ndsol = model->nradp2 * shps_read_d;
  }
  /* 
     
     room for spatial expansion 

  */
  npoints = (vsol+i1*shps_read)->npoints;
  if((vsol+i1*shps_read)->type != SH_RICK)
    HC_ERROR("sh_extract_spatial","SH_RICK type required");
  /* geographic set up */
  nlat = (vsol+i1*shps_read)->rick.nlat;
  nlon = (vsol+i1*shps_read)->rick.nlon;
  
  ndata =     npoints * shps ;
  ndata_d =   npoints * shps_read_d;
  ndata_all = npoints * (shps + shps_read_d);

  if((mode == 5)||(mode==6)){			/* save all layers */
    hc_vecalloc(&data,model->nradp2 * ndata_all,"hc_extract_spatial");
  }else
    hc_vecalloc(&data, ndata_all,"hc_extract_spatial");
  for(lc=0,ilayer=i1;ilayer <= i2;ilayer++,lc++){
    /* 
       output 
    */

    zlabel = HC_Z_DEPTH(model->r[ilayer]);
    switch(mode){
    case 1:
      /*  */
      if(verbose)
	fprintf(stderr,"%s: printing v_r at layer %i (depth: %g)\n",argv[0],ilayer,
		(double)zlabel);

      ivec=FALSE;sh_compute_spatial((vsol+ilayer*shps_read),ivec,TRUE,&plm,data,verbose);
      sh_print_spatial_data_to_stream((vsol+ilayer*shps_read),shps,data,TRUE,zlabel,stdout);
      break;
    case 2:
      /*  */
      if(verbose)
	fprintf(stderr,"%s: printing v_theta v_phi SHE at layer %i (depth: %g)\n",argv[0],ilayer,(double)zlabel);
      ivec=TRUE;sh_compute_spatial((vsol+ilayer*shps_read+1),ivec,TRUE,&plm,data,verbose);
      sh_print_spatial_data_to_stream((vsol+ilayer*shps_read+1),shps,data,TRUE,zlabel,stdout);
      break;
    case 3:
      if(verbose)
	fprintf(stderr,"%s: printing v_r v_theta v_phi SHE at layer %i (depth: %g)\n",argv[0],ilayer,(double)zlabel);
      ivec=FALSE;sh_compute_spatial((vsol+ilayer*shps_read),  ivec,TRUE,&plm,data,verbose); /* radial */
      ivec=TRUE; sh_compute_spatial((vsol+ilayer*shps_read+1),ivec,TRUE,&plm,(data+npoints),verbose); /* theta,phi */
      sh_print_spatial_data_to_stream((vsol+ilayer*shps_read),shps,data,TRUE,zlabel,stdout);
      break;
    case 4:
      fprintf(stdout,"%5i %11g\n",ilayer,(double)HC_Z_DEPTH(model->r[ilayer]));
      break;
    case 5:			/* compute all and store */
    case 6:
      ivec=FALSE;sh_compute_spatial((vsol+ilayer*shps_read),  ivec,TRUE,&plm,(data+lc*ndata_all),verbose); /* radial */
      ivec=TRUE; sh_compute_spatial((vsol+ilayer*shps_read+1),ivec,TRUE,&plm,(data+lc*ndata_all+npoints),verbose); /* theta,phi */
      if(read_dsol){
	if(!shps_read_d)
	  HC_ERROR("sh_extract_spatial","logic error");
	ivec=FALSE;sh_compute_spatial((dsol+ilayer*shps_read_d),ivec,TRUE,&plm,(data+lc*ndata_all+npoints*shps),verbose); /* radial */
      }
      break;
    default:
      fprintf(stderr,"%s: error, mode %i undefined\n",argv[0],mode);
      exit(-1);
      break;
    }
  }
  /* clear and exit */
  sh_free_expansion(vsol,nvsol);
  if(read_dsol)
    sh_free_expansion(dsol,ndsol);
  free(plm);
  /*  */
  if((mode == 5)||(mode==6)){
    /* 
       print the already stored properties
    */
    if(shps != 3)HC_ERROR("hc_extract_spatial","shps has to be 3 for mode 5 and 6");
    /* convert */
    hc_vecalloc(&xpos,model->nradp2 * ndata,"hc_extract_spatial"); 
    hc_vecalloc(&xvec,model->nradp2 * ndata,"hc_extract_spatial");
    if(read_dsol)
      hc_vecalloc(&xscalar,model->nradp2 * ndata_d,"hc_extract_spatial");
    for(i=0;i < npoints;i++){	/* loop through all points */
      /* lon lat coordinates */
      sh_get_coordinates((vsol+i1*3),i,&lon,&lat);
      theta = LAT2THETA(lat);phi = LON2PHI(lon);
      xtmp[0] = xtmp[1] = sin(theta);
      xtmp[0] *= cos(phi);	/* x */
      xtmp[1] *= sin(phi);	/* y */
      xtmp[2] = cos(theta);	/* z */
      /* for conversion */
      calc_polar_base_at_theta_phi(theta,phi,polar_base);
      for(ilayer=0;ilayer < model->nradp2;ilayer++){
	/* this is the slow data storage loop but it avoids
	   recomputing the polar basis vector */
	poff = ilayer * ndata + i*shps;	/* point offset */
	for(j=0;j < 3;j++){
	  xpos[poff+j]   = xtmp[j] * model->r[ilayer]; /* cartesian coordinates */
	}
	/* data are stored a bit weirdly, this makes for lots of
	   jumping around in memory ... */
	pvec[0] = data[ilayer*ndata_all +           i];
	pvec[1] = data[ilayer*ndata_all + npoints  +i];
	pvec[2] = data[ilayer*ndata_all + npoints*2+i];
	lonlatpv2cv_with_base(pvec,polar_base,(xvec+poff));
	/* assign scalar fata if any */
	for(j=0;j < shps_read_d;j++)
	  xscalar[j * model->nradp2 * ndata_d + ilayer * npoints  + i] = 
	    data[ilayer * ndata_all + npoints*(shps+j) + i];
      }
    }
    free(data);
    /* print in VTK format */
    hc_print_vtk(stdout,xpos,xvec,npoints,model->nradp2,(mode==6),
		 shps_read_d,xscalar,nlon,nlat);
    free(xvec);free(xpos);
    if(shps_read_d)
      free(xscalar);
    
  }else{
    free(data);
  }

  return 0;
}